u-msgpack-python

2.8.0 · active · verified Sun Apr 12

u-msgpack-python is a lightweight MessagePack serializer and deserializer module written entirely in pure Python. It is compatible with both Python 2 and 3, as well as CPython and PyPy implementations, and fully complies with the latest MessagePack specification, including support for binary, UTF-8 string, application-defined ext, and timestamp types. The current version is 2.8.0, with releases occurring a few times per year to maintain compliance and add features.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to pack Python objects into MessagePack bytes and unpack them. It also shows an example of streaming serialization and deserialization using file-like objects for multiple MessagePack objects.

import umsgpack

data = {"name": "Alice", "age": 30, "is_student": False}

# Serialize a Python object to MessagePack bytes
packed_data = umsgpack.packb(data)
print(f"Packed data: {packed_data}")

# Deserialize MessagePack bytes back to a Python object
unpacked_data = umsgpack.unpackb(packed_data)
print(f"Unpacked data: {unpacked_data}")

# Example with streaming to/from a file-like object
from io import BytesIO

f = BytesIO()
umsgpack.pack({"compact": True, "schema": 0}, f)
umsgpack.pack([1, 2, 3], f)
f.seek(0)

# Unpack multiple objects from the stream
obj1 = umsgpack.unpack(f)
obj2 = umsgpack.unpack(f)
print(f"Object 1 from stream: {obj1}")
print(f"Object 2 from stream: {obj2}")

view raw JSON →