-
Notifications
You must be signed in to change notification settings - Fork 2
/
serializers.py
62 lines (46 loc) · 1.56 KB
/
serializers.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
55
56
57
58
59
60
61
62
import dataclasses
from typing import Any, Callable, Dict, Type, _TypedDictMeta # type: ignore
import orjson
from .fields import SerializeFields
def dataclass_asjson(instance: Any) -> bytes:
fields = SerializeFields.get_fields(type(instance))
if not fields:
return orjson.dumps(
instance, default=OrjsonDefaultTypes.default_function
)
instance = dataclasses.asdict(instance)
return orjson.dumps(
{f.name: instance[f.name] for f in fields},
default=OrjsonDefaultTypes.default_function,
)
def typed_dict_asjson(
typed_dict: Any, typed_dict_type: _TypedDictMeta
) -> bytes:
return orjson.dumps(
_choose_typed_dict_fields(typed_dict, typed_dict_type),
default=OrjsonDefaultTypes.default_function,
)
def _choose_typed_dict_fields(
typed_dict: Dict[str, Any], typed_dict_type: _TypedDictMeta
) -> Dict[str, Any]:
fields = SerializeFields.get_fields(typed_dict_type)
if not fields:
return typed_dict
return {
f.name: (
_choose_typed_dict_fields(typed_dict[f.name], f.type)
if isinstance(typed_dict[f.name], dict)
else typed_dict[f.name]
)
for f in fields
}
class OrjsonDefaultTypes:
types_default_map: Dict[Type[Any], Callable[[Any], Any]] = dict()
@classmethod
def default_function(cls, v: Any) -> Any:
if isinstance(v, bytes):
return v.decode()
try:
return cls.types_default_map[type(v)](v)
except KeyError:
orjson.JSONEncodeError(v)