Implementation:Alibaba MNN RapidJSON Document
| Knowledge Sources | |
|---|---|
| Domains | JSON_Parsing, DOM |
| Last Updated | 2026-02-10 12:00 GMT |
Overview
3rd_party/rapidjson/document.h (2652 lines) provides the DOM-style JSON document parser and value container for the vendored RapidJSON library within Alibaba MNN. This header defines the core classes GenericValue, GenericDocument, and GenericMember that represent parsed JSON data as an in-memory tree structure. It is the primary interface through which MNN reads and manipulates JSON configuration files (model configs, LLM configs, quantization settings).
Usage note: Vendored dependency used internally by MNN for JSON configuration parsing (model configs, LLM configs). Not directly imported by end users.
Key Classes
GenericDocument
The top-level DOM document class, inheriting from GenericValue. It owns the memory allocator and provides the Parse() entry point.
template <typename Encoding, typename Allocator = MemoryPoolAllocator<>, typename StackAllocator = CrtAllocator>
class GenericDocument : public GenericValue<Encoding, Allocator> {
public:
typedef typename Encoding::Ch Ch;
typedef GenericValue<Encoding, Allocator> ValueType;
typedef Allocator AllocatorType;
explicit GenericDocument(Type type, Allocator* allocator = 0,
size_t stackCapacity = kDefaultStackCapacity,
StackAllocator* stackAllocator = 0);
GenericDocument(Allocator* allocator = 0,
size_t stackCapacity = kDefaultStackCapacity,
StackAllocator* stackAllocator = 0);
template <unsigned parseFlags, typename SourceEncoding, typename InputStream>
GenericDocument& ParseStream(InputStream& is);
template <unsigned parseFlags, typename SourceEncoding>
GenericDocument& Parse(const typename SourceEncoding::Ch* str);
bool HasParseError() const;
ParseErrorCode GetParseError() const;
size_t GetErrorOffset() const;
Allocator& GetAllocator();
};
The default typedef is:
typedef GenericDocument<UTF8<>> Document;
GenericValue
Represents any JSON value (null, bool, number, string, array, object). Uses a compact union-based data representation to store values of different types without virtual dispatch.
template <typename Encoding, typename Allocator = MemoryPoolAllocator<>>
class GenericValue {
public:
typedef GenericMember<Encoding, Allocator> Member;
typedef Encoding EncodingType;
typedef Allocator AllocatorType;
// Type queries
Type GetType() const;
bool IsNull() const;
bool IsBool() const;
bool IsObject() const;
bool IsArray() const;
bool IsString() const;
bool IsNumber() const;
// Accessors
const Ch* GetString() const;
SizeType GetStringLength() const;
int GetInt() const;
double GetDouble() const;
bool GetBool() const;
// Object members
MemberIterator FindMember(const Ch* name);
bool HasMember(const Ch* name) const;
GenericValue& operator[](const Ch* name);
// Array elements
GenericValue& operator[](SizeType index);
SizeType Size() const;
};
GenericMember
A name-value pair used by JSON objects:
template <typename Encoding, typename Allocator>
struct GenericMember {
GenericValue<Encoding, Allocator> name; // must be a string
GenericValue<Encoding, Allocator> value;
};
Architecture
The document header depends on the following internal headers:
reader.h-- SAX parser used internally byGenericDocument::Parse()internal/meta.h-- Template metaprogramming utilitiesinternal/strfunc.h-- String utility functionsmemorystream.h-- In-memory input streamencodedstream.h-- Encoding-aware stream wrappers
The GenericDocument class acts as both a SAX handler and the root value. When Parse() is invoked, it creates a GenericReader, feeds the input stream to it, and receives SAX events to build the DOM tree using a stack-based algorithm.
Value Type Flags
Internally, GenericValue uses a 16-bit flag system to encode the JSON type and subtype information:
| Flag | Value | Description |
|---|---|---|
kBoolFlag |
0x0008 | Value is a boolean |
kNumberFlag |
0x0010 | Value is a number |
kIntFlag |
0x0020 | Value is a signed integer |
kUintFlag |
0x0040 | Value is an unsigned integer |
kInt64Flag |
0x0080 | Value is a 64-bit signed integer |
kUint64Flag |
0x0100 | Value is a 64-bit unsigned integer |
kDoubleFlag |
0x0200 | Value is a double |
Compiler Compatibility
The header includes diagnostic push/pop directives for Clang (padded, switch-enum, c++98-compat), MSVC (C4127, C4244), and GCC (effc++). C++11 rvalue references and move semantics are conditionally supported via RAPIDJSON_HAS_CXX11_RVALUE_REFS.
License
MIT License. Copyright (C) 2015 THL A29 Limited (Tencent) and Milo Yip.
See Also
- Alibaba_MNN_RapidJSON_Reader -- SAX-style streaming parser
- Alibaba_MNN_RapidJSON_Writer -- JSON serializer
- Alibaba_MNN_RapidJSON_Core -- Core configuration header
- Alibaba_MNN_RapidJSON_Pointer -- JSON Pointer path-based access