multiple attached nodes

16 views
Skip to first unread message

Paul Harris

unread,
May 31, 2012, 1:20:55 PM5/31/12
to jansso...@googlegroups.com
Hi again,

It seems logical and possible to attach our nodes multiple times in the tree, like so:

      json_t * host = json_object();
      json_t * child = json_string("hello");
      json_object_set(host, "child1", child);
      json_object_set(host, "child2", child);

which yields:  {"child1": "hello", "child2": "hello"}

Perhaps this can be mentioned in the reference count or cycles part of the documentation?

It has importance when deciding how to manage the tree, for example,

I wanted to track the parent of a node.

At the moment, I am tracking the parents explicitly in my little wrapper, but I thought it would be cool if I could call something like
json_t * json_node_parent( json_t * );

which returns null (if no parent), or the object/array parent of the node.

The purpose is so that I can calculate the address of a node for error reporting,
eg if I expect node C to be a string of "ABC", and the JSON looks like this:
{ "A" : [ {}, {}, { "B" : { "C" : "ABC" } } ] }

then I'd like to print out
Expected A[2].B.C to equal "ABC"

I can do this either 2 ways:
1) Do a recursive search of the entire JSON tree and find the node and its parent chain,
2) Trace up through parent pointers.

If you can attach a node more than once to the tree, then option #1 is not viable, nor is a json_node_parent() function.
So, I shall continue with my wrapper, which will check to ensure the wrapper isn't attached more than once (even if the underlying node is).

thanks,
Paul

Reply all
Reply to author
Forward
0 new messages