API Reference

orjson

Fast JSON library for Python that serializes and deserializes as bytes with dataclass and numpy support.

Official Documentation ↗
CompatibilityThis documentation covers version v3. Check the official docs for the latest API surface.

Installation

pippip install orjson

Methods

Fast JSON library for Python that serializes and deserializes as bytes with dataclass and numpy support.

Overview

orjson is a Rust-backed JSON serializer that optimizes the common path: dumps produces UTF-8 bytes and loads accepts bytes or str, both several times faster than the stdlib for realistic payloads.

It accepts Python-native types beyond the stdlib: dataclasses, datetime, UUID, and numpy arrays with the right option flags. Strictness is deliberate, non-string keys require an opt-in flag, and there are no hooks like default or object_hook, keeping the fast path unpolluted.

Core Methods

The most frequently used entry points, with signatures and runnable examples:

  • orjson.dumps — Serializes to UTF-8 bytes, not str, with optional flags for indentation and non-string keys.
  • orjson.loads — Parses JSON from bytes or str into Python objects, accepting UTF-8 encoded input directly.
  • serialization options — Bit flags enabling pretty printing, non-string dict keys, numpy array serialization, and UTC Z suffixes.

Usage Notes

  • Write dumps output directly to binary-mode files or response bodies; skip the decode round trip.
  • Represent line-delimited datasets as JSON Lines so each loads call stays small.
  • Validate parsed structures at the boundary; speed of parsing says nothing about shape correctness.

Common Pitfalls

  • Passing dumps output to APIs that expect str raises type errors; decode explicitly there.
  • Dict keys that are int or datetime raise by default; opt in with OPT_NON_STR_KEYS when the format allows it.
  • There is no object_hook: custom classes need pre-conversion to dicts before dumps.

Errors

The following points are quoted verbatim from the official documentation. 来源:https://cdn.jsdelivr.net/gh/ijl/orjson@latest/README.md · 取证方式:HTTP GET 官方 README/文档页(HTML 去标签) · 抓取时间:2026-11-07T13:51:23.277Z. Any error type, throw condition or recovery step not stated there is left unspecified; this page does not infer exception classes or return values.

FAQ

Why does dumps return bytes instead of str?
orjson serializes straight into UTF-8 bytes, which is what network responses and file writes consume. Decoding to str would add a copy for no benefit; call .decode() only when a str API truly requires it.
How does it handle datetimes and dataclasses?
datetime, date, and UUID serialize natively to RFC 3339 strings, and dataclasses serialize by their fields, both faster than any default-json customization. Use option flags such as OPT_UTC_Z to control datetime rendering.
Can orjson parse files incrementally?
No. orjson loads whole documents, one JSON value per loads call. For very large files, combine it with a framing format such as JSON Lines, reading and parsing line by line.