C Collections

cjson · Section 3

cjson_clone

cjson_clone - deep-copy a DOM subtree

C Collections (libccollections, -lccollections)

#include <ccollections/cjson.h>
 
cjson cjson_clone(cjson node);

Returns a copy of the subtree under node that shares nothing with
the source.  Every string is duplicated, and every array and object
is copied with its contents.  The clone uses the allocator that the
source's root node keeps, and each object of the copy keeps its
members in the order of the source object.
 
A subtree that nests more than 500 levels deep is refused, and the
function reports it the same way as an allocation failure.  Each
array or object counts as one level, in any combination, and a
scalar adds none.  The parser has the same limit, so any tree that
parsed can be cloned.  Only the construction API (cjson_list_push()
and cjson_dictionary_set()) can build a deeper tree, since it has no
depth bound of its own.  The limit is a policy and has nothing to do
with the stack size of the calling thread: The function walks the
nesting with an explicit worklist on the heap, so a tree at the
limit uses the same small, fixed amount of native stack as a flat
one.  An ordinary tree never comes near this depth.

On success, a new root node that the caller owns.  NULL when an
allocation fails or when node nests more than 500 levels deep.

cjson_destroy(3), cjson_dictionary_first(3)