msgspec
msgspec
is a fast and friendly implementation of the MessagePack protocol for Python 3.8+. In addition to serialization/deserialization, it supports runtime message validation using schemas defined via Python’s type annotations.
from typing import Optional, List
import msgspec
# Define a schema for a `User` type
class User(msgspec.Struct):
name: str
groups: List[str] = []
email: Optional[str] = None
# Create a `User` object
alice = User("alice", groups=["admin", "engineering"])
# Serialize `alice` to `bytes` using the MessagePack protocol
serialized_data = msgspec.encode(alice)
# Deserialize and validate the message as a User type
user = msgspec.Decoder(User).decode(serialized_data)
assert user == alice
msgspec
is designed to be as performant as possible, while retaining some of the nicities of validation libraries like pydantic. For supported types, serializing a message with msgspec
can be ~2-4x faster than alternative libraries.
See the documentation for more information.
LICENSE
New BSD. See the License File.