MessagePack Format
Lodum provides MessagePack support for efficient binary serialization. MessagePack is significantly smaller and faster than JSON while maintaining similar simplicity.Installation
MessagePack support requires themsgpack 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 formsgpack.packb
- The MessagePack bytes if
targetis None, otherwise None
use_bin_type=True is set by default for proper binary handling.
Example:
dumps()
dump(obj). Provided for compatibility.
load()
cls: The class to instantiatesource: MessagePack 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
MessagePack natively supports binary data, which is one of its key advantages over JSON:Performance Benefits
MessagePack offers significant advantages over text formats:- Size: Typically 30-50% smaller than JSON
- Speed: 2-3x faster serialization/deserialization
- Binary support: No base64 encoding needed for bytes
- Type preservation: Better integer and float precision
ND-MessagePack Streaming
MessagePack supports newline-delimited (ND) streaming for processing large datasets:Use Cases
MessagePack is ideal for:- High-performance APIs
- Binary protocols
- Data streaming
- Mobile applications (reduced bandwidth)
- IoT and embedded systems
- Caching layers
- Human-readable configuration
- Web browsers without additional libraries
- Systems requiring text-based debugging