# RapidJSON — Fast JSON Parser and Generator for C++ > RapidJSON is an extremely fast JSON parser and generator for C++ with SAX and DOM style APIs. Developed by Tencent, it is header-only, has no external dependencies, and handles UTF-8, UTF-16, and UTF-32 encodings natively. ## Install Save in your project root: # RapidJSON — Fast JSON Parser and Generator for C++ ## Quick Use ```cpp #include "rapidjson/document.h" #include "rapidjson/writer.h" #include "rapidjson/stringbuffer.h" #include int main() { rapidjson::Document d; d.Parse("{"name":"RapidJSON","stars":15100}"); printf("name: %s, stars: %d ", d["name"].GetString(), d["stars"].GetInt()); } ``` ## 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) or `PrettyWriter` (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/rapidjson` directory 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=1` to enable `std::string` interop - 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. ## Sources - https://github.com/Tencent/rapidjson - https://rapidjson.org/ --- Source: https://tokrepo.com/en/workflows/asset-d617043a Author: AI Open Source