Implementation:Alibaba MNN RapidJSON Writer
| Knowledge Sources | |
|---|---|
| Domains | JSON_Parsing, Serialization |
| Last Updated | 2026-02-10 12:00 GMT |
Overview
3rd_party/rapidjson/writer.h (709 lines) implements a JSON writer/serializer for the vendored RapidJSON library within Alibaba MNN. The Writer class implements the Handler concept, generating JSON text from programmatic API calls or from DOM tree traversal via Document::Accept(). It produces compact (non-pretty-printed) JSON output.
Usage note: Vendored dependency used internally by MNN for JSON configuration parsing (model configs, LLM configs). Not directly imported by end users.
Key Class
Writer
template<typename OutputStream,
typename SourceEncoding = UTF8<>,
typename TargetEncoding = UTF8<>,
typename StackAllocator = CrtAllocator,
unsigned writeFlags = kWriteDefaultFlags>
class Writer {
public:
typedef typename SourceEncoding::Ch Ch;
static const int kDefaultMaxDecimalPlaces = 324;
explicit Writer(OutputStream& os,
StackAllocator* stackAllocator = 0,
size_t levelDepth = kDefaultLevelDepth);
explicit Writer(StackAllocator* allocator = 0,
size_t levelDepth = kDefaultLevelDepth);
// Reset for reuse
void Reset(OutputStream& os);
// Handler interface
bool Null();
bool Bool(bool b);
bool Int(int i);
bool Uint(unsigned u);
bool Int64(int64_t i);
bool Uint64(uint64_t u);
bool Double(double d);
bool RawNumber(const Ch* str, SizeType length, bool copy = false);
bool String(const Ch* str, SizeType length, bool copy = false);
bool StartObject();
bool Key(const Ch* str, SizeType length, bool copy = false);
bool EndObject(SizeType memberCount = 0);
bool StartArray();
bool EndArray(SizeType elementCount = 0);
bool RawValue(const Ch* json, size_t length, Type type);
// Configuration
void SetMaxDecimalPlaces(int maxDecimalPlaces);
bool IsComplete() const;
};
Write Flags
enum WriteFlag {
kWriteNoFlags = 0,
kWriteValidateEncodingFlag = 1, // Validate encoding of JSON strings
kWriteNanAndInfFlag = 2, // Allow writing of Infinity, -Infinity and NaN
kWriteDefaultFlags = RAPIDJSON_WRITE_DEFAULT_FLAGS
};
Number Serialization
The writer uses optimized internal routines for number output:
- Integers -- Uses
internal/itoa.hfor fast integer-to-string conversion via digit lookup tables - Doubles -- Uses
internal/dtoa.himplementing the Grisu2 algorithm for fast and accurate double-to-string conversion - Decimal places -- Configurable via
SetMaxDecimalPlaces()(default: 324, i.e., full precision)
SIMD Acceleration
Like the Reader, the Writer supports SIMD-accelerated string scanning for characters that need escaping:
- SSE4.2 (via
RAPIDJSON_SSE42) - SSE2 (via
RAPIDJSON_SSE2) - NEON (via
RAPIDJSON_NEON)
String Escaping
JSON strings are output with proper escaping for control characters and Unicode:
| Character | Escape Sequence |
|---|---|
" |
\"
|
\ |
\\
|
| Backspace (U+0008) | \b
|
| Form feed (U+000C) | \f
|
| Line feed (U+000A) | \n
|
| Carriage return (U+000D) | \r
|
| Tab (U+0009) | \t
|
| Other control chars (U+0000-U+001F) | \uXXXX
|
Dependencies
stream.h-- Output stream abstractionsinternal/meta.h-- Template metaprogramming utilitiesinternal/stack.h-- Internal stack for nesting level trackinginternal/strfunc.h-- String utility functionsinternal/dtoa.h-- Double-to-string (Grisu2 algorithm)internal/itoa.h-- Integer-to-string conversionstringbuffer.h-- In-memory string output stream
License
MIT License. Copyright (C) 2015 THL A29 Limited (Tencent) and Milo Yip.
See Also
- Alibaba_MNN_RapidJSON_Reader -- SAX parser (inverse operation)
- Alibaba_MNN_RapidJSON_Document -- DOM document with
Accept()for serialization - Alibaba_MNN_RapidJSON_Itoa -- Integer-to-string conversion used by Writer