-
Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy pathhjson_decode.cpp
More file actions
831 lines (705 loc) · 20.4 KB
/
hjson_decode.cpp
File metadata and controls
831 lines (705 loc) · 20.4 KB
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
#include "hjson.h"
#include <vector>
#include <algorithm>
#include <cctype>
#include <cstring>
#include <fstream>
namespace Hjson {
class CommentInfo {
public:
bool hasComment;
// cmStart is the first char of the key, cmEnd is the first char after the key.
int cmStart, cmEnd;
};
class Parser {
public:
const unsigned char *data;
size_t dataSize;
int indexNext;
unsigned char ch;
DecoderOptions opt;
};
bool tryParseNumber(Value *pNumber, const char *text, size_t textSize, bool stopAtNext);
static Value _readValue(Parser *p);
static inline void _setComment(Value& val, void (Value::*fp)(const std::string&),
Parser *p, const CommentInfo& ci)
{
if (ci.hasComment) {
(val.*fp)(std::string(p->data + ci.cmStart, p->data + ci.cmEnd));
}
}
static inline void _setComment(Value& val, void (Value::*fp)(const std::string&),
Parser *p, const CommentInfo& ciA, const CommentInfo& ciB)
{
if (ciA.hasComment && ciB.hasComment) {
(val.*fp)(std::string(p->data + ciA.cmStart, p->data + ciA.cmEnd) +
std::string(p->data + ciB.cmStart, p->data + ciB.cmEnd));
} else {
_setComment(val, fp, p, ciA);
_setComment(val, fp, p, ciB);
}
}
static bool _next(Parser *p) {
// get the next character.
if (p->indexNext < p->dataSize) {
p->ch = p->data[p->indexNext++];
return true;
}
++p->indexNext;
p->ch = 0;
return false;
}
static bool _prev(Parser *p) {
// get the previous character.
if (p->indexNext > 1) {
p->ch = p->data[p->indexNext-- - 2];
return true;
}
return false;
}
static void _resetAt(Parser *p) {
p->indexNext = 0;
_next(p);
}
static bool _isPunctuatorChar(char c) {
return c == '{' || c == '}' || c == '[' || c == ']' || c == ',' || c == ':';
}
static std::string _errAt(Parser *p, const std::string& message) {
if (p->dataSize && p->indexNext <= p->dataSize) {
size_t decoderIndex = std::max(static_cast<size_t>(1), std::min(p->dataSize,
static_cast<size_t>(p->indexNext))) - 1;
size_t i = decoderIndex, col = 0, line = 1;
for (; i > 0 && p->data[i] != '\n'; i--) {
col++;
}
for (; i > 0; i--) {
if (p->data[i] == '\n') {
line++;
}
}
size_t samEnd = std::min((size_t)20, p->dataSize - (decoderIndex - col));
return message + " at line " + std::to_string(line) + "," +
std::to_string(col) + " >>> " + std::string((char*)p->data + decoderIndex - col, samEnd);
} else {
return message;
}
}
static unsigned char _peek(Parser *p, int offs) {
int pos = p->indexNext + offs;
if (pos >= 0 && pos < p->dataSize) {
return p->data[pos];
}
return 0;
}
static unsigned char _escapee(unsigned char c) {
switch (c)
{
case '"':
case '\'':
case '\\':
case '/':
return c;
case 'b':
return '\b';
case 'f':
return '\f';
case 'n':
return '\n';
case 'r':
return '\r';
case 't':
return '\t';
}
return 0;
}
// Parse a multiline string value.
static std::string _readMLString(Parser *p) {
// Store the string in a new vector, because the length of it might be
// different than the length in the input data.
std::vector<char> res;
int triple = 0;
// we are at ''' +1 - get indent
int indent = 0;
for (;;) {
auto c = _peek(p, -indent - 5);
if (c == 0 || c == '\n') {
break;
}
indent++;
}
auto skipIndent = [&]() {
auto skip = indent;
while (p->ch > 0 && p->ch <= ' ' && p->ch != '\n' && skip > 0) {
skip--;
_next(p);
}
};
// skip white/to (newline)
while (p->ch > 0 && p->ch <= ' ' && p->ch != '\n') {
_next(p);
}
if (p->ch == '\n') {
_next(p);
skipIndent();
}
// When parsing multiline string values, we must look for ' characters.
bool lastLf = false;
for (;;) {
if (p->ch == 0) {
throw syntax_error(_errAt(p, "Bad multiline string"));
} else if (p->ch == '\'') {
triple++;
_next(p);
if (triple == 3) {
auto sres = res.data();
if (lastLf) {
return std::string(sres, res.size() - 1); // remove last EOL
}
return std::string(sres, res.size());
}
continue;
} else {
while (triple > 0) {
res.push_back('\'');
triple--;
lastLf = false;
}
}
if (p->ch == '\n') {
res.push_back('\n');
lastLf = true;
_next(p);
skipIndent();
} else {
if (p->ch != '\r') {
res.push_back(p->ch);
lastLf = false;
}
_next(p);
}
}
}
static void _toUtf8(std::vector<char> &res, uint32_t uIn) {
if (uIn < 0x80) {
res.push_back(uIn);
} else if (uIn < 0x800) {
res.push_back(0xc0 | ((uIn >> 6) & 0x1f));
res.push_back(0x80 | (uIn & 0x3f));
} else if (uIn < 0x10000) {
res.push_back(0xe0 | ((uIn >> 12) & 0xf));
res.push_back(0x80 | ((uIn >> 6) & 0x3f));
res.push_back(0x80 | (uIn & 0x3f));
} else if (uIn < 0x110000) {
res.push_back(0xf0 | ((uIn >> 18) & 0x7));
res.push_back(0x80 | ((uIn >> 12) & 0x3f));
res.push_back(0x80 | ((uIn >> 6) & 0x3f));
res.push_back(0x80 | (uIn & 0x3f));
} else {
throw std::logic_error("Invalid unicode code point");
}
}
// Parse a string value.
// callers make sure that (ch === '"' || ch === "'")
// When parsing for string values, we must look for " and \ characters.
static std::string _readString(Parser *p, bool allowML) {
// Store the string in a new vector, because the length of it might be
// different than the length in the input data.
std::vector<char> res;
char exitCh = p->ch;
while (_next(p)) {
if (p->ch == exitCh) {
_next(p);
if (allowML && exitCh == '\'' && p->ch == '\'' && res.size() == 0) {
// ''' indicates a multiline string
_next(p);
return _readMLString(p);
} else {
return std::string(res.data(), res.size());
}
}
if (p->ch == '\\') {
unsigned char ech;
_next(p);
if (p->ch == 'u') {
uint32_t uffff = 0;
for (int i = 0; i < 4; i++) {
_next(p);
unsigned char hex;
if (p->ch >= '0' && p->ch <= '9') {
hex = p->ch - '0';
} else if (p->ch >= 'a' && p->ch <= 'f') {
hex = p->ch - 'a' + 0xa;
} else if (p->ch >= 'A' && p->ch <= 'F') {
hex = p->ch - 'A' + 0xa;
} else {
throw syntax_error(_errAt(p, std::string("Bad \\u char ") + (char)p->ch));
}
uffff = uffff * 16 + hex;
}
_toUtf8(res, uffff);
} else if (p->ch == '\n') {
// Escaped line feed is ignored (line continuation is allowed).
} else if (p->ch == '\r' && _peek(p, 0) == '\n') {
// Escaped line feed is ignored (line continuation is allowed).
_next(p);
} else if ((ech = _escapee(p->ch))) {
res.push_back(ech);
} else {
throw syntax_error(_errAt(p, std::string("Bad escape \\") + (char)p->ch));
}
} else if (p->ch == '\n' || p->ch == '\r') {
throw syntax_error(_errAt(p, "Bad string containing newline"));
} else {
res.push_back(p->ch);
}
}
throw syntax_error(_errAt(p, "Bad string"));
}
// quotes for keys are optional in Hjson
// unless they include {}[],: or whitespace.
static std::string _readKeyname(Parser *p) {
if (p->ch == '"' || p->ch == '\'') {
return _readString(p, false);
}
// keyStart is the index for the first char of the key.
size_t keyStart = p->indexNext - 1;
// keyEnd is the index for the first char after the key (i.e. not included in the key).
size_t keyEnd = keyStart;
int firstSpace = -1;
for (;;) {
if (p->ch == ':') {
if (keyEnd <= keyStart) {
throw syntax_error(_errAt(p, "Found ':' but no key name (for an empty key name use quotes)"));
} else if (firstSpace >= 0 && firstSpace != keyEnd) {
p->indexNext = firstSpace + 1;
throw syntax_error(_errAt(p, "Found whitespace in your key name (use quotes to include)"));
}
return std::string(reinterpret_cast<const char*>(p->data) + keyStart, keyEnd - keyStart);
} else if (p->ch <= ' ') {
if (p->ch == 0) {
throw syntax_error(_errAt(p, "Found EOF while looking for a key name (check your syntax)"));
}
if (firstSpace < 0) {
firstSpace = p->indexNext - 1;
}
} else {
if (_isPunctuatorChar(p->ch)) {
throw syntax_error(_errAt(p, std::string("Found '") + (char)p->ch + std::string(
"' where a key name was expected (check your syntax or use quotes if the key name includes {}[],: or whitespace)")));
}
keyEnd = p->indexNext;
}
_next(p);
}
}
static CommentInfo _white(Parser *p) {
CommentInfo ci = {
p->opt.whitespaceAsComments,
p->indexNext - 1,
0
};
while (p->ch > 0) {
// Skip whitespace.
while (p->ch > 0 && p->ch <= ' ') {
_next(p);
}
// Hjson allows comments
if (p->ch == '#' || (p->ch == '/' && _peek(p, 0) == '/')) {
if (p->opt.comments) {
ci.hasComment = true;
}
while (p->ch > 0 && p->ch != '\n') {
_next(p);
}
} else if (p->ch == '/' && _peek(p, 0) == '*') {
if (p->opt.comments) {
ci.hasComment = true;
}
_next(p);
_next(p);
while (p->ch > 0 && !(p->ch == '*' && _peek(p, 0) == '/')) {
_next(p);
}
if (p->ch > 0) {
_next(p);
_next(p);
}
} else {
break;
}
}
// cmEnd is the first char after the comment (i.e. not included in the comment).
ci.cmEnd = p->indexNext - 1;
return ci;
}
static CommentInfo _getCommentAfter(Parser *p) {
CommentInfo ci = {
p->opt.whitespaceAsComments,
p->indexNext - 1,
0
};
while (p->ch > 0) {
// Skip whitespace, but only until EOL.
while (p->ch > 0 && p->ch <= ' ' && p->ch != '\n') {
_next(p);
}
// Hjson allows comments
if (p->ch == '#' || (p->ch == '/' && _peek(p, 0) == '/')) {
if (p->opt.comments) {
ci.hasComment = true;
}
while (p->ch > 0 && p->ch != '\n') {
_next(p);
}
} else if (p->ch == '/' && _peek(p, 0) == '*') {
if (p->opt.comments) {
ci.hasComment = true;
}
_next(p);
_next(p);
while (p->ch > 0 && !(p->ch == '*' && _peek(p, 0) == '/')) {
_next(p);
}
if (p->ch > 0) {
_next(p);
_next(p);
}
} else {
break;
}
}
// cmEnd is the first char after the comment (i.e. not included in the comment).
ci.cmEnd = p->indexNext - 1;
return ci;
}
// Hjson strings can be quoteless
// returns string, true, false, or null.
static Value _readTfnns(Parser *p) {
if (_isPunctuatorChar(p->ch)) {
throw syntax_error(_errAt(p, std::string("Found a punctuator character '") +
(char)p->ch + std::string("' when expecting a quoteless string (check your syntax)")));
}
size_t valStart = p->indexNext - 1, valEnd = 0;
if (std::isspace(p->ch)) {
++valStart;
} else {
// valEnd is the first char after the value.
valEnd = p->indexNext;
}
for (;;) {
_next(p);
bool isEol = (p->ch == '\r' || p->ch == '\n' || p->ch == 0);
if (isEol ||
p->ch == ',' || p->ch == '}' || p->ch == ']' ||
p->ch == '#' ||
(p->ch == '/' && (_peek(p, 0) == '/' || _peek(p, 0) == '*')))
{
const char *pVal = reinterpret_cast<const char*>(p->data) + valStart;
size_t valLen = valEnd - valStart;
switch (*pVal)
{
case 'f':
if (valLen == 5 && !std::strncmp(pVal, "false", 5)) {
return false;
}
break;
case 'n':
if (valLen == 4 && !std::strncmp(pVal, "null", 4)) {
return Value(Type::Null);
}
break;
case 't':
if (valLen == 4 && !std::strncmp(pVal, "true", 4)) {
return true;
}
break;
default:
if (*pVal == '-' || (*pVal >= '0' && *pVal <= '9')) {
Value number;
if (tryParseNumber(&number, pVal, valLen, false)) {
return number;
}
}
}
if (isEol) {
return std::string(pVal, valLen);
}
}
if (std::isspace(p->ch)) {
if (valEnd <= valStart) {
++valStart;
}
} else {
// valEnd is the first char after the value.
valEnd = p->indexNext;
}
}
}
// Parse an array value.
// assuming ch == '['
static Value _readArray(Parser *p) {
Value array(Hjson::Type::Vector);
// Skip '['.
_next(p);
auto ciBefore = _white(p);
if (p->ch == ']') {
_setComment(array, &Value::set_comment_inside, p, ciBefore);
_next(p);
return array; // empty array
}
CommentInfo ciExtra = {};
while (p->ch > 0) {
auto elem = _readValue(p);
_setComment(elem, &Value::set_comment_before, p, ciBefore, ciExtra);
auto ciAfter = _white(p);
// in Hjson the comma is optional and trailing commas are allowed
if (p->ch == ',') {
_next(p);
// It is unlikely that someone writes a comment after the value but
// before the comma, so we include any such comment in "comment_after".
ciExtra = _white(p);
} else {
ciExtra = {};
}
if (p->ch == ']') {
auto existingAfter = elem.get_comment_after();
_setComment(elem, &Value::set_comment_after, p, ciAfter, ciExtra);
if (!existingAfter.empty()) {
elem.set_comment_after(existingAfter + elem.get_comment_after());
}
array.push_back(elem);
_next(p);
return array;
}
array.push_back(elem);
ciBefore = ciAfter;
}
throw syntax_error(_errAt(p, "End of input while parsing an array (did you forget a closing ']'?)"));
}
// Parse an object value.
static Value _readObject(Parser *p, bool withoutBraces) {
Value object(Hjson::Type::Map);
if (!withoutBraces) {
// assuming ch == '{'
_next(p);
}
auto ciBefore = _white(p);
if (p->ch == '}' && !withoutBraces) {
_setComment(object, &Value::set_comment_inside, p, ciBefore);
_next(p);
return object; // empty object
}
CommentInfo ciExtra = {};
while (p->ch > 0) {
auto key = _readKeyname(p);
if (p->opt.duplicateKeyException && object[key].defined()) {
throw syntax_error(_errAt(p, "Found duplicate of key '" + key + "'"));
}
auto ciKey = _white(p);
if (p->ch != ':') {
throw syntax_error(_errAt(p, std::string(
"Expected ':' instead of '") + (char)(p->ch) + "'"));
}
_next(p);
// duplicate keys overwrite the previous value
auto elem = _readValue(p);
_setComment(elem, &Value::set_comment_key, p, ciKey);
if (!elem.get_comment_before().empty()) {
elem.set_comment_key(elem.get_comment_key() +
elem.get_comment_before());
elem.set_comment_before("");
}
_setComment(elem, &Value::set_comment_before, p, ciBefore, ciExtra);
auto ciAfter = _white(p);
// in Hjson the comma is optional and trailing commas are allowed
if (p->ch == ',') {
_next(p);
// It is unlikely that someone writes a comment after the value but
// before the comma, so we include any such comment in "comment_after".
ciExtra = _white(p);
} else {
ciExtra = {};
}
if (p->ch == '}' && !withoutBraces) {
auto existingAfter = elem.get_comment_after();
_setComment(elem, &Value::set_comment_after, p, ciAfter, ciExtra);
if (!existingAfter.empty()) {
elem.set_comment_after(existingAfter + elem.get_comment_after());
}
object[key].assign_with_comments(std::move(elem));
_next(p);
return object;
}
object[key].assign_with_comments(std::move(elem));
ciBefore = ciAfter;
}
if (withoutBraces) {
if (object.empty()) {
_setComment(object, &Value::set_comment_inside, p, ciBefore);
} else {
_setComment(object[static_cast<int>(object.size() - 1)],
&Value::set_comment_after, p, ciBefore, ciExtra);
}
return object;
}
throw syntax_error(_errAt(p, "End of input while parsing an object (did you forget a closing '}'?)"));
}
// Parse a Hjson value. It could be an object, an array, a string, a number or a word.
static Value _readValue(Parser *p) {
Hjson::Value ret;
auto ciBefore = _white(p);
switch (p->ch) {
case '{':
ret = _readObject(p, false);
break;
case '[':
ret = _readArray(p);
break;
case '"':
case '\'':
ret = _readString(p, true);
break;
default:
ret = _readTfnns(p);
// Make sure that any comment will include preceding whitespace.
if (p->ch == '#' || p->ch == '/') {
while (_prev(p) && std::isspace(p->ch)) {}
_next(p);
}
break;
}
auto ciAfter = _getCommentAfter(p);
_setComment(ret, &Value::set_comment_before, p, ciBefore);
_setComment(ret, &Value::set_comment_after, p, ciAfter);
return ret;
}
static Value _hasTrailing(Parser *p, CommentInfo *ci) {
*ci = _white(p);
return p->ch > 0;
}
// Braces for the root object are optional
static Value _rootValue(Parser *p) {
Value ret;
std::string errMsg;
CommentInfo ciExtra;
auto ciBefore = _white(p);
switch (p->ch) {
case '{':
ret = _readObject(p, false);
if (_hasTrailing(p, &ciExtra)) {
throw syntax_error(_errAt(p, "Syntax error, found trailing characters"));
}
break;
case '[':
ret = _readArray(p);
if (_hasTrailing(p, &ciExtra)) {
throw syntax_error(_errAt(p, "Syntax error, found trailing characters"));
}
break;
}
if (!ret.defined()) {
// assume we have a root object without braces
try {
ret = _readObject(p, true);
if (_hasTrailing(p, &ciExtra)) {
// Syntax error, or maybe a single JSON value.
ret = Value();
} else if (ret.size() > 0) {
// if there were no braces, the first comment belongs to the first child
// of the root object, not to the root object itself.
_setComment(ret[0], &Value::set_comment_before, p, ciBefore);
ciBefore = CommentInfo();
}
} catch(const syntax_error& e) {
errMsg = std::string(e.what());
}
}
if (!ret.defined()) {
// test if we are dealing with a single JSON value instead (true/false/null/num/"")
_resetAt(p);
ret = _readValue(p);
if (_hasTrailing(p, &ciExtra)) {
// Syntax error.
ret = Value();
}
}
if (ret.defined()) {
_setComment(ret, &Value::set_comment_before, p, ciBefore);
auto existingAfter = ret.get_comment_after();
_setComment(ret, &Value::set_comment_after, p, ciExtra);
if (!existingAfter.empty()) {
ret.set_comment_after(existingAfter + ret.get_comment_after());
}
return ret;
}
if (!errMsg.empty()) {
throw syntax_error(errMsg);
}
throw syntax_error(_errAt(p, "Syntax error, found trailing characters"));
}
// Unmarshal parses the Hjson-encoded data and returns a tree of Values.
//
// Unmarshal uses the inverse of the encodings that Marshal uses.
//
Value Unmarshal(const char *data, size_t dataSize, const DecoderOptions& options) {
Parser parser = {
(const unsigned char*) data,
dataSize,
0,
' ',
options
};
if (parser.opt.whitespaceAsComments) {
parser.opt.comments = true;
}
_resetAt(&parser);
return _rootValue(&parser);
}
Value Unmarshal(const char *data, const DecoderOptions& options) {
if (!data) {
return Value();
}
return Unmarshal(data, std::strlen(data), options);
}
Value Unmarshal(const std::string &data, const DecoderOptions& options) {
return Unmarshal(data.c_str(), data.size(), options);
}
Value UnmarshalFromFile(const std::string &path, const DecoderOptions& options) {
std::ifstream infile(path, std::ifstream::ate | std::ifstream::binary);
if (!infile.is_open()) {
throw file_error("Could not open file '" + path + "' for reading");
}
std::string inStr;
size_t len = infile.tellg();
inStr.resize(len);
infile.seekg(0, std::ios::beg);
infile.read(&inStr[0], inStr.size());
infile.close();
while (len > 0 && inStr.at(len - 1) == '\0') {
--len;
}
if (len > 0 && inStr.at(len - 1) == '\n') {
--len;
}
if (len > 0 && inStr.at(len - 1) == '\r') {
--len;
}
return Unmarshal(inStr.c_str(), len, options);
}
StreamDecoder::StreamDecoder(Value& _v, const DecoderOptions& _o)
: v(_v), o(_o)
{
}
std::istream &operator >>(std::istream& in, StreamDecoder& sd) {
std::string inStr{ std::istreambuf_iterator<char>(in),
std::istreambuf_iterator<char>() };
sd.v.assign_with_comments(Unmarshal(inStr, sd.o));
return in;
}
std::istream &operator >>(std::istream& in, StreamDecoder&& sd) {
return operator >>(in, sd);
}
std::istream &operator >>(std::istream& in, Value& v) {
return in >> StreamDecoder(v, DecoderOptions());
}
}