basic_parser.htm 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. <HTML>
  2. <HEAD>
  3. <title>libjson Basic Parser Example</title>
  4. <script type="text/javascript" src="../Library Interface/scripts/shCore.js"></script>
  5. <script type="text/javascript" src="../Library Interface/scripts/shBrushCpp.js"></script>
  6. <link type="text/css" rel="stylesheet" href="../Library Interface/styles/shCoreDefault.css"/>
  7. <script type="text/javascript">SyntaxHighlighter.all();</script>
  8. </HEAD>
  9. <BODY>
  10. <h1>libjson Basic Parser</h1>
  11. <p>Previously we looked at how to create a new JSON node tree programmatically that could be sent over the wire to consumer. Now we&#39;ll look at the consumer side and see how to parse the JSON string into our application.</p>
  12. <p><span id="more-135"></span></p>
  13. <p>First we&#39;ll look at the main function in our program that will take a JSON string as input, parse it using the library and pass the tree to a function that will extract useful data.</p>
  14. <pre class="brush:cpp;wrap-lines:true">std::string json = &quot;{\&quot;RootA\&quot;:\&quot;Value in parent node\&quot;,\&quot;ChildNode\&quot;:{\&quot;ChildA\&quot;:\&quot;String Value\&quot;,\&quot;ChildB\&quot;:42}}&quot;;
  15. JSONNode n = libjson::parse(json);
  16. ParseJSON(n);</pre>
  17. <p>The first line is just a simple JSON string containing a child object. You&#39;ll likely get a string from a web service, message buss or even a file.</p>
  18. <p>Line 2 is where the magic happens in the library. We just pass the string to <em>libjson::parse</em> and if all is well, we&#39;ll receive a node tree in return. NOTE that the parser is going to allocate memory on the stack, so do not try and delete it, let it go out of scope.</p>
  19. <p>Line 3 is a function call that we define for iterating through the JSON tree. While mine isn&#39;t pretty, it gets the job done for simple JSON objects.</p>
  20. <pre class="brush:cpp;wrap-lines:true">void ParseJSON(const JSONNode &amp; n){
  21. JSONNode::const_iterator i = n.begin();
  22. while (i != n.end()){
  23. // recursively call ourselves to dig deeper into the tree
  24. if (i -> type() == JSON_ARRAY || i -> type() == JSON_NODE){
  25. ParseJSON(*i);
  26. }
  27. // get the node name and value as a string
  28. std::string node_name = i -> name();
  29. // find out where to store the values
  30. if (node_name == &quot;RootA&quot;){
  31. rootA = i -> as_string();
  32. }
  33. else if (node_name == &quot;ChildA&quot;){
  34. childA = i -> as_string();
  35. }
  36. else if (node_name == &quot;ChildB&quot;)
  37. childB = i -> as_int();
  38. //increment the iterator
  39. ++i;
  40. }
  41. }</pre>
  42. <p>Next on line 7 we get a pointer to the first iterator of the node we&#39;re currently dealing with. The iterator lets us navigate through the child nodes, one by one.</p>
  43. <p>Line 8 begins a while loop that will continue until we&#39;ve reached the final iterator returned by <em>json_end</em>.</p>
  44. <p>If the iterator is currenly pointing to a node of type JSON_ARRAY or JSON_NODE, that means we are at a branch that requires a new iterator for processing. Thus line 16 makes a recursive call to the function so that we can start processing on the child node. Without this call, we would get the name of the node but trying to get a value would return nothing.</p>
  45. <p>On line 20 we call the <em>name</em> method that will return a string with the name of the node. If the node is not named or it&#39;s an array&#39;s value, the string will be empty, so check for that.</p>
  46. <p>Lines 23 through 34 are a simple decision tree that attempt to match the name of the node to known values and if a match is made, we use one of the library functions to extract the value of the node. <em>as_string</em> naturally returns the value of the node as a string. This is probably the easiest to use in that it doesn&#39;t care if the value of the node is encased in quotation marks or not, it will always return a string. You can read any node as a string and then typecast to whatever need.</p>
  47. <p>Line 38 increments our iterator to the next node.</p>
  48. <p>So there you have a very simple little parser that will iterate through your tree and grab extract the data. Naturally you&#39;ll want to add error handling and tweak it for your own use.</p>
  49. <BODY>
  50. </HTML>