ait.core.json module

AIT Javascript Object Notation (JSON)

The ait.core.json module provides JSON utilities and mixin classes for encoding and decoding between AIT data structures and JSON.

class ait.core.json.SlotSerializer

Bases: object

toJSON()
ait.core.json.slotsToJSON(obj, slots=None)

Converts the given Python object to one suitable for Javascript Object Notation (JSON) serialization via json.dump() or json.dumps(). This function delegates to toJSON().

Specifically only attributes in the list of slots are converted. If slots is not provided, it defaults to the object’s __slots__` and any inherited ``__slots__.

To omit certain slots from serialization, the object may define a __jsonOmit__(key, val)() method. When the method returns True for any particular slot name (i.e. key) and value combination, the slot will not serialized.

ait.core.json.toJSON(obj)

Converts the given Python object to one suitable for Javascript Object Notation (JSON) serialization via json.dump() or json.dumps(). If the Python object has a toJSON() method, it is always given preference and will be called to peform the conversion.

Otherwise, plain mapping and sequence types are converted to Python dictionaries and lists, respectively, by recursively calling this toJSON() function on mapping keys and values or iterable items. Python primitive types handled natively by the JSON encoder (int, long, float, str, unicode, and None) are returned as-is.

If no other conversion is appropriate, the Python builtin function str() is used to convert the object.