-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathJSONParser.cpp
More file actions
388 lines (341 loc) · 8.28 KB
/
JSONParser.cpp
File metadata and controls
388 lines (341 loc) · 8.28 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
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
/* A simple recursive descent JSON parser.
*
* Copyright (C) 2011 Panagiotis Koutsourakis
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <cstdlib>
#include <iostream>
#include <string>
#include <sstream>
#include "JSON.h"
#include "JSONParser.h"
using std::string;
using std::stringstream;
namespace json_parser {
void Parser::skipWhitespace() {
char c;
while(isspace(c = stream_->get())) {
if (c == '\n') {
line_no_++;
}
}
// Put the first non whitespace char back in the stream
stream_->unget();
}
char Parser::readCharacter() {
skipWhitespace();
char c = stream_->get();
return c;
}
/**
* A JSON object is delimited by curly braces, and contains a
* (possibly empty) list of members.
*/
Object *Parser::jObject() {
skipWhitespace();
char c;
if ((c = readCharacter()) != '{') {
parseError("character '{'", c);
}
Object *ret = NULL;
// The JSON object can either be empty...
if ((c = readCharacter()) == '}') {
ret = new Object();
} else { // ...or contain a list of pairs
stream_->unget();
ret = new Object(jMembers());
if ((c = readCharacter()) != '}') {
parseError("character '}'", c);
}
}
return ret;
}
/**
* The JSON members are a list of pairs.
*/
Members *Parser::jMembers() {
skipWhitespace();
// Read a pair from the stream
std::pair<std::string, Value*> pair = jPair();
Members *ret;
char c = readCharacter();
// If the next char is ',' we have more pairs to read
if (c == ',') {
ret = jMembers();
} else {
stream_->unget();
ret = new Members();
}
// Because we are working recursivelly we need to put the pair we
// read in the front of the member list in order to maintain the
// order.
ret->insert(pair);
return ret;
}
/**
* A pair is a string followed by the character ':' followed by a JSON
* value.
*/
std::pair<string, Value*> Parser::jPair() {
skipWhitespace();
Value *str = jString();
char c = readCharacter();
if (c != ':') {
parseError("character ':'", c);
}
Value *val = jValue();
std::string key = str->toJSON(0, false);
// Strip the first and last double quote from the key
// for easier searching in the map.
key = key.substr(key.find_first_of("\"") + 1, key.find_last_of("\"") - 1);
std::pair<std::string, Value*> ret(key, val);
delete str;
return ret;
}
/**
* A JSON array is a (possibly empty) list of elements delimited by
* braces.
*/
Array *Parser::jArray() {
skipWhitespace();
char c;
if ((c = readCharacter()) != '[') {
parseError("character '['", c);
}
Array *ret = NULL;
if ((c = readCharacter()) == ']') {
ret = new Array();
} else {
stream_->unget();
ret = new Array(jElements());
if ((c = readCharacter()) != ']') {
parseError("character ']'", c);
}
}
return ret;
}
/**
* JSON elements is a list of JSON values.
*/
Elements *Parser::jElements() {
skipWhitespace();
Value *val = jValue();
Elements *ret;
char c;
if ((c = readCharacter()) == ',') {
ret = jElements();
} else {
stream_->unget();
ret = new Elements();
}
ret->insert(val);
return ret;
}
/**
* A JSON value is either a JSON Object, a JSON array, a JSON string,
* a JSON number or one of the literals true, false and null.
*/
Value *Parser::jValue() {
skipWhitespace();
Value *ret = NULL;
char c = readCharacter();
char true_buf[] = "rue";
char false_buf[] = "alse";
char null_buf[] = "ull";
switch(c) {
case '"':
stream_->unget();
ret = jString();
break;
case '1': case '2': case '3': case '4': case '5':
case '6': case '7': case '8': case '9': case '0':
case '-':
stream_->unget();
ret = jNumber();
break;
case '{':
stream_->unget();
ret = jObject();
break;
case '[':
stream_->unget();
ret = jArray();
break;
case 't':
for(int i = 0; i < 3; i++) {
if ((c = readCharacter()) != true_buf[i]) {
stringstream exp;
exp << "character '" << true_buf[i] << "'";
parseError(exp.str(), c);
}
}
ret = new Value();
ret->SetValue("true");
break;
case 'f':
for(int i = 0; i < 4; i++) {
if ((c = readCharacter()) != false_buf[i]) {
stringstream exp;
exp << "character '" << false_buf[i] << "'";
parseError(exp.str(), c);
}
}
ret = new Value();
ret->SetValue("false");
break;
case 'n':
for(int i = 0; i < 3; i++) {
if ((c = readCharacter()) != null_buf[i]) {
stringstream exp;
exp << "character '" << null_buf[i] << "'";
parseError(exp.str(), c);
}
}
ret = new Value();
ret->SetValue("null");
break;
default:
parseError("JSON Value", c);
break;
};
return ret;
}
/**
* A JSON string is the characters between two double quotation marks.
*
* @todo This method does not currently support Unicode or control
* characters as it should.
*/
Value *Parser::jString() {
skipWhitespace();
string str;
char c = stream_->get();
if (c != '"') {
parseError("character '\"'", c);
}
while((c = stream_->get()) != '"') {
str.push_back(c);
}
Value *ret = new Value();
ret->SetValue("\"" + str + "\"");
return ret;
}
/**
* A JSON number is either an int, or an int followed by a fractional
* part, or by an exponent, or both.
*/
Value *Parser::jNumber() {
skipWhitespace();
Value *ret = new Value();
std::string str = jInt();
char c = stream_->get();
switch(c) {
case '.': // Fractional part
str += "." + jDigits();
c = stream_->get();
if (c == 'e' || c == 'E') { // Maybe there is still an exponent.
stream_->unget();
str += jExponent();
} else {
stream_->unget();
}
break;
case 'e': case 'E': // Exponent
stream_->unget();
str += jExponent();
break;
default:
stream_->unget();
break;
};
ret->SetValue(str);
return ret;
}
/**
* An int is either a single digit, or a non zero digit, followed by
* more digits. Possibly there is a minus at the start.
*/
std::string Parser::jInt() {
skipWhitespace();
char c = stream_->get();
std::string ret;
switch(c) {
case '-':
ret = "-" + jDigits();
break;
case '1': case '2': case '3': case '4': case '5':
case '6': case '7': case '8': case '9':
stream_->unget();
ret = jDigits();
break;
case '0':
ret = "0";
break;
default:
parseError("digit or '-'", c);
break;
}
return ret;
}
/**
* Reads a sequence of digits.
*/
std::string Parser::jDigits() {
string str;
char c = stream_->get();
if (!isdigit(c)) {
parseError("digit", c);
}
str.push_back(c);
while(isdigit(c = stream_->get())) {
str.push_back(c);
}
stream_->unget();
return str;
}
/**
* An exponent is the character 'e' or 'E' followed optionally by a
* sign character, followed by a sequence of digits.
*/
std::string Parser::jExponent() {
std::string ret;
char c = stream_->get();
if (c != 'e' && c != 'E') {
parseError("'e' or 'E'", c);
}
ret += c;
c = stream_->get();
if (c == '+' || c == '-') {
ret += c;
ret += jDigits();
} else if (isdigit(c)) {
stream_->unget();
ret += jDigits();
} else {
parseError("'+', '-' or digit", c);
}
return ret;
}
void Parser::parseError(const std::string &expected, char found) {
if (found == -1) {
throw ParseError("Unexpected EOF!");
}
stringstream msg;
msg << "Expected " << expected << ". Found: '"
<< found << "' (int=" << static_cast<int>(found)
<< ") at line " << line_no_;
throw ParseError(msg.str());
}
} // namespace json