base64¶
Utilities to encode and decode Base64.
New in version 1.10.
-
oslo_serialization.base64.decode_as_bytes(encoded)¶ Decode a Base64 encoded string.
Parameters: encoded – bytes or text Base64 encoded string to be decoded Returns: decoded bytes string (bytes) Use decode_as_text() to get the decoded string as text.
-
oslo_serialization.base64.decode_as_text(encoded, encoding='utf-8')¶ Decode a Base64 encoded string.
Decode the Base64 string and then decode the result from encoding (UTF-8 by default).
Parameters: encoded – bytes or text Base64 encoded string to be decoded Returns: decoded text string (bytes) Use decode_as_bytes() to get the decoded string as bytes.
-
oslo_serialization.base64.encode_as_bytes(s, encoding='utf-8')¶ Encode a string using Base64.
If s is a text string, first encode it to encoding (UTF-8 by default).
Parameters: - s – bytes or text string to be encoded
- encoding – encoding used to encode s if it’s a text string
Returns: Base64 encoded byte string (bytes)
Use encode_as_text() to get the Base64 encoded string as text.
-
oslo_serialization.base64.encode_as_text(s, encoding='utf-8')¶ Encode a string using Base64.
If s is a text string, first encode it to encoding (UTF-8 by default).
Parameters: - s – bytes or text string to be encoded
- encoding – encoding used to encode s if it’s a text string
Returns: Base64 encoded text string (Unicode)
Use encode_as_bytes() to get the Base64 encoded string as bytes.
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
anyjsonto use theloads()anddumps()wrappers ifanyjsonis available.
-
oslo_serialization.jsonutils.dump(obj, fp, *args, **kwargs)¶ Serialize
objas a JSON formatted stream tofpParameters: - obj – object to be serialized
- fp – a
.write()-supporting file-like object - default – function that returns a serializable version of an object,
to_primitive()is used by default. - args – extra arguments, please see documentation of json.dump
- kwargs –
extra named parameters, please see documentation of json.dump
Changed in version 1.3: The default parameter now uses
to_primitive()by default.
-
oslo_serialization.jsonutils.dump_as_bytes(obj, default=<function to_primitive>, encoding='utf-8', **kwargs)¶ Serialize
objto a JSON formattedbytes.Parameters: - obj – object to be serialized
- default – function that returns a serializable version of an object,
to_primitive()is used by default. - encoding – encoding used to encode the serialized JSON output
- kwargs – extra named parameters, please see documentation of json.dumps
Returns: json formatted string
New in version 1.10.
-
oslo_serialization.jsonutils.dumps(obj, default=<function to_primitive>, **kwargs)¶ Serialize
objto a JSON formattedstr.Parameters: - obj – object to be serialized
- default – function that returns a serializable version of an object,
to_primitive()is used by default. - kwargs –
extra named parameters, please see documentation of json.dumps
Returns: json formatted string
Use dump_as_bytes() to ensure that the result type is
byteson Python 2 and Python 3.
-
oslo_serialization.jsonutils.load(fp, encoding='utf-8', **kwargs)¶ Deserialize
fpto 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(astrorunicodeinstance 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=Trueis lossy ... be aware.Changed in version 1.3: Support UUID encoding.
Changed in version 1.6: Dictionary keys are now also encoded.