1 | _SERIALIZED_STRING = '@Serialized:{}' |
---|
2 | _DOUBLE_QUOTED = '"{}"' |
---|
3 | _FIELD_STRING = '"{}":{}' |
---|
4 | _LIST_STRING = '[{}]' |
---|
5 | _OBJECT_STRING = '{{{}}}' |
---|
6 | |
---|
7 | |
---|
8 | def _serialize_value(value): |
---|
9 | return _SERIALIZED_STRING.format(_serialize(value)) |
---|
10 | |
---|
11 | |
---|
12 | def _serialize(target): |
---|
13 | if isinstance(target, list): |
---|
14 | return _serialize_list(target) |
---|
15 | if isinstance(target, dict): |
---|
16 | return _serialize_object(target) |
---|
17 | if isinstance(target, str): |
---|
18 | return _serialize_string(target) |
---|
19 | if target is None: |
---|
20 | return 'null' |
---|
21 | return str(target) |
---|
22 | |
---|
23 | |
---|
24 | def _serialize_list(target): |
---|
25 | serialized = [_serialize(el) for el in target] |
---|
26 | return _to_list_string(serialized) |
---|
27 | |
---|
28 | |
---|
29 | def _serialize_object(target): |
---|
30 | serialized = [_to_field_string(k, v) for k, v in target.items()] |
---|
31 | return _to_object_string(serialized) |
---|
32 | |
---|
33 | |
---|
34 | def _serialize_string(target): |
---|
35 | return _DOUBLE_QUOTED.format(target) |
---|
36 | |
---|
37 | |
---|
38 | def _to_field_string(key, value): |
---|
39 | return _FIELD_STRING.format(key, _serialize(value)) |
---|
40 | |
---|
41 | |
---|
42 | def _to_list_string(target): |
---|
43 | return _LIST_STRING.format(','.join(target)) |
---|
44 | |
---|
45 | |
---|
46 | def _to_object_string(target): |
---|
47 | return _OBJECT_STRING.format(','.join(target)) |
---|