forked from yukinarit/pyserde
-
Notifications
You must be signed in to change notification settings - Fork 0
/
custom_class_serializer.py
54 lines (41 loc) · 1.42 KB
/
custom_class_serializer.py
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
44
45
46
47
48
49
50
51
52
53
54
from dataclasses import dataclass
from datetime import datetime
from serde import SerdeSkip, default_deserializer, default_serializer, field, serde
from serde.json import from_json, to_json
def serializer(cls, o):
"""
Custom class level serializer.
"""
# You can provide a custom serialize/deserialize logic for certain types.
if cls is datetime:
return o.strftime('%d/%m/%y')
# Raise SerdeSkip to tell serde to use the default serializer/deserializer.
else:
raise SerdeSkip()
def deserializer(cls, o):
"""
Custom class level deserializer.
"""
if cls is datetime:
return datetime.strptime(o, '%d/%m/%y')
else:
raise SerdeSkip()
@serde(serializer=serializer, deserializer=deserializer)
@dataclass
class Foo:
i: int
dt1: datetime
# Override by field serializer/deserializer.
dt2: datetime = field(
serializer=lambda x: x.strftime('%y.%m.%d'), deserializer=lambda x: datetime.strptime(x, '%y.%m.%d')
)
# Override by the default serializer/deserializer.
dt3: datetime = field(serializer=default_serializer, deserializer=default_deserializer)
def main():
dt = datetime(2021, 1, 1, 0, 0, 0)
f = Foo(10, dt, dt, dt)
print(f"Into Json: {to_json(f)}")
s = '{"i": 10, "dt1": "01/01/21", "dt2": "01.01.21", "dt3": "2021-01-01T00:00:00"}'
print(f"From Json: {from_json(Foo, s)}")
if __name__ == '__main__':
main()