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_tuple_serializer(obj: tuple, **kwargs) -> list: |
27
|
|
|
""" |
28
|
|
|
Serialize the given `obj` to a list of serialized objects. |
29
|
|
|
:param obj: the tuple that is to be serialized. |
30
|
|
|
:param kwargs: any keyword arguments that may be given to the serialization |
31
|
|
|
process. |
32
|
|
|
:return: a list of which all elements are serialized. |
33
|
|
|
""" |
34
|
|
|
return dump_impl(list(obj), **kwargs) |
35
|
|
|
|
36
|
|
|
|
37
|
|
|
def default_dict_serializer(obj: list, **kwargs) -> dict: |
38
|
|
|
""" |
39
|
|
|
Serialize the given `obj` to a dict of serialized objects. |
40
|
|
|
:param obj: the dict that is to be serialized. |
41
|
|
|
:param kwargs: any keyword arguments that may be given to the serialization |
42
|
|
|
process. |
43
|
|
|
:return: a dict of which all elements are serialized. |
44
|
|
|
""" |
45
|
|
|
return {key: dump_impl(obj[key], **kwargs) for key in obj} |
46
|
|
|
|
47
|
|
|
|
48
|
|
|
def default_enum_serializer(obj: EnumMeta, use_enum_name: bool = True, |
49
|
|
|
**_) -> str: |
50
|
|
|
""" |
51
|
|
|
Serialize the given obj. By default, the name of the enum element is |
52
|
|
|
returned. |
53
|
|
|
:param obj: an instance of an enum. |
54
|
|
|
:param use_enum_name: determines whether the name or the value should be |
55
|
|
|
used for serialization. |
56
|
|
|
:param _: not used. |
57
|
|
|
:return: `obj` serialized as a string. |
58
|
|
|
""" |
59
|
|
|
attr = 'name' if use_enum_name else 'value' |
60
|
|
|
return getattr(obj, attr) |
61
|
|
|
|
62
|
|
|
|
63
|
|
|
def default_datetime_serializer(obj: datetime, **_) -> str: |
64
|
|
|
""" |
65
|
|
|
Serialize the given datetime instance to a string. It uses the RFC3339 |
66
|
|
|
pattern. If datetime is a localtime, an offset is provided. If datetime is |
67
|
|
|
in UTC, the result is suffixed with a 'Z'. |
68
|
|
|
:param obj: the datetime instance that is to be serialized. |
69
|
|
|
:param _: not used. |
70
|
|
|
:return: `datetime` as an RFC3339 string. |
71
|
|
|
""" |
72
|
|
|
timezone = obj.tzinfo |
73
|
|
|
offset = 'Z' |
74
|
|
|
pattern = RFC3339_DATETIME_PATTERN |
75
|
|
|
if timezone: |
76
|
|
|
if timezone.tzname(None) != 'UTC': |
77
|
|
|
offset = timezone.tzname(None).split('UTC')[1] |
78
|
|
|
if obj.microsecond: |
79
|
|
|
pattern += '.%f' |
80
|
|
|
return obj.strftime("{}{}".format(pattern, offset)) |
81
|
|
|
|
82
|
|
|
|
83
|
|
|
def default_primitive_serializer(obj, **_) -> object: |
84
|
|
|
""" |
85
|
|
|
Serialize a primitive; simply return the given `obj`. |
86
|
|
|
:param obj: |
87
|
|
|
:param _: not used. |
88
|
|
|
:return: `obj`. |
89
|
|
|
""" |
90
|
|
|
return obj |
91
|
|
|
|
92
|
|
|
|
93
|
|
|
def default_object_serializer(obj: object, |
94
|
|
|
key_transformer: Callable[[str], str] = None, |
95
|
|
|
**kwargs) -> dict: |
96
|
|
|
""" |
97
|
|
|
Serialize the given `obj` to a dict. All values within `obj` are also |
98
|
|
|
serialized. If `key_transformer` is given, it will be used to transform the |
99
|
|
|
casing (e.g. snake_case) to a different format (e.g. camelCase). |
100
|
|
|
:param obj: the object that is to be serialized. |
101
|
|
|
:param key_transformer: a function that will be applied to all keys in the |
102
|
|
|
resulting dict. |
103
|
|
|
:param kwargs: any keyword arguments that are to be passed to the |
104
|
|
|
serializer functions. |
105
|
|
|
:return: a Python dict holding the values of `obj`. |
106
|
|
|
""" |
107
|
|
|
d = obj.__dict__ |
|
|
|
|
108
|
|
|
key_transformer_ = key_transformer or (lambda key: key) |
109
|
|
|
return {key_transformer_(key): dump_impl(d[key], |
110
|
|
|
key_transformer=key_transformer, |
111
|
|
|
**kwargs) for key in d} |
112
|
|
|
|
113
|
|
|
|
114
|
|
|
# The following default key transformers can be used with the |
115
|
|
|
# default_object_serializer. |
116
|
|
|
KEY_TRANSFORMER_SNAKECASE = snakecase |
117
|
|
|
KEY_TRANSFORMER_CAMELCASE = camelcase |
118
|
|
|
KEY_TRANSFORMER_PASCALCASE = pascalcase |
119
|
|
|
KEY_TRANSFORMER_LISPCASE = lispcase |
120
|
|
|
|
This check looks for invalid names for a range of different identifiers.
You can set regular expressions to which the identifiers must conform if the defaults do not match your requirements.
If your project includes a Pylint configuration file, the settings contained in that file take precedence.
To find out more about Pylint, please refer to their site.