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.
-
ait.core.json.
slotsToJSON
(obj, slots=None)¶ Converts the given Python object to one suitable for Javascript Object Notation (JSON) serialization via
json.dump()
orjson.dumps()
. This function delegates totoJSON()
.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()
orjson.dumps()
. If the Python object has atoJSON()
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
, andNone
) are returned as-is.If no other conversion is appropriate, the Python builtin function
str()
is used to convert the object.