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
215
216
217
218
219
|
/*
* Copyright (C) 2005-2018 Team Kodi
* This file is part of Kodi - https://kodi.tv
*
* SPDX-License-Identifier: GPL-2.0-or-later
* See LICENSES/README.md for more information.
*/
#include "JSONVariantParser.h"
#include <rapidjson/reader.h>
class CJSONVariantParserHandler
{
public:
explicit CJSONVariantParserHandler(CVariant& parsedObject);
bool Null();
bool Bool(bool b);
bool Int(int i);
bool Uint(unsigned u);
bool Int64(int64_t i);
bool Uint64(uint64_t u);
bool Double(double d);
bool RawNumber(const char* str, rapidjson::SizeType length, bool copy);
bool String(const char* str, rapidjson::SizeType length, bool copy);
bool StartObject();
bool Key(const char* str, rapidjson::SizeType length, bool copy);
bool EndObject(rapidjson::SizeType memberCount);
bool StartArray();
bool EndArray(rapidjson::SizeType elementCount);
private:
template <typename... TArgs>
bool Primitive(TArgs... args)
{
PushObject(CVariant(std::forward<TArgs>(args)...));
PopObject();
return true;
}
void PushObject(const CVariant& variant);
void PopObject();
CVariant& m_parsedObject;
std::vector<CVariant *> m_parse;
std::string m_key;
CVariant m_root;
enum class PARSE_STATUS
{
Variable,
Array,
Object
};
PARSE_STATUS m_status;
};
CJSONVariantParserHandler::CJSONVariantParserHandler(CVariant& parsedObject)
: m_parsedObject(parsedObject),
m_parse(),
m_key(),
m_status(PARSE_STATUS::Variable)
{ }
bool CJSONVariantParserHandler::Null()
{
PushObject(CVariant::ConstNullVariant);
PopObject();
return true;
}
bool CJSONVariantParserHandler::Bool(bool b)
{
return Primitive(b);
}
bool CJSONVariantParserHandler::Int(int i)
{
return Primitive(i);
}
bool CJSONVariantParserHandler::Uint(unsigned u)
{
return Primitive(u);
}
bool CJSONVariantParserHandler::Int64(int64_t i)
{
return Primitive(i);
}
bool CJSONVariantParserHandler::Uint64(uint64_t u)
{
return Primitive(u);
}
bool CJSONVariantParserHandler::Double(double d)
{
return Primitive(d);
}
bool CJSONVariantParserHandler::RawNumber(const char* str, rapidjson::SizeType length, bool copy)
{
return Primitive(str, length);
}
bool CJSONVariantParserHandler::String(const char* str, rapidjson::SizeType length, bool copy)
{
return Primitive(str, length);
}
bool CJSONVariantParserHandler::StartObject()
{
PushObject(CVariant::VariantTypeObject);
return true;
}
bool CJSONVariantParserHandler::Key(const char* str, rapidjson::SizeType length, bool copy)
{
m_key = std::string(str, 0, length);
return true;
}
bool CJSONVariantParserHandler::EndObject(rapidjson::SizeType memberCount)
{
PopObject();
return true;
}
bool CJSONVariantParserHandler::StartArray()
{
PushObject(CVariant::VariantTypeArray);
return true;
}
bool CJSONVariantParserHandler::EndArray(rapidjson::SizeType elementCount)
{
PopObject();
return true;
}
void CJSONVariantParserHandler::PushObject(const CVariant& variant)
{
if (m_status == PARSE_STATUS::Object)
{
(*m_parse[m_parse.size() - 1])[m_key] = variant;
m_parse.push_back(&(*m_parse[m_parse.size() - 1])[m_key]);
}
else if (m_status == PARSE_STATUS::Array)
{
CVariant *temp = m_parse[m_parse.size() - 1];
temp->push_back(variant);
m_parse.push_back(&(*temp)[temp->size() - 1]);
}
else if (m_parse.empty())
{
m_root = variant;
m_parse.push_back(&m_root);
}
if (variant.isObject())
m_status = PARSE_STATUS::Object;
else if (variant.isArray())
m_status = PARSE_STATUS::Array;
else
m_status = PARSE_STATUS::Variable;
}
void CJSONVariantParserHandler::PopObject()
{
CVariant *variant = m_parse[m_parse.size() - 1];
m_parse.pop_back();
if (!m_parse.empty())
{
variant = m_parse[m_parse.size() - 1];
if (variant->isObject())
m_status = PARSE_STATUS::Object;
else if (variant->isArray())
m_status = PARSE_STATUS::Array;
else
m_status = PARSE_STATUS::Variable;
}
else
{
m_parsedObject = *variant;
m_status = PARSE_STATUS::Variable;
}
}
bool CJSONVariantParser::Parse(const char* json, CVariant& data)
{
if (json == nullptr)
return false;
rapidjson::Reader reader;
rapidjson::StringStream stringStream(json);
CJSONVariantParserHandler handler(data);
// use kParseIterativeFlag to eliminate possible stack overflow
// from json parsing via reentrant calls
if (reader.Parse<rapidjson::kParseIterativeFlag>(stringStream, handler))
return true;
return false;
}
bool CJSONVariantParser::Parse(const std::string& json, CVariant& data)
{
return Parse(json.c_str(), data);
}
|