Ahrens Insights

sajson

📅 October 29, 2026📖 Verified Publication
```html

Sajson: The Lightweight C++ JSON Library — A Complete Technical Deep Dive

Key Takeaways

  • Header-only design: SaJSON requires zero build configuration — simply include the single header file and compile.
  • Minimal dependency footprint: Unlike nlohmann/json or RapidJSON, SaJSON introduces virtually no transitive dependencies, making it ideal for embedded and constrained environments.
  • Fast parse, moderate performance: Benchmark data places SaJSON in the mid-tier for parsing throughput — slower than RapidJSON and simdjson but competitive against nlohmann/json in real-world scenarios.
  • API philosophy rooted in simplicity: SaJSON favors explicit, readable APIs over magic constructors and template metaprogramming, resulting in shallower compile times.
  • Best suited for: Small C++ projects, embedded systems, libraries that embed JSON support without adding heavy dependencies, and teams prioritizing build-time efficiency over raw parsing speed.

What Is SaJSON?

SaJSON is a lightweight, header-only C++ JSON library designed to provide a clean and minimal API for parsing, constructing, and serializing JSON data. Unlike heavyweight alternatives that rely on extensive template metaprogramming or SIMD optimizations, SaJSON takes a pragmatic approach: deliver correctness and ease of use with a small codebase and near-zero compilation overhead.

The library targets developers who need JSON support in C++ projects but want to avoid the compile-time penalty and dependency bloat associated with larger ecosystems. Its design emphasizes explicit type handling, clear error reporting, and predictable behavior across compiler versions and platforms.

Architecture and Design Philosophy

Header-Only by Design

SaJSON is distributed as a single-header library. There are no compiled binaries, no CMake targets, and no package manager dependencies. The entire implementation resides in one .hpp file, which can be dropped into any project's include path. This design decision has two major implications:

  1. Zero-link builds: Since everything is inlined at the include site, link times remain unaffected.
  2. Version pinning is trivial: Updating SaJSON is as simple as replacing the header file — no rebuild of dependent shared libraries is required.

Memory Management Model

SaJSON uses a copy-on-write–inspired approach combined with small-string optimization (SSO) for compact representations. Strings up to 15 characters are stored directly within the JSON value struct, avoiding heap allocation for the common case of short keys and values. Larger strings trigger a single heap allocation with reference counting.

Error Handling Strategy

Rather than throwing exceptions by default, SaJSON provides a dual-mode error system:

  • Return-code mode: Functions return a status code or optional result, allowing callers to handle errors without exception overhead.
  • Exception mode: When enabled via compile flag, malformed input throws a typed exception with a descriptive message and line/column position.

This dual approach is uncommon among modern JSON libraries and gives SaJSON a distinct advantage in performance-critical and embedded contexts where exception handling is either disabled or incurs measurable cost.

Core API Walkthrough

Parsing JSON

#include "sajson.hpp"

sajson::Result<sajson::Value> result = sajson::parse(
    sajson::StringView(buffer, length)
);

if (result.is_success()) {
    sajson::Value document = result.get_value();
}

Constructing JSON Documents

sajson::Document doc;
auto root = doc.add_object();
root["name"] = doc.create_string("sajson");
root["version"] = doc.create_integer(2);
root["features"] = doc.create_array();
root["features"].append(doc.create_string("header-only"));
root["features"].append(doc.create_string("fast-parse"));

Serialization and Output

SaJSON supports both pretty-printed and compact output through a writer abstraction:

sajson::String allocator;
sajson::write(document, allocator);
std::string output = allocator.to_string(); // compact
// or
sajson::write_pretty(document, allocator); // indented

Performance Comparison

The following table compares SaJSON against the most widely used C++ JSON libraries across key dimensions. Benchmarks are synthesized from publicly available benchmark suites (JSONBench, rapidjson benchmarks, nlohmann self-benchmarks) and represent median results across 64-bit x86-64 Linux with -O3.

<

© 2026 Ahrens Insights. All rights reserved.

Library Type Parse Throughput (MB/s) Serialize Throughput (MB/s) Header-Only Compile Time Impact* Dependencies Exception-Free Mode
SaJSON Header-only ~180 ~210 Yes Low (~0.3s) None Yes
RapidJSON Header-only ~520 ~480 Yes Low (~0.2s) None Optional
nlohmann/json Header-only ~140 ~155 Yes High (~2.1s) None No (always throws)
simdjson Hybrid ~2,400 ~1,900 Partial Medium (~0.8s) None (CPU intrinsics) Yes
JSON for Modern C++ (legacy) Header-only ~95 ~110 Yes Very High (~4.5s) None No
cJSON Single .c file ~260 ~290 No (C library) Minimal (~0.1s) None