From d84d5fe0551634d1057adf6eb163ce8a63d00f1a Mon Sep 17 00:00:00 2001 From: StilesCrisis Date: Sat, 4 Feb 2017 00:41:34 -0800 Subject: [PATCH] Add example SimplePullHandler code Example code to demonstrate how the token-pulling reader can be used. --- example/simplepullreader/simplepullreader.cpp | 40 +++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 example/simplepullreader/simplepullreader.cpp diff --git a/example/simplepullreader/simplepullreader.cpp b/example/simplepullreader/simplepullreader.cpp new file mode 100644 index 0000000..0cce08b --- /dev/null +++ b/example/simplepullreader/simplepullreader.cpp @@ -0,0 +1,40 @@ +#include "rapidjson/reader.h" +#include + +using namespace rapidjson; +using namespace std; + +struct MyHandler { + const char* type; + std::string data; + + bool Null() { type = "Null"; data.clear(); return true; } + bool Bool(bool b) { type = "Bool"; data = b? "true": "false"; return true; } + bool Int(int i) { type = "Int"; data = std::to_string(i); return true; } + bool Uint(unsigned u) { type = "Uint"; data = std::to_string(u); return true; } + bool Int64(int64_t i) { type = "Int64"; data = std::to_string(i); return true; } + bool Uint64(uint64_t u) { type = "Uint64"; data = std::to_string(u); return true; } + bool Double(double d) { type = "Double"; data = std::to_string(d); return true; } + bool RawNumber(const char* str, SizeType length, bool) { type = "Number"; data = std::string(str, length); return true; } + bool String(const char* str, SizeType length, bool) { type = "String" data = std::string(str, length); return true; } + bool StartObject() { type = "StartObject"; data.clear(); return true; } + bool Key(const char* str, SizeType length, bool) { type = "Key" data = std::string(str, length); return true; } + bool EndObject(SizeType memberCount) { type = "EndObject"; data = std::to_string(memberCount); return true; } + bool StartArray() { type = "StartArray"; data.clear(); return true; } + bool EndArray(SizeType elementCount) { type = "EndArray"; data = std::to_string(elementCount); return true; } +}; + +int main() { + const char json[] = " { \"hello\" : \"world\", \"t\" : true , \"f\" : false, \"n\": null, \"i\":123, \"pi\": 3.1416, \"a\":[1, 2, 3, 4] } "; + + MyHandler handler; + Reader reader; + StringStream ss(json); + reader.IterativeParseInit(); + while (!reader.IterativeParseComplete()) { + reader.IterativeParseNext(ss, handler); + cout << handler.type << ": " << handler.data << endl; + } + + return 0; +}