Wire Sysio Wire Sysion 1.0.0
Loading...
Searching...
No Matches
simplepullreader.cpp
Go to the documentation of this file.
1#include "rapidjson/reader.h"
2#include <iostream>
3#include <sstream>
4
5using namespace rapidjson;
6using namespace std;
7
8// If you can require C++11, you could use std::to_string here
9template <typename T> std::string stringify(T x) {
10 std::stringstream ss;
11 ss << x;
12 return ss.str();
13}
14
15struct 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; }
35private:
36 MyHandler(const MyHandler& noCopyConstruction);
37 MyHandler& operator=(const MyHandler& noAssignment);
38};
39
40int 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}
bool IterativeParseNext(InputStream &is, Handler &handler)
Parse one token from JSON text.
Definition reader.h:618
void IterativeParseInit()
Initialize JSON text token-by-token parsing.
Definition reader.h:605
RAPIDJSON_FORCEINLINE bool IterativeParseComplete() const
Check if token-by-token parsing JSON text is complete.
Definition reader.h:675
main RapidJSON namespace
Definition name.hpp:106
#define T(meth, val, expected)
RAPIDJSON_NAMESPACE_BEGIN typedef unsigned SizeType
Size type (for string lengths, array sizes, etc.)
Definition rapidjson.h:384
@ kParseDefaultFlags
Default parse flags. Can be customized by defining RAPIDJSON_PARSE_DEFAULT_FLAGS.
Definition reader.h:156
std::string stringify(T x)
int main()
signed __int64 int64_t
Definition stdint.h:135
unsigned __int64 uint64_t
Definition stdint.h:136
Read-only string stream.
Definition stream.h:154
bool Bool(bool b)
bool EndObject(SizeType memberCount)
std::string data
bool Uint(unsigned u)
bool RawNumber(const char *str, SizeType length, bool)
bool Int64(int64_t i)
const char * type
bool String(const char *str, SizeType length, bool)
bool Double(double d)
bool EndArray(SizeType elementCount)
bool Uint64(uint64_t u)
bool Int(int i)
bool Key(const char *str, SizeType length, bool)
CK_ULONG d