msl.network.json module

This module is used as the JSON (de)serializer.

class msl.network.json.Package(value, names=None, *, module=None, qualname=None, type=None, start=1, boundary=None)[source]

Bases: Enum

Supported Python packages for (de)serializing JSON objects.

By default, the builtin json module is used.

To change which JSON package to use you can call use() to set the backend during runtime, or you can specify an MSL_NETWORK_JSON environment variable as the default backend. For example, creating an environment variable named MSL_NETWORK_JSON and setting its value to be ULTRA would use UltraJSON to (de)serialize JSON objects.

Changed in version 1.0: Moved from the msl.network.constants module and renamed. Added JSON, UJSON, RAPIDJSON and SIMPLEJSON aliases. Added OR (and alias ORJSON) for orjson. Removed YAJL.

BUILTIN = 'BUILTIN'

json

JSON = 'BUILTIN'

json

ULTRA = 'ULTRA'

UltraJSON

UJSON = 'ULTRA'

UltraJSON

RAPID = 'RAPID'

RapidJSON

RAPIDJSON = 'RAPID'

RapidJSON

SIMPLE = 'SIMPLE'

simplejson

SIMPLEJSON = 'SIMPLE'

simplejson

OR = 'OR'

orjson

ORJSON = 'OR'

orjson

msl.network.json.use(backend, *, loads_kwargs=None, dumps_kwargs=None)[source]

Set which JSON backend to use.

New in version 1.0.

Changed in version 1.1: Added the loads_kwargs and dumps_kwargs keyword arguments.

Parameters:
  • backend (Package or str) – An enum value or member name (case-insensitive).

  • loads_kwargs (dict, optional) – Keyword arguments to use for the loads function of the backend. If not specified, default options are used.

  • dumps_kwargs (dict, optional) – Keyword arguments to use for the dumps function of the backend. If not specified, default options are used.

Examples

>>> from msl.network import json
>>> json.use(json.Package.UJSON)
>>> json.use('ujson')
msl.network.json.serialize(obj)[source]

Serialize an object as a JSON-formatted string.

Parameters:

obj – A JSON-serializable object.

Returns:

str – The JSON-formatted string.

msl.network.json.deserialize(s)[source]

Deserialize a JSON-formatted string to Python objects.

Parameters:

s (str, bytes or bytearray) – A JSON-formatted string.

Returns:

The deserialized Python object.