Replies: 1 comment
-
Hi, from dataclasses import dataclass
from apischema import deserialize, deserializer, serialize, serializer
@dataclass
class CodeSnippet:
filename: str
start_line: int
end_line: int
@dataclass
class NestedCodeSnippet:
filename: str
@dataclass
class Lines:
start: int
end: int
lines: Lines
@deserializer
def from_nested(nested: NestedCodeSnippet) -> CodeSnippet:
return CodeSnippet(nested.filename, nested.lines.start, nested.lines.end)
@serializer
def to_nested(cs: CodeSnippet) -> NestedCodeSnippet:
return NestedCodeSnippet(
cs.filename, NestedCodeSnippet.Lines(cs.start_line, cs.end_line)
)
data = {"filename": "abc.py", "lines": {"start": 13, "end": 37}}
obj = CodeSnippet("abc.py", 13, 37)
assert deserialize(CodeSnippet, data) == obj
assert serialize(CodeSnippet, obj) == data But I know that in the case where there is a lot of fields in Your idea with |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Hello,
I'm trying
apischema
out, and was wondering if there is an easy way to serialize/deserialize nested data, something like this:I made some progress and got the following code working for serialization:
I guess I could go this route and add deserialization in a similar manner, but I'm new to
apischema
and wanted to know if there was an easier way, e.g. something likealias(["lines", "start"])
.Beta Was this translation helpful? Give feedback.
All reactions