qpanda3_runtime.utils.convert_json_bytes#

Functions#

bytes_to_json_safe(data)

Convert data containing bytes to JSON-safe format.

json_safe_to_bytes(data)

Restore bytes data from JSON-safe format.

demo_bytes_serialization()

Demonstrate bytes serialization and deserialization.

Module Contents#

qpanda3_runtime.utils.convert_json_bytes.bytes_to_json_safe(data)#

Convert data containing bytes to JSON-safe format.

This function recursively traverses the data structure and converts any bytes objects to a JSON-safe dictionary format using base64 encoding.

Parameters:

data (any) – The input data that may contain bytes objects. Can be a simple value, list, tuple, or dictionary.

Returns:

JSON-safe data structure where bytes objects are replaced with dictionaries containing base64-encoded strings.

Return type:

any

Notes

  • Bytes objects are encoded as dictionaries with ‘__bytes__’: True key and base64-encoded ‘data’ field

  • The function handles nested structures recursively

  • Use json_safe_to_bytes to restore the original data

Examples

>>> data = {"text": "Hello", "binary": b""}
>>> json_safe = bytes_to_json_safe(data)
>>> json_str = json.dumps(json_safe)
qpanda3_runtime.utils.convert_json_bytes.json_safe_to_bytes(data)#

Restore bytes data from JSON-safe format.

This function recursively traverses the JSON-safe data structure and restores bytes objects from the base64-encoded dictionary format.

Parameters:

data (any) – JSON-safe data structure that may contain dictionaries with ‘__bytes__’: True key and base64-encoded ‘data’ field.

Returns:

Original data structure with bytes objects restored.

Return type:

any

Raises:

ValueError – If the base64 decoding fails for any reason.

Notes

  • This function is the inverse of bytes_to_json_safe

  • It handles nested structures recursively

  • The input should be the output of bytes_to_json_safe or compatible format

Examples

>>> json_safe = {"text": "Hello", "binary": {"__bytes__": True, "data": "AAEC"}}
>>> restored = json_safe_to_bytes(json_safe)
>>> restored["binary"] == b""
True
qpanda3_runtime.utils.convert_json_bytes.demo_bytes_serialization()#

Demonstrate bytes serialization and deserialization.

This function shows a complete example of converting data containing bytes to JSON-safe format and restoring it back.

Examples

>>> demo_bytes_serialization()
=== Bytes Serialization Demo ===
Original data:
  binary_data: b'\x00\x01\x02\x03\x04\x05'
  nested.image_data: b'fake_image_data_here'
...