|
| 1 | +#include "rapidjson/reader.h" |
| 2 | +#include <iostream> |
| 3 | + |
| 4 | +using namespace rapidjson; |
| 5 | +using namespace std; |
| 6 | + |
| 7 | +struct MyHandler { |
| 8 | + const char* type; |
| 9 | + std::string data; |
| 10 | + |
| 11 | + bool Null() { type = "Null"; data.clear(); return true; } |
| 12 | + bool Bool(bool b) { type = "Bool"; data = b? "true": "false"; return true; } |
| 13 | + bool Int(int i) { type = "Int"; data = std::to_string(i); return true; } |
| 14 | + bool Uint(unsigned u) { type = "Uint"; data = std::to_string(u); return true; } |
| 15 | + bool Int64(int64_t i) { type = "Int64"; data = std::to_string(i); return true; } |
| 16 | + bool Uint64(uint64_t u) { type = "Uint64"; data = std::to_string(u); return true; } |
| 17 | + bool Double(double d) { type = "Double"; data = std::to_string(d); return true; } |
| 18 | + bool RawNumber(const char* str, SizeType length, bool) { type = "Number"; data = std::string(str, length); return true; } |
| 19 | + bool String(const char* str, SizeType length, bool) { type = "String" data = std::string(str, length); return true; } |
| 20 | + bool StartObject() { type = "StartObject"; data.clear(); return true; } |
| 21 | + bool Key(const char* str, SizeType length, bool) { type = "Key" data = std::string(str, length); return true; } |
| 22 | + bool EndObject(SizeType memberCount) { type = "EndObject"; data = std::to_string(memberCount); return true; } |
| 23 | + bool StartArray() { type = "StartArray"; data.clear(); return true; } |
| 24 | + bool EndArray(SizeType elementCount) { type = "EndArray"; data = std::to_string(elementCount); return true; } |
| 25 | +}; |
| 26 | + |
| 27 | +int main() { |
| 28 | + const char json[] = " { \"hello\" : \"world\", \"t\" : true , \"f\" : false, \"n\": null, \"i\":123, \"pi\": 3.1416, \"a\":[1, 2, 3, 4] } "; |
| 29 | + |
| 30 | + MyHandler handler; |
| 31 | + Reader reader; |
| 32 | + StringStream ss(json); |
| 33 | + reader.IterativeParseInit(); |
| 34 | + while (!reader.IterativeParseComplete()) { |
| 35 | + reader.IterativeParseNext<kParseDefaultFlags>(ss, handler); |
| 36 | + cout << handler.type << ": " << handler.data << endl; |
| 37 | + } |
| 38 | + |
| 39 | + return 0; |
| 40 | +} |
0 commit comments