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 Pointer

From Leeroopedia


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

Overview

3rd_party/rapidjson/pointer.h (1414 lines) implements JSON Pointer (RFC 6901) for the vendored RapidJSON library within Alibaba MNN. A JSON Pointer is a string syntax (e.g., /foo/0/bar) for identifying a specific value within a JSON document. This enables path-based navigation and manipulation of DOM trees without manual iteration through nested objects and arrays.

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

Key Class

GenericPointer

template <typename ValueType, typename Allocator = CrtAllocator>
class GenericPointer {
public:
    typedef typename ValueType::EncodingType EncodingType;
    typedef typename ValueType::Ch Ch;

    // Token represents one level of the pointer path
    struct Token {
        const Ch* name;       // name of the token (for object keys)
        SizeType length;      // length of the name string
        SizeType index;       // array index (kPointerInvalidIndex if not an index)
    };

    // Constructors
    GenericPointer(const Ch* source, Allocator* allocator = 0);
    GenericPointer(const Ch* source, size_t length, Allocator* allocator = 0);
    GenericPointer(const Token* tokens, size_t tokenCount);

    // Copy and assignment
    GenericPointer(const GenericPointer& rhs);
    GenericPointer& operator=(const GenericPointer& rhs);

    // Navigation
    GenericPointer Append(const Ch* name, SizeType length, Allocator* allocator = 0) const;
    GenericPointer Append(SizeType index, Allocator* allocator = 0) const;

    // Resolution
    ValueType* Get(ValueType& root) const;
    const ValueType* Get(const ValueType& root) const;

    // Creation (creates missing intermediate values)
    ValueType& Create(ValueType& root, typename ValueType::AllocatorType& allocator) const;
    ValueType& GetWithDefault(ValueType& root, const ValueType& defaultValue,
                               typename ValueType::AllocatorType& allocator) const;

    // Mutation
    ValueType& Set(ValueType& root, ValueType& value,
                    typename ValueType::AllocatorType& allocator) const;
    ValueType& Swap(ValueType& root, ValueType& value,
                     typename ValueType::AllocatorType& allocator) const;
    bool Erase(ValueType& root) const;

    // Error handling
    bool IsValid() const;
    PointerParseErrorCode GetParseErrorCode() const;
    size_t GetParseErrorOffset() const;

    // Token access
    const Token* GetTokens() const;
    size_t GetTokenCount() const;
};

The default typedef is:

typedef GenericPointer<Value> Pointer;

Error Codes

enum PointerParseErrorCode {
    kPointerParseErrorNone = 0,
    kPointerParseErrorTokenMustBeginWithSolidus,
    kPointerParseErrorInvalidEscape,
    kPointerParseErrorInvalidPercentEncoding,
    kPointerParseErrorCharacterMustPercentEncode
};

URI Fragment Support

In addition to standard JSON Pointer strings (beginning with /), the implementation supports URI fragment representation (beginning with #). URI fragments use percent-encoding for special characters according to RFC 3986.

Free Function Helpers

The header provides convenience free functions that create temporary Pointer objects:

template <typename T>
typename T::ValueType& CreateValueByPointer(T& root, const GenericPointer<...>& pointer, ...);

template <typename T>
typename T::ValueType* GetValueByPointer(T& root, const GenericPointer<...>& pointer, ...);

template <typename T>
typename T::ValueType& SetValueByPointer(T& root, const GenericPointer<...>& pointer, ...);

Dependencies

  • document.h -- GenericValue and GenericDocument types
  • internal/itoa.h -- Integer-to-string conversion for array index tokens

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