To convert a Python object to a Python dict (Dictionary) object, you use the convert_to_data function.
convert_to_data(obj, _reached=None)
| Argument | Type | Description | Default Value |
|---|---|---|---|
| obj | Any | The object to convert to a Python dict object |
required |
| _reached | Set | A value passed to the function internally for recursive data structures | None |
>>> class A:
... def __init__(self, obj):
... self.obj = obj
...
>>> obj1 = A('Hello World!')
>>> data = convert_to_data(obj1)
>>> data
{'version': 1, 'min_version': 1, 'module': '__main__', 'type': 'A', 'attrs': {'obj': {'value': 'Hello World!', 'uuid': 192575949315149374534195982055635280394, 'mode': 'fallback'}}, 'uuid': 192575949235921212021180267458779800074}To convert a Python mapping back into a normal Python object, you use the convert_to_obj function.
convert_to_obj(data, allow_mode_repr=True, _reached=None)
| Argument | Type | Description | Default Value |
|---|---|---|---|
| data | Mapping | The mapping to convert back to a Python object (a dict is a mapping) |
required |
| allow_mode_repr | bool | Whether to enable MODE_REPR; you might want to have this enabled because the deserialization function of MODE_REPR uses eval which is insecure |
True |
| _reached | Set | A value passed to the function internally for recursive data structures | None |
>>> data = {'version': 1, 'min_version': 1, 'module': '__main__', 'type': 'A', 'attrs': {'obj': {'value': 'Hello World!', 'uuid': 192575949315149374534195982055635280394, 'mode': 'fallback'}}, 'uuid': 192575949235921212021180267458779800074}
>>> obj2 = convert_to_obj(data)
>>> obj2
<__main__.A at 0x2473cbb88e0>To store and read JSON-serialized objects, you use the dump, dumps, load, and loads functions.
Serializes obj and passes *jargs and **jkwargs to json.dump and json.dumps respectively.
Deserializes the JSON data from json.dump or json.dumps. It passes *jargs and **jkwargs to json.dump and json.dumps respectively.
To modify how the serializer serializes specific types you use the type_settings descriptor.
@type_settings(serialization_mode=MODE_YES, serialization_function=None, deserialization_function=None)
| Argument | Type | Description | Default Value |
|---|---|---|---|
| serialization_mode | any of the MODE_* constants | The method used to serialize (and possibly deserialize the type) | MODE_YES |
| serialization_function | Union[None, Callable[[object, Set], JsonSupported]] | The function to serialize with (for MODE_FUNCTION); or None for others |
None |
| deserialization_function | Union[None, Callable[[JsonSupported, bool, Set], object]] | The function to serialize with (for MODE_FUNCTION); or None for others |
None |
serialize the type
don't serialize the type, always deserializes as None
fallback to the JSON module types, the types can be found at https://docs.python.org/3/library/json.html#json.JSONDecoder
serializes with repr(obj), deserializes with eval(data)
uses serialization_function to serialize, and deserialization_function to deserialize