JSON related utilities.
This module provides a few things:
1) A handy function for getting an object down to something that can be JSON serialized. See to_primitive().
2) Wrappers around loads() and dumps(). The dumps() wrapper will automatically use to_primitive() for you if needed.
3) This sets up anyjson to use the loads() and dumps() wrappers if anyjson is available.
Serialize obj as a JSON formatted stream to fp :param obj: object to be serialized :param fp: a .write()-supporting file-like object :param args: extra arguments, please see documentation of json.dump :param kwargs: extra named parameters, please see documentation of json.dump
Serialize obj to a JSON formatted str. :param obj: object to be serialized :param default: function that returns a serializable version of an object :param kwargs: extra named parameters, please see documentation of json.dumps :returns: json formatted string
Deserialize fp (a .read()-supporting file-like object containing a JSON document) to a Python object. :param fp: a .write()-supporting file-like object :param encoding: encoding used to interpret the string :param kwargs: extra named parameters, please see documentation of json.loads :returns: python object
Deserialize s (a str or unicode instance containing a JSON :param s: string to deserialize :param encoding: encoding used to interpret the string :param kwargs: extra named parameters, please see documentation of json.loads :returns: python object
Convert a complex object into primitives.
Handy for JSON serialization. We can optionally handle instances, but since this is a recursive function, we could have cyclical data structures.
To handle cyclical data structures we could track the actual objects visited in a set, but not all objects are hashable. Instead we just track the depth of the object inspections and don’t go too deep.
Therefore, convert_instances=True is lossy ... be aware.