|
1 | 1 | #include "rapidjson/reader.h"
|
2 | 2 | #include <iostream>
|
| 3 | +#include <sstream> |
3 | 4 |
|
4 | 5 | using namespace rapidjson;
|
5 | 6 | using namespace std;
|
6 | 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 | + return (std::stringstream() << x).str(); |
| 11 | +} |
| 12 | + |
7 | 13 | struct MyHandler {
|
8 | 14 | const char* type;
|
9 | 15 | std::string data;
|
10 | 16 |
|
11 | 17 | bool Null() { type = "Null"; data.clear(); return true; }
|
12 | 18 | 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; } |
| 19 | + bool Int(int i) { type = "Int:"; data = stringify(i); return true; } |
| 20 | + bool Uint(unsigned u) { type = "Uint:"; data = stringify(u); return true; } |
| 21 | + bool Int64(int64_t i) { type = "Int64:"; data = stringify(i); return true; } |
| 22 | + bool Uint64(uint64_t u) { type = "Uint64:"; data = stringify(u); return true; } |
| 23 | + bool Double(double d) { type = "Double:"; data = stringify(d); return true; } |
18 | 24 | bool RawNumber(const char* str, SizeType length, bool) { type = "Number:"; data = std::string(str, length); return true; }
|
19 | 25 | bool String(const char* str, SizeType length, bool) { type = "String:"; data = std::string(str, length); return true; }
|
20 | 26 | bool StartObject() { type = "StartObject"; data.clear(); return true; }
|
21 | 27 | 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; } |
| 28 | + bool EndObject(SizeType memberCount) { type = "EndObject:"; data = stringify(memberCount); return true; } |
23 | 29 | bool StartArray() { type = "StartArray"; data.clear(); return true; }
|
24 |
| - bool EndArray(SizeType elementCount) { type = "EndArray:"; data = std::to_string(elementCount); return true; } |
| 30 | + bool EndArray(SizeType elementCount) { type = "EndArray:"; data = stringify(elementCount); return true; } |
25 | 31 | };
|
26 | 32 |
|
27 | 33 | int main() {
|
|
0 commit comments