Skip to main content

Using the Module

Converting to a Python dict object

To convert a Python object to a Python dict (Dictionary) object, you use the convert_to_data function.

Usage

convert_to_data(obj, _reached=None)

Arguments

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

Example

>>> 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}

Converting back to a normal Python object

To convert a Python mapping back into a normal Python object, you use the convert_to_obj function.

Usage

convert_to_obj(data, allow_mode_repr=True, _reached=None)

Arguments

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

Example

>>> 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>

Storing and Reading JSON

To store and read JSON-serialized objects, you use the dump, dumps, load, and loads functions.

dump and dumps

Serializes obj and passes *jargs and **jkwargs to json.dump and json.dumps respectively.

load and loads

Deserializes the JSON data from json.dump or json.dumps. It passes *jargs and **jkwargs to json.dump and json.dumps respectively.

Modifying how the serializer serializes specific types

To modify how the serializer serializes specific types you use the type_settings descriptor.

Usage

@type_settings(serialization_mode=MODE_YES, serialization_function=None, deserialization_function=None)

Arguments

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

Modes

MODE_YES

serialize the type

MODE_NO

don't serialize the type, always deserializes as None

MODE_FALLBACK

fallback to the JSON module types, the types can be found at https://docs.python.org/3/library/json.html#json.JSONDecoder

MODE_REPR

serializes with repr(obj), deserializes with eval(data)

MODE_FUNCTION

uses serialization_function to serialize, and deserialization_function to deserialize

Metadata

Release files for JSON-Serialize 1.0.2

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for JSON-Serialize 1.0.2
File Size Uploaded
JSON Serialize-1.0.2.tar.gz 5.1 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for JSON-Serialize 1.0.2
File Interpreter ABI Platform
JSON_Serialize-1.0.2-py3-none-any.whl Python 3 none any Details

Total release size: 22.8 kB

Release files / JSON Serialize-1.0.2.tar.gz

Download URL JSON Serialize-1.0.2.tar.gz
Size 5.1 kB
Tags Source
SHA-256 checksum
How to use checksums
cc206e1645a9f2afd55481b8b318d0f36219c90063402c953e9a6cfc92a7a83b
BLAKE2b-256 checksum
How to use checksums
84bac55e64b4d430473e9ea364e93fc66c7211b0c54775154d2dce4606cac738
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/47.1.0 requests-toolbelt/0.9.1 tqdm/4.43.0 CPython/3.8.5

Release files / JSON_Serialize-1.0.2-py3-none-any.whl

Download URL JSON_Serialize-1.0.2-py3-none-any.whl
Size 17.7 kB
Tags Python 3
SHA-256 checksum
How to use checksums
0e49c21e926e0919efb6f2fb7402f7419e01f88f46c2bd9dfa07812e8b882fc0
BLAKE2b-256 checksum
How to use checksums
2b8f464b0564672b5919a621fe54c6dae49d328421976165480205e291a3861c
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/47.1.0 requests-toolbelt/0.9.1 tqdm/4.43.0 CPython/3.8.5

Release history Release notifications | RSS feed

This release

1.0.2 This release

2 release files

1.0.1

2 release files

1.0.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page