# ijl/orjson architecture

> Architecture diagram of the GitHub repository ijl/orjson, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-10-12). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/ijl/orjson
- Repository: https://github.com/ijl/orjson

## Overview

orjson is a native Python JSON library: Python callers pass objects to `dumps()` for UTF-8 JSON bytes or to `loads()` for Python values. The graph separates the extension entry point, encoding, decoding, and Python-object/FFI support. The README documents native dataclass, datetime, NumPy, UUID, Fragment, and custom-default serialization; some of those implementation paths are represented by their source directories because their internals were not sampled. Edges reflect shown calls, source-index references, or README-documented behavior; unsampled internal details are not inferred.

Main entry points: [`src/lib.rs`](https://github.com/ijl/orjson/blob/master/src/lib.rs) (Extension API), [`src/serialize/mod.rs`](https://github.com/ijl/orjson/blob/master/src/serialize/mod.rs) (Serialization dispatch), [`src/serialize/writer/container.rs`](https://github.com/ijl/orjson/blob/master/src/serialize/writer/container.rs) (Containers and defaults), [`src/deserialize/deserializer.rs`](https://github.com/ijl/orjson/blob/master/src/deserialize/deserializer.rs) (Decode orchestration)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_entry["Python API"]
  node_api["Extension API<br/>[lib.rs]"]
  node_options["Output options<br/>[opt.rs]"]
end

subgraph group_encoding["JSON Encoding"]
  node_serializer["Serialization dispatch<br/>[mod.rs]"]
  node_containers["Containers and defaults<br/>[container.rs]"]
  node_writer["JSON byte writer<br/>[byteswriter.rs]"]
  node_date["Date and time<br/>[datetime.rs]"]
  node_numpy["NumPy encoding<br/>[array.rs]"]
  node_special["Special types"]
  node_fragment["Fragment encoding<br/>[fragment.rs]"]
end

subgraph group_decoding["JSON Decoding"]
  node_decoder["Decode orchestration<br/>[deserializer.rs]"]
  node_input["Input handling<br/>[input.rs]"]
  node_backend["yyjson backend<br/>[yyjson.rs]"]
  node_cache["Key cache<br/>[cache.rs]"]
  node_pyvalues["Python value creation<br/>[pyobject.rs]"]
end

subgraph group_interop["Python Interop"]
  node_ffi["CPython FFI<br/>[mod.rs]"]
  node_typerefs["Type references<br/>[typeref.rs]"]
  node_fragmenttype["Fragment type<br/>[fragment.rs]"]
end

node_caller(("Python caller"))
node_loadsresult["Python values"]
node_dumpsresult["JSON bytes"]

node_caller -->|"calls API"| node_api
node_api -->|"serializes with"| node_serializer
node_api -->|"deserializes with"| node_decoder
node_api -->|"exports flags"| node_options
node_serializer -->|"dispatches values"| node_containers
node_containers -->|"writes JSON"| node_writer
node_containers -->|"formats dates"| node_date
node_containers -->|"serializes fragments"| node_fragment
node_serializer -->|"supports NumPy"| node_numpy
node_serializer -->|"supports native types"| node_special
node_serializer -->|"applies flags"| node_options
node_api -->|"returns bytes"| node_dumpsresult
node_decoder -->|"reads input"| node_input
node_decoder -->|"parses JSON"| node_backend
node_backend -->|"builds values"| node_pyvalues
node_backend -.->|"uses key cache"| node_cache
node_decoder -->|"returns values"| node_loadsresult
node_api -->|"uses Python API"| node_ffi
node_ffi -->|"registers type"| node_fragmenttype
node_fragmenttype -->|"uses type reference"| node_typerefs

click node_api "https://github.com/ijl/orjson/blob/master/src/lib.rs"
click node_options "https://github.com/ijl/orjson/blob/master/src/opt.rs"
click node_serializer "https://github.com/ijl/orjson/blob/master/src/serialize/mod.rs"
click node_containers "https://github.com/ijl/orjson/blob/master/src/serialize/writer/container.rs"
click node_writer "https://github.com/ijl/orjson/blob/master/src/serialize/writer/byteswriter.rs"
click node_date "https://github.com/ijl/orjson/blob/master/src/serialize/datetime.rs"
click node_numpy "https://github.com/ijl/orjson/blob/master/src/serialize/numpy/array.rs"
click node_special "https://github.com/ijl/orjson/tree/master/src/serialize"
click node_fragment "https://github.com/ijl/orjson/blob/master/src/serialize/fragment.rs"
click node_decoder "https://github.com/ijl/orjson/blob/master/src/deserialize/deserializer.rs"
click node_input "https://github.com/ijl/orjson/blob/master/src/deserialize/input.rs"
click node_backend "https://github.com/ijl/orjson/blob/master/src/deserialize/backend/yyjson.rs"
click node_cache "https://github.com/ijl/orjson/blob/master/src/deserialize/cache.rs"
click node_pyvalues "https://github.com/ijl/orjson/blob/master/src/deserialize/pyobject.rs"
click node_ffi "https://github.com/ijl/orjson/blob/master/src/ffi/mod.rs"
click node_typerefs "https://github.com/ijl/orjson/blob/master/src/typeref.rs"
click node_fragmenttype "https://github.com/ijl/orjson/blob/master/src/ffi/fragment.rs"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_api,node_options toneBlue
class node_serializer,node_containers,node_writer,node_date,node_numpy,node_special,node_fragment toneAmber
class node_decoder,node_input,node_backend,node_cache,node_pyvalues toneMint
class node_ffi,node_typerefs,node_fragmenttype toneRose
class node_caller,node_loadsresult,node_dumpsresult toneIndigo
```

## Components

### Python API

- [Extension API](https://github.com/ijl/orjson/blob/master/src/lib.rs): `src/lib.rs`
- [Output options](https://github.com/ijl/orjson/blob/master/src/opt.rs): `src/opt.rs`

### JSON Encoding

- [Serialization dispatch](https://github.com/ijl/orjson/blob/master/src/serialize/mod.rs): `src/serialize/mod.rs`
- [Containers and defaults](https://github.com/ijl/orjson/blob/master/src/serialize/writer/container.rs): `src/serialize/writer/container.rs`
- [JSON byte writer](https://github.com/ijl/orjson/blob/master/src/serialize/writer/byteswriter.rs): `src/serialize/writer/byteswriter.rs`
- [Date and time](https://github.com/ijl/orjson/blob/master/src/serialize/datetime.rs): `src/serialize/datetime.rs`
- [NumPy encoding](https://github.com/ijl/orjson/blob/master/src/serialize/numpy/array.rs): `src/serialize/numpy/array.rs`
- [Special types](https://github.com/ijl/orjson/tree/master/src/serialize): `src/serialize`
- [Fragment encoding](https://github.com/ijl/orjson/blob/master/src/serialize/fragment.rs): `src/serialize/fragment.rs`

### JSON Decoding

- [Decode orchestration](https://github.com/ijl/orjson/blob/master/src/deserialize/deserializer.rs): `src/deserialize/deserializer.rs`
- [Input handling](https://github.com/ijl/orjson/blob/master/src/deserialize/input.rs): `src/deserialize/input.rs`
- [yyjson backend](https://github.com/ijl/orjson/blob/master/src/deserialize/backend/yyjson.rs): `src/deserialize/backend/yyjson.rs`
- [Key cache](https://github.com/ijl/orjson/blob/master/src/deserialize/cache.rs): `src/deserialize/cache.rs`
- [Python value creation](https://github.com/ijl/orjson/blob/master/src/deserialize/pyobject.rs): `src/deserialize/pyobject.rs`

### Python Interop

- [CPython FFI](https://github.com/ijl/orjson/blob/master/src/ffi/mod.rs): `src/ffi/mod.rs`
- [Type references](https://github.com/ijl/orjson/blob/master/src/typeref.rs): `src/typeref.rs`
- [Fragment type](https://github.com/ijl/orjson/blob/master/src/ffi/fragment.rs): `src/ffi/fragment.rs`

### Other components

- **Python caller**
- **Python values**
- **JSON bytes**

## Connections

- Python caller → Extension API: calls API (evidence: `README.md`)
- Extension API → Serialization dispatch: serializes with (evidence: `src/lib.rs`)
- Extension API → Decode orchestration: deserializes with (evidence: `src/lib.rs`)
- Extension API → Output options: exports flags (evidence: `src/lib.rs`)
- Serialization dispatch → Containers and defaults: dispatches values (evidence: `src/serialize/mod.rs`)
- Containers and defaults → JSON byte writer: writes JSON (evidence: `src/serialize/writer/container.rs`)
- Containers and defaults → Date and time: formats dates (evidence: `src/serialize/writer/container.rs`)
- Containers and defaults → Fragment encoding: serializes fragments (evidence: `src/serialize/writer/container.rs`)
- Serialization dispatch → NumPy encoding: supports NumPy (evidence: `README.md`)
- Serialization dispatch → Special types: supports native types (evidence: `README.md`)
- Serialization dispatch → Output options: applies flags (evidence: `src/serialize/mod.rs`)
- Extension API → JSON bytes: returns bytes (evidence: `README.md`)
- Decode orchestration → Input handling: reads input (evidence: `src/deserialize/deserializer.rs`)
- Decode orchestration → yyjson backend: parses JSON (evidence: `src/deserialize/deserializer.rs`)
- yyjson backend → Python value creation: builds values (evidence: `src/deserialize/backend/yyjson.rs`)
- yyjson backend → Key cache: uses key cache
- Decode orchestration → Python values: returns values (evidence: `README.md`)
- Extension API → CPython FFI: uses Python API (evidence: `src/lib.rs`)
- CPython FFI → Fragment type: registers type (evidence: `src/ffi/mod.rs`)
- Fragment type → Type references: uses type reference (evidence: `src/ffi/fragment.rs`)
