Universal Binary JSON

0.16.1 · active · verified Sat Apr 11

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

Install

Imports

Quickstart

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

view raw JSON →