cjson · Section 3
cjson_set
NAME
cjson_set - write a C scalar to the DOM leaf at a dot-separated path
LIBRARY
C Collections (libccollections, -lccollections)
SYNOPSIS
#include <ccollections/cjson.h> ccol_retval cjson_set(cjson root, const char *path, val);
DESCRIPTION
The macro finds the type and the raw value of val at compile time
through _Generic, and writes that value as a scalar to the leaf node
that path addresses.
Accepted C types for val:
bool -> CJSON_BOOL
any integer type -> CJSON_INTEGER (stored as long long);
an unsigned value > LLONG_MAX
-> CJSON_FLOAT (stored as double)
float / double -> CJSON_FLOAT (stored as double)
char * / const char * -> CJSON_STRING (strdup'd copy)
char array (char buf[N]) -> CJSON_STRING
string literal ("...") -> CJSON_STRING
NULL, C23 nullptr -> CJSON_NULL
Before C23 the macro true is the int 1, so cjson_set(doc, k, true)
stores the CJSON_INTEGER 1, and you have to write (bool)true to get a
CJSON_BOOL. In C23 true has the type bool and needs no cast. The
library headers do not include <stdbool.h> under C23, but a C library
whose <stdbool.h> defines true as 1 even there (FreeBSD 14) brings the
int back into every file that includes it.
The macro copies val into a local whose type is the type of val after
array-to-pointer conversion, with the top-level qualifiers removed.
So a char array, such as a buffer that snprintf(3) filled, arrives as
a pointer to its first character, and a pointer that is itself const,
such as a static const char *const, arrives as an ordinary pointer;
in both cases the macro gets the text of the string.
cjson_set rejects every other C type, long double, a struct and every
pointer type other than char * and const char * included. For such a
type it gives ccol_invalid_args and leaves the target leaf alone: It
creates no new leaf and does not change the value of a leaf that
exists.
cjson_set also rejects a void * value that is not NULL, with
ccol_invalid_args. Only a NULL value writes CJSON_NULL, although
void * is also the compile-time type of the bare NULL literal.
A bit-field is not accepted as val: C forbids __typeof__ on one, so
the macro does not compile for it. Cast the bit-field to its integer
type, for example (unsigned)cfg.port, or to bool.
A char *, const char * or string-literal value must be valid UTF-8.
JSON text is UTF-8 (RFC 8259 section 8.1), so any other byte sequence
has no JSON form, and cjson_set returns ccol_invalid_args without
touching the target leaf, as it does for a double that is not finite.
The same rule holds for the leaf component of the path, which becomes
an object key when the library has to create the leaf. Every key in
the tree is valid UTF-8, so the library refuses a key that is not
instead of storing it. cjson_get(3) and cjson_remove(3) need no such
rule because they only look a component up, and a component that is
not valid UTF-8 can never match a key that exists, so they report it
as absent.
Signed integer types are sign-extended to long long correctly, and an
unsigned integer in the range [0, LLONG_MAX] round-trips exactly. An
unsigned value > LLONG_MAX, which only a 64-bit unsigned type such as
uint64_t can hold, has no long long form, so the macro stores it as
the nearest CJSON_FLOAT, which is also what cjson_parse(3) makes of
the same decimal literal. Read such a leaf with cjson_double_val(3).
A double holds an integer exactly only up to 2^53, so the low digits
of such a value can be lost.
The macro creates the leaf if it does not exist, but the immediate
parent of the leaf must exist. If the leaf exists, the macro updates
that node in place, so a handle to the node stays valid. The node
gets the type of val whatever its current type is, and if the node is
an array or an object the macro deep-frees its children before it
writes the new value.
PARAMETERS
root Root cjson node.
path Dot-separated path string to the target leaf.
Escape sequences: '\.' -> literal '.', '\\' -> literal '\'.
A '\' before any other character is left unchanged.
val C value, whose type must be one of the accepted types above.
RETURN VALUE
ccol_retval: ccol_success on success.
ccol_invalid_args; an empty path or path component (leading,
trailing, or consecutive dots), a path whose
shape cannot apply to the tree (a component
that must descend into or below a string, a
number, a bool or null, or a component that
is not '#N' on an array), a
malformed index on an array (non-numeric,
negative, or bare '#' with no digits) or a
path component that is not valid UTF-8, at
any position of the path, a string value that is
not valid UTF-8, val's C type is not one of the
accepted types listed above, or val is a void *
value that is not NULL.
ccol_key_not_found; a well-formed name or index on the parent path,
or a syntactically valid but out-of-range
array index of the leaf, does not exist. An
index of any number of digits is valid syntax,
so an index too large for any integer type is
out of range and gives this code. An
array, unlike an object, has no way to
auto-extend to fit an arbitrary index.
ccol_not_enough_memory; allocation failure.
ccol_container_full; the parent object has already reached its
maximum element count (creating a new key only).
NOTES
A bool literal needs an explicit cast, (bool)true. Without it the macro sees the integer 1 instead of a _Bool, and the result is a CJSON_INTEGER instead of a CJSON_BOOL. Plain char has the platform's default signedness, which is signed on x86/x86-64 Linux and unsigned where -funsigned-char applies, on ARM for example. Write signed char or unsigned char explicitly when the code has to be portable.
EXAMPLES
cjson_set(doc, "users.#0.active", (bool)true); cjson_set(doc, "users.#0.score", 99); cjson_set(doc, "users.#0.label", "champion"); cjson_set(doc, "users.#0.ratio", 0.75); /* Type change: replace an existing object subtree with a scalar */ cjson_set(doc, "users.#0.metadata", 0); /* deep-frees old subtree */ /* Set to null */ cjson_set(doc, "users.#0.deprecated", NULL);
SEE ALSO
cjson_get(3), cjson_remove(3), cjson_double_val(3), cjson_create_string(3), cjson_dictionary_set(3), cjson_list_push(3)