-
Notifications
You must be signed in to change notification settings - Fork 0
/
Jzon.cpp
1189 lines (1103 loc) · 26.3 KB
/
Jzon.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
/*
Copyright (c) 2015 Johannes Häggqvist
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
#ifdef JZON_DLL
# if defined _WIN32 || defined __CYGWIN__
# define JZON_API __declspec(dllexport)
# define JZON_STL_EXTERN
# endif
#endif
#include "Jzon.h"
#include <sstream>
#include <fstream>
#include <stack>
#include <algorithm>
#include <cassert>
namespace Jzon
{
namespace
{
inline bool isWhitespace(char c)
{
return (c == '\n' || c == ' ' || c == '\t' || c == '\r' || c == '\f');
}
const char charsUnescaped[] = { '\\' , '/' , '\"' , '\n' , '\t' , '\b' , '\f' , '\r' };
const char *charsEscaped[] = { "\\\\", "\\/", "\\\"", "\\n", "\\t", "\\b", "\\f", "\\r" };
const unsigned int numEscapeChars = 8;
const char nullUnescaped = '\0';
const char *nullEscaped = "\0\0";
const char *getEscaped(const char c)
{
for (unsigned int i = 0; i < numEscapeChars; ++i)
{
const char &ue = charsUnescaped[i];
if (c == ue)
{
return charsEscaped[i];
}
}
return nullEscaped;
}
char getUnescaped(const char c1, const char c2)
{
for (unsigned int i = 0; i < numEscapeChars; ++i)
{
const char *e = charsEscaped[i];
if (c1 == e[0] && c2 == e[1])
{
return charsUnescaped[i];
}
}
return nullUnescaped;
}
}
Node::Node() : data(NULL)
{
}
Node::Node(Type type) : data(NULL)
{
if (type != T_INVALID)
{
data = new Data(type);
}
}
Node::Node(const Node &other) : data(other.data)
{
if (data != NULL)
{
data->addRef();
}
}
Node::Node(Type type, const std::string &value) : data(new Data(T_NULL)) { set(type, value); }
Node::Node(const std::string &value) : data(new Data(T_STRING)) { set(value); }
Node::Node(const char *value) : data(new Data(T_STRING)) { set(value); }
Node::Node(int value) : data(new Data(T_NUMBER)) { set(value); }
Node::Node(unsigned int value) : data(new Data(T_NUMBER)) { set(value); }
Node::Node(long long value) : data(new Data(T_NUMBER)) { set(value); }
Node::Node(unsigned long long value) : data(new Data(T_NUMBER)) { set(value); }
Node::Node(float value) : data(new Data(T_NUMBER)) { set(value); }
Node::Node(double value) : data(new Data(T_NUMBER)) { set(value); }
Node::Node(bool value) : data(new Data(T_BOOL)) { set(value); }
Node::~Node()
{
if (data != NULL && data->release())
{
delete data;
data = NULL;
}
}
void Node::detach()
{
if (data != NULL && data->refCount > 1)
{
Data *newData = new Data(*data);
if (data->release())
{
delete data;
}
data = newData;
}
}
std::string Node::toString(const std::string &def) const
{
if (isValue())
{
if (isNull())
{
return std::string("null");
}
else
{
return data->valueStr;
}
}
else
{
return def;
}
}
#define GET_NUMBER(T) \
if (isNumber())\
{\
std::stringstream sstr(data->valueStr);\
T val;\
sstr >> val;\
return val;\
}\
else\
{\
return def;\
}
int Node::toInt(int def) const { GET_NUMBER(int) }
float Node::toFloat(float def) const { GET_NUMBER(float) }
double Node::toDouble(double def) const { GET_NUMBER(double) }
#undef GET_NUMBER
bool Node::toBool(bool def) const
{
if (isBool())
{
return (data->valueStr == "true");
}
else
{
return def;
}
}
void Node::setNull()
{
if (isValue())
{
detach();
data->type = T_NULL;
data->valueStr.clear();
}
}
void Node::set(Type type, const std::string &value)
{
if (isValue() && (type == T_NULL || type == T_STRING || type == T_NUMBER || type == T_BOOL))
{
detach();
data->type = type;
if (type == T_STRING)
{
data->valueStr = unescapeString(value);
}
else
{
data->valueStr = value;
}
}
}
void Node::set(const std::string &value)
{
if (isValue())
{
detach();
data->type = T_STRING;
data->valueStr = unescapeString(value);
}
}
void Node::set(const char *value)
{
if (isValue())
{
detach();
data->type = T_STRING;
data->valueStr = unescapeString(std::string(value));
}
}
#define SET_NUMBER \
if (isValue())\
{\
detach();\
data->type = T_NUMBER;\
std::stringstream sstr;\
sstr << value;\
data->valueStr = sstr.str();\
}
void Node::set(int value) { SET_NUMBER }
void Node::set(unsigned int value) { SET_NUMBER }
void Node::set(long long value) { SET_NUMBER }
void Node::set(unsigned long long value) { SET_NUMBER }
void Node::set(float value) { SET_NUMBER }
void Node::set(double value) { SET_NUMBER }
#undef SET_NUMBER
void Node::set(bool value)
{
if (isValue())
{
detach();
data->type = T_BOOL;
data->valueStr = (value ? "true" : "false");
}
}
Node &Node::operator=(const Node &rhs)
{
if (this != &rhs)
{
if (data != NULL && data->release())
{
delete data;
}
data = rhs.data;
if (data != NULL)
{
data->addRef();
}
}
return *this;
}
Node &Node::operator=(const std::string &rhs) { set(rhs); return *this; }
Node &Node::operator=(const char *rhs) { set(rhs); return *this; }
Node &Node::operator=(int rhs) { set(rhs); return *this; }
Node &Node::operator=(unsigned int rhs) { set(rhs); return *this; }
Node &Node::operator=(long long rhs) { set(rhs); return *this; }
Node &Node::operator=(unsigned long long rhs) { set(rhs); return *this; }
Node &Node::operator=(float rhs) { set(rhs); return *this; }
Node &Node::operator=(double rhs) { set(rhs); return *this; }
Node &Node::operator=(bool rhs) { set(rhs); return *this; }
void Node::add(const Node &node)
{
if (isArray())
{
detach();
data->children.push_back(std::make_pair(std::string(), node));
}
}
void Node::add(const std::string &name, const Node &node)
{
if (isObject())
{
detach();
data->children.push_back(std::make_pair(name, node));
}
}
void Node::append(const Node &node)
{
if ((isObject() && node.isObject()) || (isArray() && node.isArray()))
{
detach();
data->children.insert(data->children.end(), node.data->children.begin(), node.data->children.end());
}
}
void Node::remove(size_t index)
{
if (isContainer() && index < data->children.size())
{
detach();
NamedNodeList::iterator it = data->children.begin()+index;
data->children.erase(it);
}
}
void Node::remove(const std::string &name)
{
if (isObject())
{
detach();
NamedNodeList &children = data->children;
for (NamedNodeList::iterator it = children.begin(); it != children.end(); ++it)
{
if ((*it).first == name)
{
children.erase(it);
break;
}
}
}
}
void Node::clear()
{
if (data != NULL && !data->children.empty())
{
detach();
data->children.clear();
}
}
bool Node::has(const std::string &name) const
{
if (isObject())
{
NamedNodeList &children = data->children;
for (NamedNodeList::const_iterator it = children.begin(); it != children.end(); ++it)
{
if ((*it).first == name)
{
return true;
}
}
}
return false;
}
size_t Node::getCount() const
{
return data != NULL ? data->children.size() : 0;
}
Node Node::get(const std::string &name) const
{
if (isObject())
{
NamedNodeList &children = data->children;
for (NamedNodeList::const_iterator it = children.begin(); it != children.end(); ++it)
{
if ((*it).first == name)
{
return (*it).second;
}
}
}
return Node(T_INVALID);
}
Node Node::get(size_t index) const
{
if (isContainer() && index < data->children.size())
{
return data->children.at(index).second;
}
return Node(T_INVALID);
}
Node::iterator Node::begin()
{
if (data != NULL && !data->children.empty())
return Node::iterator(&data->children.front());
else
return Node::iterator(NULL);
}
Node::const_iterator Node::begin() const
{
if (data != NULL && !data->children.empty())
return Node::const_iterator(&data->children.front());
else
return Node::const_iterator(NULL);
}
Node::iterator Node::end()
{
if (data != NULL && !data->children.empty())
return Node::iterator(&data->children.back()+1);
else
return Node::iterator(NULL);
}
Node::const_iterator Node::end() const
{
if (data != NULL && !data->children.empty())
return Node::const_iterator(&data->children.back()+1);
else
return Node::const_iterator(NULL);
}
bool Node::operator==(const Node &other) const
{
return (
(data == other.data) ||
(isValue() && (data->type == other.data->type)&&(data->valueStr == other.data->valueStr)));
}
bool Node::operator!=(const Node &other) const
{
return !(*this == other);
}
Node::Data::Data(Type type) : refCount(1), type(type)
{
}
Node::Data::Data(const Data &other) : refCount(1), type(other.type), valueStr(other.valueStr), children(other.children)
{
}
Node::Data::~Data()
{
assert(refCount == 0);
}
void Node::Data::addRef()
{
++refCount;
}
bool Node::Data::release()
{
return (--refCount == 0);
}
std::string escapeString(const std::string &value)
{
std::string escaped;
escaped.reserve(value.length());
for (std::string::const_iterator it = value.begin(); it != value.end(); ++it)
{
const char &c = (*it);
const char *a = getEscaped(c);
if (a[0] != '\0')
{
escaped += a[0];
escaped += a[1];
}
else
{
escaped += c;
}
}
return escaped;
}
std::string unescapeString(const std::string &value)
{
std::string unescaped;
for (std::string::const_iterator it = value.begin(); it != value.end(); ++it)
{
const char c = (*it);
char c2 = '\0';
if (it+1 != value.end())
c2 = *(it+1);
const char a = getUnescaped(c, c2);
if (a != '\0')
{
unescaped += a;
if (it+1 != value.end())
++it;
}
else
{
unescaped += c;
}
}
return unescaped;
}
Node invalid()
{
return Node(Node::T_INVALID);
}
Node null()
{
return Node(Node::T_NULL);
}
Node object()
{
return Node(Node::T_OBJECT);
}
Node array()
{
return Node(Node::T_ARRAY);
}
Writer::Writer(const Format &format)
{
setFormat(format);
}
Writer::~Writer()
{
}
void Writer::setFormat(const Format &format)
{
this->format = format;
indentationChar = (format.useTabs ? '\t' : ' ');
spacing = (format.spacing ? " " : "");
newline = (format.newline ? "\n" : spacing);
}
void Writer::writeStream(const Node &node, std::ostream &stream) const
{
writeNode(node, 0, stream);
}
void Writer::writeString(const Node &node, std::string &json) const
{
std::ostringstream stream(json);
writeStream(node, stream);
json = stream.str();
}
void Writer::writeFile(const Node &node, const std::string &filename) const
{
std::ofstream stream(filename.c_str(), std::ios::out | std::ios::trunc);
writeStream(node, stream);
}
void Writer::writeNode(const Node &node, unsigned int level, std::ostream &stream) const
{
switch (node.getType())
{
case Node::T_INVALID: break;
case Node::T_OBJECT: writeObject(node, level, stream); break;
case Node::T_ARRAY: writeArray(node, level, stream); break;
case Node::T_NULL: // Fallthrough
case Node::T_STRING: // Fallthrough
case Node::T_NUMBER: // Fallthrough
case Node::T_BOOL: writeValue(node, stream); break;
}
}
void Writer::writeObject(const Node &node, unsigned int level, std::ostream &stream) const
{
stream << "{" << newline;
for (Node::const_iterator it = node.begin(); it != node.end(); ++it)
{
const std::string &name = (*it).first;
const Node &value = (*it).second;
if (it != node.begin())
stream << "," << newline;
stream << getIndentation(level+1) << "\""<<name<<"\"" << ":" << spacing;
writeNode(value, level+1, stream);
}
stream << newline << getIndentation(level) << "}";
}
void Writer::writeArray(const Node &node, unsigned int level, std::ostream &stream) const
{
stream << "[" << newline;
for (Node::const_iterator it = node.begin(); it != node.end(); ++it)
{
const Node &value = (*it).second;
if (it != node.begin())
stream << "," << newline;
stream << getIndentation(level+1);
writeNode(value, level+1, stream);
}
stream << newline << getIndentation(level) << "]";
}
void Writer::writeValue(const Node &node, std::ostream &stream) const
{
if (node.isString())
{
stream << "\""<<escapeString(node.toString())<<"\"";
}
else
{
stream << node.toString();
}
}
std::string Writer::getIndentation(unsigned int level) const
{
if (!format.newline)
{
return "";
}
else
{
return std::string(format.indentSize * level, indentationChar);
}
}
StreamWriter::StreamWriter(std::ostream & stream, const Format & format)
: stream(stream), first(true)
{
this->format = format;
indentationChar = (format.useTabs ? '\t' : ' ');
spacing = (format.spacing ? " " : "");
newline = (format.newline ? "\n" : spacing);
}
StreamWriter::~StreamWriter()
{
}
void StreamWriter::startObject()
{
if (!isComplete() && streamStack.back() == StreamState::SS_OBJECT)
throw "Object members must be properties";
if (isComplete() && !first)
throw "Root JSON is complete";
if (!first) stream << "," << newline;
stream << getIndentation(streamStack.size()) << "{" << newline;
streamStack.push_back(StreamState::SS_OBJECT);
first = true;
}
void StreamWriter::startObject(const std::string & name)
{
if (!isComplete() && streamStack.back() != StreamState::SS_OBJECT)
throw "Cannot create property outside of an object";
if (isComplete() && !first)
throw "Root JSON is complete";
if (!first) stream << "," << newline;
stream << getIndentation(streamStack.size()) << "\"" << name << "\":" << spacing << "{" << newline;
streamStack.push_back(StreamState::SS_OBJECT);
first = true;
}
void StreamWriter::endObject()
{
if (streamStack.back() != StreamState::SS_OBJECT)
throw "Cannot end object while not in object";
streamStack.pop_back();
stream << newline << getIndentation(streamStack.size()) << "}";
first = false;
}
void StreamWriter::startArray()
{
if (!isComplete() && streamStack.back() == StreamState::SS_OBJECT)
throw "Object members must be properties";
if (isComplete() && !first)
throw "Root JSON is complete";
if (!first) stream << "," << newline;
stream << getIndentation(streamStack.size()) << "[" << newline;
streamStack.push_back(StreamState::SS_ARRAY);
first = true;
}
void StreamWriter::startArray(const std::string & name)
{
if (!isComplete() && streamStack.back() != StreamState::SS_OBJECT)
throw "Cannot create property outside of an object";
if (isComplete() && !first)
throw "Root JSON is complete";
if (!first) stream << "," << newline;
stream << getIndentation(streamStack.size()) << "\"" << name << "\":" << spacing << "[" << newline;
streamStack.push_back(StreamState::SS_ARRAY);
first = true;
}
void StreamWriter::endArray()
{
if (streamStack.back() != StreamState::SS_ARRAY)
throw "Cannot end array while not in array";
streamStack.pop_back();
stream << newline << getIndentation(streamStack.size()) << "]";
first = false;
}
void StreamWriter::addNode(const Node & node)
{
if (!isComplete() && streamStack.back() == StreamState::SS_OBJECT)
throw "Object members must be properties";
if (isComplete() && !first)
throw "Root JSON is complete";
if (!first) stream << "," << newline;
stream << getIndentation(streamStack.size());
writeNode(node, streamStack.size());
first = false;
}
void StreamWriter::addNode(const std::string & name, const Node & node)
{
if (!isComplete() && streamStack.back() != StreamState::SS_OBJECT)
throw "Cannot create property outside of an object";
if (isComplete() && !first)
throw "Root JSON is complete";
if (!first) stream << "," << newline;
stream << getIndentation(streamStack.size()) << "\"" << name << "\":" << spacing;
writeNode(node, streamStack.size());
first = false;
}
bool StreamWriter::isComplete()
{
return streamStack.size() == 0;
}
std::string StreamWriter::getIndentation(unsigned int level) const
{
if (!format.newline) return "";
else return std::string(format.indentSize * level, indentationChar);
}
void StreamWriter::writeNode(const Node& node, unsigned int level) const
{
switch (node.getType())
{
case Node::T_INVALID: break;
case Node::T_OBJECT: writeObject(node, level); break;
case Node::T_ARRAY: writeArray(node, level); break;
case Node::T_NULL: // Fallthrough
case Node::T_STRING: // Fallthrough
case Node::T_NUMBER: // Fallthrough
case Node::T_BOOL: writeValue(node); break;
}
}
void StreamWriter::writeObject(const Node& node, unsigned int level) const
{
stream << "{" << newline;
for (Node::const_iterator it = node.begin(); it != node.end(); ++it)
{
const std::string& name = (*it).first;
const Node& value = (*it).second;
if (it != node.begin())
stream << "," << newline;
stream << getIndentation(level + 1) << "\"" << name << "\"" << ":" << spacing;
writeNode(value, level + 1);
}
stream << newline << getIndentation(level) << "}";
}
void StreamWriter::writeArray(const Node& node, unsigned int level) const
{
stream << "[" << newline;
for (Node::const_iterator it = node.begin(); it != node.end(); ++it)
{
const Node& value = (*it).second;
if (it != node.begin())
stream << "," << newline;
stream << getIndentation(level + 1);
writeNode(value, level + 1);
}
stream << newline << getIndentation(level) << "]";
}
void StreamWriter::writeValue(const Node& node) const
{
if (node.isString())
{
stream << "\"" << escapeString(node.toString()) << "\"";
}
else
{
stream << node.toString();
}
}
Parser::Parser()
{
}
Parser::~Parser()
{
}
Node Parser::parseStream(std::istream &stream)
{
TokenQueue tokens;
DataQueue data;
tokenize(stream, tokens, data);
Node node = assemble(tokens, data);
return node;
}
Node Parser::parseString(const std::string &json)
{
std::istringstream stream(json);
return parseStream(stream);
}
Node Parser::parseFile(const std::string &filename)
{
std::ifstream stream(filename.c_str(), std::ios::in);
return parseStream(stream);
}
const std::string &Parser::getError() const
{
return error;
}
void Parser::tokenize(std::istream &stream, TokenQueue &tokens, DataQueue &data)
{
Token token = T_UNKNOWN;
std::string valueBuffer;
bool saveBuffer;
char c = '\0';
while (stream.peek() != std::char_traits<char>::eof())
{
stream.get(c);
if (isWhitespace(c))
continue;
saveBuffer = true;
switch (c)
{
case '{':
{
token = T_OBJ_BEGIN;
break;
}
case '}':
{
token = T_OBJ_END;
break;
}
case '[':
{
token = T_ARRAY_BEGIN;
break;
}
case ']':
{
token = T_ARRAY_END;
break;
}
case ',':
{
token = T_SEPARATOR_NODE;
break;
}
case ':':
{
token = T_SEPARATOR_NAME;
break;
}
case '"':
{
token = T_VALUE;
readString(stream, data);
break;
}
case '/':
{
char p = static_cast<char>(stream.peek());
if (p == '*')
{
jumpToCommentEnd(stream);
saveBuffer = false;
break;
}
else if (p == '/')
{
jumpToNext('\n', stream);
saveBuffer = false;
break;
}
// Intentional fallthrough
}
default:
{
valueBuffer += c;
saveBuffer = false;
break;
}
}
if ((saveBuffer || stream.peek() == std::char_traits<char>::eof()) && (!valueBuffer.empty())) // Always save buffer on the last character
{
if (interpretValue(valueBuffer, data))
{
tokens.push(T_VALUE);
}
else
{
// Store the unknown token, so we can show it to the user
data.push(std::make_pair(Node::T_STRING, valueBuffer));
tokens.push(T_UNKNOWN);
}
valueBuffer.clear();
}
// Push the token last so that any data
// will get pushed first from above.
// If saveBuffer is false, it means that
// we are in the middle of a value, so we
// don't want to push any tokens now.
if (saveBuffer)
{
tokens.push(token);
}
}
}
Node Parser::assemble(TokenQueue &tokens, DataQueue &data)
{
std::stack<NamedNode> nodeStack;
Node root(Node::T_INVALID);
std::string nextName = "";
Token token;
while (!tokens.empty())
{
token = tokens.front();
tokens.pop();
switch (token)
{
case T_UNKNOWN:
{
const std::string &unknownToken = data.front().second;
error = "Unknown token: "+unknownToken;
data.pop();
return Node(Node::T_INVALID);
}
case T_OBJ_BEGIN:
{
nodeStack.push(std::make_pair(nextName, object()));
nextName.clear();
break;
}
case T_ARRAY_BEGIN:
{
nodeStack.push(std::make_pair(nextName, array()));
nextName.clear();
break;
}
case T_OBJ_END:
case T_ARRAY_END:
{
if (nodeStack.empty())
{
error = "Found end of object or array without beginning";
return Node(Node::T_INVALID);
}
if (token == T_OBJ_END && !nodeStack.top().second.isObject())
{
error = "Mismatched end and beginning of object";
return Node(Node::T_INVALID);
}
if (token == T_ARRAY_END && !nodeStack.top().second.isArray())
{
error = "Mismatched end and beginning of array";
return Node(Node::T_INVALID);
}
std::string nodeName = nodeStack.top().first;
Node node = nodeStack.top().second;
nodeStack.pop();
if (!nodeStack.empty())
{
Node &stackTop = nodeStack.top().second;
if (stackTop.isObject())
{
stackTop.add(nodeName, node);
}
else if (stackTop.isArray())
{
stackTop.add(node);
}
else
{
error = "Can only add elements to objects and arrays";
return Node(Node::T_INVALID);
}
}
else
{
root = node;
}
break;
}
case T_VALUE:
{
if (data.empty())
{
error = "Missing data for value";
return Node(Node::T_INVALID);
}
const std::pair<Node::Type, std::string> &dataPair = data.front();
if (!tokens.empty() && tokens.front() == T_SEPARATOR_NAME)
{
tokens.pop();
if (dataPair.first != Node::T_STRING)
{
error = "A name has to be a string";
return Node(Node::T_INVALID);
}
else
{