Universal Binary JSON
py-ubjson is a Python library providing a Universal Binary JSON (UBJSON) encoder and decoder, adhering to the draft-12 specification. It aims to offer an API similar to Python's built-in `json` module for seamless serialization and deserialization of UBJSON data. The library includes an optional C extension for significant performance improvements. The current version is 0.16.1.
Warnings
- gotcha The C extension is not strictly required but offers a significant speed boost. If performance is critical, ensure Cython is available during installation to build the extension. Installations via `pip install py-ubjson` typically attempt to build the extension by default.
- gotcha The UBJSON 'No-Op' type is only supported by the decoder, not the encoder. Additionally, its usage is restricted to the start or between elements of a container and only within un-typed containers, as its presence in typed containers can lead to ambiguity.
- gotcha Strongly-typed containers (e.g., for arrays or objects) are primarily supported by the decoder (with the exception of bytes/bytearray). The encoder does not currently support generating strongly-typed containers, and they do not support the No-Op type.
Install
-
pip install py-ubjson
Imports
- ubjson
import ubjson
- dumpb
from ubjson import dumpb
- loadb
from ubjson import loadb
- dump
from ubjson import dump
- load
from ubjson import load
Quickstart
import ubjson
data = {
'name': 'Alice',
'age': 30,
'isStudent': False,
'courses': ['Math', 'Science'],
'grades': {'Math': 95, 'Science': 88}
}
# Encode to UBJSON bytes
encoded_data = ubjson.dumpb(data)
print(f"Encoded (bytes): {encoded_data!r}")
# Decode from UBJSON bytes
decoded_data = ubjson.loadb(encoded_data)
print(f"Decoded: {decoded_data}")
assert data == decoded_data