I am attempting to use the cJSON library, written by Dave Gamble, to read in the following JSON array:
\"items\":
[
{
\"name\": \"command\",
IMHO, this is one example of a case where you should burst the library's encapsulation and work directly with it's object data structure. cJSON.h
defines the core object as the following struct
:
/* The cJSON structure: */
typedef struct cJSON {
struct cJSON *next,*prev; /* next/prev allow you to walk array/object chains. Alternatively, use GetArraySize/GetArrayItem/GetObjectItem */
struct cJSON *child; /* An array or object item will have a child pointer pointing to a chain of the items in the array/object. */
int type; /* The type of the item, as above. */
char *valuestring; /* The item's string, if type==cJSON_String */
int valueint; /* The item's number, if type==cJSON_Number */
double valuedouble; /* The item's number, if type==cJSON_Number */
char *string; /* The item's name string, if this item is the child of, or is in the list of subitems of an object. */
} cJSON;
(One could quibble with some of the naming choices the author made, of course. But good naming is hard.)
The key thing to note is that both JSON Objects and JSON Arrays have a non-null child
field, which points to a doubly-linked list of their children. Children of JSON Objects also have non-null string
fields which contains the field name associated with that child.
So, to generically iterate over the JSON Array ja
in O(n) time, calling a function for each element, you write something like this:
cJSON_ForEachItem(cJSON *ja, int (*f)(cJSON *ja, int i, cJSON *jchild))
{
cJSON *jchild;
int i;
for (jchild=ja->child, i=0; jchild; jchild=jchild->next, ++i) {
// do something here with the ith child...
if (f(ja, i, jchild))
break;
}
}
Since Objects and Arrays only differ internally in the presence of names for each child item, that function will also iterate the fields of an object. The callback can tell because ja->type
will be either cJSON_Array
or cJSON_Object
, and jchild->string
will be non-null for Objects as well.
Doing the same iteration by calling cJSON_GetArraySize()
and using cJSON_GetArrayItem()
will be order O(n^2) because it has to traverse the linked list each time to locate the nth item.
Arguably, cJSON should include some generic ForEach
functions, but that might represent the beginning of a significant amount of scope-creep away from it's professed original goal of being "the dumbest possible parser that you can get your job done with".