Skip to main content

CBOR-LD encode and decode for JSON-LD documents

Project description

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 = await 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 = await decode(
    cborld_bytes=cborld_bytes,
    document_loader=my_loader,
)

Document Loader

Both encode and decode require a document_loader -- an async 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 = await 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:

async 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 = await 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 = await 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 async (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 async (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 async (str) -> dict Resolves context URLs.
type_table_loader async (registry_entry_id=int) -> dict Resolves registry IDs to type tables.
diagnose (str) -> None Optional diagnostic callback.

Contribute

Please follow the existing code style.

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

Specifications

License

BSD-3-Clause © Digital Bazaar / Subfile

Project details


Download files

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

Source Distribution

cborld-0.1.0.tar.gz (30.7 kB view details)

Uploaded Source

Built Distribution

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

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

Uploaded Python 3

File details

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

File metadata

  • Download URL: cborld-0.1.0.tar.gz
  • Upload date:
  • Size: 30.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for cborld-0.1.0.tar.gz
Algorithm Hash digest
SHA256 51d76709bf4d63f00f95f2b114a7f9396671b53ea6328bbc612d6484b3ae5d47
MD5 c17579fd0d0957b1b91b5f62a179f505
BLAKE2b-256 7b7665ce972c7998b9b9827913573a8ee1beae64fa5e0a710d8cb8266859e91c

See more details on using hashes here.

Provenance

The following attestation bundles were made for cborld-0.1.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-0.1.0-py3-none-any.whl.

File metadata

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

File hashes

Hashes for cborld-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 6bcce902c9684292a8737a002ab69753ad4cc9f0ca7927b6c4e16f1a135336bd
MD5 3f806d7921ff643699ec2b36f1a18624
BLAKE2b-256 431e26c03a957c9dc4fcb53b93727360350d32ab0835118d361aa81e6a347870

See more details on using hashes here.

Provenance

The following attestation bundles were made for cborld-0.1.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.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page