|
1
|
|
|
""" |
|
|
|
|
|
|
2
|
|
|
This module contains default serializers. You can override the serialization |
|
3
|
|
|
process of a particular type as follows: |
|
4
|
|
|
|
|
5
|
|
|
`jsons.set_serializer(custom_deserializer, SomeClass)` |
|
6
|
|
|
""" |
|
7
|
|
|
from datetime import datetime |
|
8
|
|
|
from enum import EnumMeta |
|
9
|
|
|
from typing import Callable |
|
10
|
|
|
from jsons import dump_impl |
|
11
|
|
|
from jsons._common_impl import RFC3339_DATETIME_PATTERN, snakecase, \ |
|
12
|
|
|
camelcase, pascalcase, lispcase |
|
13
|
|
|
|
|
14
|
|
|
|
|
15
|
|
|
def default_list_serializer(obj: list, **kwargs) -> list: |
|
16
|
|
|
""" |
|
17
|
|
|
Serialize the given `obj` to a list of serialized objects. |
|
18
|
|
|
:param obj: the list that is to be serialized. |
|
19
|
|
|
:param kwargs: any keyword arguments that may be given to the serialization |
|
20
|
|
|
process. |
|
21
|
|
|
:return: a list of which all elements are serialized. |
|
22
|
|
|
""" |
|
23
|
|
|
return [dump_impl(elem, **kwargs) for elem in obj] |
|
24
|
|
|
|
|
25
|
|
|
|
|
26
|
|
|
def default_dict_serializer(obj: list, **kwargs) -> dict: |
|
27
|
|
|
""" |
|
28
|
|
|
Serialize the given `obj` to a dict of serialized objects. |
|
29
|
|
|
:param obj: the dict that is to be serialized. |
|
30
|
|
|
:param kwargs: any keyword arguments that may be given to the serialization |
|
31
|
|
|
process. |
|
32
|
|
|
:return: a dict of which all elements are serialized. |
|
33
|
|
|
""" |
|
34
|
|
|
return {key: dump_impl(obj[key], **kwargs) for key in obj} |
|
35
|
|
|
|
|
36
|
|
|
|
|
37
|
|
|
def default_enum_serializer(obj: EnumMeta, use_enum_name: bool = True, |
|
38
|
|
|
**_) -> str: |
|
39
|
|
|
""" |
|
40
|
|
|
Serialize the given obj. By default, the name of the enum element is |
|
41
|
|
|
returned. |
|
42
|
|
|
:param obj: an instance of an enum. |
|
43
|
|
|
:param use_enum_name: determines whether the name or the value should be |
|
44
|
|
|
used for serialization. |
|
45
|
|
|
:param _: not used. |
|
46
|
|
|
:return: `obj` serialized as a string. |
|
47
|
|
|
""" |
|
48
|
|
|
attr = 'name' if use_enum_name else 'value' |
|
49
|
|
|
return getattr(obj, attr) |
|
50
|
|
|
|
|
51
|
|
|
|
|
52
|
|
|
def default_datetime_serializer(obj: datetime, **_) -> str: |
|
53
|
|
|
""" |
|
54
|
|
|
Serialize the given datetime instance to a string. It uses the RFC3339 |
|
55
|
|
|
pattern. If datetime is a localtime, an offset is provided. If datetime is |
|
56
|
|
|
in UTC, the result is suffixed with a 'Z'. |
|
57
|
|
|
:param obj: the datetime instance that is to be serialized. |
|
58
|
|
|
:param _: not used. |
|
59
|
|
|
:return: `datetime` as an RFC3339 string. |
|
60
|
|
|
""" |
|
61
|
|
|
timezone = obj.tzinfo |
|
62
|
|
|
offset = 'Z' |
|
63
|
|
|
pattern = RFC3339_DATETIME_PATTERN |
|
64
|
|
|
if timezone: |
|
65
|
|
|
if timezone.tzname(None) != 'UTC': |
|
66
|
|
|
offset = timezone.tzname(None).split('UTC')[1] |
|
67
|
|
|
if obj.microsecond: |
|
68
|
|
|
pattern += '.%f' |
|
69
|
|
|
return obj.strftime("{}{}".format(pattern, offset)) |
|
70
|
|
|
|
|
71
|
|
|
|
|
72
|
|
|
def default_primitive_serializer(obj, **_) -> object: |
|
73
|
|
|
""" |
|
74
|
|
|
Serialize a primitive; simply return the given `obj`. |
|
75
|
|
|
:param obj: |
|
76
|
|
|
:param _: not used. |
|
77
|
|
|
:return: `obj`. |
|
78
|
|
|
""" |
|
79
|
|
|
return obj |
|
80
|
|
|
|
|
81
|
|
|
|
|
82
|
|
|
def default_object_serializer(obj: object, |
|
83
|
|
|
key_transformer: Callable[[str], str] = None, |
|
84
|
|
|
**kwargs) -> dict: |
|
85
|
|
|
""" |
|
86
|
|
|
Serialize the given `obj` to a dict. All values within `obj` are also |
|
87
|
|
|
serialized. If `key_transformer` is given, it will be used to transform the |
|
88
|
|
|
casing (e.g. snake_case) to a different format (e.g. camelCase). |
|
89
|
|
|
:param obj: the object that is to be serialized. |
|
90
|
|
|
:param key_transformer: a function that will be applied to all keys in the |
|
91
|
|
|
resulting dict. |
|
92
|
|
|
:param kwargs: any keyword arguments that are to be passed to the |
|
93
|
|
|
serializer functions. |
|
94
|
|
|
:return: a Python dict holding the values of `obj`. |
|
95
|
|
|
""" |
|
96
|
|
|
d = obj.__dict__ |
|
|
|
|
|
|
97
|
|
|
key_transformer_ = key_transformer or (lambda key: key) |
|
98
|
|
|
return {key_transformer_(key): dump_impl(d[key], |
|
99
|
|
|
key_transformer=key_transformer, |
|
100
|
|
|
**kwargs) for key in d} |
|
101
|
|
|
|
|
102
|
|
|
|
|
103
|
|
|
# The following default key transformers can be used with the |
|
104
|
|
|
# default_object_serializer. |
|
105
|
|
|
KEY_TRANSFORMER_SNAKECASE = snakecase |
|
106
|
|
|
KEY_TRANSFORMER_CAMELCASE = camelcase |
|
107
|
|
|
KEY_TRANSFORMER_PASCALCASE = pascalcase |
|
108
|
|
|
KEY_TRANSFORMER_LISPCASE = lispcase |
|
109
|
|
|
|
Cyclic imports may cause partly loaded modules to be returned. This might lead to unexpected runtime behavior which is hard to debug.