jsonutils
JSON related utilities.
This module provides a few things:
- A handy function for getting an object down to something that can be
JSON serialized. See to_primitive().
- Wrappers around loads() and dumps(). The dumps()
wrapper will automatically use to_primitive() for you if needed.
- This sets up anyjson to use the loads() and dumps()
wrappers if anyjson is available.
-
oslo_serialization.jsonutils.dump(obj, fp, *args, **kwargs)
Serialize obj as a JSON formatted stream to fp
-
oslo_serialization.jsonutils.dumps(obj, default=<function to_primitive at 0x1c68cf8>, **kwargs)
Serialize obj to a JSON formatted str.
| Parameters: |
- obj – object to be serialized
- default – function that returns a serializable version of an object
- kwargs – extra named parameters, please see documentation of json.dumps
|
| Returns: | json formatted string
|
-
oslo_serialization.jsonutils.load(fp, encoding='utf-8', **kwargs)
Deserialize fp to a Python object.
| Parameters: |
- fp – a .read() -supporting file-like object
- encoding – encoding used to interpret the string
- kwargs – extra named parameters, please see documentation of json.loads
|
| Returns: | python object
|
-
oslo_serialization.jsonutils.loads(s, encoding='utf-8', **kwargs)
Deserialize s (a str or unicode instance containing a JSON
| Parameters: |
|
| Returns: | python object
|
-
oslo_serialization.jsonutils.to_primitive(value, convert_instances=False, convert_datetime=True, level=0, max_depth=3)
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.
msgpackutils