CBOR-LD encode and decode for JSON-LD documents
Project description
Python CBOR-LD Processor
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
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
51d76709bf4d63f00f95f2b114a7f9396671b53ea6328bbc612d6484b3ae5d47
|
|
| MD5 |
c17579fd0d0957b1b91b5f62a179f505
|
|
| BLAKE2b-256 |
7b7665ce972c7998b9b9827913573a8ee1beae64fa5e0a710d8cb8266859e91c
|
Provenance
The following attestation bundles were made for cborld-0.1.0.tar.gz:
Publisher:
publish.yml on subfile-llc/cborld
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
cborld-0.1.0.tar.gz -
Subject digest:
51d76709bf4d63f00f95f2b114a7f9396671b53ea6328bbc612d6484b3ae5d47 - Sigstore transparency entry: 1634040634
- Sigstore integration time:
-
Permalink:
subfile-llc/cborld@b5a6a2c536c68b2e8e65c7a1f118d2d0684763eb -
Branch / Tag:
refs/tags/v0.1.0 - Owner: https://github.com/subfile-llc
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@b5a6a2c536c68b2e8e65c7a1f118d2d0684763eb -
Trigger Event:
release
-
Statement type:
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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
6bcce902c9684292a8737a002ab69753ad4cc9f0ca7927b6c4e16f1a135336bd
|
|
| MD5 |
3f806d7921ff643699ec2b36f1a18624
|
|
| BLAKE2b-256 |
431e26c03a957c9dc4fcb53b93727360350d32ab0835118d361aa81e6a347870
|
Provenance
The following attestation bundles were made for cborld-0.1.0-py3-none-any.whl:
Publisher:
publish.yml on subfile-llc/cborld
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
cborld-0.1.0-py3-none-any.whl -
Subject digest:
6bcce902c9684292a8737a002ab69753ad4cc9f0ca7927b6c4e16f1a135336bd - Sigstore transparency entry: 1634040662
- Sigstore integration time:
-
Permalink:
subfile-llc/cborld@b5a6a2c536c68b2e8e65c7a1f118d2d0684763eb -
Branch / Tag:
refs/tags/v0.1.0 - Owner: https://github.com/subfile-llc
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@b5a6a2c536c68b2e8e65c7a1f118d2d0684763eb -
Trigger Event:
release
-
Statement type: