nuropb.encodings.json_serialisation

This module provides entire nuropb package with json serialisation logic and features

Module Contents

Classes

NuropbEncoder

NOTE 1 This class must be kept in step with the function to_json_compatible, above. Next, read NOTE 2.

JsonSerializor

Serializes and deserializes nuropb payloads to and from JSON format.

Functions

to_json_compatible

Returns a json compatible value for obj, if obj is not a native json type, then return a string representation.

to_json

Returns a json string representation of the input object, if not a native json type

API

nuropb.encodings.json_serialisation.to_json_compatible(obj: Any, recursive: bool = True, max_depth: int = 4) Any[source]

Returns a json compatible value for obj, if obj is not a native json type, then return a string representation.

NOTE 1 This function must be kept in step with the custom json encoder, NuropbEncoder, below. Next, read NOTE 2.

NOTE 2 This function does not exactly follow the structure of the custom json encoder, NuropbEncoder, below. Difference is that the json library implements its own object traversal logic. In this function it’s required to be done explicitly.

datetime.datetime: isoformat() + “Z”. if there’s timezone info, the datetime is converted to utc. if there is no timezone info, the datetime is assumed to be utc.

Parameters:
  • obj – Any

  • recursive – bool, whether to recursively convert obj to json compatible types

  • max_depth – int, the maximum depth to recurse

Returns:

str, or other json compatible type

class nuropb.encodings.json_serialisation.NuropbEncoder(*, skipkeys=False, ensure_ascii=True, check_circular=True, allow_nan=True, sort_keys=False, indent=None, separators=None, default=None)[source]

Bases: json.JSONEncoder

NOTE 1 This class must be kept in step with the function to_json_compatible, above. Next, read NOTE 2.

NOTE 2 This class does not exactly follow the structure of the function, to_json_compatible, above. Difference is that the json library implements its own object traversal logic. In the function it’s required to be done explicitly.

Initialization

Constructor for JSONEncoder, with sensible defaults.

If skipkeys is false, then it is a TypeError to attempt encoding of keys that are not str, int, float or None. If skipkeys is True, such items are simply skipped.

If ensure_ascii is true, the output is guaranteed to be str objects with all incoming non-ASCII characters escaped. If ensure_ascii is false, the output can contain non-ASCII characters.

If check_circular is true, then lists, dicts, and custom encoded objects will be checked for circular references during encoding to prevent an infinite recursion (which would cause an RecursionError). Otherwise, no such check takes place.

If allow_nan is true, then NaN, Infinity, and -Infinity will be encoded as such. This behavior is not JSON specification compliant, but is consistent with most JavaScript based encoders and decoders. Otherwise, it will be a ValueError to encode such floats.

If sort_keys is true, then the output of dictionaries will be sorted by key; this is useful for regression tests to ensure that JSON serializations can be compared on a day-to-day basis.

If indent is a non-negative integer, then JSON array elements and object members will be pretty-printed with that indent level. An indent level of 0 will only insert newlines. None is the most compact representation.

If specified, separators should be an (item_separator, key_separator) tuple. The default is (’, ‘, ‘: ‘) if indent is None and (‘,’, ‘: ‘) otherwise. To get the most compact JSON representation, you should specify (‘,’, ‘:’) to eliminate whitespace.

If specified, default is a function that gets called for objects that can’t otherwise be serialized. It should return a JSON encodable version of the object or raise a TypeError.

default(obj: Any) Any[source]
nuropb.encodings.json_serialisation.to_json(obj: Any) str[source]

Returns a json string representation of the input object, if not a native json type

class nuropb.encodings.json_serialisation.JsonSerializor[source]

Bases: object

Serializes and deserializes nuropb payloads to and from JSON format.

Initialization

Initializes a new JsonSerializor instance.

_encryption_keys: Dict[str, Any]

None

encryption keys related to a given correlation_id

encode(payload: Any) str[source]

Encodes a nuropb encoded payload to JSON.

Parameters:

payload – Any, The encoded payload to encode.

Returns:

str, The JSON-encoded encoded payload.

decode(json_payload: str) Any[source]

Decodes a JSON-encoded nuropb encoded payload.

Parameters:

json_payload – str, The JSON-encoded encoded payload to decode.

Returns:

Any, The decoded encoded payload.

TODO: Re-check the serialised datetime, date, time and timedelta formats. Look for standards.