Fast JSON with orjson
orjson is a Rust-backed JSON library that is 3–10x faster than the standard library and natively supports datetime, numpy, and dataclasses.
orjson is a Rust-backed JSON library that is 3–10x faster than the standard library and natively supports datetime, numpy, and dataclasses.
import orjson
from datetime import datetime
data = {"ts": datetime.now(), "values": list(range(1000))}
# Serialise
raw_bytes = orjson.dumps(data) # returns bytes, not str
json_str = raw_bytes.decode()
# Deserialise
obj = orjson.loads(raw_bytes)
# Option flags
orjson.dumps(data, option=orjson.OPT_INDENT_2 | orjson.OPT_SORT_KEYS)
orjson.dumps() returns bytes — use .decode() for str or pass bytes directly (e.g., FastAPI does this).