cjson · Section 3
cjson_dictionary_set
NAME
cjson_dictionary_set - set or replace a key in an object node
LIBRARY
C Collections (libccollections, -lccollections)
SYNOPSIS
#include <ccollections/cjson.h> ccol_retval cjson_dictionary_set(cjson obj, const char *key, cjson child);
DESCRIPTION
Sets the value for key in obj, or replaces it. When a child is already stored under key, the library deep-frees it and puts the new child in its place in one step that cannot fail, so a failed insert never leaves the slot dangling. On success the object owns child and the caller must not free it. Members stay in insertion order: A new key goes after every other member, and a replaced key keeps its place. See cjson_dictionary_first(3). Each return code carries one ownership rule, the same as for cjson_list_push: ccol_invalid_args always means that the call refused its arguments and touched nothing. child is exactly as it was before the call, and whoever owned it before (the caller, or the container it is attached to) owns it and has to destroy it. Any other code that is not ccol_success (ccol_not_enough_memory, ccol_container_full) always means that ownership passed to this call and the call has deep-freed child, so freeing child again is a double free. child must not be attached to a list or dictionary parent, with one exception: You can pass back the exact node that is stored under key, as in cjson_dictionary_set(obj, k, cjson_dictionary_get(obj, k)). That call does nothing, returns ccol_success and leaves obj as it is. These are the cases that give ccol_invalid_args: child is NULL; child is attached somewhere else, under another key or in another container; child is obj itself; obj is NULL or not a CJSON_DICTIONARY; key is NULL or not valid UTF-8. One more case is refused: obj is reachable when you descend from child. Attaching child would then make obj a new ancestor of child while obj is also a descendant of child, which is a cycle. When memory is low the library cannot always finish that last check. It doesn't risk an undetected cycle in that case: It returns ccol_not_enough_memory, and the call deep-frees child just as it does when the insert itself runs out of memory. A set, a get and a remove take constant expected time, also when an untrusted peer chooses the keys. The object hashes its keys with a secret seed that the library picks once per process, and when keys collide it switches to a hash keyed with a secret 128-bit key; see cjson_parse(3). Every key in the tree is valid UTF-8, and that is what lets cjson_serialize() write keys out unchanged. A key that is not valid UTF-8 is refused instead of stored; cjson_create_string(3) applies the same rule to string values.
PARAMETERS
obj The target node, which must be a CJSON_DICTIONARY.
key Null-terminated key, which must be valid UTF-8. The library
stores a copy of it.
child The value node. It must not be attached anywhere else and
must not contain obj in its own subtree (see above).
RETURN VALUE
ccol_success, ccol_invalid_args, ccol_not_enough_memory, or ccol_container_full when obj has reached its maximum element count.
NOTES
A node that cjson_get(), cjson_list_get() or cjson_dictionary_get() returns is a borrowed reference into an existing tree, and the caller doesn't own it. Never pass such a node as child, unless it is the value stored under key (see above). Call cjson_clone() first to get an independent copy that is safe to store somewhere else.
SEE ALSO
cjson_dictionary_get(3), cjson_dictionary_first(3), cjson_create_dictionary(3), cjson_create_string(3), cjson_list_push(3), cjson_clone(3)