MessagePack serializer for efficient binary data exchange among multiple languages
—
High-level convenience functions that provide simple interfaces for the most common MessagePack serialization tasks. These functions handle one-shot packing and unpacking operations with automatic resource management.
Converts Python objects to MessagePack binary format with automatic type detection and encoding.
def packb(o, **kwargs):
"""
Pack object `o` and return packed bytes.
Parameters:
- o: Python object to pack
- default: callable to handle unsupported types
- use_single_float: bool, use 32-bit floats (default: False)
- use_bin_type: bool, use bin type for bytes (default: True)
- strict_types: bool, check exact types (default: False)
- datetime: bool, pack datetime as Timestamp (default: False)
- unicode_errors: str, Unicode error handling (default: 'strict')
Returns:
bytes: Packed binary data
Raises:
TypeError: When object cannot be serialized
OverflowError: When numbers are too large
"""Converts MessagePack binary data back to Python objects with configurable options for type handling and security.
def unpackb(packed, **kwargs):
"""
Unpack an object from `packed` bytes.
Parameters:
- packed: bytes, MessagePack binary data
- raw: bool, return bytes instead of str (default: False)
- use_list: bool, use list instead of tuple (default: True)
- timestamp: int, timestamp unpacking mode (0-3, default: 0)
- strict_map_key: bool, restrict map key types (default: True)
- object_hook: callable, hook for dict objects
- object_pairs_hook: callable, hook for key-value pairs
- unicode_errors: str, Unicode error handling (default: 'strict')
- max_buffer_size: int, buffer size limit (default: 100MB)
- ext_hook: callable, extension type handler
Returns:
object: Unpacked Python object
Raises:
ExtraData: When packed contains extra bytes
ValueError: When packed is incomplete
FormatError: When packed is not valid msgpack
StackError: When packed contains too nested data
"""Packs Python objects and writes directly to a file-like object, useful for efficient file operations.
def pack(o, stream, **kwargs):
"""
Pack object `o` and write it to `stream`.
Parameters:
- o: Python object to pack
- stream: file-like object with write() method
- **kwargs: All Packer options (see packb)
Returns:
None
Raises:
TypeError: When object cannot be serialized
OverflowError: When numbers are too large
"""Unpacks MessagePack data from a file-like object, automatically reading all available data.
def unpack(stream, **kwargs):
"""
Unpack an object from `stream`.
Parameters:
- stream: file-like object with read() method
- **kwargs: All Unpacker options (see unpackb)
Returns:
object: Unpacked Python object
Raises:
ExtraData: When stream contains extra bytes
ValueError: When stream data is incomplete
FormatError: When stream data is not valid msgpack
StackError: When stream data contains too nested data
"""JSON and pickle compatibility functions that provide familiar interfaces for users migrating from other serialization libraries.
def loads(packed, **kwargs):
"""Alias for unpackb (compatibility with json and pickle)."""
def dumps(o, **kwargs):
"""Alias for packb (compatibility with json and pickle)."""
def load(stream, **kwargs):
"""Alias for unpack (compatibility with json and pickle)."""
def dump(o, stream, **kwargs):
"""Alias for pack (compatibility with json and pickle)."""import datetime
import msgpack
def default_handler(obj):
if isinstance(obj, datetime.datetime):
return {'__datetime__': True, 'iso': obj.isoformat()}
raise TypeError(f'Object of type {type(obj)} is not JSON serializable')
def object_hook(obj):
if '__datetime__' in obj:
return datetime.datetime.fromisoformat(obj['iso'])
return obj
# Pack with custom type
data = {'timestamp': datetime.datetime.now()}
packed = msgpack.packb(data, default=default_handler)
# Unpack with custom type
unpacked = msgpack.unpackb(packed, object_hook=object_hook)import msgpack
# Safe unpacking with limits
try:
data = msgpack.unpackb(
untrusted_data,
max_buffer_size=1024*1024, # 1MB limit
strict_map_key=True, # Only str/bytes keys
raw=False # Decode strings
)
except msgpack.ExtraData as e:
print(f"Extra data found: {len(e.extra)} bytes")
data = e.unpackedInstall with Tessl CLI
npx tessl i tessl/pypi-msgpack