Skip to content

llllllllll/msgspec

 
 

Repository files navigation

msgspec

github pypi

msgspec is a fast and friendly implementation of the MessagePack (msgpack) protocol for Python 3.8 . In addition to serialization/deserialization, it supports 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.decode(serialized_data, type=User)

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.

https://github.com/jcrist/msgspec/raw/master/docs/source/_static/bench-1.png

See the documentation for more information.

LICENSE

New BSD. See the License File.

About

A fast and friendly MessagePack library for Python

Resources

License

Stars

Watchers

Forks

Packages

 
 
 

Languages

  • Python 52.8%
  • C 47.2%