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
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
|
#include "internalJSONNode.h"
#include "NumberToString.h" //So that I can convert numbers into strings
#include "JSONNode.h" //To fill in the foreward declaration
#include "JSONWorker.h" //For fetching and parsing and such
/*
The point of these constants is for faster assigning, if I
were to constantly assign to a string literal, there would be
lots of copies, but since strings are copy on write, this assignment
is much faster
*/
static const json_string CONST_TRUE(JSON_TEXT("true"));
static const json_string CONST_FALSE(JSON_TEXT("false"));
static const json_string CONST_NULL(JSON_TEXT("null"));
#ifdef JSON_UNIT_TEST
void internalJSONNode::incinternalAllocCount(void){ JSONNode::incinternalAllocCount(); }
void internalJSONNode::decinternalAllocCount(void){ JSONNode::decinternalAllocCount(); }
#endif
internalJSONNode::internalJSONNode(const internalJSONNode & orig) :
_type(orig._type), _name(orig._name), _name_encoded(orig._name_encoded), Children(),
_string(orig._string), _string_encoded(orig._string_encoded), _value(orig._value)
initializeRefCount(1)
initializeFetch(orig.fetched)
initializeMutex(0)
initializeComment(orig._comment)
initializeValid(orig.isValid){
incinternalAllocCount();
#ifdef JSON_MUTEX_CALLBACKS
_set_mutex(orig.mylock, false);
#endif
if (!orig.Children.empty()) {
Children.reserve(orig.Children.size());
json_foreach(orig.Children, myrunner){
Children.push_back(JSONNode::newJSONNode((*myrunner) -> duplicate()));
}
}
}
#ifdef JSON_PREPARSE
#define SetFetchedFalseOrDo(code) code
#else
#define SetFetchedFalseOrDo(code) SetFetched(false)
#endif
#ifdef JSON_VALIDATE
#define NOTVALID false
#else
#define NOTVALID
#endif
//this one is specialized because the root can only be array or node
internalJSONNode::internalJSONNode(const json_string & unparsed) : _type(), _name(),_name_encoded(false), _string(unparsed), _string_encoded(), _value(), Children()
initializeMutex(0)
initializeRefCount(1)
initializeFetch(false)
initializeComment(0)
initializeValid(0){
incinternalAllocCount();
switch (unparsed[0]) {
case JSON_TEXT('{'): //node
_type = JSON_NODE;
#ifdef JSON_PREPARSE
FetchNode();
#endif
break;
case JSON_TEXT('['): //array
_type = JSON_ARRAY;
#ifdef JSON_PREPARSE
FetchArray();
#endif
break;
default:
JSON_FAIL_SAFE(JSON_TEXT("root not starting with either { or ["), Nullify(NOTVALID););
break;
}
}
internalJSONNode::internalJSONNode(const json_string & name_t, const json_string & value_t) : _type(), _name_encoded(), _name(JSONWorker::FixString(name_t, NAME_ENCODED)), _string(), _string_encoded(), _value(), Children()
initializeMutex(0)
initializeRefCount(1)
initializeFetch(0)
initializeComment(0)
initializeValid(0){
incinternalAllocCount();
if (value_t.empty()) {
_type = JSON_NULL;
#ifdef JSON_VALIDATE
isValid = true;
#endif
SetFetched(true);
return;
}
_string = value_t;
const json_char firstchar = value_t[0];
#if defined JSON_DEBUG || defined JSON_SAFE
const json_char lastchar = value_t[value_t.length() - 1];
#endif
switch (firstchar){
case JSON_TEXT('\"'): //a json_string literal, still escaped and with leading and trailing quotes
JSON_ASSERT_SAFE(lastchar == JSON_TEXT('\"'), JSON_TEXT("Unterminated quote"), Nullify(NOTVALID); return;);
_type = JSON_STRING;
SetFetchedFalseOrDo(FetchString());
break;
case JSON_TEXT('{'): //a child node, or set of children
JSON_ASSERT_SAFE(lastchar == JSON_TEXT('}'), JSON_TEXT("Missing }"), Nullify(NOTVALID); return;);
_type = JSON_NODE;
SetFetchedFalseOrDo(FetchNode());
break;
case JSON_TEXT('['): //an array
JSON_ASSERT_SAFE(lastchar == JSON_TEXT(']'), JSON_TEXT("Missing ]"), Nullify(NOTVALID); return;);
_type = JSON_ARRAY;
SetFetchedFalseOrDo(FetchArray());
break;
case JSON_TEXT('t'):
JSON_ASSERT_SAFE(value_t == JSON_TEXT("true"), json_string(json_string(JSON_TEXT("unknown JSON literal: ")) + value_t).c_str(), Nullify(NOTVALID); return;);
_value._bool = true;
_type = JSON_BOOL;
SetFetched(true);
break;
case JSON_TEXT('f'):
JSON_ASSERT_SAFE(value_t == JSON_TEXT("false"), json_string(json_string(JSON_TEXT("unknown JSON literal: ")) + value_t).c_str(), Nullify(NOTVALID); return;);
_value._bool = false;
_type = JSON_BOOL;
SetFetched(true);
break;
case JSON_TEXT('n'):
JSON_ASSERT_SAFE(value_t == JSON_TEXT("null"), json_string(json_string(JSON_TEXT("unknown JSON literal: ")) + value_t).c_str(), Nullify(NOTVALID); return;);
_type = JSON_NULL;
SetFetched(true);
#ifdef JSON_VALIDATE
isValid = true;
#endif
break;
default:
JSON_ASSERT_SAFE(value_t.find_first_not_of(JSON_TEXT("0123456789.e+-")) == json_string::npos, json_string(json_string(JSON_TEXT("unknown JSON literal: ")) + value_t).c_str(), Nullify(NOTVALID); return;);
_type = JSON_NUMBER;
SetFetchedFalseOrDo(FetchNumber());
break;
}
}
internalJSONNode::~internalJSONNode(void){
decinternalAllocCount();
#ifdef JSON_MUTEX_CALLBACKS
_unset_mutex();
#endif
//DO NOT delete the children! It automatically gets removed
}
void internalJSONNode::FetchString(void) const {
JSON_ASSERT_SAFE(!_string.empty(), JSON_TEXT("JSON json_string type is empty?"), Nullify(NOTVALID); return;);
JSON_ASSERT_SAFE(_string[0] == JSON_TEXT('\"'), JSON_TEXT("JSON json_string type doesn't start with a quotation?"), Nullify(NOTVALID); return;);
JSON_ASSERT_SAFE(_string[_string.length() - 1] == JSON_TEXT('\"'), JSON_TEXT("JSON json_string type doesn't end with a quotation?"), Nullify(NOTVALID); return;);
_string = JSONWorker::FixString(_string.substr(1, _string.length() - 2), STRING_ENCODED);
}
void internalJSONNode::FetchNode(void) const {
JSON_ASSERT_SAFE(!_string.empty(), JSON_TEXT("JSON node type is empty?"), Nullify(NOTVALID); return;);
JSON_ASSERT_SAFE(_string[0] == JSON_TEXT('{'), JSON_TEXT("JSON node type doesn't start with a bracket?"), Nullify(NOTVALID); return;);
JSON_ASSERT_SAFE(_string[_string.length() - 1] == JSON_TEXT('}'), JSON_TEXT("JSON node type doesn't end with a bracket?"), Nullify(NOTVALID); return;);
JSONWorker::DoNode(this, _string);
clearString(_string);
}
void internalJSONNode::FetchArray(void) const {
JSON_ASSERT_SAFE(!_string.empty(), JSON_TEXT("JSON node type is empty?"), Nullify(NOTVALID); return;);
JSON_ASSERT_SAFE(_string[0] == JSON_TEXT('['), JSON_TEXT("JSON node type doesn't start with a square bracket?"), Nullify(NOTVALID); return;);
JSON_ASSERT_SAFE(_string[_string.length() - 1] == JSON_TEXT(']'), JSON_TEXT("JSON node type doesn't end with a square bracket?"), Nullify(NOTVALID); return;);
JSONWorker::DoArray(this, _string);
clearString(_string);
}
void internalJSONNode::FetchNumber(void) const {
#ifdef JSON_UNICODE
{
const size_t len = _string.length();
json_auto<char> temp(len + 1);
wcstombs(temp.ptr, _string.c_str(), len);
temp.ptr[len] = '\0';
_value._number = (json_number)atof(temp.ptr);
}
#else
_value._number = (json_number)atof(_string.c_str());
#endif
}
#ifndef JSON_PREPARSE
void internalJSONNode::Fetch(void) const {
if (fetched) return;
switch (type()) {
case JSON_STRING:
FetchString();
break;
case JSON_NODE:
FetchNode();
break;
case JSON_ARRAY:
FetchArray();
break;
case JSON_NUMBER:
FetchNumber();
break;
#if defined JSON_DEBUG || defined JSON_SAFE
default:
JSON_FAIL(JSON_TEXT("Fetching an unknown type"));
Nullify(NOTVALID);
#endif
}
fetched = true;
}
#endif
void internalJSONNode::Set(const json_string & val){
_type = JSON_STRING;
_string = val;
_string_encoded = true;
SetFetched(true);
}
#ifdef JSON_LIBRARY
void internalJSONNode::Set(long val){
_type = JSON_NUMBER;
_value._number = (json_number)val;
_string = NumberToString::_itoa<long>(val, sizeof(long));
SetFetched(true);
}
void internalJSONNode::Set(json_number val){
_type = JSON_NUMBER;
_value._number = val;
_string = NumberToString::_ftoa<json_number>(val);
SetFetched(true);
}
#else
#define SET(converter, type)\
void internalJSONNode::Set(type val){\
_type = JSON_NUMBER;\
_value._number = (json_number)val;\
_string = NumberToString::converter<type>(val, sizeof(type));\
SetFetched(true);\
}
#define SET_INTEGER(type) SET(_itoa, type) SET(_uitoa, unsigned type)
#define SET_FLOAT(type) \
void internalJSONNode::Set(type val){\
_type = JSON_NUMBER;\
_value._number = (json_number)val;\
_string = NumberToString::_ftoa<type>(val);\
SetFetched(true);\
}
SET_INTEGER(char)
SET_INTEGER(short)
SET_INTEGER(int)
SET_INTEGER(long)
#ifndef JSON_ISO_STRICT
SET_INTEGER(long long)
#endif
SET_FLOAT(float)
SET_FLOAT(double)
#endif
void internalJSONNode::Set(bool val){
_type = JSON_BOOL;
_value._bool = val;
_string = val ? CONST_TRUE : CONST_FALSE;
SetFetched(true);
}
bool internalJSONNode::IsEqualTo(const internalJSONNode * val) const {
#ifdef JSON_REF_COUNT
if (this == val) return true; //reference counting the same internal object, so they must be equal
#endif
if (type() != val -> type()) return false; //aren't even same type
if (_name != val -> _name) return false; //names aren't the same
if (type() == JSON_NULL) return true; //both null, can't be different
#ifndef JSON_PREPARSE
Fetch();
val -> Fetch();
#endif
switch (type()) {
case JSON_STRING:
return val -> _string == _string;
case JSON_NUMBER:
return NumberToString::areEqual(val -> _value._number, _value._number);
case JSON_BOOL:
return val -> _value._bool == _value._bool;
};
JSON_ASSERT(type() == JSON_NODE || type() == JSON_ARRAY, JSON_TEXT("Checking for equality, not sure what type"));
if (Children.size() != val -> Children.size()) return false; //if they arne't he same size then they certainly aren't equal
//make sure each children is the same
JSONNode ** valrunner = val -> Children.begin();
json_foreach(Children, myrunner){
JSON_ASSERT(*myrunner, JSON_TEXT("a null pointer within the children"));
JSON_ASSERT(*valrunner, JSON_TEXT("a null pointer within the children"));
JSON_ASSERT(valrunner != val -> Children.end(), JSON_TEXT("at the end of other one's children, but they're the same size?"));
if (**myrunner != **valrunner) return false;
++valrunner;
}
return true;
}
#ifdef JSON_VALIDATE
void internalJSONNode::Nullify(bool validation) const {
isValid = validation;
#else
void internalJSONNode::Nullify(void) const {
#endif
_type = JSON_NULL;
_string = CONST_NULL;
SetFetched(true);
}
#ifdef JSON_MUTEX_CALLBACKS
#define JSON_MUTEX_COPY ,mylock
#else
#define JSON_MUTEX_COPY
#endif
#ifdef JSON_LIBRARY
void internalJSONNode::push_back(JSONNode * node){
#else
void internalJSONNode::push_back(const JSONNode & node){
#endif
JSON_ASSERT(type() == JSON_ARRAY || type() == JSON_NODE, JSON_TEXT("pushing back to something that is not an array or object"));
#ifdef JSON_LIBRARY
#ifdef JSON_MUTEX_CALLBACKS
if (mylock) node -> set_mutex(mylock);
#endif
Children.push_back(node);
#else
Children.push_back(JSONNode::newJSONNode(node JSON_MUTEX_COPY));
#endif
}
void internalJSONNode::push_front(const JSONNode & node){
JSON_ASSERT(type() == JSON_ARRAY || type() == JSON_NODE, JSON_TEXT("pushing front to something that is not an array or object"));
Children.push_front(JSONNode::newJSONNode(node JSON_MUTEX_COPY));
}
JSONNode * internalJSONNode::pop_back(json_index_t pos){
JSONNode * result = Children[pos];
JSONNode ** temp = Children.begin() + pos;
Children.erase(temp);
return result;
}
JSONNode * internalJSONNode::pop_back(const json_string & name_t){
if (JSONNode ** res = at(name_t)) {
JSONNode * result = *res;
Children.erase(res);
return result;
}
return 0;
}
#ifdef JSON_CASE_INSENSITIVE_FUNCTIONS
JSONNode * internalJSONNode::pop_back_nocase(const json_string & name_t){
if (JSONNode ** res = at_nocase(name_t)) {
JSONNode * result = *res;
Children.erase(res);
return result;
}
return 0;
}
#endif
JSONNode ** internalJSONNode::at(const json_string & name_t){
Fetch();
json_foreach(Children, myrunner){
JSON_ASSERT(*myrunner, JSON_TEXT("a null pointer within the children"));
if ((*myrunner) -> name() == name_t) return myrunner;
}
return 0;
}
#ifdef JSON_CASE_INSENSITIVE_FUNCTIONS
bool internalJSONNode::AreEqualNoCase(const json_char * ch_one, const json_char * ch_two){
while (*ch_one){ //only need to check one, if the other one terminates early, the check will cause it to fail
const json_char c_one = *ch_one;
const json_char c_two = *ch_two;
if (c_one != c_two){
if ((c_two > 64) && (c_two < 91)) { //A - Z
if (c_one != (json_char)(c_two + 32)) return false;
} else if ((c_two > 96) && (c_two < 123)) { //a - z
if (c_one != (json_char)(c_two - 32)) return false;
} else { //not a letter, so return false
return false;
}
}
++ch_one;
++ch_two;
}
return *ch_two == '\0'; //this one has to be null terminated too, or else json_string two is longer, hence, not equal
}
JSONNode ** internalJSONNode::at_nocase(const json_string & name_t){
Fetch();
json_foreach(Children, myrunner){
JSON_ASSERT(*myrunner, JSON_TEXT("a null pointer within the children"));
if (AreEqualNoCase((*myrunner) -> name().c_str(), name_t.c_str())) return myrunner;
}
return 0;
}
#endif
#ifndef JSON_PREPARSE
void internalJSONNode::preparse(void){
Fetch();
json_foreach(Children, myrunner){
(*myrunner) -> preparse();
}
}
#endif
#ifdef JSON_VALIDATE
bool internalJSONNode::validate(void){
json_foreach(Children, myrunner){
if ((*myrunner) -> type() != JSON_NULL){
#ifndef JSON_PREPARSE
(*myrunner) -> internal -> Fetch();
#endif
if ((*myrunner) -> type() == JSON_NULL) return false;
} else if (!((*myrunner) -> internal -> isValid)) {
JSON_FAIL(_name + JSON_TEXT(" is null and not valid"));
return false;
}
}
json_foreach(Children, runner){
if (!((*runner) -> internal -> validate())) return false;
}
return true;
}
#endif
#ifdef JSON_DEBUG
#ifndef JSON_LIBRARY
JSONNode internalJSONNode::Dump(size_t & totalbytes) const {
JSONNode dumpage(JSON_NODE);
dumpage.set_name(JSON_TEXT("internalJSONNode"));
dumpage.push_back(JSON_NEW(JSONNode(JSON_TEXT("this"), (long)this)));
size_t memory = sizeof(internalJSONNode);
memory += _name.capacity() * sizeof(json_char);
memory += _string.capacity() * sizeof(json_char);
memory += Children.capacity() * sizeof(JSONNode*);
#ifdef JSON_COMMENTS
memory += _comment.capacity() * sizeof(json_char);
#endif
totalbytes += memory;
dumpage.push_back(JSON_NEW(JSONNode(JSON_TEXT("bytes used"), memory)));
#ifdef JSON_REF_COUNT
dumpage.push_back(JSON_NEW(JSONNode(JSON_TEXT("refcount"), refcount)));
#endif
#ifdef JSON_MUTEX_CALLBACKS
dumpage.push_back(JSON_NEW(DumpMutex()));
#endif
#define DUMPCASE(ty)\
case ty:\
dumpage.push_back(JSON_NEW(JSONNode(JSON_TEXT("_type"), JSON_TEXT(#ty))));\
break;
switch(type()) {
DUMPCASE(JSON_NULL)
DUMPCASE(JSON_STRING)
DUMPCASE(JSON_NUMBER)
DUMPCASE(JSON_BOOL)
DUMPCASE(JSON_ARRAY)
DUMPCASE(JSON_NODE)
default:
dumpage.push_back(JSON_NEW(JSONNode(JSON_TEXT("_type"), JSON_TEXT("Unknown"))));
}
JSONNode str(JSON_NODE);
str.set_name(JSON_TEXT("_name"));
str.push_back(JSON_NEW(JSONNode(json_string(JSON_TEXT("value")), _name)));
str.push_back(JSON_NEW(JSONNode(JSON_TEXT("length"), _name.length())));
str.push_back(JSON_NEW(JSONNode(JSON_TEXT("capactiy"), _name.capacity())));
#ifdef JSON_LESS_MEMORY
dumpage.push_back(JSON_NEW(JSONNode(JSON_TEXT("_name_encoded"), _type & 0x10)));
#else
dumpage.push_back(JSON_NEW(JSONNode(JSON_TEXT("_name_encoded"), _name_encoded)));
#endif
dumpage.push_back(JSON_NEW(str));
dumpage.push_back(JSON_NEW(JSONNode(JSON_TEXT("_string_encoded"), _string_encoded)));
str.clear();
str.set_name(JSON_TEXT("_string"));
str.push_back(JSON_NEW(JSONNode(json_string(JSON_TEXT("value")), _string)));
str.push_back(JSON_NEW(JSONNode(JSON_TEXT("length"), _string.length())));
str.push_back(JSON_NEW(JSONNode(JSON_TEXT("capactiy"), _string.capacity())));
dumpage.push_back(JSON_NEW(str));
JSONNode unio(JSON_NODE);
unio.set_name(JSON_TEXT("_value"));
unio.push_back(JSON_NEW(JSONNode(JSON_TEXT("_bool"), _value._bool)));
unio.push_back(JSON_NEW(JSONNode(JSON_TEXT("_number"), _value._number)));
dumpage.push_back(JSON_NEW(unio));
#ifndef JSON_PREPARSE
dumpage.push_back(JSON_NEW(JSONNode(JSON_TEXT("fetched"), fetched)));
#endif
#ifdef JSON_COMMENTS
str.clear();
str.set_name(JSON_TEXT("_comment"));
str.push_back(JSON_NEW(JSONNode(JSON_TEXT("value"), _comment)));
str.push_back(JSON_NEW(JSONNode(JSON_TEXT("length"), _comment.length())));
str.push_back(JSON_NEW(JSONNode(JSON_TEXT("capactiy"), _comment.capacity())));
dumpage.push_back(JSON_NEW(str));
#endif
JSONNode arra(JSON_NODE);
arra.set_name(JSON_TEXT("Children"));
arra.push_back(JSON_NEW(JSONNode(JSON_TEXT("size"), Children.size())));
arra.push_back(JSON_NEW(JSONNode(JSON_TEXT("capacity"), Children.capacity())));
JSONNode chil(JSON_ARRAY);
chil.set_name(JSON_TEXT("array"));
json_foreach(Children, it){
chil.push_back(JSON_NEW((*it) -> dump(totalbytes)));
}
arra.push_back(JSON_NEW(chil));
dumpage.push_back(JSON_NEW(arra));
return dumpage;
}
#endif
#endif
|