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.
pip install py-ubjsonVerified import paths — ran on the pinned version, not inferred.
This example demonstrates how to encode a Python dictionary into UBJSON bytes and then decode it back using `ubjson.dumpb` and `ubjson.loadb`.
To explicitly skip the extension build (e.g., for troubleshooting), set the environment variable `PYUBJSON_NO_EXTENSION=1` before installation: `PYUBJSON_NO_EXTENSION=1 pip install py-ubjson`. To verify if the extension is enabled at runtime, check `ubjson.EXTENSION_ENABLED`.
Avoid generating No-Op types in your data when encoding. When decoding, be aware of its specific limitations if encountering No-Op types in incoming UBJSON streams.
When encoding, `py-ubjson` will produce dynamically-typed containers. When decoding, understand that while strongly-typed containers can be consumed, you cannot explicitly create them using the `py-ubjson` encoder at this time.
Ensure the library is installed using pip: `pip install py-ubjson`. Then, import it correctly in your Python code: `import ubjson`.
To fix this, install the necessary C/C++ build tools for your operating system (e.g., Visual Studio Build Tools on Windows). Alternatively, you can skip building the C extension by setting an environment variable before installation: `PYUBJSON_NO_EXTENSION=1 pip install py-ubjson`.
Convert the non-serializable object into a UBJSON-compatible type (like a string, list, or dictionary) before encoding, or implement a custom UBJSON encoder if you frequently deal with specific complex types.
Use `ubjson.load()` for reading UBJSON data directly from a file-like object, or read the file's content into a bytes object first and then use `ubjson.loads()`: `with open('file.ubjson', 'rb') as f: data = ubjson.load(f)` or `with open('file.ubjson', 'rb') as f: content = f.read(); data = ubjson.loads(content)`.Ensure that the encoding used when creating the UBJSON data matches the encoding expected when decoding. If the UBJSON stream contains text that isn't UTF-8, you might need to handle the decoding explicitly or ensure the source data uses a consistent, supported encoding.