1
|
|
|
import inspect |
|
|
|
|
2
|
|
|
import re |
3
|
|
|
from datetime import datetime, timezone, timedelta |
4
|
|
|
from enum import Enum, EnumMeta |
5
|
|
|
from typing import List, Callable |
6
|
|
|
from jsons._common_impl import RFC3339_DATETIME_PATTERN, load_impl, \ |
7
|
|
|
snakecase, camelcase, pascalcase, lispcase |
8
|
|
|
|
9
|
|
|
|
10
|
|
|
def default_datetime_deserializer(obj: str, _: datetime, **__) -> datetime: |
|
|
|
|
11
|
|
|
pattern = RFC3339_DATETIME_PATTERN |
12
|
|
|
if '.' in obj: |
13
|
|
|
pattern += '.%f' |
14
|
|
|
# strptime allows a fraction of length 6, so trip the rest (if exists). |
15
|
|
|
regex_pattern = re.compile('(\.[0-9]+)') |
|
|
|
|
16
|
|
|
frac = regex_pattern.search(obj).group() |
17
|
|
|
obj = obj.replace(frac, frac[0:7]) |
18
|
|
|
if obj[-1] == 'Z': |
19
|
|
|
dattim_str = obj[0:-1] |
20
|
|
|
dattim_obj = datetime.strptime(dattim_str, pattern) |
21
|
|
|
else: |
22
|
|
|
dattim_str, offset = obj.split('+') |
23
|
|
|
dattim_obj = datetime.strptime(dattim_str, pattern) |
24
|
|
|
hours, minutes = offset.split(':') |
25
|
|
|
tz = timezone(offset=timedelta(hours=int(hours), minutes=int(minutes))) |
|
|
|
|
26
|
|
|
datetime_list = [dattim_obj.year, dattim_obj.month, dattim_obj.day, |
27
|
|
|
dattim_obj.hour, dattim_obj.minute, dattim_obj.second, |
28
|
|
|
dattim_obj.microsecond, tz] |
29
|
|
|
dattim_obj = datetime(*datetime_list) |
30
|
|
|
return dattim_obj |
31
|
|
|
|
32
|
|
|
|
33
|
|
|
def default_list_deserializer(obj: List, cls, **kwargs) -> object: |
|
|
|
|
34
|
|
|
cls_ = None |
35
|
|
|
if cls and hasattr(cls, '__args__'): |
36
|
|
|
cls_ = cls.__args__[0] |
37
|
|
|
return [load_impl(x, cls_, **kwargs) for x in obj] |
38
|
|
|
|
39
|
|
|
|
40
|
|
|
def default_enum_deserializer(obj: Enum, cls: EnumMeta, **__) -> object: |
|
|
|
|
41
|
|
|
return cls[obj] |
42
|
|
|
|
43
|
|
|
|
44
|
|
|
def default_string_deserializer(obj: str, _: type = None, **kwargs) -> object: |
|
|
|
|
45
|
|
|
try: |
46
|
|
|
return load_impl(obj, datetime, **kwargs) |
47
|
|
|
except: |
|
|
|
|
48
|
|
|
return obj |
49
|
|
|
|
50
|
|
|
|
51
|
|
|
def default_primitive_deserializer(obj: object, |
|
|
|
|
52
|
|
|
_: type = None, **__) -> object: |
53
|
|
|
return obj |
54
|
|
|
|
55
|
|
|
|
56
|
|
|
def default_object_deserializer(obj: dict, cls: type, |
|
|
|
|
57
|
|
|
key_transformer: Callable[[str], str] = None, |
58
|
|
|
**kwargs) -> object: |
59
|
|
|
if key_transformer: |
60
|
|
|
obj = {key_transformer(key): obj[key] for key in obj} |
61
|
|
|
signature_parameters = inspect.signature(cls.__init__).parameters |
62
|
|
|
# Loop through the signature of cls: the type we try to deserialize to. For |
63
|
|
|
# every required parameter, we try to get the corresponding value from |
64
|
|
|
# json_obj. |
65
|
|
|
constructor_args = dict() |
66
|
|
|
for signature_key, signature in signature_parameters.items(): |
67
|
|
|
if obj and signature_key is not 'self': |
|
|
|
|
68
|
|
|
if signature_key in obj: |
69
|
|
|
cls_ = None |
70
|
|
|
if signature.annotation != inspect._empty: |
|
|
|
|
71
|
|
|
cls_ = signature.annotation |
72
|
|
|
value = load_impl(obj[signature_key], cls_, |
73
|
|
|
key_transformer=key_transformer, **kwargs) |
74
|
|
|
constructor_args[signature_key] = value |
75
|
|
|
|
76
|
|
|
# The constructor arguments are gathered, create an instance. |
77
|
|
|
instance = cls(**constructor_args) |
78
|
|
|
# Set any remaining attributes on the newly created instance. |
79
|
|
|
remaining_attrs = {attr_name: obj[attr_name] for attr_name in obj |
80
|
|
|
if attr_name not in constructor_args} |
81
|
|
|
for attr_name in remaining_attrs: |
82
|
|
|
loaded_attr = load_impl(remaining_attrs[attr_name], |
83
|
|
|
type(remaining_attrs[attr_name]), |
84
|
|
|
key_transformer=key_transformer, **kwargs) |
85
|
|
|
setattr(instance, attr_name, loaded_attr) |
86
|
|
|
return instance |
87
|
|
|
|
88
|
|
|
|
89
|
|
|
# The following default key transformers can be used with the |
90
|
|
|
# default_object_serializer. |
91
|
|
|
KEY_TRANSFORMER_SNAKECASE = snakecase |
92
|
|
|
KEY_TRANSFORMER_CAMELCASE = camelcase |
93
|
|
|
KEY_TRANSFORMER_PASCALCASE = pascalcase |
94
|
|
|
KEY_TRANSFORMER_LISPCASE = lispcase |
95
|
|
|
|
The coding style of this project requires that you add a docstring to this code element. Below, you find an example for methods:
If you would like to know more about docstrings, we recommend to read PEP-257: Docstring Conventions.