simplepullreader.cpp
Go to the documentation of this file.
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 }
bool Int64(int64_t i)
RAPIDJSON_NAMESPACE_BEGIN typedef unsigned SizeType
Size type (for string lengths, array sizes, etc.)
Definition: rapidjson.h:389
Read-only string stream.
Definition: fwd.h:47
RAPIDJSON_FORCEINLINE bool IterativeParseComplete()
Check if token-by-token parsing JSON text is complete.
Definition: reader.h:676
bool String(const char *str, SizeType length, bool)
bool EndObject(SizeType memberCount)
Default parse flags. Can be customized by defining RAPIDJSON_PARSE_DEFAULT_FLAGS. ...
Definition: reader.h:158
bool Uint64(uint64_t u)
static const char json[]
bool Int(int i)
const char * type
unsigned __int64 uint64_t
Definition: stdint.h:136
bool IterativeParseNext(InputStream &is, Handler &handler)
Parse one token from JSON text.
Definition: reader.h:619
bool Bool(bool b)
main RapidJSON namespace
int main()
bool Uint(unsigned u)
#define T(x)
signed __int64 int64_t
Definition: stdint.h:135
bool EndArray(SizeType elementCount)
bool StartObject()
void IterativeParseInit()
Initialize JSON text token-by-token parsing.
Definition: reader.h:606
bool RawNumber(const char *str, SizeType length, bool)
std::string data
std::string stringify(T x)
bool Double(double d)
bool Key(const char *str, SizeType length, bool)


choreo_rapidjson
Author(s):
autogenerated on Thu Jul 18 2019 03:59:09