cjson · Section 3
cjson_parse_mp, cjson_parse
NAME
cjson_parse_mp, cjson_parse - parse a null-terminated JSON string into a DOM tree
LIBRARY
C Collections (libccollections, -lccollections)
SYNOPSIS
#include <ccollections/cjson.h>
cjson cjson_parse_mp(const char *json_str, char **err_str,
ccol_memmgmt_procs *mp);
/* convenience wrapper; default allocator */
static inline cjson cjson_parse(const char *json_str,
char **err_str);
DESCRIPTION
Parses json_str and returns the root node of the new DOM tree. All seven kinds of JSON value are accepted: - null. - boolean. - integer, stored as long long. - number, stored as double. - string, stored as an owned heap copy in UTF-8. - array, stored as an ordered list of child nodes. - object, stored as string keys, each with a child node. An object keeps its members in the order of the text (see cjson_dictionary_first(3)). When a key appears more than once in an object, the last value wins but the member keeps the place where the key first appeared, and every earlier value is deep-freed. So {"a":1,"b":2,"a":3} gives the members a = 3 and then b = 2. A parse takes expected time linear in the number of members of an object, even when the document comes from an untrusted peer. Each object hashes its keys with a secret seed that the library picks once per process, and an object whose keys collide anyway switches to a hash keyed with a secret 128-bit key, so a peer cannot keep a set of keys in one chain. See chashmap_create_full(3). An integer literal with no decimal point and no exponent is stored as CJSON_INTEGER, and every other number, an integer that overflows long long included, as CJSON_FLOAT. A double holds an integer exactly only up to 2^53, so an integer literal above LLONG_MAX can lose its low digits after a parse and a serialize, which is what most JSON libraries do with large integers as well. The decimal text of a number literal can be of any length, but a number that overflows an IEEE 754 double (one for which strtod returns infinity) is a parse error. String escape sequences \", \\, \/, \b, \f, \n, \r, \t, and \uXXXX (with surrogate pairs) decode to UTF-8. The escape \u0000 is a parse error, because cjson stores a string as a null-terminated char * value and a string cannot hold a null byte. RFC 8259 section 8.1 requires JSON text to be UTF-8, so a string or an object key that is not well-formed UTF-8 is a parse error, and the parser never repairs one. It refuses a raw byte sequence that is truncated, a continuation byte with no lead byte, an overlong form, an encoded UTF-16 surrogate, a code point above U+10FFFF or a byte from 0xF5 to 0xFF. It also refuses a \uXXXX escape of a surrogate that is not one half of a high-low pair, such as \uD800 at the end of a string or \uDC00 alone. The message names the defect, the bytes as 0xNN or the escape, and the byte offset in the input. So every string in a parsed tree is well-formed UTF-8, and two keys that differ in their bytes are always two keys. A document that nests more than 500 levels deep is a parse error. Each array or object counts as one level, in any combination, and a scalar adds none: 500 containers inside one another, with any value in the innermost, parse, and a 501st container is refused. The limit is a policy on which documents the library accepts and has nothing to do with the stack. The parser walks the nesting with an explicit worklist on the heap, so a document at the limit uses the same small, fixed amount of native stack as a flat one, and parsing is safe on a thread with a small stack. An ordinary JSON document never comes near this depth. The tree a parse builds is linear in the length of the input, and an empty array or object costs one node. The densest documents (deep chains of one-member objects, or long arrays of one-member objects) hold at most about 65 bytes of tree per byte of input on a 64-bit target. A program that parses input from a peer it doesn't trust can therefore bound the memory of a parse by bounding the length of the input. cjson_parse() is a static inline wrapper that calls cjson_parse_mp(json_str, err_str, NULL).
PARAMETERS
json_str Null-terminated JSON text.
err_str Out-parameter for the parse error message. On success the
function sets *err_str to NULL. On failure it points
*err_str at a null-terminated message that the LIBRARY
owns, so never free it. The message is not an allocation
and mp never sees it. It stays readable until the next
FAILING parse on the same thread, the same lifetime that
strerror(3) gives. The storage is per thread, so two
threads that parse at the same time never overwrite each
other's message; copy the text if you need it longer.
When the thread cannot keep a message buffer (memory runs
out, or the process has no thread-specific key left for
the buffer), the message is a fixed text that names no
reason. Pass NULL to throw the message away. The
message is printable ASCII whatever the input holds: A
printable ASCII byte of the input is quoted as it is, and
any other byte is spelled as 0xNN.
mp Custom memory management procedures for every node of the
resulting tree, or NULL for the default allocator.
The library keeps its own copy of the struct, so the caller
may release or reuse the struct once this call returns.
The functions that it names must stay callable until the
last node built with them is destroyed. A process can use
at most 64 distinct sets of four functions, and a call that
would need a 65th fails and returns NULL.
RETURN VALUE
The root cjson node on success. NULL when the parse fails. When err_str is not NULL, *err_str then holds the reason, a string that belongs to the library and that the caller never frees.
NOTES
The caller owns the returned tree and must free it with cjson_destroy(). The caller does NOT own the error string. The err and err_str out-parameters of every module in this library follow one rule: The library owns the string and the caller never frees it. See the note at the top of common.h. A number's decimal separator is always '.', as RFC 8259 asks, whatever the LC_NUMERIC locale of the calling thread.
EXAMPLES
char *err = NULL; cjson doc = cjson_parse_mp(input, &err, NULL); if (!doc) { fprintf(stderr, "parse failed: %s\n", err); return -1; } /* use doc ... */ cjson_destroy(doc);
SEE ALSO
cjson_parse_n_mp(3), cjson_destroy(3), cjson_serialize(3), cjson_serialize_free_mp(3), cjson_dictionary_first(3)