Skills2026年5月4日·1 分钟阅读

orjson — Fast Correct JSON Library for Python

orjson is a high-performance JSON library for Python written in Rust, offering 3-10x faster serialization and deserialization than the standard library json module with native support for dataclasses, datetimes, and NumPy arrays.

Agent 就绪

这个资产可以被 Agent 直接读取和安装

TokRepo 同时提供通用 CLI 命令、安装契约、metadata JSON、按适配器生成的安装计划和原始内容链接,方便 Agent 判断适配度、风险和下一步动作。

Native · 98/100策略:允许
Agent 入口
任意 MCP/CLI Agent
类型
Skill
安装
Single
信任
信任等级:Established
入口
orjson Guide
通用 CLI 安装命令
npx tokrepo install cfb4c30a-4792-11f1-9bc6-00163e2b0d79

Introduction

orjson is a drop-in replacement for Python's json module that achieves significant speedups through a Rust implementation. It natively serializes types the standard library cannot (datetime, dataclass, UUID, NumPy) without custom encoders.

What orjson Does

  • Serializes Python objects to JSON bytes 3-10x faster than stdlib json
  • Deserializes JSON bytes/str to Python objects with strict RFC 8259 compliance
  • Natively handles datetime, date, time, UUID, dataclass, and enum types
  • Serializes NumPy arrays directly without manual conversion
  • Provides option flags for pretty printing, sorted keys, and non-string dict keys

Architecture Overview

orjson is compiled from Rust using PyO3 bindings. The serializer walks Python objects through the C API, formats them directly into a byte buffer without intermediate string allocations. The deserializer uses a SIMD-accelerated JSON parser (simd-json inspired) for fast number and string parsing on modern CPUs.

Self-Hosting & Configuration

  • Install via pip; pre-built wheels available for Linux, macOS, and Windows
  • Requires no configuration; use orjson.dumps() and orjson.loads() directly
  • Control output format via option flags: orjson.OPT_INDENT_2, OPT_SORT_KEYS, OPT_NON_STR_KEYS
  • Handle custom types with the default parameter (a callable for unrecognized types)
  • Returns bytes (not str) from dumps(); use .decode() if a string is needed

Key Features

  • Strict RFC 8259 compliance with correct Unicode surrogate handling
  • Native datetime serialization to ISO 8601 format
  • Direct NumPy ndarray serialization without .tolist() conversion
  • Dataclass serialization without dict overhead
  • Consistent 3-10x performance advantage in benchmarks across data shapes

Comparison with Similar Tools

  • json (stdlib) — slower and cannot serialize datetime/dataclass without custom encoders
  • ujson — faster than stdlib but slower than orjson; less strict on edge cases
  • rapidjson — C++ based; comparable speed but lacks native datetime/NumPy support
  • msgspec — similar performance with schema validation but different API design philosophy

FAQ

Q: Is orjson a true drop-in replacement for json? A: Nearly. The main difference is dumps() returns bytes instead of str, and it does not support the cls parameter. Use the default kwarg for custom types.

Q: Why does dumps() return bytes? A: Avoiding the bytes-to-str decode step improves performance. Most web frameworks (FastAPI, Starlette) accept bytes directly for responses.

Q: Does orjson support streaming or incremental parsing? A: No. orjson processes complete documents in memory. For streaming JSON, use ijson or the stdlib json.JSONDecoder with raw_decode.

Q: Can I use orjson with FastAPI? A: Yes. FastAPI supports custom JSON encoders. Set ORJSONResponse as the default response class for automatic orjson usage.

Sources

讨论

登录后参与讨论。
还没有评论,来写第一条吧。

相关资产