-
Notifications
You must be signed in to change notification settings - Fork 1
/
json_parser.h
407 lines (328 loc) · 9.87 KB
/
json_parser.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
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
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
#ifndef _JSON_PARSER_H_
#define _JSON_PARSER_H_
#include "math3.h"
#include <vector>
#include <string>
#include <cstring>
#include <cerrno>
using namespace std;
namespace Renzoku {
/**
* There are two styles to write a JSON parser:
* - DOM style: read the entire JSON file into a tree. Traverse the tree from the root in any orders to process the data.
* DOM is easy to implement, provide nice and clear code, but can cost memory as the entire tree needs to be stored.
*
* - SAX style: event-based; every time an opening/close tag is detected, a callback or delegate function is called.
* In the parser we maintain a stack of JsonObjects. The top of the stack indicates the current object which is receiving
* data from the parser.
* When a close tag is encountered, the top stack object is completed, and can be processed further, and/or attached to
* the next object in the stack.
* If we don't count the processed data, the stack depth is equal to the JSON object depth.
*
* This parser experiments with the SAX style.
*/
struct JsonException : std::exception {
std::string s;
JsonException(std::string ss) : s(ss) {}
~JsonException() throw () {}
const char* what() const throw() { return s.c_str(); }
};
struct JsonPrimitive {
enum JsonPrimitiveType {
JSON_PRIMITIVE_STRING,
JSON_PRIMITIVE_INT,
JSON_PRIMITIVE_FLOAT,
JSON_PRIMITIVE_BOOL
};
JsonPrimitive(const char *data, int length) : length(length), type(JSON_PRIMITIVE_STRING) {
this->data = new char[length + 1];
strcpy(this->data, data);
}
JsonPrimitive(JsonPrimitiveType type, const char *data, int length) : length(length), type(type) {
this->data = new char[length + 1];
strcpy(this->data, data);
}
~JsonPrimitive() {
if (data)
delete[] data;
}
char *data;
int length;
int type;
bool is_int() const {
return type == JSON_PRIMITIVE_INT;
}
bool is_float() const {
return type == JSON_PRIMITIVE_FLOAT;
}
bool is_string() const {
return type == JSON_PRIMITIVE_STRING;
}
int to_int() {
if (type == JSON_PRIMITIVE_INT) {
return atoi(data);
}
throw JsonException("Invalid type cast.");
}
float to_float() {
if (type == JSON_PRIMITIVE_FLOAT) {
return (float)atof(data);
}
throw JsonException("Invalid type cast.");
}
double to_double() {
if (type == JSON_PRIMITIVE_FLOAT) {
return atof(data);
}
throw JsonException("Invalid type cast.");
}
bool to_bool() {
if (type == JSON_PRIMITIVE_BOOL) {
if (data[0] == '0')
return false;
else
return true;
}
throw JsonException("Invalid type cast.");
}
string to_string() {
if (type == JSON_PRIMITIVE_STRING) {
return data;
}
throw JsonException("Invalid type cast.");
}
};
struct JsonObject;
struct JsonArray;
struct JsonValue {
enum JsonValueType {
JSON_VALUE_PRIMITIVE,
JSON_VALUE_OBJECT,
JSON_VALUE_ARRAY
};
int type;
union {
JsonPrimitive *primitive;
JsonObject *object;
JsonArray *array;
} data;
JsonValue(JsonObject *object) {
type = JSON_VALUE_OBJECT;
data.object = object;
}
JsonValue(JsonPrimitive *primitive) {
type = JSON_VALUE_PRIMITIVE;
data.primitive = primitive;
}
JsonValue(JsonArray *array) {
type = JSON_VALUE_ARRAY;
data.array = array;
}
JsonArray *to_array() {
if (type == JSON_VALUE_ARRAY)
return data.array;
throw JsonException("Invalid type cast.");
}
JsonObject *to_object() {
if (type == JSON_VALUE_OBJECT)
return data.object;
throw JsonException("Invalid type cast.");
}
bool is_string() {
if (type == JSON_VALUE_PRIMITIVE && data.primitive->type == JsonPrimitive::JSON_PRIMITIVE_STRING) {
return true;
}
return false;
}
bool is_primitive() {
if (type == JSON_VALUE_PRIMITIVE)
return true;
return false;
}
bool is_object() {
if (type == JSON_VALUE_OBJECT)
return true;
return false;
}
bool is_array() {
if (type == JSON_VALUE_ARRAY)
return true;
return false;
}
string to_string() {
if (type == JSON_VALUE_PRIMITIVE)
return data.primitive->to_string();
throw JsonException("Invalid type cast.");
}
int to_int() {
if (type == JSON_VALUE_PRIMITIVE)
return data.primitive->to_int();
throw JsonException("Invalid type cast.");
}
float to_numeric() {
if (type == JSON_VALUE_PRIMITIVE) {
if (data.primitive->is_int())
return (float)data.primitive->to_int();
if (data.primitive->is_float())
return (float)data.primitive->to_float();
throw JsonException("Invalid type cast.");
}
throw JsonException("Invalid type cast.");
}
bool to_bool() {
if (type == JSON_VALUE_PRIMITIVE)
return data.primitive->to_bool();
throw JsonException("Invalid type cast.");
}
};
struct JsonArray {
vector<JsonValue *> values;
Vec2 to_vec2() const {
return Vec2(values[0]->to_numeric(),
values[1]->to_numeric());
}
Vec3 to_vec3() const {
return Vec3(values[0]->to_numeric(),
values[1]->to_numeric(),
values[2]->to_numeric());
}
Rgb to_rgb() const {
return Rgb(values[0]->to_numeric(),
values[1]->to_numeric(),
values[2]->to_numeric());
}
Size2 to_size2() const {
return Size2(values[0]->to_numeric(),
values[1]->to_numeric());
}
};
struct JsonObject {
vector<string> keys;
vector<JsonValue *> values;
bool is_empty() const {
return (keys.size() == 0 && values.size() == 0);
}
bool is_empty_keys() const {
return keys.size() == 0;
}
bool is_empty_values() const {
return values.size() == 0;
}
JsonValue *get_value(string key) {
for (int i = 0; i < keys.size(); ++i) // only a few pairs, use linear search
if (key.compare(keys[i]) == 0)
return values[i];
return NULL;
}
//
// Some utility functions to retrieve values.
//
/**
* Use this function if the key is compulsory.
*/
JsonValue *get_value_strict(string key) {
JsonValue *val = get_value(key);
if (!val) {
throw JsonException("Key " + key + " not found.");
}
return val;
}
bool has_key(string key) {
for (int i = 0; i < keys.size(); ++i) // only a few pairs, use linear search
if (key.compare(keys[i]) == 0)
return true;
return false;
}
int get_int(string key) {
return get_value_strict(key)->to_int();
}
bool get_bool(string key) {
return get_value_strict(key)->to_bool();
}
float get_numeric(string key) {
return get_value_strict(key)->to_numeric();
}
string get_string(string key) {
return get_value_strict(key)->to_string();
}
Vec2 get_vec2(string key) {
return get_value_strict(key)->to_array()->to_vec2();
}
Vec3 get_vec3(string key) {
return get_value_strict(key)->to_array()->to_vec3();
}
Rgb get_rgb(string key) {
return get_value_strict(key)->to_array()->to_rgb();
}
Size2 get_size2(string key) {
return get_value_strict(key)->to_array()->to_size2();
}
};
struct JsonContainer {
enum JsonContainerType {
JSON_CONTAINER_OBJECT,
JSON_CONTAINER_ARRAY
};
int type;
union {
JsonObject *object;
JsonArray *array;
} data;
JsonContainer(JsonObject *object) {
type = JSON_CONTAINER_OBJECT;
data.object = object;
}
JsonContainer(JsonArray *array) {
type = JSON_CONTAINER_ARRAY;
data.array = array;
}
};
class JsonElementVisitor {
public:
virtual bool process_object(int stack_level, string last_key, JsonObject *obj) = 0;
virtual bool process_array(string last_key, JsonArray *array) = 0;
virtual void process_root(JsonObject *root) = 0;
};
/**
* This class is a bridge between the low-level events in libjson to retrieve the JSON data
* and a "visitor" or "importer" class which is responsible to process the JSON array and objects found.
* e.g., load the scene from such data.
*/
class JsonParser {
public:
JsonParser(JsonElementVisitor *visitor);
void load(const string file);
void begin_object();
void end_object();
void begin_array();
void end_array();
void add_key(const char *data, int length);
void add_value(JsonValue *value);
private:
JsonContainer *get_current_container() const;
JsonObject *get_current_object() const;
JsonArray *get_current_array() const;
string get_last_key() const;
void delete_last_key();
/**
* Try to process an object or array as soon as it is ready.
*/
bool process_object(string last_key, JsonObject *obj);
bool process_array(string last_key, JsonArray *array);
/**
* The tree (similar to DOM) is eventually created when the end object is the root object.
*
* This function can work on the unprocessed objects left in the tree.
*/
void process_root(JsonObject *root);
/**
* Unprocessed objects and arrays are handed back to the parent object for processing in later attempts.
*/
void add_object(JsonObject *obj);
void add_array(JsonArray *array);
private:
vector<JsonContainer *> stack;
JsonElementVisitor *visitor;
};
} // end namespace
#endif