pygsti.serialization.jsoncodec

Defines JSON-format encoding and decoding functions

Module Contents

Functions

_class_hasattr(instance, attr)

Helper function for checking if instance.__class__ has an attribute

encode_obj(py_obj, binary)

Returns JSON-compatible version of py_obj.

_encode_std_obj(py_obj, binary)

Helper to encode_obj() that encodes only "standard" (non-pyGSTi) types

decode_obj(json_obj, binary)

Inverse of encode_obj().

_decode_std_base(json_obj, start, binary)

Helper to decode_obj() for decoding pyGSTi objects that are derived from a standard type.

_decode_std_obj(json_obj, binary)

Helper to decode_obj() that decodes standard (non-pyGSTi) types.

_tostr(x)

Convert a value to the native string format.

_tobin(x)

Serialize strings to UTF8

_ident(x)

pygsti.serialization.jsoncodec._class_hasattr(instance, attr)

Helper function for checking if instance.__class__ has an attribute

Parameters
  • instance (obj) – instance to check

  • attr (str) – attribute name

Returns

bool

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_obj (object) – The object to encode.

  • binary (bool) – 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._encode_std_obj(py_obj, binary)

Helper to encode_obj() that encodes only “standard” (non-pyGSTi) types

Parameters
  • py_obj (object) – standard Python object to encode

  • binary (bool) – whether to use binary-mode strings

Returns

dict

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_obj (object) – 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).

  • binary (bool) – 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.

pygsti.serialization.jsoncodec._decode_std_base(json_obj, start, binary)

Helper to decode_obj() for decoding pyGSTi objects that are derived from a standard type.

Parameters
  • json_obj (dict) – json-loaded dict to decode from

  • start (various) – Starting object that serves as a container for elements of the standard-Python base class (e.g. a list).

  • binary (bool) – Whether or not to use binary-mode strings as dict keys.

Returns

object

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

Helper to decode_obj() that decodes standard (non-pyGSTi) types.

Parameters
  • json_obj (dict) – json-loaded dictionary encoding an object

  • binary (bool) – Whether or not to use binary-mode strings as dict keys.

Returns

object

pygsti.serialization.jsoncodec._tostr(x)

Convert a value to the native string format.

Parameters

x (str or bytes) – value to convert to a native string.

Returns

str

pygsti.serialization.jsoncodec._tobin(x)

Serialize strings to UTF8

Parameters

x (str or bytes) – value to convert to a UTF8 binary string.

Returns

bytes

pygsti.serialization.jsoncodec._ident(x)