Configs2026年7月6日·1 分钟阅读

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.

Agent 就绪

Agent 可直接安装

这个资产可安装;Agent 先选择当前运行时、检查安装计划,再运行匹配命令。

Native · 98/100策略:允许
Agent 入口
任意 MCP/CLI Agent
类型
Skill
安装
Single
信任
信任等级:Established
入口
RapidJSON Overview
直接安装命令
npx -y tokrepo@latest install d617043a-7978-11f1-9bc6-00163e2b0d79 --target codex

先 dry-run 确认安装计划,再运行此命令。

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

讨论

登录后参与讨论。
还没有评论,来写第一条吧。

相关资产