-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathjsonparser.cc
More file actions
322 lines (284 loc) · 7.85 KB
/
jsonparser.cc
File metadata and controls
322 lines (284 loc) · 7.85 KB
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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
// Parser for RFC7159 JSON with some missing implementations.
#include "jsonparser.h"
#include <assert.h>
#include <iostream>
#include <map>
#include <memory>
#include <set>
#include <vector>
namespace jjson {
const Value& Value::operator[](size_t pos) const {
return *dynamic_cast<const ArrayValue*>(this)->value_.at(pos).get();
}
const Value& Value::operator[](const std::string& key) const {
return *dynamic_cast<const ObjectValue*>(this)->value_.find(key)->second;
}
const std::vector<std::unique_ptr<Value> >& Value::get_array() const {
return dynamic_cast<const ArrayValue*>(this)->value_;
}
const Value& Value::get(const std::string& key) const {
const auto o = dynamic_cast<const ObjectValue*>(this);
assert(o != nullptr);
const auto& it = o->value_.find(key);
assert(it != o->value_.end());
const auto& n = it->second;
assert(n.get() != nullptr);
return *n;
}
const std::string& Value::get_string() const {
const auto s = dynamic_cast<const StringValue*>(this);
assert(s != nullptr);
return s->value_;
}
float Value::get_number() const {
const auto n = dynamic_cast<const NumberValue*>(this);
assert(n != nullptr);
return n->value_;
}
float Value::get_int() const {
assert(this != nullptr);
const auto n = dynamic_cast<const NumberValue*>(this);
assert(n != nullptr);
return static_cast<int>(n->value_);
}
bool Value::is_true() const {
return dynamic_cast<const TrueValue*>(this) != nullptr;
}
bool Value::is_false() const {
return dynamic_cast<const FalseValue*>(this) != nullptr;
}
bool Value::is_null() const {
return dynamic_cast<const NullValue*>(this) != nullptr;
}
namespace {
class Parser {
// Internal class used from Parse() method to do the actual parsing.
public:
explicit Parser(const std::string& s) : text_(s) {}
~Parser() {}
std::unique_ptr<Value> ParseJsonText() {
SkipWhitespace();
std::unique_ptr<Value> v = ObtainValue();
SkipWhitespace();
return v;
}
private:
char Peek() const {
if (Eof()) return 0;
return text_[position_];
}
// Returns true on usual case, if there's exceptional case of EOF
// already, returns false.
bool Skip() {
if (Eof()) return false;
position_++;
return true;
}
char Get() {
if (Eof()) return 0;
return text_[position_++];
}
bool Eof() const { return position_ >= text_.size(); }
void ReportError(const std::string& error) {
// TODO: do useful error reporting.
std::cout << "Error: " << error << " at " << position_ << " in ["
<< text_.substr(position_) << "]" << std::endl;
}
std::string Consume(const std::set<char>& valid) {
std::string data{};
data.reserve(16);
while (valid.find(Peek()) != valid.end()) {
data += Peek();
if (!Skip()) break;
}
// Replacing with substr didn't improve speed.
return data;
}
void SkipValid(const std::set<char>& valid) {
while (valid.find(Peek()) != valid.end()) {
if (!Skip()) break;
}
}
const std::set<char> valid_whitespace{0x20, 0x09, 0x0a, 0x0d};
void SkipWhitespace() { SkipValid(valid_whitespace); }
const std::set<char> valid_number{'-', '+', '0', '1', '2', '3', '4',
'5', '6', '7', '8', '9', '.', 'E'};
std::unique_ptr<Value> ObtainNumber() {
std::string number = Consume(valid_number);
// TODO: implement proper handling of JSON number format.
return std::make_unique<NumberValue>(strtof(number.c_str(), nullptr));
}
std::unique_ptr<Value> ObtainObject() {
if (Get() != '{') {
ReportError("Unexpected error.");
return nullptr;
}
SkipWhitespace();
std::map<std::string, std::unique_ptr<Value> > object;
while (!Eof()) {
if (Peek() == '}') {
Skip();
return std::make_unique<ObjectValue>(std::move(object));
}
auto key = ObtainString();
SkipWhitespace();
if (Get() != ':') {
ReportError("':' expected in Object.");
return nullptr;
}
SkipWhitespace();
auto value = ObtainValue();
SkipWhitespace();
object.emplace(std::move(dynamic_cast<StringValue*>(key.get())->value_),
move(value));
if (Peek() == '}') {
continue;
}
if (Peek() == ',') {
Skip();
SkipWhitespace();
continue;
}
ReportError("',' or '}' expected in Object.");
}
ReportError("Unexpected end reached while parsing Object.");
return nullptr;
}
std::unique_ptr<Value> ObtainArray() {
if (Get() != '[') {
return nullptr;
}
SkipWhitespace();
std::vector<std::unique_ptr<Value> > array;
while (!Eof()) {
if (Peek() == ']') {
Skip();
return std::make_unique<ArrayValue>(std::move(array));
}
std::unique_ptr<Value> value = ObtainValue();
if (value.get() == nullptr) {
ReportError("Invalid value parsing array.");
return nullptr;
}
array.emplace_back(move(value));
SkipWhitespace();
switch (Peek()) {
case ']':
continue;
case ',':
Skip();
SkipWhitespace();
continue;
default:
ReportError("Unexpected char during array.");
return nullptr;
}
}
ReportError("Unexpected end in array.");
return nullptr;
}
std::unique_ptr<Value> ObtainString() {
std::string s{};
// Just to reduce the number of reallocations, try a random large enough
// buffer.
s.reserve(1024);
if (Get() != '"') return nullptr;
while (!Eof()) {
if (Peek() == '"') {
// End of string.
Skip();
// TODO: some kind of char code conversion needed?
return std::make_unique<StringValue>(std::move(s));
} else if (Peek() == '\\') {
// escaped character.
Skip();
switch (char c = Get()) {
case 0x22:
case 0x5c:
case 0x2f:
s += c;
break;
case 0x62:
s += 0x8;
break;
case 0x66:
s += 0xc;
break;
case 0x6e:
s += 0xa;
break;
case 0x72:
s += 0xd;
break;
case 0x74:
s += 0x9;
break;
case 0x75:
// 4 hex digits. TODO implement properly.
char buf[5] = {Get(), Get(), Get(), Get(), 0};
long charcode = strtol(buf, nullptr, 16);
if (charcode >= 0 && charcode < 256) {
s += static_cast<char>(charcode);
} else {
ReportError("Unimplemented.");
return nullptr;
}
break;
}
} else {
s += Get();
}
}
ReportError("Unexpected EOF found on string parsing.");
return nullptr;
}
template <class T>
std::unique_ptr<Value> AssertConsume(const std::string& expect) {
for (char c : expect) {
if (c != Peek()) {
return nullptr;
}
if (!Skip()) return nullptr;
}
return std::make_unique<T>();
}
std::unique_ptr<Value> ObtainValue() {
switch (Peek()) {
case 'f':
return AssertConsume<FalseValue>("false");
case 'n':
return AssertConsume<NullValue>("null");
case 't':
return AssertConsume<TrueValue>("true");
case '-':
case '0':
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9':
case '+':
return ObtainNumber();
case '[':
return ObtainArray();
case '{':
return ObtainObject();
case '"':
return ObtainString();
}
return nullptr;
}
const std::string& text_;
size_t position_{0};
DISALLOW_COPY_AND_ASSIGN(Parser);
};
} // anonymous namespace
std::unique_ptr<Value> Parse(const std::string& s) {
Parser p(s);
return p.ParseJsonText();
}
} // namespace jjson