import json class Example def __init__ self str_field int_field self s

 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
import json
class Example:
def __init__(self, str_field, int_field):
self.str_field = str_field
self.int_field = int_field
self.array_str_field = []
def to_dict(self) -> dict:
return {
"str_field": str_field,
"int_field": int_field,
"array_str_field": array_str_field
}
@staticmethod
def from_dict(data: dict) -> Example:
instance = Example(data["str_field"], data["int_field"])
instance.array_str_field = data["array_str_field"]
return instance
example = Example("foo", 666, ["haha", "hoho", "hihi"])
print(json.dumps(example.to_dict(), indent=4))
example2 = Example.from_dict(example.to_dict())
print(json.dumps(example2.to_dict(), indent=4))