BSON Format
Lodum provides BSON (Binary JSON) support for MongoDB-compatible binary serialization. BSON is designed for efficient storage and traversal of documents.Installation
BSON support requires thepymongo 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 forbson.encode
- The BSON bytes if
targetis None, otherwise None
{"_v": value}.
Example:
dumps()
dump(obj). Provided for compatibility.
load()
cls: The class to instantiatesource: BSON 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
BSON natively supports binary data with multiple binary subtypes:MongoDB Integration
BSON is the native format for MongoDB, making Lodum ideal for MongoDB workflows:BSON Document Structure
BSON requires a document (dictionary) at the root level. Lodum handles this automatically:Type Preservation
BSON provides good type preservation with some MongoDB-specific types:Streaming Large Collections
BSON’s streaming support enables efficient processing of large datasets:BSON Limitations
BSON has some constraints to be aware of:- Document size: Limited to 16MB per document in MongoDB
- Key names: Cannot contain null characters or start with
$(MongoDB restriction) - Nesting depth: Practical limits on nested documents (MongoDB: 100 levels)
- Field order: BSON preserves field order, unlike standard JSON
Use Cases
BSON is ideal for:- MongoDB integration
- Document-oriented storage
- Binary data with metadata
- Traversable binary formats
- Applications requiring field order preservation
- Non-MongoDB applications (consider CBOR or MessagePack)
- Maximum space efficiency (BSON includes type information)
- Very large documents (>16MB)
- Network protocols (more overhead than MessagePack/CBOR)