-
Notifications
You must be signed in to change notification settings - Fork 3
/
JSON.cpp
470 lines (375 loc) · 13.3 KB
/
JSON.cpp
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
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
/* File : JSON.cpp */
#include <string.h>
#include <string>
#include <cctype>
#include <iostream>
#include <vector>
#include <unordered_map>
#include <typeinfo>
using namespace std;
#define JSON_VAL_UNDEF -1
#define JSON_VAL_NULL 0
#define JSON_VAL_TRUE 1
#define JSON_VAL_FALSE 2
#define JSON_VAL_NUMBER 3
#define JSON_VAL_STRING 4
#define JSON_VAL_ARRAY 5
#define JSON_VAL_OBJECT 6
#define JSON_VAL_KEYVAL 7
class json_element {
public:
/*
const int JSON_VAL_UNDEF=-1;
const int JSON_VAL_NULL=0;
const int JSON_VAL_TRUE=1;
const int JSON_VAL_FALSE=2;
const int JSON_VAL_NUMBER=3;
const int JSON_VAL_STRING=4;
const int JSON_VAL_ARRAY=5;
const int JSON_VAL_OBJECT=6;
const int JSON_VAL_KEYVAL=7;
*/
int type = JSON_VAL_UNDEF;
int start = 0;
int end = 0;
int depth = 0;
json_element(int what, int start, int end, int depth) {
this->type = what;
this->start = start;
this->end = end;
this->depth = depth;
}
void update_element(int what, int start, int end, int depth) {
this->type = what;
this->start = start;
this->end = end;
this->depth = depth;
}
string get_name () {
if (type == JSON_VAL_UNDEF) return ( "Undefined" );
if (type == JSON_VAL_NULL) return ( "NULL" );
if (type == JSON_VAL_TRUE) return ( "True" );
if (type == JSON_VAL_FALSE) return ( "False" );
if (type == JSON_VAL_NUMBER) return ( "Number" );
if (type == JSON_VAL_STRING) return ( "String" );
if (type == JSON_VAL_ARRAY) return ( "Array" );
if (type == JSON_VAL_OBJECT) return ( "Object" );
if (type == JSON_VAL_KEYVAL) return ( "Key:Val" );
return ( "Unknown" );
}
};
class json_object;
class json_array;
class json_parser {
public:
const char *leading_number_chars = "-0123456789";
const char *null_template = "null";
const char *true_template = "true";
const char *false_template = "false";
char *text = NULL;
vector<json_element *> elements;
json_parser ( char *text ) {
this->text = text;
elements = vector<json_element *>();
}
void parse ( json_array *parent ) {
parse_element ( parent, 0, 0 );
}
json_element *pre_store_skipped ( int what, int start, int end, int depth ) {
json_element *je = new json_element ( what, start, end, depth );
elements.push_back(je);
// System.out.println ( "Pre-Skipped " + what + " at depth " + depth + " from " + start + " to " + end );
return je;
}
void post_store_skipped ( int what, int start, int end, int depth ) {
json_element *je = new json_element ( what, start, end, depth );
elements.push_back(je);
// System.out.println ( "Post-Skipped " + what + " at depth " + depth + " from " + start + " to " + end );
}
void post_store_skipped ( int what, int start, int end, int depth, json_element *je ) {
je->update_element ( what, start, end, depth );
// System.out.println ( "Post-Skipped " + what + " at depth " + depth + " from " + start + " to " + end );
}
void dump ( int max_len ) {
json_element *j;
for (int i=0; i<elements.size(); i++) {
j = elements.at(i);
for (int d=0; d<j->depth; d++) {
cout << " ";
}
string local_text;
local_text.assign (text);
string display;
if ( (j->end - j->start) <= max_len ) {
display = local_text.substr(j->start,j->end-j->start);
} else {
display = local_text.substr(j->start,max_len-4);
}
cout << "|-" << j->get_name() << " at depth " << j->depth << " from " << j->start << " to " << (j->end-1) << " = " << display << endl;
}
}
int skip_whitespace ( int index, int depth ) {
int i = index;
int max = strlen(text);
while ( isspace(text[i]) ) {
i++;
if (i >= max) {
return ( -1 );
}
}
return i;
}
int skip_sepspace ( int index, int depth ) {
int i = index;
int max = strlen(text);
while ( (text[i]==',') || isspace(text[i]) ) {
i++;
if (i >= max) {
return ( -1 );
}
}
return i;
}
int parse_element ( void *parent, int index, int depth );
int parse_keyval ( void *parent, int index, int depth );
int parse_object ( void *parent, int index, int depth );
int parse_array ( void *parent, int index, int depth );
int parse_string ( void *parent, int index, int depth );
int parse_number ( void *parent, int index, int depth );
};
//////////////// Internal Representation of Data after Parsing /////////////////
//--> This was an attempt to get the typeid just one time for each object: type_info json_object_type = typeid(new json_object());
class json_object : public unordered_map<string,void *> {
public:
virtual void print_self() {
cout << "json_object" << endl;
cout << "type = " << typeid(this).name() << endl;
}
};
int global_depth = 0;
class json_array : public vector<void *> {
public:
virtual void print_self() {
global_depth += 1;
cout << "json_array is at " << this << endl;
cout << "json_array contains " << this->size() << " elements" << endl;
for (int i=0; i<this->size(); i++) {
cout << "SUB OBJECT " << i << " at depth " << global_depth << " is a " << typeid(this[i]).name() << " at " << (void *)this << endl;
this[i].print_self();
}
global_depth += -1;
cout << "type.name = " << typeid(this).name() << endl;
bool match = (typeid(this) == typeid(new json_array()));
cout << "match = " << match << endl;
match = (typeid(this) == typeid(new json_object()));
cout << "match = " << match << endl;
//cout << "type = " << typeid(this) << endl;
}
};
class json_primitive {
public:
string text;
virtual void print_self() {
cout << "json_primitive" << endl;
}
};
class json_number : public json_primitive {
public:
double value = 0.0;
bool as_int = false;
json_number ( string s ) {
this->text = s;
}
json_number ( int v ) {
this->value = v;
this->as_int = true;
}
json_number ( double v ) {
this->value = v;
this->as_int = false;
}
virtual void print_self() {
cout << "json_number" << endl;
}
};
class json_string : public json_primitive {
public:
json_string ( string s ) {
this->text = s;
}
virtual void print_self() {
cout << "json_string" << endl;
}
};
class json_true : public json_primitive {
public:
json_true () { }
json_true ( string s ) {
this->text = s;
}
virtual void print_self() {
cout << "json_true" << endl;
}
};
class json_false : public json_primitive {
public:
json_false () { }
json_false ( string s ) {
this->text = s;
}
virtual void print_self() {
cout << "json_false" << endl;
}
};
class json_null : public json_primitive {
public:
json_null () { }
json_null ( string s ) {
this->text = s;
}
virtual void print_self() {
cout << "json_null" << endl;
}
};
////////////////////
// These are currently down here because I couldn't figure out how to forward reference the various json_type classes directly above
////////////////////
int json_parser::parse_element ( void *parent, int index, int depth ) {
int start = skip_whitespace ( index, depth );
if (start >= 0) {
if ( text[start] == '{' ) {
// This will add an object object to the parent
start = parse_object ( parent, start, depth );
} else if ( text[start] == '[' ) {
// This will add an array object to the parent
start = parse_array ( parent, start, depth );
} else if ( text[start] == '\"' ) {
// This will add a string object to the parent
start = parse_string ( parent, start, depth );
} else if ( strchr(leading_number_chars,text[start]) != NULL ) {
// This will add a number object to the parent
start = parse_number ( parent, start, depth );
} else if ( strncmp ( null_template, &text[start], 4) == 0 ) {
post_store_skipped ( JSON_VAL_NULL, start, start+4, depth );
// Add a null object to the parent
json_array *p = (json_array *)parent;
json_null *val = new json_null;
cout << "Created a json_null at " << (void *)val << " with parent at " << parent << endl;
p->push_back( (void *)val );
start += 4;
} else if ( strncmp ( true_template, &text[start], 4) == 0 ) {
post_store_skipped ( JSON_VAL_TRUE, start, start+4, depth );
// Add a true object to the parent
json_array *p = (json_array *)parent;
json_true *val = new json_true;
p->push_back( (void *)val );
cout << "Created a json_true at " << (void *)val << " with parent at " << parent << endl;
start += 4;
} else if ( strncmp ( false_template, &text[start], 5) == 0 ) {
post_store_skipped ( JSON_VAL_FALSE, start, start+5, depth );
// Add a false object to the parent
json_array *p = (json_array *)parent;
json_false *val = new json_false;
p->push_back( (void *)val );
cout << "Created a json_false at " << (void *)val << " with parent at " << parent << endl;
start += 5;
} else {
cout << "Unexpected char (" << text[start] << ") in " << text << endl;
}
}
return start;
}
int json_parser::parse_keyval ( void *parent, int index, int depth ) {
//////////////// NOTE: Should probably use std::pair to store these key/value pairs!!!!
json_array *key_val = new json_array();
json_element *je = pre_store_skipped ( JSON_VAL_KEYVAL, index, index, depth );
index = skip_whitespace ( index, depth );
int end = index;
end = parse_string ( key_val, end, depth );
end = skip_whitespace ( end, depth );
end = end + 1; // This is the colon separator (:)
end = parse_element ( key_val, end, depth );
post_store_skipped ( JSON_VAL_KEYVAL, index, end, depth, je );
json_object *p = (json_object *)parent;
json_string *s = (json_string *)(key_val->at(0));
// cout << "Key = " << s->text << endl;
p->insert ( { s->text, (void *)(key_val->at(1)) } );
cout << "Created a json_keyval with parent at " << parent << endl;
return (end);
}
int json_parser::parse_object ( void *parent, int index, int depth ) {
json_element *je = pre_store_skipped ( JSON_VAL_OBJECT, index, index, depth );
int end = index+1;
depth += 1;
json_array *p = (json_array *)parent;
json_object *o = new json_object();
p->push_back ( (void *)o );
cout << "Created a json_object at " << (void *)o << " with parent at " << parent << endl;
while (text[end] != '}') {
end = parse_keyval ( o, end, depth );
end = skip_sepspace ( end, depth );
}
depth += -1;
post_store_skipped ( JSON_VAL_OBJECT, index, end+1, depth, je );
return (end + 1);
}
int json_parser::parse_array ( void *parent, int index, int depth ) {
json_element *je = pre_store_skipped ( JSON_VAL_ARRAY, index, index, depth );
int end = index+1;
depth += 1;
json_array *p = (json_array *)parent;
json_array *child = new json_array();
p->push_back ( (void *)child );
cout << "Created a json_array at " << (void *)child << " with parent at " << parent << endl;
while (text[end] != ']') {
end = parse_element ( child, end, depth );
end = skip_sepspace ( end, depth );
}
depth += -1;
post_store_skipped ( JSON_VAL_ARRAY, index, end+1, depth, je );
return (end + 1);
}
int json_parser::parse_string ( void *parent, int index, int depth ) {
int end = index+1;
while (text[end] != '"') {
end++;
}
post_store_skipped ( JSON_VAL_STRING, index, end+1, depth );
json_array *p = (json_array *)parent;
string local_text;
local_text.assign (text);
string sub_string = local_text.substr(index+1,end-index);
json_string *val = new json_string(sub_string);
p->push_back( (void *)val );
cout << "Created a json_string at " << (void *)val << " with parent at " << parent << endl;
return (end + 1);
}
int json_parser::parse_number ( void *parent, int index, int depth ) {
int end = index;
const char *number_chars = "0123456789.-+eE";
while ( strchr(number_chars,text[end]) != NULL ) {
end++;
}
post_store_skipped ( JSON_VAL_NUMBER, index, end, depth );
json_array *p = (json_array *)parent;
string local_text;
local_text.assign (text);
string sub_string = local_text.substr(index,end-index);
json_number *val = new json_number(sub_string);
p->push_back( (void *)val );
cout << "Created a json_string at " << (void *)val << " with parent at " << parent << endl;
return (end);
}
int main() {
cout << "JSON C++ Parser" << endl;
//char *text = "{\"A\":true,\"mc\":[{\"a\":0.01},1e-5,2,true,[9,[0,3],\"a\",345],false,null,5,[1,2,3],\"xyz\"],\"x\":\"y\"}";
char *text = "[9,[0,3],{\"a\":5}]";
json_array top;
json_parser p = json_parser(text);
p.parse ( &top );
p.dump(90);
top.print_self();
json_element *je = new json_element(0,0,0,0);
cout << "Hello!! " << JSON_VAL_ARRAY << endl;
return ( 0 );
}