-
Notifications
You must be signed in to change notification settings - Fork 1
/
JSON.cpp
910 lines (764 loc) · 19.1 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
/* $Id: JSON.cpp 1375 2014-04-16 12:17:54Z dpd $
*
* Copyright : (c) 2010 by Open Source Solutions Pty Ltd. All Rights Reserved
* Project :
* File : JSON
*
* Author : Denis Dowling
* Created : 9/12/2010
*
* Description : Methods to encode and later decode JavaScript Object Notation
* messages
*/
#include "JSON.h"
#include <assert.h>
#include <stdlib.h>
#include <sstream>
#include <iomanip>
#include <boost/lexical_cast.hpp>
namespace JSON
{
// **** Value ****
std::string Value::getString() const
{
throw Exception("Not String Type");
}
// Return a JSON value as a string
std::string Value::getAsString() const
{
if (isString())
return getString();
else
return encode();
}
double Value::getNumber() const
{
throw Exception("Not Number Type");
}
double Value::getAsNumber() const
{
if (isNumber())
return getNumber();
else if (isString())
{
std::string s = getString();
return atof(s.c_str());
}
else if (isBoolean())
return getBoolean() ? 1.0 : 0.0;
else
return 0;
}
bool Value::getBoolean() const
{
throw Exception("Not Boolean Type");
}
bool Value::getAsBoolean() const
{
if (isBoolean())
return getBoolean();
else if (isString())
{
std::string s = getString();
if (s.size() > 0 && s == "true")
return true;
else
return false;
}
else if (isNumber())
return getNumber() != 0.0;
else
return false;
}
std::string Value::encode() const
{
throw Exception("No Encoding");
}
std::string Value::encodePretty(int indent) const
{
return encode();
}
// **** String ****
std::string String::getString() const
{
return val;
}
// Static method that can be used on other classes to do raw encoding
// of strings
std::string String::encode(const std::string &raw)
{
std::string result;
// Reserve enough space to store the original string, the quotes and
// a couple of escaped characters. If more is needed then the string
// will automatically resize
result.reserve(raw.size() + 4);
// MS VC++ 6.0 complains about push_back :-(
result += '"';
for (size_t i = 0; i < raw.size(); i++)
{
int c = (unsigned char)raw[i];
// Common case first
if (c >= 0x20 && c != '"' && c != '\\' && c < 0x7f)
result += c;
else
{
result += '\\';
if (c == '"' || c == '\\')
result += c;
else if (c == 0x08) // BS
result += 'b';
else if (c == 0x0c) // FF
result += 'f';
else if (c == 0x0a) // NL
result += 'n';
else if (c == 0x0d) // CR
result += 'r';
else if (c == 0x09) // HT
result += 't';
else
{
std::ostringstream ss;
ss << "u"
<< std::setfill('0') << std::hex << std::setw(4)
<< c
<< std::setfill(' ') << std::dec;
result += ss.str();
}
}
}
result += '"';
return result;
}
std::string String::encode() const
{
return encode(val);
}
// **** Number ****
double Number::getNumber() const
{
return val;
}
std::string Number::encode(double d)
{
#if 0
std::ostringstream ss;
ss << std::setprecision(15) << d;
return ss.str();
#else
// This is meant to automatically determine the precision required and
// it should be faster than the ostringstream
return boost::lexical_cast<std::string>(d);
#endif
}
std::string Number::encode() const
{
return encode(val);
}
// **** Boolean ****
bool Boolean::getBoolean() const
{
return val;
}
std::string Boolean::encode() const
{
if (val)
return "true";
else
return "false";
}
// **** Object ****
void Object::setProperty(const std::string &name, ValuePtr value)
{
properties[name] = value;
}
void Object::setProperty(const std::string &name,
const std::string &value)
{
properties[name].reset(new String(value));
}
void Object::setProperty(const std::string &name,
const char *value)
{
properties[name].reset(new String(value));
}
void Object::setProperty(const std::string &name, double value)
{
properties[name].reset(new Number(value));
}
void Object::setProperty(const std::string &name, long value)
{
properties[name].reset(new Number(value));
}
void Object::setProperty(const std::string &name, unsigned long value)
{
properties[name].reset(new Number(value));
}
void Object::setProperty(const std::string &name, int value)
{
properties[name].reset(new Number(value));
}
void Object::setProperty(const std::string &name, bool value)
{
properties[name].reset(new Boolean(value));
}
void Object::setProperty(const std::string &name, ObjectPtr value)
{
properties[name] = value;
}
void Object::setProperty(const std::string &name, ArrayPtr value)
{
properties[name] = value;
}
bool Object::hasProperty(const std::string &name)
{
return properties.find(name) != properties.end();
}
ValuePtr Object::getProperty(const std::string &name) const
{
Properties::const_iterator iter = properties.find(name);
if (iter == properties.end())
return ValuePtr();
return (*iter).second;
}
bool Object::getProperty(const std::string &name, std::string &value) const
{
Properties::const_iterator iter = properties.find(name);
if (iter == properties.end())
return false;
value = (*iter).second->getAsString();
return true;
}
bool Object::getProperty(const std::string &name, double &value) const
{
Properties::const_iterator iter = properties.find(name);
if (iter == properties.end())
return false;
value = (*iter).second->getAsNumber();
return true;
}
bool Object::getProperty(const std::string &name, long &value) const
{
Properties::const_iterator iter = properties.find(name);
if (iter == properties.end())
return false;
value = (*iter).second->getAsNumber();
return true;
}
bool Object::getProperty(const std::string &name, unsigned long &value) const
{
Properties::const_iterator iter = properties.find(name);
if (iter == properties.end())
return false;
value = (*iter).second->getAsNumber();
return true;
}
bool Object::getProperty(const std::string &name, int &value) const
{
Properties::const_iterator iter = properties.find(name);
if (iter == properties.end())
return false;
value = (*iter).second->getAsNumber();
return true;
}
bool Object::getProperty(const std::string &name, bool &value) const
{
Properties::const_iterator iter = properties.find(name);
if (iter == properties.end())
return false;
value = (*iter).second->getAsBoolean();
return true;
}
std::string Object::encode() const
{
std::string result;
result = "{";
Properties::const_iterator iter;
for (iter = properties.begin(); iter != properties.end(); ++iter)
{
if (iter != properties.begin())
result += ",";
result += String::encode((*iter).first) +
":" + (*iter).second->encode();
}
result += "}";
return result;
}
std::string Object::encodePretty(int indent) const
{
std::string result;
result = "{";
indent += 4;
Properties::const_iterator iter;
for (iter = properties.begin(); iter != properties.end(); ++iter)
{
if (iter != properties.begin())
result += ",";
result += "\n";
result += std::string(indent, ' ');
result += String::encode((*iter).first) +
" : " + (*iter).second->encodePretty(indent);
}
indent -= 4;
result += "\n";
result += std::string(indent, ' ');
result += "}";
return result;
}
// **** Array ****
void Array::add(ValuePtr value)
{
elements.push_back(value);
}
void Array::add(const std::string &value)
{
ValuePtr p(new String(value));
elements.push_back(p);
}
void Array::add(const char *value)
{
ValuePtr p(new String(value));
elements.push_back(p);
}
void Array::add(double value)
{
ValuePtr p(new Number(value));
elements.push_back(p);
}
void Array::add(long value)
{
ValuePtr p(new Number(value));
elements.push_back(p);
}
void Array::add(int value)
{
ValuePtr p(new Number(value));
elements.push_back(p);
}
void Array::add(bool value)
{
ValuePtr p(new Boolean(value));
elements.push_back(p);
}
void Array::add(ObjectPtr value)
{
elements.push_back(value);
}
void Array::add(ArrayPtr value)
{
elements.push_back(value);
}
size_t Array::size() const
{
return elements.size();
}
std::string Array::encode() const
{
std::string result;
result = "[";
Elements::const_iterator iter;
for (iter = elements.begin(); iter != elements.end(); ++iter)
{
if (iter != elements.begin())
result += ",";
result += (*iter)->encode();
}
result += "]";
return result;
}
std::string Array::encodePretty(int indent) const
{
std::string result;
result = "[";
Elements::const_iterator iter;
for (iter = elements.begin(); iter != elements.end(); ++iter)
{
if (iter != elements.begin())
result += ", ";
result += (*iter)->encodePretty(indent);
}
result += "]";
return result;
}
// **** Null ****
std::string Null::encode() const
{
return "null";
}
// **** Exception ****
Exception::Exception(const std::string &str)
: reason(str)
{
}
Exception::~Exception() throw()
{
}
const char * Exception::what() const throw()
{
return reason.c_str();
}
// **** ParserException *****
ParserException::ParserException(const std::string &str, size_t line, size_t column)
: Exception(str)
{
std::ostringstream ss;
ss << " at line " << line << " and column " << column;
reason += ss.str();
}
// **** Parser ****
std::vector<Parser::TokenType> Parser::tokenType;
std::vector<std::string> Parser::tokenName;
Parser::Parser(int features_)
: features(features_)
{
if (tokenType.empty())
{
// Initialise the token type array if it is empty
tokenType.reserve(256);
size_t i;
for (i = 0; i < 256; i++)
tokenType.push_back(TOK_ERROR);
tokenType['{'] = TOK_OPEN_BRACE;
tokenType['}'] = TOK_CLOSE_BRACE;
tokenType[','] = TOK_COMMA;
tokenType[':'] = TOK_COLON;
tokenType['['] = TOK_OPEN_BRACKET;
tokenType[']'] = TOK_CLOSE_BRACKET;
tokenType['"'] = TOK_DOUBLE_QUOTE;
// Only recognise the single quote token if this feature us enabled
if (features & ALLOW_SINGLE_QUOTES)
tokenType['\''] = TOK_SINGLE_QUOTE;
tokenType['-'] = TOK_NUMBER;
for(i = 0; i < 10; i++)
tokenType['0' + i] = TOK_NUMBER;
tokenType[' '] = TOK_WHITE_SPACE;
tokenType['\t'] = TOK_WHITE_SPACE;
tokenType['\n'] = TOK_WHITE_SPACE;
tokenType['\r'] = TOK_WHITE_SPACE;
char c;
for(c = 'A'; c <= 'Z'; c++)
tokenType[c] = TOK_IDENTIFIER;
for(c = 'a'; c <= 'z'; c++)
tokenType[c] = TOK_IDENTIFIER;
tokenType['_'] = TOK_IDENTIFIER;
if (features & ALLOW_COMMENTS)
tokenType['/'] = TOK_COMMENT;
}
if (tokenName.empty())
{
tokenName.push_back("open brace '{'"); // TOK_OPEN_BRACE
tokenName.push_back("close brace '}'"); // TOK_CLOSE_BRACE
tokenName.push_back("comma ','"); // TOK_COMMA
tokenName.push_back("colon ':'"); // TOK_COLON
tokenName.push_back("open bracket '('"); // TOK_OPEN_BRACKET
tokenName.push_back("close bracket ')'"); // TOK_CLOSE_BRACKET
tokenName.push_back("double quote '\"'"); // TOK_DOUBLE_QUOTE
tokenName.push_back("single quote '''"); // TOK_SINGLE_QUOTE
tokenName.push_back("number"); // TOK_NUMBER
tokenName.push_back("white space"); // TOK_WHITE_SPACE
tokenName.push_back("forward slash '/'"); // TOK_COMMENT
tokenName.push_back("identifier"); // TOK_IDENTIFIER
tokenName.push_back("end of string"); // TOK_EOS
tokenName.push_back("error"); // TOK_ERROR
}
}
ValuePtr Parser::parse(const std::string &str_)
{
// Setup the tokenizer
pos = 0;
line = 1;
column = 1;
str = str_;
ValuePtr v = parseValue();
// Explicitly check that no more input as this can happen if an extra closing brace
// causes the parser to bomb out early
getToken(TOK_EOS);
return v;
}
ValuePtr Parser::parseValue()
{
while(true)
{
char c = getChar();
TokenType t = tokenType[c];
switch(t)
{
case TOK_DOUBLE_QUOTE:
case TOK_SINGLE_QUOTE:
return parseStringValue(t);
case TOK_NUMBER:
ungetChar();
return parseNumericValue();
case TOK_OPEN_BRACE:
return parseObject();
case TOK_OPEN_BRACKET:
return parseArray();
case TOK_IDENTIFIER:
{
std::string ident = parseIdentifier();
if (ident == "true")
return ValuePtr(new Boolean(true));
else if (ident == "false")
return ValuePtr(new Boolean(false));
else if (ident == "null")
return ValuePtr(new Null);
else
throw ParserException("Illegal identifier", line, column);
}
case TOK_WHITE_SPACE:
break; // continue with next character
case TOK_COMMENT:
parseComment();
break;
default:
{
std::string err = "Unexpected '";
err += c;
err += "'";
throw ParserException(err, line, column);
}
}
}
}
std::string Parser::parseString(TokenType start_token)
{
std::string res;
while(true)
{
char c = getChar();
if (c == '\\')
{
c = getChar();
if (c == 'b')
res += 0x08;
else if (c == 'f')
res += 0x0c;
else if (c == 'n')
res += 0x0a;
else if (c == 'r')
res += 0x0d;
else if (c == 't')
res += 0x09;
else if (c == 'u')
{
int val = 0;
for (int i = 0; i < 4; i++)
{
c = getChar();
val <<= 4;
if (c >= '0' && c <= '9')
val += (c - '0');
else if (c >= 'a' && c <= 'f')
val += (c - 'a' + 0x0a);
else if (c >= 'A' && c <= 'F')
val += (c - 'A' + 0x0a);
else
throw ParserException("Illegal hex digit", line, column);
res += val;
}
}
else
// MS VC++ 6.0 will not let us use push_back here
res += c;
}
else if ((c == '"' && start_token == TOK_DOUBLE_QUOTE))
break;
else if ((c == '\'' && start_token == TOK_SINGLE_QUOTE))
break;
else
res += c;
}
return res;
}
std::string Parser::parseIdentifier()
{
// Identifier needs to get the first TOK_IDENTIFIER character
ungetChar();
std::string res;
while(true)
{
char c = getChar();
TokenType token = tokenType[c]; // Used for whitespace detection
if (token != TOK_IDENTIFIER && ! (c >= '0' && c <= '9'))
{
// When we get a non TOK_IDENTIFIER or digit character then
// we know it is end of identifer but need to put the
// char back
ungetChar();
break;
}
res += c;
}
return res;
}
ValuePtr Parser::parseStringValue(TokenType start_token)
{
return ValuePtr(new String(parseString(start_token)));
}
ValuePtr Parser::parseNumericValue()
{
std::string res;
while(true)
{
if (pos >= str.size())
break;
char c = str[pos++];
if (isspace(c))
continue;
else if ((c >= '0' && c <= '9') || c == '-' || c == '+' || c == '.' ||
c == 'e' || c == 'E')
{
res += c;
}
else
{
// Numbers dont have an explicit end character
ungetChar();
break;
}
}
double d = atof(res.c_str());
return ValuePtr(new Number(d));
}
char Parser::getChar()
{
if (pos >= str.size())
throw ParserException("Unexpected end of buffer", line, column);
char c = str[pos++];
if (c == '\n')
{
line++;
column = 1;
}
return c;
}
void Parser::ungetChar()
{
if (pos <= 0)
throw ParserException("Unexpected start of buffer", line, column);
pos--;
// Decrement line count as it will be incremented again on the next
// getChar() call
char c = str[pos];
if (c == '\n')
line--;
}
Parser::TokenType Parser::getNextToken()
{
while (pos < str.size())
{
char c = getChar();
TokenType t = tokenType[c];
if (t == TOK_WHITE_SPACE)
continue;
else if (t == TOK_COMMENT)
{
parseComment();
continue;
}
else
return t;
}
return TOK_EOS;
}
void Parser::getToken(TokenType match)
{
TokenType token = getNextToken();
if (token == match)
return;
else
throw ParserException("Expected " + tokenName[match], line, column);
}
ValuePtr Parser::parseObject()
{
Object *obj = new Object;
ValuePtr ret(obj);
while(true)
{
std::string name;
TokenType token = getNextToken();
if (token == TOK_DOUBLE_QUOTE || token == TOK_SINGLE_QUOTE)
name = parseString(token);
else if (token == TOK_IDENTIFIER &&
(features & ALLOW_UNQUOTED_FIELD_NAMES))
name = parseIdentifier();
else if (token == TOK_CLOSE_BRACE &&
(obj->properties.size() == 0 ||
(features & ALLOW_TRAILING_COMMAS)))
return ret;
else
throw ParserException("Expected string or identifier", line, column);
getToken(TOK_COLON);
ValuePtr value = parseValue();
if (obj->hasProperty(name))
throw ParserException("Duplicate property '" + name +
"' on object", line, column);
obj->setProperty(name, value);
token = getNextToken();
if (token == TOK_CLOSE_BRACE)
return ret;
else if (token != TOK_COMMA)
throw ParserException("Expected " + tokenName[TOK_COMMA] +
" or " + tokenName[TOK_CLOSE_BRACE],
line, column);
}
}
ValuePtr Parser::parseArray()
{
Array *arr = new Array;
// Wrap the raw pointer in an shared_ptr so will be deleted on return
ValuePtr ret(arr);
while(true)
{
TokenType token = getNextToken();
if (token == TOK_CLOSE_BRACKET)
{
if (arr->elements.size() != 0 &&
!(features & ALLOW_TRAILING_COMMAS))
throw ParserException("Expected value", line, column);
return ret;
}
ungetChar();
ValuePtr v = parseValue();
arr->add(v);
token = getNextToken();
if (token == TOK_CLOSE_BRACKET)
return ret;
if (token != TOK_COMMA)
throw ParserException("Expected " + tokenName[TOK_COMMA] +
" or " + tokenName[TOK_CLOSE_BRACKET],
line, column);
}
}
void Parser::parseComment()
{
char c = getChar();
if (c == '*')
{
// C style comment. Look for closing '*/'
while(true)
{
c = getChar();
// Use 'while' here instead of 'if' as we want to match
// things like '****/'
while (c == '*')
{
c = getChar();
if (c == '/')
return;
}
}
}
else if (c == '/')
{
// C++ style comment. Look for EOL
while(true)
{
c = getChar();
if (c == '\n')
return;
}
}
else
throw ParserException("Expected start of comment", line, column);
}
}