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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
|
#include "serializer.h"
#include <stdexcept>
#include <rapidjson/allocators.h>
#include "json.h"
//// Elementary types
void JsonNull::swap(JsonNull& arg) noexcept
{
}
void Reflect(Reader& visitor, uint8_t& value) {
if (!visitor.IsInt())
throw std::invalid_argument("uint8_t");
value = (uint8_t)visitor.GetInt();
}
void Reflect(Writer& visitor, uint8_t& value) {
visitor.Int(value);
}
void Reflect(Reader& visitor, short& value) {
if (!visitor.IsInt())
throw std::invalid_argument("short");
value = (short)visitor.GetInt();
}
void Reflect(Writer& visitor, short& value) {
visitor.Int(value);
}
void Reflect(Reader& visitor, unsigned short& value) {
if (!visitor.IsInt())
throw std::invalid_argument("unsigned short");
value = (unsigned short)visitor.GetInt();
}
void Reflect(Writer& visitor, unsigned short& value) {
visitor.Int(value);
}
void Reflect(Reader& visitor, int& value) {
if (!visitor.IsInt())
throw std::invalid_argument("int");
value = visitor.GetInt();
}
void Reflect(Writer& visitor, int& value) {
visitor.Int(value);
}
void Reflect(Reader& visitor, unsigned& value) {
if (!visitor.IsUint64())
throw std::invalid_argument("unsigned");
value = visitor.GetUint32();
}
void Reflect(Writer& visitor, unsigned& value) {
visitor.Uint32(value);
}
void Reflect(Reader& visitor, long& value) {
if (!visitor.IsInt64())
throw std::invalid_argument("long");
value = long(visitor.GetInt64());
}
void Reflect(Writer& visitor, long& value) {
visitor.Int64(value);
}
void Reflect(Reader& visitor, unsigned long& value) {
if (!visitor.IsUint64())
throw std::invalid_argument("unsigned long");
value = (unsigned long)visitor.GetUint64();
}
void Reflect(Writer& visitor, unsigned long& value) {
visitor.Uint64(value);
}
void Reflect(Reader& visitor, long long& value) {
if (!visitor.IsInt64())
throw std::invalid_argument("long long");
value = visitor.GetInt64();
}
void Reflect(Writer& visitor, long long& value) {
visitor.Int64(value);
}
void Reflect(Reader& visitor, unsigned long long& value) {
if (!visitor.IsUint64())
throw std::invalid_argument("unsigned long long");
value = visitor.GetUint64();
}
void Reflect(Writer& visitor, unsigned long long& value) {
visitor.Uint64(value);
}
void Reflect(Reader& visitor, double& value) {
if (!visitor.IsNumber())
throw std::invalid_argument("double");
value = visitor.GetDouble();
}
void Reflect(Writer& visitor, double& value) {
visitor.Double(value);
}
void Reflect(Reader& visitor, bool& value) {
if (!visitor.IsBool())
throw std::invalid_argument("bool");
value = visitor.GetBool();
}
void Reflect(Writer& visitor, bool& value) {
visitor.Bool(value);
}
void Reflect(Reader& visitor, std::string& value) {
if (!visitor.IsString())
throw std::invalid_argument("std::string");
value = visitor.GetString();
}
void Reflect(Writer& visitor, std::string& value) {
visitor.String(value.c_str(), (rapidjson::SizeType)value.size());
}
void Reflect(Reader& visitor, JsonNull& value) {
visitor.GetNull();
}
void Reflect(Writer& visitor, JsonNull& value) {
visitor.Null();
}
void Reflect(Reader& visitor, SerializeFormat& value) {
std::string fmt = visitor.GetString();
value = fmt[0] == 'm' ? SerializeFormat::MessagePack : SerializeFormat::Json;
}
void Reflect(Writer& visitor, SerializeFormat& value) {
switch (value) {
case SerializeFormat::Json:
visitor.String("json");
break;
case SerializeFormat::MessagePack:
visitor.String("msgpack");
break;
}
}
std::string JsonReader::ToString() const
{
rapidjson::StringBuffer strBuf;
strBuf.Clear();
rapidjson::Writer<rapidjson::StringBuffer> writer(strBuf);
m_->Accept(writer);
std::string strJson = strBuf.GetString();
return strJson;
}
void JsonReader::IterMap(std::function<void(const char*, Reader&)> fn)
{
path_.push_back("0");
for (auto& entry : m_->GetObject())
{
auto saved = m_;
m_ = &(entry.value);
fn(entry.name.GetString(), *this);
m_ = saved;
}
path_.pop_back();
}
void JsonReader::IterArray(std::function<void(Reader&)> fn)
{
if (!m_->IsArray())
throw std::invalid_argument("array");
// Use "0" to indicate any element for now.
path_.push_back("0");
for (auto& entry : m_->GetArray())
{
auto saved = m_;
m_ = &entry;
fn(*this);
m_ = saved;
}
path_.pop_back();
}
void JsonReader::DoMember(const char* name, std::function<void(Reader&)> fn)
{
path_.push_back(name);
auto it = m_->FindMember(name);
if (it != m_->MemberEnd())
{
auto saved = m_;
m_ = &it->value;
fn(*this);
m_ = saved;
}
path_.pop_back();
}
std::string JsonReader::GetPath() const
{
std::string ret;
for (auto& t : path_)
{
ret += '/';
ret += t;
}
ret.pop_back();
return ret;
}
|