Debian Codegraph @

protolib/examples/jsonExample.cpp

raw
1 #include <stdio.h>
2
3 #include "protoJson.h"
4 #include "protoDebug.h"
5
6 int main(int argc, char* argv[])
7 {
8 // Here we use the ProtoJson::Parser to parse JSON text from
9 // STDIN. If a complete JSON document is processed, the resulting
10 // document is printed to STDOUT in a pretty format.
11 ProtoJson::Parser parser;
12 ProtoJson::Parser::Status status = ProtoJson::Parser::PARSE_MORE;
13 int result;
14 char buffer[1024];
15 while (0 != (result = fread(buffer, sizeof(char), 1023, stdin)))
16 {
17 status = parser.ProcessInput(buffer, result);
18 // Note future extensions to parser could easily include
19 // ongoing "stream" process with PARSE_DONE returned for
20 // completed root level items. I.e., an indefinite stream
21 // of JSON content could be incrementally processed if
22 // desired.
23 if (ProtoJson::Parser::PARSE_ERROR == status) break;
24 }
25
26 if (ProtoJson::Parser::PARSE_DONE == status)
27 {
28
29 ProtoJson::Document* doc = parser.AccessDocument();
30
31 // If uncommented, the code below adds a top level object
32 // to the loaded document to illustrate how a document can
33 // be built or, in this casee augmented.
34 /*
35 ProtoJson::Object* object = new ProtoJson::Object();
36 ProtoJson::Array* array = new ProtoJson::Array();
37 for (unsigned int i = 0; i < 10; i++)
38 {
39 if (0 != (i & 1))
40 {
41 // odd index array entries are integers
42 ProtoJson::Number* number = new ProtoJson::Number((int)(i*2));
43 array->AppendValue(*number);
44 }
45 else
46 {
47 // even index array entries are floating
48 ProtoJson::Number* number = new ProtoJson::Number((double)i / 4.0);
49 array->AppendValue(*number);
50 }
51 }
52 object->InsertEntry("exampleArray", *array);
53 doc->AddItem(*object);
54 */
55
56 // Note the ProtoJson::Document::Print() method uses
57 // the Document::Iterator class to do a depth-first
58 // traversal of the JSON document content the parser loaded.
59 // In the future, this example may be expanded to provide
60 // more detailed illustration of the Document::Iterator _and_
61 // methods to build ProtoJson::Document instances from
62 // scratch.
63 doc->Print(stdout);
64 }
65 else if (ProtoJson::Parser::PARSE_MORE == status)
66 {
67 TRACE("jsonExample: input document was not complete?!\n");
68 }
69 else // PARSE_ERROR
70 {
71 TRACE("jsonExample: error parsing input document!\n");
72 }
73
74 TRACE("jsonExample::Done.\n");
75 } // end main()