Skip to main content

Python CBOR-LD Processor

CI PyPI Python

A Python CBOR-LD processor for encoding and decoding JSON-LD documents.

Table of Contents

Background

This library provides a CBOR-LD 1.0 processor for Python applications, ported from @digitalbazaar/cborld.

CBOR-LD is a compact binary serialization of JSON-LD that uses semantic compression to achieve significantly better compression ratios than general-purpose algorithms.

Features

  • Encode JSON-LD documents to compact CBOR-LD binary format
  • Decode CBOR-LD bytes back to JSON-LD
  • CBOR-LD 1.0 tag support (0xCB1D / 51997)
  • Legacy format support (legacy-range, legacy-singleton)
  • Built-in codecs: HTTP URLs, UUID URNs, DID URLs, data URLs, multibase, XSD dates/datetimes, cryptosuite strings
  • Custom type table / registry entry support

Requirements

Python 3.12+

Install

PyPI

pip install cborld

With optional PyLD support for remote context resolution:

pip install cborld[context]

Development

To install locally (for development):

git clone https://github.com/subfile-llc/cborld.git
cd cborld
pip install -e ".[dev]"

Usage

Encode

from cborld import encode

cborld_bytes = encode(
    jsonld_document={
        "@context": "https://www.w3.org/ns/activitystreams",
        "type": "Note",
        "summary": "CBOR-LD",
        "content": "CBOR-LD is awesome!",
    },
    document_loader=my_loader,
    registry_entry_id=1,
)

Decode

from cborld import decode

jsonld_document = decode(
    cborld_bytes=cborld_bytes,
    document_loader=my_loader,
)

Document Loader

Both encode and decode accept an optional document_loader — a sync callable that resolves JSON-LD context URLs. If you have PyLD installed, you can use the built-in adapter:

from cborld import pyld_document_loader

loader = pyld_document_loader(extra_documents={
    "https://example.org/v1": {"@context": {...}},
})

cborld_bytes = encode(
    jsonld_document=doc,
    document_loader=loader,
    registry_entry_id=1,
)

Using in Async Apps

encode and decode are synchronous. When called from an async application (FastAPI, Starlette, etc.) and the document loader performs blocking network I/O, run them in a thread pool:

import asyncio
from cborld import encode

cborld_bytes = await asyncio.to_thread(
    encode,
    jsonld_document=doc,
    document_loader=loader,
    registry_entry_id=1,
)

Custom Type Tables

For registry entry IDs other than 0 (uncompressed) and 1 (default table), supply a type_table_loader:

def my_type_table_loader(*, registry_entry_id: int) -> dict:
    return {
        "context": {"https://example.org/v1": 0x8000},
        "url": {"https://example.org/v1": 0x8000},
        "none": {"https://example.org/v1": 0x8000},
    }

cborld_bytes = encode(
    jsonld_document=doc,
    document_loader=loader,
    registry_entry_id=100,
    type_table_loader=my_type_table_loader,
)

Error Handling

All CBOR-LD processing errors raise CborldError, which carries a machine-readable .code attribute:

from cborld import CborldError

try:
    doc = decode(cborld_bytes=data, document_loader=loader)
except CborldError as e:
    print(f"Error [{e.code}]: {e}")

API

encode(**kwargs) -> bytes

Encodes a given JSON-LD document into CBOR-LD bytes.

Parameter Type Description
jsonld_document dict The JSON-LD document to encode.
document_loader (str) -> dict Resolves context URLs.
format str "cbor-ld-1.0" (default), "legacy-range", or "legacy-singleton".
registry_entry_id int Registry entry ID (0 = uncompressed, 1 = default table).
type_table_loader (registry_entry_id=int) -> dict Resolves registry IDs to type tables. Required when registry_entry_id > 1.
diagnose (str) -> None Optional diagnostic callback.

decode(**kwargs) -> dict

Decodes CBOR-LD bytes into a JSON-LD document.

Parameter Type Description
cborld_bytes bytes The CBOR-LD bytes to decode.
document_loader (str) -> dict Resolves context URLs.
type_table_loader (registry_entry_id=int) -> dict Resolves registry IDs to type tables.
diagnose (str) -> None Optional diagnostic callback.

Migrating from 0.1.x

Version 1.0.0 makes the public API synchronous. Drop await on encode / decode, and pass sync document_loader / type_table_loader callables. See CHANGELOG.md for the full migration notes.

Contribute

Please follow the existing code style.

pip install -e ".[dev]"
pytest
ruff check src tests

Specifications

License

BSD-3-Clause © Digital Bazaar / Subfile

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

cborld-1.0.0.tar.gz (31.0 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

cborld-1.0.0-py3-none-any.whl (36.5 kB view details)

Uploaded Python 3

File details

Details for the file cborld-1.0.0.tar.gz.

File metadata

  • Download URL: cborld-1.0.0.tar.gz
  • Upload date:
  • Size: 31.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/7.0.0 CPython/3.13.14

File hashes

Hashes for cborld-1.0.0.tar.gz
Algorithm Hash digest
SHA256 247206d3a4efae3f146ee7c9281be0482ff01abd8c5a0d58e031f319f2c34e92
MD5 f463c6718061fc509d64bc4b35668589
BLAKE2b-256 59b2e7603acf3d65056c7ab875c9f95d78cbda7d05fc15531d22c0b740b92b4f

See more details on using hashes here.

Provenance

The following attestation bundles were made for cborld-1.0.0.tar.gz:

Publisher: publish.yml on subfile-llc/cborld

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file cborld-1.0.0-py3-none-any.whl.

File metadata

  • Download URL: cborld-1.0.0-py3-none-any.whl
  • Upload date:
  • Size: 36.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/7.0.0 CPython/3.13.14

File hashes

Hashes for cborld-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 4316679ea0333d574130884c43da12c411bc46a331dc22170ec99a753f42be71
MD5 3a5430c94c52d43b43399685c83a1c15
BLAKE2b-256 8f529e05a5593bc08646a2e1e948278b220413867c1f76688b9a84967d43f292

See more details on using hashes here.

Provenance

The following attestation bundles were made for cborld-1.0.0-py3-none-any.whl:

Publisher: publish.yml on subfile-llc/cborld

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Release history Release notifications | RSS feed

This release

1.0.0 This release

2 files

0.1.0

2 files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page