-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjsonlib.h
145 lines (120 loc) · 3 KB
/
jsonlib.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
//
// jsonlib.h
//
// Copyright (c) 2021 Yuji Hirose. All rights reserved.
// MIT License
//
#pragma once
#include <rapidjson/reader.h>
#include <functional>
#include <string>
namespace jsonlib {
using namespace rapidjson;
struct Handlers {
std::function<void()> Null;
std::function<void(bool b)> Bool;
std::function<void(int i)> Int;
std::function<void(unsigned u)> Uint;
std::function<void(int64_t i)> Int64;
std::function<void(uint64_t i)> Uint64;
std::function<void(double i)> Double;
std::function<void(const char* str, SizeType length)> String;
std::function<void()> StartObject;
std::function<void(const char* str, SizeType length)> Key;
std::function<void(SizeType memberCount)> EndObject;
std::function<void()> StartArray;
std::function<void(SizeType elementCount)> EndArray;
};
inline bool parse(const char* json, const Handlers& functions) {
struct ReaderHandler : public BaseReaderHandler<UTF8<>, ReaderHandler> {
ReaderHandler(const Handlers& functions) : functions_(functions) {}
bool Null() {
if (functions_.Null) {
functions_.Null();
}
return true;
}
bool Bool(bool b) {
if (functions_.Bool) {
functions_.Bool(b);
}
return true;
}
bool Int(int i) {
if (functions_.Int) {
functions_.Int(i);
}
return true;
}
bool Uint(unsigned u) {
if (functions_.Uint) {
functions_.Uint(u);
}
return true;
}
bool Int64(int i) {
if (functions_.Int64) {
functions_.Int64(i);
}
return true;
}
bool Uint64(unsigned u) {
if (functions_.Uint64) {
functions_.Uint64(u);
}
return true;
}
bool Double(double d) {
if (functions_.Double) {
functions_.Double(d);
}
return true;
}
bool String(const char* str, SizeType length, bool /*copy*/) {
if (functions_.String) {
functions_.String(str, length);
}
return true;
}
bool StartObject() {
if (functions_.StartObject) {
functions_.StartObject();
}
return true;
}
bool Key(const char* str, SizeType length, bool /*copy*/) {
if (functions_.Key) {
functions_.Key(str, length);
}
return true;
}
bool EndObject(SizeType memberCount) {
if (functions_.EndObject) {
functions_.EndObject(memberCount);
}
return true;
}
bool StartArray() {
if (functions_.StartArray) {
functions_.StartArray();
}
return true;
}
bool EndArray(SizeType elementCount) {
if (functions_.EndArray) {
functions_.EndArray(elementCount);
}
return true;
}
const Handlers& functions_;
std::string key_;
};
StringStream ss(json);
ReaderHandler handler(functions);
Reader reader;
return reader.Parse(ss, handler);
}
inline bool parse(const std::string& json, const Handlers& functions) {
return parse(json.data(), functions);
}
}; // namespace jsonlib