Debian Codegraph @

LspCpp/third_party/rapidjson/example/simplepullreader/simplepullreader.cpp

raw
1 #include "rapidjson/reader.h"
2 #include <iostream>
3 #include <sstream>
4
5 using namespace rapidjson;
6 using namespace std;
7
8 // If you can require C++11, you could use std::to_string here
9 template <typename T> std::string stringify(T x) {
10 std::stringstream ss;
11 ss << x;
12 return ss.str();
13 }
14
15 struct MyHandler {
16 const char* type;
17 std::string data;
18
19 MyHandler() : type(), data() {}
20
21 bool Null() { type = "Null"; data.clear(); return true; }
22 bool Bool(bool b) { type = "Bool:"; data = b? "true": "false"; return true; }
23 bool Int(int i) { type = "Int:"; data = stringify(i); return true; }
24 bool Uint(unsigned u) { type = "Uint:"; data = stringify(u); return true; }
25 bool Int64(int64_t i) { type = "Int64:"; data = stringify(i); return true; }
26 bool Uint64(uint64_t u) { type = "Uint64:"; data = stringify(u); return true; }
27 bool Double(double d) { type = "Double:"; data = stringify(d); return true; }
28 bool RawNumber(const char* str, SizeType length, bool) { type = "Number:"; data = std::string(str, length); return true; }
29 bool String(const char* str, SizeType length, bool) { type = "String:"; data = std::string(str, length); return true; }
30 bool StartObject() { type = "StartObject"; data.clear(); return true; }
31 bool Key(const char* str, SizeType length, bool) { type = "Key:"; data = std::string(str, length); return true; }
32 bool EndObject(SizeType memberCount) { type = "EndObject:"; data = stringify(memberCount); return true; }
33 bool StartArray() { type = "StartArray"; data.clear(); return true; }
34 bool EndArray(SizeType elementCount) { type = "EndArray:"; data = stringify(elementCount); return true; }
35 private:
36 MyHandler(const MyHandler& noCopyConstruction);
37 MyHandler& operator=(const MyHandler& noAssignment);
38 };
39
40 int main() {
41 const char json[] = " { \"hello\" : \"world\", \"t\" : true , \"f\" : false, \"n\": null, \"i\":123, \"pi\": 3.1416, \"a\":[1, 2, 3, 4] } ";
42
43 MyHandler handler;
44 Reader reader;
45 StringStream ss(json);
46 reader.IterativeParseInit();
47 while (!reader.IterativeParseComplete()) {
48 reader.IterativeParseNext<kParseDefaultFlags>(ss, handler);
49 cout << handler.type << handler.data << endl;
50 }
51
52 return 0;
53 }