C Collections

cjson · Section 3

cjson_dictionary_first

cjson_dictionary_first - start an iteration over the members of an object node

C Collections (libccollections, -lccollections)

#include <ccollections/cjson.h>
 
typedef struct cjson_dictionary_iter {
    const char *key;
    cjson       value;
    /* private fields, whose names begin with cjson__ */
} cjson_dictionary_iter;
 
bool cjson_dictionary_first(cjson dict, cjson_dictionary_iter *it);

Fills the cursor it and puts it on the first member of the object
node dict, and cjson_dictionary_next(3) then moves it to each later
member.  After a call that returns true, it->key is the member's key
and it->value is its value, a borrowed reference with the same rules
as for cjson_dictionary_get(3).
 
Members come in insertion order, the order in which each key first
entered the object.  For a parsed document that is the order of the
members in the text, and for an object the program builds it is the
order of the cjson_dictionary_set(3) and cjson_set(3) calls that
added each key.  A key whose value is replaced keeps its place, but
a key that is removed and then set again goes after every other
member.  cjson_serialize(3), cjson_serialize_pretty(3) and
cjson_clone(3) use the same order.
 
The cursor is a plain struct that the caller owns, usually on its
stack.  Iterating allocates nothing and cannot fail, so there is
nothing to release and the caller can stop at any point.

dict  The object node to walk.
it    The cursor to fill.  It must not be NULL.

true when it stands on the first member.
 
false when dict has no members, is NULL or is not a
CJSON_DICTIONARY, and the function then sets it->key and it->value
to NULL.  Also false when it is NULL.

it->key points at the object's own copy of the key, which stays
valid until that member is removed or the object is destroyed.
 
The cursor remembers the member that the next step reads, the
successor of the member the last successful call gave.  Any change
to the object that leaves that successor in place keeps the cursor
valid:
 
  - the replacement of the value of any member, the current one
    included, with cjson_dictionary_set(3) or cjson_set(3).  The
    member keeps its place, so the walk goes on in the same order.
  - the removal of the current member, or of any member other than
    the successor.  To remove the current member, pass it->key to
    cjson_dictionary_remove(3).
  - the insert of a new key.  The new member goes after every other
    member, and whether this walk reaches it is unspecified.
 
Removing the successor, through cjson_dictionary_remove(3),
cjson_remove(3) or any other call, invalidates the cursor, and so
does destroying the object.  Calling cjson_dictionary_next(3) on an
invalid cursor is undefined behavior, so start again with
cjson_dictionary_first() after such a change.
 
it->key and it->value describe the current member as it was when
the cursor reached it.  cjson_dictionary_set(3) on the current key
destroys the old value, which leaves it->value pointing at freed
memory, so read the new value with cjson_dictionary_get(3) and
it->key.  cjson_set(3) updates the existing node in place instead,
so it->value names the same node after it.

A cjson tree is not thread safe, but several threads can walk the
same object at once, each with its own cursor, as long as no thread
changes the object.

cjson doc = cjson_parse("{\"b\":1,\"a\":2}", NULL);
cjson_dictionary_iter it;
for (bool ok = cjson_dictionary_first(doc, &it); ok;
     ok = cjson_dictionary_next(&it)) {
    printf("%s = %lld\n", it.key, cjson_int_val(it.value));
}
/* prints "b = 1" and then "a = 2" */
 
/* Remove every member whose value is null, in one pass. */
for (bool ok = cjson_dictionary_first(doc, &it); ok;
     ok = cjson_dictionary_next(&it)) {
    if (cjson_type(it.value) == CJSON_NULL)
        cjson_dictionary_remove(doc, it.key);
}
cjson_destroy(doc);

cjson_dictionary_next(3), cjson_dictionary_get(3), cjson_dictionary_remove(3), cjson_dictionary_size(3), cjson_serialize(3)