CBOR Format
Lodum provides CBOR (Concise Binary Object Representation) support for efficient, standards-based binary serialization. CBOR is defined in RFC 8949 and is designed for small code size and message size.Installation
CBOR support requires thecbor2 library:
API Reference
dump()
obj: The object to encode (must be a@lodum-decorated class)target: Optional file-like object or Path to write to**kwargs: Additional arguments forcbor2.dump(s)
- The CBOR bytes if
targetis None, otherwise None
dumps()
dump(obj). Provided for compatibility.
load()
cls: The class to instantiatesource: CBOR bytes, file-like object, or Pathmax_size: Maximum allowed size for bytes input (default: 10MB)
- An instance of
cls
loads()
load(cls, source). Provided for compatibility.
stream()
cls: The class to instantiate for each itemsource: A binary stream, file-like object, or Path
- An iterator yielding instances of
cls
Binary Data Handling
CBOR natively supports binary data with dedicated byte string types:CBOR vs MessagePack
Both CBOR and MessagePack are binary formats, but they have different design goals:| Feature | CBOR | MessagePack |
|---|---|---|
| Standard | RFC 8949 | De facto standard |
| Design goal | Small code size | Small message size |
| Extensibility | Excellent (tags) | Limited |
| Floating point | Full IEEE 754 | Full IEEE 754 |
| Timestamps | Native support | Extension |
| Ecosystem | Growing | Mature |
- You need standards compliance
- You want extensibility via CBOR tags
- You’re working in constrained environments
- You need semantic tagging
- You need maximum speed
- You want the smallest message size
- You need mature library support
Type Preservation
CBOR provides excellent type preservation:Streaming for Large Datasets
CBOR’s streaming support enables memory-efficient processing:Use Cases
CBOR is ideal for:- IoT and embedded systems
- Constrained network protocols
- Standards-compliant systems
- Interoperability with other languages
- Cryptographic applications (COSE)
- Semantic data representation
- Human-readable data
- Legacy systems requiring text formats
- Maximum performance scenarios (use MessagePack)