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()
anddumps()
. Thedumps()
wrapper will automatically useto_primitive()
for you if needed. - This sets up
anyjson
to use theloads()
anddumps()
wrappers ifanyjson
is available.
-
oslo_serialization.jsonutils.
dump
(obj, fp, *args, **kwargs)¶ Serialize
obj
as a JSON formatted stream tofp
Parameters:
-
oslo_serialization.jsonutils.
dumps
(obj, default=<function to_primitive>, **kwargs)¶ Serialize
obj
to a JSON formattedstr
.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
- fp – a
-
oslo_serialization.jsonutils.
loads
(s, encoding='utf-8', **kwargs)¶ Deserialize
s
(astr
orunicode
instance containing a JSONParameters: - s – string to deserialize
- encoding – encoding used to interpret the string
- kwargs –
extra named parameters, please see documentation of json.loads
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¶
Msgpack related utilities.
This module provides a few things:
- A handy registry for getting an object down to something that can be
msgpack serialized. See
HandlerRegistry
. - Wrappers around
loads()
anddumps()
. Thedumps()
wrapper will automatically use thedefault_registry
for you if needed.
-
class
oslo_serialization.msgpackutils.
HandlerRegistry
¶ Registry of type specific msgpack handlers extensions.
See: https://github.com/msgpack/msgpack/blob/master/spec.md#formats-ext
Do note that due to the current limitations in the msgpack python library we can not currently dump/load a tuple without converting it to a list.
This may be fixed in: https://github.com/msgpack/msgpack-python/pull/100
-
get
(identity)¶ Get the handle for the given numeric identity (or none).
-
match
(obj)¶ Match the registries handlers to the given object (or none).
-
register
(handler)¶ Register a extension handler to handle its associated type.
-
-
oslo_serialization.msgpackutils.
default_registry
= <oslo_serialization.msgpackutils.HandlerRegistry object>¶ Default, read-only/frozen registry that will be used when none is provided.
This registry has msgpack extensions for the following:
DateTime
objects.Date
objects.UUID
objects.itertools.count
objects/iterators.set
andfrozenset
container(s).netaddr.IPAddress
objects (only ifnetaddr
is importable).xmlrpclib.DateTime
datetime objects.
-
oslo_serialization.msgpackutils.
dump
(obj, fp, registry=None)¶ Serialize
obj
as a messagepack formatted stream tofp
.
-
oslo_serialization.msgpackutils.
dumps
(obj, registry=None)¶ Serialize
obj
to a messagepack formattedstr
.
-
oslo_serialization.msgpackutils.
load
(fp, registry=None)¶ Deserialize
fp
into a Python object.
-
oslo_serialization.msgpackutils.
loads
(s, registry=None)¶ Deserialize
s
messagepackstr
into a Python object.