Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/jcrist/msgspec

A fast serialization and validation library, with builtin support for JSON, MessagePack, YAML, and TOML
https://github.com/jcrist/msgspec

deserialization json json-schema jsonschema messagepack msgpack openapi3 python schema serde serialization toml validation yaml

Last synced: 13 days ago
JSON representation

A fast serialization and validation library, with builtin support for JSON, MessagePack, YAML, and TOML

Awesome Lists containing this project

README

        



msgspec




















`msgspec` is a *fast* serialization and validation library, with builtin
support for [JSON](https://json.org), [MessagePack](https://msgpack.org),
[YAML](https://yaml.org), and [TOML](https://toml.io). It features:

- 🚀 **High performance encoders/decoders** for common protocols. The JSON and
MessagePack implementations regularly
[benchmark](https://jcristharif.com/msgspec/benchmarks.html) as the fastest
options for Python.

- 🎉 **Support for a wide variety of Python types**. Additional types may be
supported through
[extensions](https://jcristharif.com/msgspec/extending.html).

- 🔍 **Zero-cost schema validation** using familiar Python type annotations. In
[benchmarks](https://jcristharif.com/msgspec/benchmarks.html) `msgspec`
decodes *and* validates JSON faster than
[orjson](https://github.com/ijl/orjson) can decode it alone.

- ✨ **A speedy Struct type** for representing structured data. If you already
use [dataclasses](https://docs.python.org/3/library/dataclasses.html) or
[attrs](https://www.attrs.org),
[structs](https://jcristharif.com/msgspec/structs.html) should feel familiar.
However, they're
[5-60x faster](https://jcristharif.com/msgspec/benchmarks.html#benchmark-structs>)
for common operations.

All of this is included in a
[lightweight library](https://jcristharif.com/msgspec/benchmarks.html#benchmark-library-size)
with no required dependencies.

---

`msgspec` may be used for serialization alone, as a faster JSON or
MessagePack library. For the greatest benefit though, we recommend using
`msgspec` to handle the full serialization & validation workflow:

**Define** your message schemas using standard Python type annotations.

```python
>>> import msgspec

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

**Encode** messages as JSON, or one of the many other supported protocols.

```python
>>> 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 objects, with optional schema validation.

```python
>>> 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 "", line 1, in
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](https://pydantic-docs.helpmanual.io/). For supported types,
encoding/decoding a message with `msgspec` can be
[~10-80x faster than alternative libraries](https://jcristharif.com/msgspec/benchmarks.html).





See [the documentation](https://jcristharif.com/msgspec/) for more information.

## LICENSE

New BSD. See the
[License File](https://github.com/jcrist/msgspec/blob/main/LICENSE).