sajson
Sajson: The Ultimate Guide to High-Performance JSON Parsing in Modern C++ Applications
Key Takeaways
- Sajson is a fast, zero-dependency C++ JSON parser built by Facebook (Meta) for internal use and open-sourced for the community.
- It delivers exceptional performance through custom memory allocation, stack-based parsing, and minimal heap overhead.
- Sajson supports both parsing and serialization with a clean, idiomatic C++ API.
- Compared to alternatives like RapidJSON, nlohmann/json, and JSON For Modern C++, Sajson excels in latency-sensitive, high-throughput scenarios.
- The library is production-ready, MIT-licensed, and integrates seamlessly into large-scale distributed systems.
What Is Sajson?
Sajson is a high-performance JSON parsing and serialization library written in C++. Originally developed by Facebook (now Meta) for use in their backend infrastructure, it was open-sourced to provide developers with a fast, lightweight, and dependency-free alternative to existing JSON libraries.
The name "Sajson" derives from the concept of efficient, streamlined JSON processing — designed to handle massive volumes of data with minimal resource consumption. Unlike general-purpose JSON libraries that prioritize ease of use and feature completeness, Sajson makes a deliberate tradeoff: it optimizes for speed and memory efficiency above all else.
Core Design Philosophy
Sajson's architecture is built around several key principles:
- Zero external dependencies — No Boost, no STL extensions, just pure C++.
- Custom memory pool allocator — Avoids per-node heap allocations during parsing.
- Stack-oriented processing — Minimizes function call overhead through iterative parsing.
- Stream-friendly API — Supports incremental parsing for large payloads.
- Type-safe DOM tree — Provides a strongly-typed representation of JSON documents.
Architecture and Internal Design
Memory Allocation Strategy
The standout feature of Sajson is its custom memory allocation system. Traditional JSON parsers like nlohmann/json allocate individual heap nodes for each value, string, and object property. Sajson instead uses a bump allocator (also known as a pool allocator) that batches memory requests.
This approach yields three critical advantages:
- Reduced fragmentation — Memory is allocated in contiguous blocks.
- Faster allocation — Bump pointers are O(1) versus malloc/free overhead.
- Predictable memory usage — Total allocation is bounded at parse time.
Parser Implementation
Sajson employs a hand-written recursive-descent parser with significant optimizations:
| Feature | Sajson | nlohmann/json | RapidJSON |
|---|---|---|---|
| Parser Type | Recursive-descent (optimized) | Recursive-descent | DOM + SAX hybrid |
| Memory Allocator | Custom pool (bump) | std::allocator | User-definable |
| Dependencies | None | None | None |
| String Interning | Optional | No | No |
| Streaming Parse | Yes | Limited | Full SAX support |
| Serialization Speed | Very Fast | Fast | Very Fast |
| API Style | IMessage / DOM | Dynamic variant | Template-heavy |
| License | MIT | MIT | MIT |
Document Object Model (DOM)
Sajson represents parsed JSON as a tree of IMessage objects. Each node corresponds to a JSON value type:
IMessage— Object container (key-value pairs)IString— String valuesIInt/IUInt— Integer typesIDouble— Floating-point valuesIBool— Boolean valuesINull— Null values
This typed DOM enables compile-time awareness and avoids the dynamic_cast overhead common in other C++ JSON libraries.
Performance Benchmarks
Below is a comparative analysis based on published benchmarks and community testing across representative JSON datasets.
| Benchmark | Sajson | RapidJSON | nlohmann/json | JSONcpp |
|---|---|---|---|---|
| Parse small (1 KB) | 0.12 ms | 0.15 ms | 0.45 ms | 0.68 ms |
| Parse medium (100 KB) | 8.2 ms | 9.1 ms | 28.5 ms | 35.2 ms |
| Parse large (10 MB) | 720 ms | 810 ms | 2,450 ms | 3,100 ms |
| Serialize small | 0.08 ms | 0.10 ms | 0.32 ms | 0.55 ms |
| Serialize medium | 5.5 ms | 6.2 ms | 18.7 ms | 24.1 ms |
| Memory overhead (100 KB) | 145 KB | 162 KB | 230 KB | 285 KB |
| Throughput (GB/s) | 1.8 | 1.6 | 0.5 | 0.3 |
Note: Benchmarks are illustrative and may vary based on hardware, compiler flags, and JSON structure. Always validate with your own workload.
When to Choose Sajson
Sajson shines in specific architectural contexts. Use this decision checklist to determine if it fits your project:
- Your application parses JSON at millisecond or sub-millisecond latency requirements
- You process high-volume JSON streams (thousands of messages per second)
- Memory footprint is a critical constraint (embedded, mobile, or edge computing)
- You want zero third-party dependencies in your build pipeline
- Your team prefers a typed, explicit API over dynamic variants
- You are building backend services, APIs, or data pipelines at scale
Conversely, consider alternatives if:
- You need extensive schema validation out of the box (try