Completed
Push — master ( 4677e5...272386 )
by Ramon
01:43
created

jsons.JsonSerializable.load()   A

Complexity

Conditions 1

Size

Total Lines 9
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
nop 3
dl 0
loc 9
rs 10
c 0
b 0
f 0
1
"""
2
Works with Python3.5+
3
4
JSON (de)serialization (jsons) from and to dicts and plain old Python objects.
5
6
Works with dataclasses (Python3.7+).
7
8
9
**Example:**
10
11
    >>> from dataclasses import dataclass
12
    >>> @dataclass
13
    ... class Car:
14
    ...     color: str
15
    >>> @dataclass
16
    ... class Person:
17
    ...     car: Car
18
    ...     name: str
19
    >>> c = Car('Red')
20
    >>> p = Person(c, 'John')
21
    >>> dumped = dump(p)
22
    >>> dumped['name']
23
    'John'
24
    >>> dumped['car']['color']
25
    'Red'
26
    >>> p_reloaded = load(dumped, Person)
27
    >>> p_reloaded.name
28
    'John'
29
    >>> p_reloaded.car.color
30
    'Red'
31
32
33
Deserialization will work with older Python classes (Python3.5+) given that
34
type hints are present for custom types (i.e. any type that is not set at
35
the bottom of this module). Serialization will work with no type hints at
36
all.
37
38
39
**Example**
40
41
    >>> class Car:
42
    ...     def __init__(self, color):
43
    ...         self.color = color
44
    >>> class Person:
45
    ...     def __init__(self, car: Car, name):
46
    ...         self.car = car
47
    ...         self.name = name
48
    >>> c = Car('Red')
49
    >>> p = Person(c, 'John')
50
    >>> dumped = dump(p)
51
    >>> dumped['name']
52
    'John'
53
    >>> dumped['car']['color']
54
    'Red'
55
    >>> p_reloaded = load(dumped, Person)
56
    >>> p_reloaded.name
57
    'John'
58
    >>> p_reloaded.car.color
59
    'Red'
60
61
62
Alternatively, you can make use of the `JsonSerializable` class.
63
64
65
**Example**
66
67
    >>> class Car(JsonSerializable):
68
    ...     def __init__(self, color):
69
    ...         self.color = color
70
    >>> class Person(JsonSerializable):
71
    ...     def __init__(self, car: Car, name):
72
    ...         self.car = car
73
    ...         self.name = name
74
    >>> c = Car('Red')
75
    >>> p = Person(c, 'John')
76
    >>> dumped = p.json
77
    >>> dumped['name']
78
    'John'
79
    >>> dumped['car']['color']
80
    'Red'
81
    >>> p_reloaded = Person.from_json(dumped)
82
    >>> p_reloaded.name
83
    'John'
84
    >>> p_reloaded.car.color
85
    'Red'
86
87
"""
88
from datetime import datetime
89
from enum import Enum
90
from jsons import _common_impl
91
from jsons.deserializers import default_list_deserializer, \
92
    default_enum_deserializer, default_datetime_deserializer, \
93
    default_string_deserializer, default_primitive_deserializer, \
94
    default_object_deserializer, default_dict_deserializer, \
95
    default_tuple_deserializer, default_set_deserializer
96
from jsons.serializers import default_list_serializer, \
97
    default_enum_serializer, default_datetime_serializer, \
98
    default_primitive_serializer, default_object_serializer, \
99
    KEY_TRANSFORMER_SNAKECASE, KEY_TRANSFORMER_CAMELCASE, \
100
    KEY_TRANSFORMER_PASCALCASE, KEY_TRANSFORMER_LISPCASE, \
101
    default_dict_serializer, default_tuple_serializer
102
103
dump = _common_impl.dump
0 ignored issues
show
Coding Style Naming introduced by
The name dump does not conform to the constant naming conventions ((([A-Z_][A-Z0-9_]*)|(__.*__))$).

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.

Loading history...
104
load = _common_impl.load
0 ignored issues
show
Coding Style Naming introduced by
The name load does not conform to the constant naming conventions ((([A-Z_][A-Z0-9_]*)|(__.*__))$).

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.

Loading history...
105
JsonSerializable = _common_impl.JsonSerializable
106
dumps = _common_impl.dumps
0 ignored issues
show
Coding Style Naming introduced by
The name dumps does not conform to the constant naming conventions ((([A-Z_][A-Z0-9_]*)|(__.*__))$).

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.

Loading history...
107
loads = _common_impl.loads
0 ignored issues
show
Coding Style Naming introduced by
The name loads does not conform to the constant naming conventions ((([A-Z_][A-Z0-9_]*)|(__.*__))$).

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.

Loading history...
108
set_serializer = _common_impl.set_serializer
0 ignored issues
show
Coding Style Naming introduced by
The name set_serializer does not conform to the constant naming conventions ((([A-Z_][A-Z0-9_]*)|(__.*__))$).

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.

Loading history...
109
set_deserializer = _common_impl.set_deserializer
0 ignored issues
show
Coding Style Naming introduced by
The name set_deserializer does not conform to the constant naming conventions ((([A-Z_][A-Z0-9_]*)|(__.*__))$).

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.

Loading history...
110
111
set_serializer(default_list_serializer, list)
112
set_serializer(default_list_serializer, set)
113
set_serializer(default_tuple_serializer, tuple)
114
set_serializer(default_dict_serializer, dict)
115
set_serializer(default_enum_serializer, Enum)
116
set_serializer(default_datetime_serializer, datetime)
117
set_serializer(default_primitive_serializer, str)
118
set_serializer(default_primitive_serializer, int)
119
set_serializer(default_primitive_serializer, float)
120
set_serializer(default_primitive_serializer, bool)
121
set_serializer(default_primitive_serializer, None)
122
set_serializer(default_object_serializer, object, False)
123
set_deserializer(default_list_deserializer, list)
124
set_deserializer(default_tuple_deserializer, tuple)
125
set_deserializer(default_set_deserializer, set)
126
set_deserializer(default_dict_deserializer, dict)
127
set_deserializer(default_enum_deserializer, Enum)
128
set_deserializer(default_datetime_deserializer, datetime)
129
set_deserializer(default_string_deserializer, str)
130
set_deserializer(default_primitive_deserializer, int)
131
set_deserializer(default_primitive_deserializer, float)
132
set_deserializer(default_primitive_deserializer, bool)
133
set_deserializer(default_primitive_deserializer, None)
134
set_deserializer(default_object_deserializer, object, False)
135