Introduction
RapidJSON is a header-only C++ library for parsing and generating JSON, designed to be one of the fastest JSON libraries available. It was created at Tencent and conforms fully to RFC 7159 and ECMA-404, offering both a tree-style DOM API and a streaming SAX API for different use cases.
What RapidJSON Does
- Parses JSON from strings, streams, or files into an in-memory DOM tree for random access
- Provides a SAX-style streaming parser for processing large JSON documents with minimal memory
- Generates JSON output via
Writer(compact) orPrettyWriter(formatted) to any output stream - Handles four Unicode encodings (UTF-8, UTF-16LE, UTF-16BE, UTF-32) with transcoding support
- Validates JSON documents against JSON Schema (Draft v4) at parse time
Architecture Overview
RapidJSON uses a two-layer design: a low-level SAX reader/handler that tokenizes the input byte stream, and a higher-level DOM layer built on top that constructs a Value tree. Memory is managed through pluggable allocators, with a default MemoryPoolAllocator that performs bulk allocation to reduce malloc overhead. The library uses SIMD intrinsics (SSE2, SSE4.2, NEON) when available to accelerate whitespace skipping and string scanning during parsing.
Self-Hosting & Configuration
- Header-only: copy the
include/rapidjsondirectory into your project and include headers - No CMake or build step required for library usage; CMake is provided for building tests and examples
- Define
RAPIDJSON_HAS_STDSTRING=1to enablestd::stringinterop - Custom allocators can be injected for arena-based or pooled memory management
- Available via vcpkg (
vcpkg install rapidjson) and Conan package managers
Key Features
- Consistently ranks among the fastest JSON parsers in C++ benchmarks
- SIMD-accelerated parsing on x86 (SSE2/4.2) and ARM (NEON) architectures
- In-situ parsing mode that modifies the input buffer to avoid string copies
- JSON Pointer (RFC 6901) support for direct value access via path expressions
- Comprehensive error reporting with byte-offset location on parse failures
Comparison with Similar Tools
- nlohmann/json — more intuitive STL-like API but significantly slower; RapidJSON prioritizes raw speed
- simdjson — faster for read-only parsing using SIMD; RapidJSON also supports mutation and generation
- sajson — single-allocation parser; RapidJSON offers richer features including Schema validation
- cJSON — pure C with small footprint; RapidJSON provides C++ ergonomics and better performance
- Boost.JSON — part of Boost with Boost.Asio integration; RapidJSON is standalone and lighter
FAQ
Q: How does RapidJSON compare to nlohmann/json in performance? A: RapidJSON is typically 5-10x faster in parsing benchmarks. nlohmann/json trades speed for a more convenient API.
Q: Can I modify a parsed document and write it back?
A: Yes. The DOM API supports adding, removing, and modifying values. Use Writer to serialize the modified tree.
Q: Does RapidJSON support JSON5 or comments?
A: The default parser is strict RFC 7159. A relaxed mode can be enabled with kParseCommentsFlag and kParseTrailingCommasFlag.
Q: What is in-situ parsing? A: In-situ parsing modifies the source string in place (decoding escapes and adding null terminators) to avoid heap-allocating copies of string values.