JSON (RFC 8259): a pull reader and a writer into caller space.
The reader hands out one event at a time, at the caller's pace: the start and the end of objects and arrays, keys, strings, numbers, true, false, null and the end of the document. It copies nothing. A string without escapes is a view into the input, and a string with escapes says so, so the caller decodes it into its own buffer with UnescapeString. A number is its raw text, converted only when the caller asks, into the type the caller wants (NumberToInt64, NumberToUInt64, NumberToDouble).
The reader is strict by default, RFC 8259 in full:
- the root may be any value, not only an object;
- numbers follow the grammar exactly: no '+', no leading zero, no '.5', no '1.', no '1.2.3', no hexadecimal, and the conversion does not depend on the locale;
- strings are UTF-8 checked, raw control characters (below 0x20) are refused, '\u' escapes take exactly four hex digits and surrogates must come in pairs;
- only space, tab, line feed and carriage return are white space, and nothing but white space may follow the root.
A repeated key is not an error of the reader. Detecting it needs memory that grows with the object, and the reader allocates nothing. The consumer (a schema, the Config) knows its fields and decides.
Every event and every error carries its position: the byte offset, the line and the column (in bytes, both from 1).
Limits with safe defaults bound depth, string, key, number and document size, so a hostile input cannot make the reader do more than the caller allowed. The nesting stack lives inside the reader, so a document read whole allocates nothing at all.
The reader also takes the document in pieces of any size, as they arrive from a socket or a model that streams its output, and gives the same events, errors and positions as with the whole document. Only a token that a piece cuts in two is copied, into a buffer that the reader keeps for the next documents.
CJsonPointer builds the RFC 6901 path of where a walk is, for error messages, in a buffer of the caller.
The writer writes into a span of the caller and never allocates. When the span runs out it stops writing, keeps counting, and says how many bytes the whole document needs. An empty span measures without writing. It escapes what JSON requires, refuses invalid UTF-8 instead of replacing it, writes integers exactly and doubles in the shortest form that reads back to the same value, and checks the nesting: closing what was not opened, or a value where a key is due, is an error.
Not here: a DOM (whoever wants a tree builds it on top of the reader), JSON5, comments, JSON Schema.
Both classes are for the exclusive use of one thread.