Skip to main content

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

Project description

github pypi conda

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.DecodeError: 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.

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.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

msgspec-0.5.0.tar.gz (122.9 kB view hashes)

Uploaded Source

Built Distributions

msgspec-0.5.0-cp310-cp310-win_amd64.whl (199.1 kB view hashes)

Uploaded CPython 3.10 Windows x86-64

msgspec-0.5.0-cp310-cp310-musllinux_1_1_x86_64.whl (433.5 kB view hashes)

Uploaded CPython 3.10 musllinux: musl 1.1+ x86-64

msgspec-0.5.0-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (434.4 kB view hashes)

Uploaded CPython 3.10 manylinux: glibc 2.17+ x86-64

msgspec-0.5.0-cp310-cp310-macosx_10_9_x86_64.whl (191.2 kB view hashes)

Uploaded CPython 3.10 macOS 10.9+ x86-64

msgspec-0.5.0-cp39-cp39-win_amd64.whl (199.0 kB view hashes)

Uploaded CPython 3.9 Windows x86-64

msgspec-0.5.0-cp39-cp39-musllinux_1_1_x86_64.whl (432.3 kB view hashes)

Uploaded CPython 3.9 musllinux: musl 1.1+ x86-64

msgspec-0.5.0-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (433.5 kB view hashes)

Uploaded CPython 3.9 manylinux: glibc 2.17+ x86-64

msgspec-0.5.0-cp39-cp39-macosx_10_9_x86_64.whl (191.0 kB view hashes)

Uploaded CPython 3.9 macOS 10.9+ x86-64

msgspec-0.5.0-cp38-cp38-win_amd64.whl (199.1 kB view hashes)

Uploaded CPython 3.8 Windows x86-64

msgspec-0.5.0-cp38-cp38-musllinux_1_1_x86_64.whl (437.8 kB view hashes)

Uploaded CPython 3.8 musllinux: musl 1.1+ x86-64

msgspec-0.5.0-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (436.8 kB view hashes)

Uploaded CPython 3.8 manylinux: glibc 2.17+ x86-64

msgspec-0.5.0-cp38-cp38-macosx_10_9_x86_64.whl (191.6 kB view hashes)

Uploaded CPython 3.8 macOS 10.9+ x86-64

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page