pygsti.serialization.jsoncodec

Defines JSON-format encoding and decoding functions

Module Contents

Functions

encode_obj(py_obj, binary)

Returns JSON-compatible version of py_obj.

decode_obj(json_obj, binary)

Inverse of encode_obj().

pygsti.serialization.jsoncodec.encode_obj(py_obj, binary)

Returns JSON-compatible version of py_obj.

Constructs in-memory a JSON-format-compatible copy of the Python object py_obj, handling pyGSTi objects appropriately. When binary=False, the output must contain only ASCII-compatible strings (no ‘bytes’), otherwise the output is allowed to contain non-ASCII string values (OK for binary formats like MSGPACK and BSON).

Parameters

py_objobject

The object to encode.

binarybool

Whether the output is allowed to have binary-mode strings or not.

Returns

object

A JSON-format compatible object. Usually a dict, list, or string.

pygsti.serialization.jsoncodec.decode_obj(json_obj, binary)

Inverse of encode_obj().

Decodes the JSON-compatible json_obj object into the original Python object that was encoded.

Parameters

json_objobject

The JSON-compabtible object to decode. Note that this is NOT a JSON string, but rather the object that would be decoded from such a string (by json.loads, for instance).

binarybool

Whether json_obj is a binary format or not. If so, then the decoding expects all strings to be binary strings i.e. b’name’ instead of just ‘name’. The value of this argument should match that used in the original call to encode_obj().

Returns

object

A Python object.