-
Notifications
You must be signed in to change notification settings - Fork 140
/
Copy pathjson.cpp
1497 lines (1400 loc) · 42.2 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
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
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/// \file json.cpp Holds all JSON-related code.
#include "bitfields.h"
#include "defines.h"
#include "json.h"
#include <arpa/inet.h> //for htonl
#include <fstream>
#include <sstream>
#include <stdint.h> //for uint64_t
#include <stdlib.h>
#include <string.h> //for memcpy
/// Construct from a root Value to iterate over.
JSON::Iter::Iter(Value &root){
myType = root.myType;
i = 0;
r = &root;
if (!root.size()){myType = JSON::EMPTY;}
if (myType == JSON::ARRAY){aIt = root.arrVal.begin();}
if (myType == JSON::OBJECT){oIt = root.objVal.begin();}
}
/// Dereferences into a Value reference.
/// If invalid iterator, returns an empty reference and prints a warning message.
JSON::Value &JSON::Iter::operator*() const{
if (myType == JSON::ARRAY && aIt != r->arrVal.end()){return **aIt;}
if (myType == JSON::OBJECT && oIt != r->objVal.end()){return *(oIt->second);}
static JSON::Value error;
WARN_MSG("Dereferenced invalid JSON iterator");
return error;
}
/// Dereferences into a Value reference.
/// If invalid iterator, returns an empty reference and prints a warning message.
JSON::Value *JSON::Iter::operator->() const{
return &(operator*());
}
/// True if not done iterating.
JSON::Iter::operator bool() const{
return ((myType == JSON::ARRAY && aIt != r->arrVal.end()) ||
(myType == JSON::OBJECT && oIt != r->objVal.end()));
}
/// Go to next iteration.
JSON::Iter &JSON::Iter::operator++(){
if (*this){
++i;
if (myType == JSON::ARRAY){++aIt;}
if (myType == JSON::OBJECT){++oIt;}
}
return *this;
}
/// Return the name of the current indice.
const std::string &JSON::Iter::key() const{
if (myType == JSON::OBJECT && *this){return oIt->first;}
static const std::string empty;
WARN_MSG("Got key from invalid JSON iterator");
return empty;
}
/// Return the number of the current indice.
uint32_t JSON::Iter::num() const{
return i;
}
/// Delete the current indice from the parent JSON::Value.
/// Resets the iterator to restart from the beginning
void JSON::Iter::remove(){
if (*this){
i = 0;
if (myType == JSON::ARRAY){
r->removeMember(aIt);
aIt = r->arrVal.begin();
}
if (myType == JSON::OBJECT){
r->removeMember(oIt);
oIt = r->objVal.begin();
}
}
}
/// Construct from a root Value to iterate over.
JSON::ConstIter::ConstIter(const Value &root){
myType = root.myType;
i = 0;
r = &root;
if (!root.size()){myType = JSON::EMPTY;}
if (myType == JSON::ARRAY){aIt = root.arrVal.begin();}
if (myType == JSON::OBJECT){oIt = root.objVal.begin();}
}
/// Dereferences into a Value reference.
/// If invalid iterator, returns an empty reference and prints a warning message.
const JSON::Value &JSON::ConstIter::operator*() const{
if (myType == JSON::ARRAY && aIt != r->arrVal.end()){return **aIt;}
if (myType == JSON::OBJECT && oIt != r->objVal.end()){return *(oIt->second);}
static JSON::Value error;
WARN_MSG("Dereferenced invalid JSON iterator");
return error;
}
/// Dereferences into a Value reference.
/// If invalid iterator, returns an empty reference and prints a warning message.
const JSON::Value *JSON::ConstIter::operator->() const{
return &(operator*());
}
/// True if not done iterating.
JSON::ConstIter::operator bool() const{
return ((myType == JSON::ARRAY && aIt != r->arrVal.end()) ||
(myType == JSON::OBJECT && oIt != r->objVal.end()));
}
/// Go to next iteration.
JSON::ConstIter &JSON::ConstIter::operator++(){
if (*this){
++i;
if (myType == JSON::ARRAY){++aIt;}
if (myType == JSON::OBJECT){++oIt;}
}
return *this;
}
/// Return the name of the current indice.
const std::string &JSON::ConstIter::key() const{
if (myType == JSON::OBJECT && *this){return oIt->first;}
static const std::string empty;
WARN_MSG("Got key from invalid JSON iterator");
return empty;
}
/// Return the number of the current indice.
uint32_t JSON::ConstIter::num() const{
return i;
}
static inline char c2hex(char c){
if (c >= '0' && c <= '9') return c - '0';
if (c >= 'a' && c <= 'f') return c - 'a' + 10;
if (c >= 'A' && c <= 'F') return c - 'A' + 10;
return 0;
}
static inline char hex2c(char c){
if (c < 10){return '0' + c;}
if (c < 16){return 'A' + (c - 10);}
return '0';
}
static std::string UTF8(uint32_t c){
std::string r;
if (c <= 0x7F){
r.append(1, c);
return r;
}
if (c <= 0x7FF){
r.append(1, 0xC0 | (c >> 6));
r.append(1, 0x80 | (c & 0x3F));
return r;
}
if (c <= 0x7FF){
r.append(1, 0xC0 | (c >> 12));
r.append(1, 0x80 | ((c >> 6) & 0x3F));
r.append(1, 0x80 | (c & 0x3F));
return r;
}
// Convert from two UTF16 chars to unicode codepoint
c = (((c >> 16) & 0x3ff) << 10) + ((c & 0xFFFF) & 0x3ff) + 0x10000;
// Encode to 4-byte UTF8 sequence
r.append(1, 0xF0 | ((c >> 18) & 0x07));
r.append(1, 0x80 | ((c >> 12) & 0x3F));
r.append(1, 0x80 | ((c >> 6) & 0x3F));
r.append(1, 0x80 | (c & 0x3F));
return r;
}
static std::string read_string(char separator, std::istream &fromstream){
std::string out;
bool escaped = false;
uint32_t fullChar = 0;
while (fromstream.good()){
char c;
fromstream.get(c);
if (!escaped && c == '\\'){
escaped = true;
continue;
}
if (escaped){
if (fullChar && c != 'u'){
out += UTF8(fullChar >> 16);
fullChar = 0;
}
switch (c){
case 'b': out += '\b'; break;
case '\\': out += '\\'; break;
case 'f': out += '\f'; break;
case 'n': out += '\n'; break;
case 'r': out += '\r'; break;
case 't': out += '\t'; break;
case 'x':
char d1, d2;
fromstream.get(d1);
fromstream.get(d2);
out.append(1, (c2hex(d2) + (c2hex(d1) << 4)));
break;
case 'u':{
char d1, d2, d3, d4;
fromstream.get(d1);
if (d1 == separator){goto stopParsing;}
fromstream.get(d2);
if (d2 == separator){goto stopParsing;}
fromstream.get(d3);
if (d3 == separator){goto stopParsing;}
fromstream.get(d4);
if (d4 == separator){goto stopParsing;}
uint32_t tmpChar = (c2hex(d4) + (c2hex(d3) << 4) + (c2hex(d2) << 8) + (c2hex(d1) << 12));
if (fullChar && (tmpChar < 0xDC00 || tmpChar > 0xDFFF)){
// not a low surrogate - handle high surrogate separately!
out += UTF8(fullChar >> 16);
fullChar = 0;
}
fullChar |= tmpChar;
if (fullChar >= 0xD800 && fullChar <= 0xDBFF){
// possibly high surrogate! Read next characters before handling...
fullChar <<= 16; // save as high surrogate
}else{
out += UTF8(fullChar);
fullChar = 0;
}
break;
}
default: out.append(1, c); break;
}
escaped = false;
}else{
if (fullChar){
out += UTF8(fullChar >> 16);
fullChar = 0;
}
if (c == separator){return out;}
out.append(1, c);
}
}
stopParsing:
if (fullChar){
out += UTF8(fullChar >> 16);
fullChar = 0;
}
return out;
}
static std::string UTF16(uint32_t c){
if (c > 0xFFFF){
c -= 0x010000;
return UTF16(0xD800 + ((c >> 10) & 0x3FF)) + UTF16(0xDC00 + (c & 0x3FF));
}
std::string ret = "\\u";
ret += hex2c((c >> 12) & 0xf);
ret += hex2c((c >> 8) & 0xf);
ret += hex2c((c >> 4) & 0xf);
ret += hex2c(c & 0xf);
return ret;
}
std::string JSON::string_escape(const std::string &val){
std::string out = "\"";
for (size_t i = 0; i < val.size(); ++i){
const char &c = val.data()[i];
switch (c){
case '"': out += "\\\""; break;
case '\\': out += "\\\\"; break;
case '\n': out += "\\n"; break;
case '\b': out += "\\b"; break;
case '\f': out += "\\f"; break;
case '\r': out += "\\r"; break;
case '\t': out += "\\t"; break;
default:
if (c < 32 || c > 126){
// we assume our data is UTF-8 encoded internally.
// JavaScript expects UTF-16, so if we recognize a valid UTF-8 sequence, we turn it into
// UTF-16 for JavaScript. Anything else is escaped as a single character UTF-16 escape.
if ((c & 0xC0) == 0xC0){
// possible UTF-8 sequence
// check for 2-byte sequence
if (((c & 0xE0) == 0XC0) && (i + 1 < val.size()) && ((val.data()[i + 1] & 0xC0) == 0x80)){
// valid 2-byte sequence
out += UTF16(((c & 0x1F) << 6) | (val.data()[i + 1] & 0x3F));
i += 1;
break;
}
// check for 3-byte sequence
if (((c & 0xF0) == 0XE0) && (i + 2 < val.size()) &&
((val.data()[i + 1] & 0xC0) == 0x80) && ((val.data()[i + 2] & 0xC0) == 0x80)){
// valid 3-byte sequence
out += UTF16(((c & 0x1F) << 12) | ((val.data()[i + 1] & 0x3F) << 6) | (val.data()[i + 2] & 0x3F));
i += 2;
break;
}
// check for 4-byte sequence
if (((c & 0xF8) == 0XF0) && (i + 3 < val.size()) && ((val.data()[i + 1] & 0xC0) == 0x80) &&
((val.data()[i + 2] & 0xC0) == 0x80) && ((val.data()[i + 3] & 0xC0) == 0x80)){
// valid 4-byte sequence
out += UTF16(((c & 0x1F) << 18) | ((val.data()[i + 1] & 0x3F) << 12) |
((val.data()[i + 2] & 0x3F) << 6) | (val.data()[i + 3] & 0x3F));
i += 3;
break;
}
}
// Anything else, we encode as a single UTF-16 character.
out += "\\u00";
out += hex2c((val.data()[i] >> 4) & 0xf);
out += hex2c(val.data()[i] & 0xf);
}else{
out += val.data()[i];
}
break;
}
}
out += "\"";
return out;
}
/// Skips an std::istream forward until any of the following characters is seen: ,]}
static void skipToEnd(std::istream &fromstream){
while (fromstream.good()){
char peek = fromstream.peek();
if (peek == ','){return;}
if (peek == ']'){return;}
if (peek == '}'){return;}
peek = fromstream.get();
}
}
/// Sets this JSON::Value to null;
JSON::Value::Value(){
null();
}
/// Sets this JSON::Value to null
JSON::Value::~Value(){
null();
}
JSON::Value::Value(const Value &rhs){
null();
*this = rhs;
}
/// Sets this JSON::Value to read from this position in the std::istream
JSON::Value::Value(std::istream &fromstream){
null();
bool reading_object = false;
bool reading_array = false;
bool negative = false;
bool stop = false;
while (!stop && fromstream.good()){
int c = fromstream.peek();
switch (c){
case '{':
reading_object = true;
c = fromstream.get();
myType = OBJECT;
break;
case '[':{
reading_array = true;
c = fromstream.get();
myType = ARRAY;
Value tmp = JSON::Value(fromstream);
if (tmp.myType != EMPTY){append(tmp);}
break;
}
case '\'':
case '"':
c = fromstream.get();
if (!reading_object){
myType = STRING;
strVal = read_string(c, fromstream);
stop = true;
}else{
std::string tmpstr = read_string(c, fromstream);
(*this)[tmpstr] = JSON::Value(fromstream);
}
break;
case '-':
c = fromstream.get();
negative = true;
break;
case '0':
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9':
c = fromstream.get();
if (myType != INTEGER && myType != DOUBLE){myType = INTEGER;}
if (myType == INTEGER){
intVal *= 10;
intVal += c - '0';
}else{
dblDivider *= 10;
dblVal += ((double)((c - '0')) / dblDivider);
}
break;
case '.':
c = fromstream.get();
myType = DOUBLE;
if (negative){
dblVal = -intVal;
dblDivider = -1;
}else{
dblVal = intVal;
dblDivider = 1;
}
break;
case ',':
if (!reading_object && !reading_array){
stop = true;
break;
}
c = fromstream.get();
if (reading_array){append(JSON::Value(fromstream));}
break;
case '}':
if (reading_object){c = fromstream.get();}
stop = true;
break;
case ']':
if (reading_array){c = fromstream.get();}
stop = true;
break;
case 't':
case 'T':
skipToEnd(fromstream);
myType = BOOL;
intVal = 1;
stop = true;
break;
case 'f':
case 'F':
skipToEnd(fromstream);
myType = BOOL;
intVal = 0;
stop = true;
break;
case 'n':
case 'N':
skipToEnd(fromstream);
myType = EMPTY;
stop = true;
break;
default:
c = fromstream.get(); // ignore this character
continue;
break;
}
}
if (negative){intVal *= -1;}
}
/// Sets this JSON::Value to the given string.
JSON::Value::Value(const std::string &val){
myType = STRING;
strVal = val;
intVal = 0;
}
/// Sets this JSON::Value to the given string.
JSON::Value::Value(const char *val){
myType = STRING;
strVal = val;
intVal = 0;
}
/// Sets this JSON::Value to the given integer.
JSON::Value::Value(uint32_t val){
myType = INTEGER;
intVal = val;
}
/// Sets this JSON::Value to the given integer.
JSON::Value::Value(uint64_t val){
myType = INTEGER;
intVal = val;
}
/// Sets this JSON::Value to the given integer.
JSON::Value::Value(int32_t val){
myType = INTEGER;
intVal = val;
}
/// Sets this JSON::Value to the given integer.
JSON::Value::Value(int64_t val){
myType = INTEGER;
intVal = val;
}
/// Sets this JSON::Value to the given integer.
JSON::Value::Value(size_t val){
myType = INTEGER;
intVal = val;
}
/// Sets this JSON::Value to the given double.
JSON::Value::Value(double val){
myType = DOUBLE;
dblVal = val;
}
/// Sets this JSON::Value to the given integer.
JSON::Value::Value(bool val){
myType = BOOL;
intVal = (val ? 1 : 0);
}
/// Compares a JSON::Value to another for equality.
bool JSON::Value::operator==(const JSON::Value &rhs) const{
if (myType != rhs.myType){return false;}
if (myType == INTEGER || myType == BOOL){return intVal == rhs.intVal;}
if (myType == DOUBLE){return dblVal == rhs.dblVal;}
if (myType == STRING){return strVal == rhs.strVal;}
if (myType == EMPTY){return true;}
if (size() != rhs.size()){return false;}
if (myType == OBJECT){
jsonForEachConst(*this, it){
if (!rhs.isMember(it.key()) || *it != rhs[it.key()]){return false;}
}
return true;
}
if (myType == ARRAY){
jsonForEachConst(*this, it){
if (*it != rhs[it.num()]){return false;}
}
return true;
}
return true;
}
/// Compares a JSON::Value to another for equality.
bool JSON::Value::operator!=(const JSON::Value &rhs) const{
return !((*this) == rhs);
}
bool JSON::Value::compareExcept(const Value &rhs, const std::set<std::string> &skip) const{
if (myType == OBJECT){
jsonForEachConst(*this, it){
if (skip.count(it.key())){continue;}
if (!rhs.isMember(it.key()) || !(*it).compareExcept(rhs[it.key()], skip)){return false;}
}
jsonForEachConst(rhs, it){
if (skip.count(it.key())){continue;}
if (!(*this).isMember(it.key())){return false;}
}
return true;
}
if (myType == ARRAY){
if (size() != rhs.size()){return false;}
jsonForEachConst(*this, it){
if (!(*it).compareExcept(rhs[it.num()], skip)){return false;}
}
return true;
}
return ((*this) == rhs);
}
bool JSON::Value::compareOnly(const Value &rhs, const std::set<std::string> &check) const{
if (myType == OBJECT){
jsonForEachConst(*this, it){
if (!check.count(it.key())){continue;}
if (!rhs.isMember(it.key()) || !(*it).compareOnly(rhs[it.key()], check)){return false;}
}
jsonForEachConst(rhs, it){
if (!check.count(it.key())){continue;}
if (!(*this).isMember(it.key())){return false;}
}
return true;
}
if (myType == ARRAY){
if (size() != rhs.size()){return false;}
jsonForEachConst(*this, it){
if (!(*it).compareOnly(rhs[it.num()], check)){return false;}
}
return true;
}
return ((*this) == rhs);
}
/// Completely clears the contents of this value,
/// changing its type to NULL in the process.
void JSON::Value::null(){
shrink(0);
strVal.clear();
intVal = 0;
dblVal = 0;
dblDivider = 1;
myType = EMPTY;
}
/// Assigns this JSON::Value to the given JSON::Value, skipping given member recursively.
JSON::Value &JSON::Value::assignFrom(const Value &rhs, const std::set<std::string> &skip){
null();
myType = rhs.myType;
if (myType == STRING){strVal = rhs.strVal;}
if (myType == BOOL || myType == INTEGER){intVal = rhs.intVal;}
if (myType == DOUBLE){dblVal = rhs.dblVal;}
if (myType == OBJECT){
jsonForEachConst(rhs, i){
if (!skip.count(i.key())){
JSON::Value tmp;
tmp.assignFrom(*i, skip);
(*this)[i.key()] = tmp;
}
}
}
if (myType == ARRAY){
jsonForEachConst(rhs, i){
JSON::Value tmp;
tmp.assignFrom(*i, skip);
append(tmp);
}
}
return *this;
}
/// Extends this JSON::Value object with the given JSON::Value object, skipping given member(s) recursively.
JSON::Value &JSON::Value::extend(const Value &rhs, const std::set<std::string> &skip){
// Null values turn into objects automatically for sanity reasons
if (myType == EMPTY){myType = OBJECT;}
// Abort if either value is not an object
if (myType != rhs.myType || myType != OBJECT){return *this;}
jsonForEachConst(rhs, i){
if (!skip.count(i.key())){
if (!objVal.count(i.key()) || !i->isObject()){
(*this)[i.key()] = *i;
}else{
(*this)[i.key()].extend(*i, skip);
}
}
}
return *this;
}
/// Sets this JSON::Value to be equal to the given JSON::Value.
JSON::Value &JSON::Value::operator=(const JSON::Value &rhs){
null();
myType = rhs.myType;
if (myType == STRING){strVal = rhs.strVal;}
if (myType == BOOL || myType == INTEGER){intVal = rhs.intVal;}
if (myType == DOUBLE){dblVal = rhs.dblVal;}
if (myType == OBJECT){
jsonForEachConst(rhs, i){(*this)[i.key()] = *i;}
}
if (myType == ARRAY){
jsonForEachConst(rhs, i){append(*i);}
}
return *this;
}
/// Sets this JSON::Value to the given boolean.
JSON::Value &JSON::Value::operator=(const bool &rhs){
null();
myType = BOOL;
intVal = (rhs ? 1 : 0);
return *this;
}
/// Sets this JSON::Value to the given string.
JSON::Value &JSON::Value::operator=(const std::string &rhs){
null();
myType = STRING;
strVal = rhs;
return *this;
}
/// Sets this JSON::Value to the given string.
JSON::Value &JSON::Value::operator=(const char *rhs){
return ((*this) = (std::string)rhs);
}
/// Sets this JSON::Value to the given integer.
JSON::Value &JSON::Value::operator=(const int64_t &rhs){
null();
myType = INTEGER;
intVal = rhs;
return *this;
}
/// Sets this JSON::Value to the given integer.
JSON::Value &JSON::Value::operator=(const int32_t &rhs){
return ((*this) = (int64_t)rhs);
}
/// Sets this JSON::Value to the given integer.
JSON::Value &JSON::Value::operator=(const uint64_t &rhs){
return ((*this) = (int64_t)rhs);
}
/// Sets this JSON::Value to the given integer.
JSON::Value &JSON::Value::operator=(const size_t &rhs){
return ((*this) = (int64_t)rhs);
}
/// Sets this JSON::Value to the given double.
JSON::Value &JSON::Value::operator=(const double &rhs){
null();
myType = DOUBLE;
dblVal = rhs;
return *this;
}
/// Sets this JSON::Value to the given integer.
JSON::Value &JSON::Value::operator=(const uint32_t &rhs){
return ((*this) = (int64_t)rhs);
}
/// Automatic conversion to long long int - returns 0 if not convertable.
JSON::Value::operator int64_t() const{
if (myType == INTEGER){return intVal;}
if (myType == DOUBLE){return (long long int)dblVal;}
if (myType == STRING){return atoll(strVal.c_str());}
return 0;
}
/// Automatic conversion to double - returns 0 if not convertable.
JSON::Value::operator double() const{
if (myType == INTEGER){return (double)intVal;}
if (myType == DOUBLE){return dblVal;}
if (myType == STRING){return atof(strVal.c_str());}
return 0;
}
/// Automatic conversion to std::string.
/// Returns the raw string value if available, otherwise calls toString().
JSON::Value::operator std::string() const{
if (myType == STRING){return strVal;}
if (myType == EMPTY){return "";}
return toString();
}
/// Automatic conversion to bool.
/// Returns true if there is anything meaningful stored into this value.
JSON::Value::operator bool() const{
if (myType == STRING){return strVal != "";}
if (myType == DOUBLE){return dblVal != 0;}
if (myType == INTEGER || myType == BOOL){return intVal != 0;}
if (myType == OBJECT || myType == ARRAY){return size() > 0;}
return false; // Empty or 'unimplemented? should never happen...'
}
/// Explicit conversion to std::string.
std::string JSON::Value::asString() const{
return (std::string) * this;
}
/// Explicit conversion to long long int.
int64_t JSON::Value::asInt() const{
return (int64_t) * this;
}
/// Explicit conversion to double.
const double JSON::Value::asDouble() const{
return (double)*this;
}
/// Explicit conversion to bool.
bool JSON::Value::asBool() const{
return (bool)*this;
}
/// Explicit conversion to std::string reference.
/// Returns a direct reference for string type JSON::Value objects,
/// but a reference to a static empty string otherwise.
/// \warning Only save to use when the JSON::Value is a string type!
const std::string &JSON::Value::asStringRef() const{
static std::string ugly_buffer;
if (myType == STRING){return strVal;}
return ugly_buffer;
}
/// Explicit conversion to c-string.
/// Returns a direct reference for string type JSON::Value objects,
/// a reference to an empty string otherwise.
/// \warning Only save to use when the JSON::Value is a string type!
const char *JSON::Value::c_str() const{
if (myType == STRING){return strVal.c_str();}
return "";
}
/// Retrieves or sets the JSON::Value at this position in the object.
/// Converts destructively to object if not already an object.
JSON::Value &JSON::Value::operator[](const std::string &i){
if (myType != OBJECT){
null();
myType = OBJECT;
}
Value *pntr = objVal[i];
if (!pntr){
objVal[i] = new JSON::Value();
pntr = objVal[i];
}
return *pntr;
}
/// Retrieves or sets the JSON::Value at this position in the object.
/// Converts destructively to object if not already an object.
JSON::Value &JSON::Value::operator[](const char *i){
if (myType != OBJECT){
null();
myType = OBJECT;
}
Value *pntr = objVal[i];
if (!pntr){
objVal[i] = new JSON::Value();
pntr = objVal[i];
}
return *pntr;
}
/// Retrieves or sets the JSON::Value at this position in the array.
/// Converts destructively to array if not already an array.
JSON::Value &JSON::Value::operator[](uint32_t i){
static JSON::Value empty;
if (myType != ARRAY){
null();
myType = ARRAY;
}
while (i >= arrVal.size()){append(empty);}
return *arrVal[i];
}
/// Retrieves the JSON::Value at this position in the object.
/// Fails horribly if that values does not exist.
const JSON::Value &JSON::Value::operator[](const std::string &i) const{
return *objVal.find(i)->second;
}
/// Retrieves the JSON::Value at this position in the object.
/// Fails horribly if that values does not exist.
const JSON::Value &JSON::Value::operator[](const char *i) const{
return *objVal.find(i)->second;
}
/// Retrieves the JSON::Value at this position in the array.
/// Fails horribly if that values does not exist.
const JSON::Value &JSON::Value::operator[](uint32_t i) const{
return *arrVal[i];
}
/// Packs to a std::string for transfer over the network.
/// If the object is a container type, this function will call itself recursively and contain all
/// contents. As a side effect, this function clear the internal buffer of any object-types.
std::string JSON::Value::toPacked() const{
std::string r;
if (isInt() || isNull() || isBool()){
r += 0x01;
uint64_t numval = intVal;
r += *(((char *)&numval) + 7);
r += *(((char *)&numval) + 6);
r += *(((char *)&numval) + 5);
r += *(((char *)&numval) + 4);
r += *(((char *)&numval) + 3);
r += *(((char *)&numval) + 2);
r += *(((char *)&numval) + 1);
r += *(((char *)&numval));
}
if (isString()){
r += 0x02;
r += strVal.size() / (256 * 256 * 256);
r += strVal.size() / (256 * 256);
r += strVal.size() / 256;
r += strVal.size() % 256;
r += strVal;
}
if (isObject()){
r += 0xE0;
if (objVal.size() > 0){
jsonForEachConst(*this, i){
if (i.key().size() > 0){
r += i.key().size() / 256;
r += i.key().size() % 256;
r += i.key();
r += i->toPacked();
}
}
}
r += (char)0x0;
r += (char)0x0;
r += (char)0xEE;
}
if (isArray()){
r += 0x0A;
jsonForEachConst(*this, i){r += i->toPacked();}
r += (char)0x0;
r += (char)0x0;
r += (char)0xEE;
}
// Note: Will output integers for doubles.
// This is intentional, as DTSC packets cannot contain doubles.
if (isDouble()){
r += 0x01;
uint64_t numval = intVal;
r += *(((char *)&numval) + 7);
r += *(((char *)&numval) + 6);
r += *(((char *)&numval) + 5);
r += *(((char *)&numval) + 4);
r += *(((char *)&numval) + 3);
r += *(((char *)&numval) + 2);
r += *(((char *)&numval) + 1);
r += *(((char *)&numval));
}
return r;
}
// toPacked
/// Packs and transfers over the network.
/// If the object is a container type, this function will call itself recursively for all contents.
void JSON::Value::sendTo(Socket::Connection &socket) const{
if (isInt() || isNull() || isBool()){
socket.SendNow("\001", 1);
int tmpHalf = htonl((int)(intVal >> 32));
socket.SendNow((char *)&tmpHalf, 4);
tmpHalf = htonl((int)(intVal & 0xFFFFFFFF));
socket.SendNow((char *)&tmpHalf, 4);
return;
}
if (isString()){
socket.SendNow("\002", 1);
int tmpVal = htonl((int)strVal.size());
socket.SendNow((char *)&tmpVal, 4);
socket.SendNow(strVal);
return;
}
if (isObject()){
if (isMember("trackid") && isMember("time")){
unsigned int trackid = objVal.find("trackid")->second->asInt();
long long time = objVal.find("time")->second->asInt();
unsigned int size = 16;
if (objVal.size() > 0){
jsonForEachConst(*this, i){
if (i.key().size() > 0 && i.key() != "trackid" && i.key() != "time" && i.key() != "datatype"){
size += 2 + i.key().size() + i->packedSize();
}
}
}
socket.SendNow("DTP2", 4);
size = htonl(size);
socket.SendNow((char *)&size, 4);
trackid = htonl(trackid);
socket.SendNow((char *)&trackid, 4);
int tmpHalf = htonl((int)(time >> 32));
socket.SendNow((char *)&tmpHalf, 4);
tmpHalf = htonl((int)(time & 0xFFFFFFFF));
socket.SendNow((char *)&tmpHalf, 4);
socket.SendNow("\340", 1);
if (objVal.size() > 0){
jsonForEachConst(*this, i){
if (i.key().size() > 0 && i.key() != "trackid" && i.key() != "time" && i.key() != "datatype"){
char sizebuffer[2] ={0, 0};
sizebuffer[0] = (i.key().size() >> 8) & 0xFF;
sizebuffer[1] = i.key().size() & 0xFF;
socket.SendNow(sizebuffer, 2);
socket.SendNow(i.key());
i->sendTo(socket);
}
}
}
socket.SendNow("\000\000\356", 3);
return;
}
if (isMember("tracks")){
socket.SendNow("DTSC", 4);
unsigned int size = htonl(packedSize());
socket.SendNow((char *)&size, 4);
}
socket.SendNow("\340", 1);
if (objVal.size() > 0){
jsonForEachConst(*this, i){
if (i.key().size() > 0){
char sizebuffer[2] ={0, 0};
sizebuffer[0] = (i.key().size() >> 8) & 0xFF;
sizebuffer[1] = i.key().size() & 0xFF;
socket.SendNow(sizebuffer, 2);
socket.SendNow(i.key());
i->sendTo(socket);
}
}
}
socket.SendNow("\000\000\356", 3);
return;
}
if (isArray()){
socket.SendNow("\012", 1);
jsonForEachConst(*this, i){i->sendTo(socket);}
socket.SendNow("\000\000\356", 3);
return;
}
}// sendTo
/// Returns the packed size of this Value.
uint64_t JSON::Value::packedSize() const{
if (isInt() || isNull() || isBool()){return 9;}