Skip to main content

Module that serializes Python objects into JSON

Project description

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

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

JSON Serialize-1.0.2.tar.gz (5.1 kB view details)

Uploaded Source

Built Distribution

JSON_Serialize-1.0.2-py3-none-any.whl (17.7 kB view details)

Uploaded Python 3

File details

Details for the file JSON Serialize-1.0.2.tar.gz.

File metadata

  • Download URL: JSON Serialize-1.0.2.tar.gz
  • Upload date:
  • Size: 5.1 kB
  • Tags: Source
  • Uploaded using 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

File hashes

Hashes for JSON Serialize-1.0.2.tar.gz
Algorithm Hash digest
SHA256 cc206e1645a9f2afd55481b8b318d0f36219c90063402c953e9a6cfc92a7a83b
MD5 4595ffc9d2a78dab02b004f7ad13b29b
BLAKE2b-256 84bac55e64b4d430473e9ea364e93fc66c7211b0c54775154d2dce4606cac738

See more details on using hashes here.

File details

Details for the file JSON_Serialize-1.0.2-py3-none-any.whl.

File metadata

  • Download URL: JSON_Serialize-1.0.2-py3-none-any.whl
  • Upload date:
  • Size: 17.7 kB
  • Tags: Python 3
  • Uploaded using 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

File hashes

Hashes for JSON_Serialize-1.0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 0e49c21e926e0919efb6f2fb7402f7419e01f88f46c2bd9dfa07812e8b882fc0
MD5 ab8645d64d68bf354e129a47204fcec0
BLAKE2b-256 2b8f464b0564672b5919a621fe54c6dae49d328421976165480205e291a3861c

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page