跳转到主要内容

一个快速的序列化和验证库,内置对JSON、MessagePack、YAML和TOML的支持。

项目描述

msgspec

msgspec 是一个 快速 的序列化和验证库,内置对 JSONMessagePackYAMLTOML 的支持。它具有以下特性

  • 🚀 高性能编码/解码器,适用于常见协议。JSON和MessagePack的实现通常在 基准测试 中被认为是Python中最快的选项。

  • 🎉 支持多种Python类型。可以通过 扩展 支持更多类型。

  • 🔍 零成本模式验证,使用熟悉的Python类型注解。在 基准测试 中,msgspec 解码和验证JSON的速度比 orjson 独自解码它要快。

  • 快速的结构类型,用于表示结构化数据。如果您已经使用 dataclassesattrs,则 structs 应该会感到熟悉。然而,它们在常见操作中的速度比 5-60倍 快。

所有这些都包含在一个无依赖项的轻量级库中。


msgspec可以作为单独的序列化使用,作为一个更快的JSON或MessagePack库。不过,为了获得最大的好处,我们建议使用msgspec来处理完整的序列化及验证工作流。

使用标准的Python类型注解来定义你的消息模式。

>>> import msgspec

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

将消息编码为JSON或众多其他支持的协议之一。

>>> 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}'

将消息解码回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 "<stdin>", line 1, in <module>
msgspec.ValidationError: Expected `str`, got `int` - at `$.groups[0]`

msgspec旨在尽可能高效,同时保留像pydantic这样的验证库的一些优点。对于支持的类型,使用msgspec进行消息的编码/解码可以比其他库快~10-80倍

有关更多信息,请参阅文档

许可协议

新BSD。请参阅许可文件

项目详情


下载文件

下载适用于您平台的文件。如果您不确定选择哪一个,请了解更多关于安装包的信息。

源代码分发

msgspec-0.18.6.tar.gz (216.8 kB 查看哈希值)

上传时间 源代码

构建分发

msgspec-0.18.6-cp312-cp312-win_amd64.whl (187.2 kB 查看哈希值)

上传时间 CPython 3.12 Windows x86-64

msgspec-0.18.6-cp312-cp312-musllinux_1_1_x86_64.whl (217.1 kB 查看哈希值)

上传时间 CPython 3.12 musllinux: musl 1.1+ x86-64

msgspec-0.18.6-cp312-cp312-musllinux_1_1_aarch64.whl (214.8 kB 查看哈希值)

上传于 CPython 3.12 musllinux: musl 1.1+ ARM64

msgspec-0.18.6-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (212.5 kB 查看哈希值)

上传于 CPython 3.12 manylinux: glibc 2.17+ x86-64

msgspec-0.18.6-cp312-cp312-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (210.3 kB 查看哈希值)

上传于 CPython 3.12 manylinux: glibc 2.17+ ARM64

msgspec-0.18.6-cp312-cp312-macosx_11_0_arm64.whl (190.3 kB 查看哈希值)

上传于 CPython 3.12 macOS 11.0+ ARM64

msgspec-0.18.6-cp312-cp312-macosx_10_9_x86_64.whl (202.1 kB 查看哈希值)

上传于 CPython 3.12 macOS 10.9+ x86-64

msgspec-0.18.6-cp311-cp311-win_amd64.whl (185.9 kB 查看哈希值)

上传于 CPython 3.11 Windows x86-64

msgspec-0.18.6-cp311-cp311-musllinux_1_1_x86_64.whl (215.8 kB 查看哈希值)

上传于 CPython 3.11 musllinux: musl 1.1+ x86-64

msgspec-0.18.6-cp311-cp311-musllinux_1_1_aarch64.whl (213.6 kB 查看哈希值)

上传于 CPython 3.11 musllinux: musl 1.1+ ARM64

msgspec-0.18.6-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (209.5 kB 查看哈希值)

上传于 CPython 3.11 manylinux: glibc 2.17+ x86-64

msgspec-0.18.6-cp311-cp311-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (208.1 kB 查看哈希值)

上传于 CPython 3.11 manylinux: glibc 2.17+ ARM64

msgspec-0.18.6-cp311-cp311-macosx_11_0_arm64.whl (189.1 kB 查看哈希值)

上传于 CPython 3.11 macOS 11.0+ ARM64

msgspec-0.18.6-cp311-cp311-macosx_10_9_x86_64.whl (200.1 kB 查看哈希值)

上传时间 CPython 3.11 macOS 10.9+ x86-64

msgspec-0.18.6-cp310-cp310-win_amd64.whl (185.7 kB 查看哈希值)

上传时间 CPython 3.10 Windows x86-64

msgspec-0.18.6-cp310-cp310-musllinux_1_1_x86_64.whl (216.6 kB 查看哈希值)

上传时间 CPython 3.10 musllinux: musl 1.1+ x86-64

msgspec-0.18.6-cp310-cp310-musllinux_1_1_aarch64.whl (214.7 kB 查看哈希值)

上传时间 CPython 3.10 musllinux: musl 1.1+ ARM64

msgspec-0.18.6-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (210.3 kB 查看哈希值)

上传时间 CPython 3.10 manylinux: glibc 2.17+ x86-64

msgspec-0.18.6-cp310-cp310-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (208.5 kB 查看哈希值)

上传时间 CPython 3.10 manylinux: glibc 2.17+ ARM64

msgspec-0.18.6-cp310-cp310-macosx_11_0_arm64.whl (192.2 kB 查看哈希值)

上传时间 CPython 3.10 macOS 11.0+ ARM64

msgspec-0.18.6-cp310-cp310-macosx_10_9_x86_64.whl (202.5 kB 查看哈希值)

上传时间 CPython 3.10 macOS 10.9+ x86-64

msgspec-0.18.6-cp39-cp39-win_amd64.whl (185.8 kB 查看哈希值)

上传时间 CPython 3.9 Windows x86-64

msgspec-0.18.6-cp39-cp39-musllinux_1_1_x86_64.whl (216.4 kB 查看哈希值)

上传时间 CPython 3.9 musllinux: musl 1.1+ x86-64

msgspec-0.18.6-cp39-cp39-musllinux_1_1_aarch64.whl (214.4 kB 查看哈希值)

上传时间: CPython 3.9 musllinux: musl 1.1+ ARM64

msgspec-0.18.6-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (210.3 kB 查看哈希值)

上传时间: CPython 3.9 manylinux: glibc 2.17+ x86-64

msgspec-0.18.6-cp39-cp39-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (208.4 kB 查看哈希值)

上传时间: CPython 3.9 manylinux: glibc 2.17+ ARM64

msgspec-0.18.6-cp39-cp39-macosx_11_0_arm64.whl (192.0 kB 查看哈希值)

上传时间: CPython 3.9 macOS 11.0+ ARM64

msgspec-0.18.6-cp39-cp39-macosx_10_9_x86_64.whl (202.5 kB 查看哈希值)

上传时间: CPython 3.9 macOS 10.9+ x86-64

msgspec-0.18.6-cp38-cp38-win_amd64.whl (187.7 kB 查看哈希值)

上传时间: CPython 3.8 Windows x86-64

msgspec-0.18.6-cp38-cp38-musllinux_1_1_x86_64.whl (218.4 kB 查看哈希值)

上传时间: CPython 3.8 musllinux: musl 1.1+ x86-64

msgspec-0.18.6-cp38-cp38-musllinux_1_1_aarch64.whl (215.5 kB 查看哈希值)

上传时间: CPython 3.8 musllinux: musl 1.1+ ARM64

msgspec-0.18.6-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (212.4 kB 查看哈希值)

上传时间: CPython 3.8 manylinux: glibc 2.17+ x86-64

msgspec-0.18.6-cp38-cp38-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (210.1 kB 查看哈希值)

上传时间: CPython 3.8 manylinux: glibc 2.17+ ARM64

msgspec-0.18.6-cp38-cp38-macosx_11_0_arm64.whl (191.6 kB 查看哈希值)

上传于 CPython 3.8 macOS 11.0+ ARM64

msgspec-0.18.6-cp38-cp38-macosx_10_9_x86_64.whl (201.3 kB 查看哈希值)

上传于 CPython 3.8 macOS 10.9+ x86-64

由以下组织支持

AWS AWS 云计算和安全赞助商 Datadog Datadog 监控 Fastly Fastly CDN Google Google 下载分析 Microsoft Microsoft PSF赞助商 Pingdom Pingdom 监控 Sentry Sentry 错误日志 StatusPage StatusPage 状态页面