Nathan,
did you look at the notes/examples in the "Working with JSON" handout on
my site?
http://www.scottklement.com/presentations/Working%20with%20JSON%20in%20RPG.pdf
Does that provide enough information for you?
What yajl_stmf_load_tree() does is parse the JSON data into a set of
data structures in memory. So once this is complete, it is no longer in
JSON format, now it is just structures in memory. The pointer returned
(docNode) is a pointer to the document node of this memory tree. from
that, you can get it's child nodes. From those child nodes, you can go
deeper and get their child nodes, etc.
You don't necessarily have to know the structure of the document in
advance, but it certainly does simplify your code, and this is what is
desired in 99% of the cases. Can you give me an example of where you'd
want to process data without knowing the format in advance? What would
you do with the results in that case?
But, it should be doable. You can do something like:
if YAJL_IS_OBJECT(docNode);
dow YAJL_OBJECT_LOOP(docnode: i: key: val);
// do something (but what?) with the key and value.
enddo
endif
likewise, you'd have to interrogate each subnode to determine if they
are objects, arrays, or just straight values. This could be done
recursively... but where I struggle is not with calling YAJL, but
rather what you do with the results.
My experience with both JSON and XML is that you typically know the
format in advance, and you code a data structure in RPG (or some similar
structure like record formats in a PF) that matches the format of the
file, so you do indeed know the format in advance, and you use something
like XML-INTO or XML-SAX to load them with XML. The same thing is
available here with YAJL's tree routines (similar to XML-INTO in a way,
though since it's not built into the compiler it can't get the names
from the DS definition) or YAJL's stream-oriented routines (similar to
XML-SAX)
Do you have situations where you parse documents without any knowledge
of their format in advance? How does that work?
On 6/1/2015 12:19 PM, Nathan Andelin wrote:
Following are a few code snippets from one of Scott Klement's presentations
on YAJL:
docNode = yajl_stmf_load_tree( '/tmp/example.json' : errMsg );
Without additional documentation or reviewing the code, it's not clear what
yajl_stmf_load_tree() may be doing doing. Is it really parsing the stream
file? Or is it just returning a pointer to the first "node" in the tree?
The following appears to be parsing, but the syntax affirms that the
programmer must know the layout of the file beforehand - that it contains
an array of objects which contains a set of name-value pairs.
list = YAJL_object_find(docNode: 'list');
i = 0;
dow YAJL_ARRAY_LOOP( list: i: node );
j = 0;
dow YAJL_OBJECT_LOOP( node: j: key: val);
exsr load_subfield;
enddo;
enddo;
As an Amazon Associate we earn from qualifying purchases.