forked from yukinarit/pyserde
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathunion_operator.py
More file actions
43 lines (29 loc) · 795 Bytes
/
union_operator.py
File metadata and controls
43 lines (29 loc) · 795 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
from dataclasses import dataclass
from serde import serde
from serde.json import from_json, to_json
@serde
@dataclass
class Bar:
v: int
@serde
@dataclass
class Baz:
v: float
@serde
@dataclass
class Foo:
a: int | str
b: dict[str, int] | list[int]
c: Bar | Baz
d: str | None = None # Should be treated as Optional
def main():
f = Foo(10, [1, 2, 3], Bar(10))
print(f"Into Json: {to_json(f)}")
s = '{"a": 10, "b": [1, 2, 3], "c": {"Bar": {"v": 10}}}'
print(f"From Json: {from_json(Foo, s)}")
f = Foo('foo', {'bar': 1, 'baz': 2}, Baz(100.0))
print(f"Into Json: {to_json(f)}")
s = '{"a": "foo", "b": {"bar": 1, "baz": 2}, "c": {"Baz": {"v": 100.0}}}'
print(f"From Json: {from_json(Foo, s)}")
if __name__ == '__main__':
main()