Skip to content
forked from jcrist/msgspec

A fast and friendly JSON/MessagePack library, with optional schema validation

License

Notifications You must be signed in to change notification settings

yutiansut/msgspec

 
 

Repository files navigation

msgspec

github pypi conda codecov

msgspec is a fast and friendly serialization library for Python, supporting both JSON and MessagePack. It integrates well with Python's type annotations, providing ergonomic (and performant!) schema validation.

Define your message schemas using standard Python type annotations.

>>> from typing import Optional, Set

>>> import msgspec

>>> class User(msgspec.Struct):
...     """A new type describing a User"""
...     name: str
...     groups: Set[str] = set()
...     email: Optional[str] = None

Encode messages as JSON or MessagePack.

>>> alice = User("alice", groups={"admin", "engineering"})

>>> alice
User(name='alice', groups={"admin", "engineering"}, email=None)

>>> msg = msgspec.json.encode(alice)

>>> msg
b'{"name":"alice","groups":["admin","engineering"],"email":null}'

Decode messages back into Python types (with optional schema validation).

>>> msgspec.json.decode(msg, type=User)
User(name='alice', groups={"admin", "engineering"}, email=None)

>>> msgspec.json.decode(b'{"name":"bob","groups":[123]}', type=User)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
msgspec.ValidationError: Expected `str`, got `int` - at `$.groups[0]`

msgspec is designed to be as performant as possible, while retaining some of the nicities of validation libraries like pydantic. For supported types, encoding/decoding a message with msgspec can be ~2-40x faster than alternative libraries.

See the documentation for more information.

LICENSE

New BSD. See the License File.

About

A fast and friendly JSON/MessagePack library, with optional schema validation

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • Python 58.5%
  • C 41.5%