Jump to content

Connect Leeroopedia MCP: Equip your AI agents to search best practices, build plans, verify code, diagnose failures, and look up hyperparameter defaults.

Implementation:Alibaba MNN RapidJSON Core

From Leeroopedia


Knowledge Sources
Domains JSON_Parsing, Configuration
Last Updated 2026-02-10 12:00 GMT

Overview

3rd_party/rapidjson/rapidjson.h (656 lines) is the core configuration header for the vendored RapidJSON library within Alibaba MNN. It defines version information, platform detection macros, memory allocator interfaces, fundamental types, and compiler-specific diagnostic controls. Every other RapidJSON header includes this file, making it the root of the dependency graph.

Usage note: Vendored dependency used internally by MNN for JSON configuration parsing (model configs, LLM configs). Not directly imported by end users.

Version Information

#define RAPIDJSON_MAJOR_VERSION 1
#define RAPIDJSON_MINOR_VERSION 1
#define RAPIDJSON_PATCH_VERSION 0
#define RAPIDJSON_VERSION_STRING \
    RAPIDJSON_STRINGIFY(RAPIDJSON_MAJOR_VERSION.RAPIDJSON_MINOR_VERSION.RAPIDJSON_PATCH_VERSION)

The vendored version is RapidJSON 1.1.0.

Namespace Configuration

The library wraps all declarations in a configurable namespace:

#ifndef RAPIDJSON_NAMESPACE
#define RAPIDJSON_NAMESPACE rapidjson
#endif

#define RAPIDJSON_NAMESPACE_BEGIN namespace RAPIDJSON_NAMESPACE {
#define RAPIDJSON_NAMESPACE_END   }

Platform Detection

The header detects and configures for multiple platforms and compilers:

Macro Purpose
RAPIDJSON_ENDIAN Byte order detection (little/big endian)
RAPIDJSON_64BIT 64-bit platform detection
RAPIDJSON_ALIGN Memory alignment macro
RAPIDJSON_HAS_CXX11_RVALUE_REFS C++11 rvalue reference support
RAPIDJSON_HAS_CXX11_NOEXCEPT C++11 noexcept support
RAPIDJSON_HAS_CXX11_TYPETRAITS C++11 type_traits availability
RAPIDJSON_HAS_CXX11_RANGE_FOR C++11 range-based for support

Memory Allocators

CrtAllocator

Thin wrapper around the C runtime malloc/realloc/free:

class CrtAllocator {
public:
    static const bool kNeedFree = true;
    void* Malloc(size_t size);
    void* Realloc(void* originalPtr, size_t originalSize, size_t newSize);
    static void Free(void* ptr);
};

MemoryPoolAllocator

A fast, chunk-based memory pool allocator optimized for JSON parsing where many small allocations are made and then freed all at once:

template <typename BaseAllocator = CrtAllocator>
class MemoryPoolAllocator {
public:
    static const bool kNeedFree = false;
    MemoryPoolAllocator(size_t chunkSize = kDefaultChunkCapacity,
                        BaseAllocator* baseAllocator = 0);
    void* Malloc(size_t size);
    void* Realloc(void* originalPtr, size_t originalSize, size_t newSize);
    static void Free(void* ptr); // no-op
    void Clear();
};

Fundamental Types

typedef unsigned SizeType;  // Size type (use 32-bit even on 64-bit for compactness)

enum Type {
    kNullType = 0,
    kFalseType = 1,
    kTrueType = 2,
    kObjectType = 3,
    kArrayType = 4,
    kStringType = 5,
    kNumberType = 6
};

Diagnostic Macros

The header provides cross-compiler diagnostic push/pop and warning suppression:

// Clang
#define RAPIDJSON_DIAG_PUSH   _Pragma("clang diagnostic push")
#define RAPIDJSON_DIAG_OFF(x) _Pragma(RAPIDJSON_STRINGIFY(clang diagnostic ignored RAPIDJSON_STRINGIFY(RAPIDJSON_JOIN(-W,x))))
#define RAPIDJSON_DIAG_POP    _Pragma("clang diagnostic pop")

// GCC
#define RAPIDJSON_DIAG_PUSH   _Pragma("GCC diagnostic push")
#define RAPIDJSON_DIAG_OFF(x) _Pragma(RAPIDJSON_STRINGIFY(GCC diagnostic ignored ...))
#define RAPIDJSON_DIAG_POP    _Pragma("GCC diagnostic pop")

// MSVC
#define RAPIDJSON_DIAG_PUSH   __pragma(warning(push))
#define RAPIDJSON_DIAG_OFF(x) __pragma(warning(disable: x))
#define RAPIDJSON_DIAG_POP    __pragma(warning(pop))

Includes

Only standard library headers are included:

  • <cstdlib> -- malloc(), realloc(), free(), size_t
  • <cstring> -- memset(), memcpy(), memmove(), memcmp()

License

MIT License. Copyright (C) 2015 THL A29 Limited (Tencent) and Milo Yip.

See Also

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment