Registry / serialization / py-ubjson

py-ubjson

JSON →
library0.16.1pypypi✓ verified 25d ago

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-ubjson
INSTALL
IMPORT
SIG · PY-UBJSON
P
py-ubjson
serializationpythonv0.16.1
Install
2.4s avg
Import
10ms
Disk
17MB
Pass rate
10/ 10
Env Coverage10 / 10
glibc
3.93.13
musl
3.93.13
Install & Compatibility
Where this runs
tested against v0.16.1 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
py 3.103.95 runs
installs and imports cleanly · install 0.0s · import 0.010s · 19.3MB
glibc
py 3.103.95 runs
installs and imports cleanly · install 2.4s · import 0.006s · 20MB
17MB installed
● package 17MB
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

ubjson
import ubjson
dumpb
from ubjson import dumpb
Used to encode Python objects to a UBJSON `bytes` object.
loadb
from ubjson import loadb
Used to decode a UBJSON `bytes` object to a Python object.
dump
from ubjson import dump
Used to encode Python objects to a file-like object.
load
from ubjson import load
Used to decode a file-like object containing UBJSON data.

This example demonstrates how to encode a Python dictionary into UBJSON bytes and then decode it back using `ubjson.dumpb` and `ubjson.loadb`.

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
Debug
Known issues
gotchaThe 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.
fix
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`.
affects: All versions
gotchaThe 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.
fix
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.
affects: All versions
gotchaStrongly-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.
fix
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.
affects: All versions
Errors
Common errors & fixes
ModuleNotFoundError: No module named 'ubjson'
The 'py-ubjson' library is either not installed in your Python environment or you are attempting to import it with an incorrect module name.
fix
Ensure the library is installed using pip: `pip install py-ubjson`. Then, import it correctly in your Python code: `import ubjson`.
error: command 'cl.exe' failed with exit status 2 (or similar compilation error during installation)
The optional C extension for 'py-ubjson' failed to compile during installation, typically due to a missing C/C++ compiler (like Visual Studio Build Tools on Windows) or a problem with Cython.
fix
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`.
TypeError: Object of type <ClassName> is not UBJSON serializable
You are attempting to encode a Python object (e.g., a `datetime` object, a `set`, or a custom class instance) that `py-ubjson` does not natively support for serialization into UBJSON.
fix
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.
TypeError: the UBJSON object must be bytes or bytearray, not TextIOWrapper
You are using `ubjson.loads()` (which expects a bytes or bytearray object as input) with a file-like object (like one returned by `open()`).
fix
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)`.
UnicodeDecodeError: 'utf-8' codec can't decode byte 0x... in position ...: invalid start byte
This error occurs when `py-ubjson` (or Python internally) attempts to decode UBJSON bytes containing string data using an incorrect or incompatible character encoding, usually when the data was originally encoded with a different codec.
fix
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.
Upgrade
Version history
0.16.1latest on PyPI · released Apr 18, 2020
Audit
Dependencies
pythonrequiredRuntime dependency for the library.
cythonoptionalOptional build dependency for the C extension, which provides a significant speed boost.
Agent activity
12 hits · last 30 days
node
8
Resources
py-ubjson — pip install py-ubjson · libregistry