C Collections

cjson · Section 3

cjson_list_push

cjson_list_push - append a child to an array node

C Collections (libccollections, -lccollections)

#include <ccollections/cjson.h>
 
ccol_retval cjson_list_push(cjson arr, cjson child);

Appends child to the end of arr.  On success the array owns child
and the caller must not free it.
 
Each return code carries one ownership rule:
 
  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.
 
These are the cases that give ccol_invalid_args: child is NULL;
child is attached to a list or dictionary parent; child is arr
itself; arr is NULL or not a CJSON_LIST.  One more case is refused:
arr is reachable when you descend from child.  Attaching child would
then make arr a new ancestor of child while arr 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.

arr    The target node, which must be a CJSON_LIST.
child  The node to append.  It must not be attached anywhere else,
       and its own subtree must not contain arr.

ccol_success, ccol_invalid_args, ccol_not_enough_memory, or
ccol_container_full when arr has reached its maximum element count.

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.  Call cjson_clone()
first to get an independent copy that is safe to push.

cjson_list_get(3), cjson_create_list(3), cjson_clone(3)