The order doesn't really matter. Do it as feels right to you. I'd do
it bottom up:
int i;
const char *addrs[] = {"Serial1", "Parallel1", "Parallel2"};
json_t *root, *computer_listing, *ports;
ports = json_array();
for(i = 0; i < 3; i++) {
json_t *tmp = json_object();
json_object_set_new(tmp, "Address", json_string(addrs[i]));
json_array_append_new(ports, tmp);
}
computer_listing = json_object();
json_object_set_new(computer_listing, "Name", json_string("STX"));
json_object_set_new(computer_listing, "Ports", ports);
root = json_object();
json_object_set_new(root, "ComputerListing", computer_listing);
This first creates the Ports array and adds members to it in the for
loop. It then creates the ComputerListing object and adds Name and
Ports to it. Finally it creates the root object and adds
ComputerListing to it.
> Also, since I have to do a few json_object() calls to create the
> different elements, I'm assuming I only need to one json_decref at
> the root object (in other words, json_decref will traverse the entire
> "tree" of JSON objects and free everything?)
json_decref() will traverse and recursively decref everything, so a
single call json_decref(root) is enough at the end, but you need to
be careful to not leave the references you created yourself hanging.
See my code above. It uses the "_new" functions to add values to
containers. For example:
json_object_set_new(computer_listing, "Name", json_string("STX"));
This is equivalent to:
json_t *tempstring = json_string("STX");
json_object_set_new(computer_listing, "Name", tempstring);
After the call to json_string(), tempstring points to a new reference.
json_object_set_new() _steals_ that reference when adding tempstring
to the computer_listing object. This is equivalent to:
json_t *tempstring = json_string("STX");
json_object_set(computer_listing, "Name", tempstring);
json_decref(tempstring);
Note that this time the function json_object_set() was used instead of
json_object_set_new(). json_object_set() is a good citizen and
increments the reference count of tempstring because it stores
tempstring inside itself. So in this case you need to decrement the
reference count for the reference you created yourself by calling
json_string().
I hope this makes sense. The reference counting and the differences
between the "_new" and "normal" function variants are documented in:
http://www.digip.org/jansson/doc/1.3/apiref.html#reference-count
> Sorry if I'm asking very basic questions, still trying to get the hang
> of this and I googled around and couldn't find any sample code.
Unfortunately, the tutorial doesn't cover value creation or
manipulation at all. This should be fixed at some point, as the
reference counting is the hardest part of Jansson to grasp if the
concept is new to the user.
Petri