/
json.hpp
1050 lines (849 loc) · 27.2 KB
/
json.hpp
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
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#ifndef __STOUT_JSON__
#define __STOUT_JSON__
// NOTE: This undef is necessary because we cannot reliably re-order the
// include statements in all cases. We define this flag globally since
// PicoJson requires it before importing <inttypes.h>. However, other
// libraries may import <inttypes.h> before we import <picojson.h>.
// Hence, we undefine the flag here to prevent the redefinition error.
#undef __STDC_FORMAT_MACROS
// We also need to define `PICOJSON_USE_INT64`, since we're
// unconditionally using the `picojson::value::get<uint64_t>()` and
// `picojson::value::is<uint64_t>()` functions below.
#define PICOJSON_USE_INT64
#include <picojson.h>
#define __STDC_FORMAT_MACROS
#include <iomanip>
#include <iostream>
#include <limits>
#include <map>
#include <string>
#include <type_traits>
#include <vector>
#include <boost/variant.hpp>
#include <stout/check.hpp>
#include <stout/foreach.hpp>
#include <stout/jsonify.hpp>
#include <stout/numify.hpp>
#include <stout/result.hpp>
#include <stout/strings.hpp>
#include <stout/try.hpp>
#include <stout/unreachable.hpp>
// TODO(benh): Replace the use of boost::variant here with our wrapper
// `Variant`.
namespace JSON {
// Implementation of the JavaScript Object Notation (JSON) grammar
// using boost::variant. We explicitly define each "type" of the
// grammar, including 'true' (json::True), 'false' (json::False), and
// 'null' (json::Null), for clarity and also because boost::variant
// "picks" the wrong type when we try and use std::string, long (or
// int), double (or float), and bool all in the same variant (while it
// does work with explicit casts, it seemed bad style to force people
// to put those casts in place). We could have avoided using
// json::String or json::Number and just used std::string and double
// respectively, but we choose to include them for completeness
// (although, this does pay a 2x cost when compiling thanks to all the
// extra template instantiations).
// Note that all of these forward declarations are not necessary
// but it serves to document the set of types which are available.
struct String;
struct Number;
struct Object;
struct Array;
struct True;
struct False;
struct Boolean;
struct Null;
struct Value;
struct String
{
String() {}
String(const char* _value) : value(_value) {}
String(const std::string& _value) : value(_value) {}
std::string value;
};
// NOTE: Due to how PicoJson parses unsigned integers, a roundtrip from Number
// to JSON and back to Number will result in:
// - a signed integer, if the value is less than or equal to INT64_MAX;
// - or a double, if the value is greater than INT64_MAX.
// See: https://github.com/kazuho/picojson/blob/rel/v1.3.0/picojson.h#L777-L781
struct Number
{
Number() : value(0) {}
template <typename T>
Number(
T _value,
typename std::enable_if<std::is_floating_point<T>::value, int>::type = 0)
: type(FLOATING), value(_value) {}
template <typename T>
Number(
T _value,
typename std::enable_if<
std::is_integral<T>::value && std::is_signed<T>::value,
int>::type = 0)
: type(SIGNED_INTEGER), signed_integer(_value) {}
template <typename T>
Number(
T _value,
typename std::enable_if<
std::is_integral<T>::value && std::is_unsigned<T>::value,
int>::type = 0)
: type(UNSIGNED_INTEGER), unsigned_integer(_value) {}
template <typename T>
T as() const
{
switch (type) {
case FLOATING:
return static_cast<T>(value);
case SIGNED_INTEGER:
return static_cast<T>(signed_integer);
case UNSIGNED_INTEGER:
return static_cast<T>(unsigned_integer);
// NOTE: By not setting a default we leverage the compiler
// errors when the enumeration is augmented to find all
// the cases we need to provide.
}
UNREACHABLE();
}
enum Type
{
FLOATING,
SIGNED_INTEGER,
UNSIGNED_INTEGER,
} type;
private:
friend struct Value;
friend struct Comparator;
friend void json(NumberWriter* writer, const Number& number);
union {
double value;
int64_t signed_integer;
uint64_t unsigned_integer;
};
};
struct Object
{
Object() = default;
Object(std::initializer_list<std::pair<const std::string, Value>> values_)
: values(values_) {}
// Returns the JSON value (specified by the type) given a "path"
// into the structure, for example:
//
// Result<JSON::Array> array = object.find<JSON::Array>("nested.array[0]");
//
// Will return 'None' if no field could be found called 'array'
// within a field called 'nested' of 'object' (where 'nested' must
// also be a JSON object).
//
// For 'null' field values, this will return 'Some(Null())' when
// looking for a matching type ('Null' or 'Value'). If looking for
// any other type (e.g. 'String', 'Object', etc), this will return
// 'None' as if the field is not present at all.
//
// Returns an error if a JSON value of the wrong type is found, or
// an intermediate JSON value is not an object that we can do a
// recursive find on.
template <typename T>
Result<T> find(const std::string& path) const;
// Returns the JSON value by indexing this object with the key. Unlike
// find(), the key is not a path into the JSON structure, it is just
// a JSON object key name.
//
// Returns 'None' if there key doesn't exist, or an error if a JSON
// value of the wrong type is found.
template <typename T>
Result<T> at(const std::string& key) const;
std::map<std::string, Value> values;
};
struct Array
{
Array() = default;
Array(std::initializer_list<Value> values_) : values(values_) {}
std::vector<Value> values;
};
struct Boolean
{
Boolean() : value(false) {}
Boolean(bool _value) : value(_value) {}
bool value;
};
// This is a helper so you can say JSON::True() instead of
// JSON::Boolean(true).
struct True : Boolean
{
True() : Boolean(true) {}
};
// This is a helper so you can say JSON::False() instead of
// JSON::Boolean(false).
struct False : Boolean
{
False() : Boolean(false) {}
};
struct Null {};
namespace internal {
// Null needs to be first so that it is the default value.
typedef boost::variant<Null,
String,
Number,
boost::recursive_wrapper<Object>,
boost::recursive_wrapper<Array>,
Boolean> Variant;
} // namespace internal {
struct Value : internal::Variant
{
// Empty constructor gets the variant default.
Value() {}
Value(bool value) : internal::Variant(JSON::Boolean(value)) {}
Value(char* value) : internal::Variant(JSON::String(value)) {}
Value(const char* value) : internal::Variant(JSON::String(value)) {}
// Arithmetic types are specifically routed through Number because
// there would be ambiguity between JSON::Bool and JSON::Number
// otherwise.
template <typename T>
Value(
const T& value,
typename std::enable_if<std::is_arithmetic<T>::value, int>::type = 0)
: internal::Variant(Number(value)) {}
// Non-arithmetic types are passed to the default constructor of
// Variant.
template <typename T>
Value(
const T& value,
typename std::enable_if<!std::is_arithmetic<T>::value, int>::type = 0)
: internal::Variant(value) {}
template <typename T>
bool is() const;
template <typename T>
const T& as() const &;
template <typename T>
T& as() &;
template <typename T>
T&& as() &&;
template <typename T>
const T&& as() const &&;
// Returns true if and only if 'other' is contained by 'this'.
// 'Other' is contained by 'this' if the following conditions are
// fulfilled:
// 1. If 'other' is a JSON object, then 'this' is also a JSON
// object, all keys of 'other' are also present in 'this' and
// the value for each key in 'this' also contain the value for
// the same key in 'other', i.e. for all keys 'k' in 'other',
// 'this[k].contains(other[k])' is true.
// 2. If 'other' is a JSON array, 'this' is also a JSON array, the
// length of both arrays is the same and each element in 'this'
// also contains the element in 'other' at the same position,
// i.e. it holds that this.length() == other.length() and
// for each i, 0 <= i < this.length,
// 'this[i].contains(other[i])'.
// 3. For all other types, 'this' is of the same type as 'other' and
// 'this == other'.
// NOTE: For a given key 'k', if 'this[k] == null' then
// 'this.contains(other)' holds if either 'k' is not present in
// 'other.keys()' or 'other[k] == null'.
// Similarly, if 'other[k] == null', 'this.contains(other)' only if
// 'this[k] == null'. This is a consequence of the containment
// definition.
bool contains(const Value& other) const;
private:
friend struct Comparator;
// A class which follows the visitor pattern and implements the
// containment rules described in the documentation of 'contains'.
// See 'bool Value::contains(const Value& other) const'.
struct ContainmentComparator : public boost::static_visitor<bool>
{
explicit ContainmentComparator(const Value& _self)
: self(_self) {}
bool operator()(const Object& other) const;
bool operator()(const Array& other) const;
bool operator()(const String& other) const;
bool operator()(const Number& other) const;
bool operator()(const Boolean& other) const;
bool operator()(const Null&) const;
private:
const Value& self;
};
};
template <typename T>
bool Value::is() const
{
const T* t = boost::get<T>(this);
return t != nullptr;
}
template <>
inline bool Value::is<Value>() const
{
return true;
}
template <typename T>
const T& Value::as() const &
{
return boost::get<T>(*this);
}
template <typename T>
T& Value::as() &
{
return boost::get<T>(*this);
}
template <typename T>
T&& Value::as() &&
{
return std::move(boost::get<T>(*this));
}
template <typename T>
const T&& Value::as() const &&
{
return std::move(boost::get<T>(*this));
}
template <>
inline const Value& Value::as<Value>() const &
{
return *this;
}
template <>
inline Value& Value::as<Value>() &
{
return *this;
}
template <typename T>
Result<T> Object::find(const std::string& path) const
{
const std::vector<std::string> names = strings::split(path, ".", 2);
if (names.empty()) {
return None();
}
std::string name = names[0];
// Determine if we have an array subscript. If so, save it but
// remove it from the name for doing the lookup.
Option<size_t> subscript = None();
size_t index = name.find('[');
if (index != std::string::npos) {
// Check for the closing bracket.
if (name.at(name.length() - 1) != ']') {
return Error("Malformed array subscript, expecting ']'");
}
// Now remove the closing bracket (last character) and everything
// before and including the opening bracket.
std::string s = name.substr(index + 1, name.length() - index - 2);
// Now numify the subscript.
Try<int> i = numify<int>(s);
if (i.isError()) {
return Error("Failed to numify array subscript '" + s + "'");
} else if (i.get() < 0) {
return Error("Array subscript '" + s + "' must be >= 0");
}
subscript = i.get();
// And finally remove the array subscript from the name.
name = name.substr(0, index);
}
std::map<std::string, Value>::const_iterator entry = values.find(name);
if (entry == values.end()) {
return None();
}
Value value = entry->second;
if (subscript.isSome()) {
if (value.is<Array>()) {
Array array = value.as<Array>();
if (subscript.get() >= array.values.size()) {
return None();
}
value = array.values[subscript.get()];
} else if (value.is<Null>()) {
return None();
} else {
// TODO(benh): Use a visitor to print out the intermediate type.
return Error("Intermediate JSON value not an array");
}
}
if (names.size() == 1) {
if (value.is<T>()) {
return value.as<T>();
} else if (value.is<Null>()) {
return None();
} else {
// TODO(benh): Use a visitor to print out the type found.
return Error("Found JSON value of wrong type");
}
}
if (!value.is<Object>()) {
// TODO(benh): Use a visitor to print out the intermediate type.
return Error("Intermediate JSON value not an object");
}
return value.as<Object>().find<T>(names[1]);
}
template <typename T>
Result<T> Object::at(const std::string& key) const
{
if (key.empty()) {
return None();
}
std::map<std::string, Value>::const_iterator entry = values.find(key);
if (entry == values.end()) {
return None();
}
Value value = entry->second;
if (!value.is<T>()) {
// TODO(benh): Use a visitor to print out the type found.
return Error("Found JSON value of wrong type");
}
return value.as<T>();
}
inline bool Value::contains(const Value& other) const
{
return boost::apply_visitor(Value::ContainmentComparator(*this), other);
}
inline bool Value::ContainmentComparator::operator()(const Object& other) const
{
if (!self.is<Object>()) {
return false;
}
// The empty set is contained in every set.
if (other.values.empty()) {
return true;
}
const Object& _self = self.as<Object>();
// All entries in 'other' should exists in 'self', which implies
// there should be at most as many entries in other as in self.
if (other.values.size() > _self.values.size()) {
return false;
}
foreachpair (const std::string& key, const Value& value, other.values) {
auto _selfIterator = _self.values.find(key);
if (_selfIterator == _self.values.end()) {
return false;
}
if (!_selfIterator->second.contains(value)) {
return false;
}
}
return true;
}
inline bool Value::ContainmentComparator::operator()(const String& other) const
{
if (!self.is<String>()) {
return false;
}
return self.as<String>().value == other.value;
}
inline bool Value::ContainmentComparator::operator()(const Number& other) const
{
if (!self.is<Number>()) {
return false;
}
// NOTE: For the following switch statements, we do not set a default
// case in order to leverage the compiler errors when the enumeration
// is augmented to find all the cases we need to provide.
// NOTE: Using '==' is unsafe for unsigned-signed integer comparisons.
// The compiler will automatically cast the signed integer to an
// unsigned integer. i.e. If the signed integer was negative, it
// might be converted to a large positive number.
// Using the '==' operator *is* safe for double-integer comparisons.
const Number& number = self.as<Number>();
switch (number.type) {
case Number::FLOATING:
switch (other.type) {
case Number::FLOATING:
return number.value == other.value;
case Number::SIGNED_INTEGER:
return number.value == other.signed_integer;
case Number::UNSIGNED_INTEGER:
return number.value == other.unsigned_integer;
}
case Number::SIGNED_INTEGER:
switch (other.type) {
case Number::FLOATING:
return number.signed_integer == other.value;
case Number::SIGNED_INTEGER:
return number.signed_integer == other.signed_integer;
case Number::UNSIGNED_INTEGER:
// See note above for why this is not a simple '==' expression.
return number.signed_integer >= 0 &&
number.as<uint64_t>() == other.unsigned_integer;
}
case Number::UNSIGNED_INTEGER:
switch (other.type) {
case Number::FLOATING:
return number.unsigned_integer == other.value;
case Number::SIGNED_INTEGER:
// See note above for why this is not a simple '==' expression.
return other.signed_integer >= 0 &&
number.unsigned_integer == other.as<uint64_t>();
case Number::UNSIGNED_INTEGER:
return number.unsigned_integer == other.unsigned_integer;
}
}
UNREACHABLE();
}
inline bool Value::ContainmentComparator::operator()(const Array& other) const
{
if (!self.is<Array>()) {
return false;
}
const Array& _self = self.as<Array>();
if (_self.values.size() != other.values.size()) {
return false;
}
for (unsigned i = 0; i < other.values.size(); ++i) {
if (!_self.values[i].contains(other.values[i])) {
return false;
}
}
return true;
}
inline bool Value::ContainmentComparator::operator()(const Boolean& other) const
{
if (!self.is<Boolean>()) {
return false;
}
return self.as<Boolean>().value == other.value;
}
inline bool Value::ContainmentComparator::operator()(const Null&) const
{
return self.is<Null>();
}
struct Comparator : boost::static_visitor<bool>
{
Comparator(const Value& _value)
: value(_value) {}
bool operator()(const Object& object) const
{
if (value.is<Object>()) {
return value.as<Object>().values == object.values;
}
return false;
}
bool operator()(const String& string) const
{
if (value.is<String>()) {
return value.as<String>().value == string.value;
}
return false;
}
bool operator()(const Number& other) const
{
// Delegate to the containment comparator.
// See Value::ContainmentComparator::operator(Number).
return Value::ContainmentComparator(value)(other);
}
bool operator()(const Array& array) const
{
if (value.is<Array>()) {
return value.as<Array>().values == array.values;
}
return false;
}
bool operator()(const Boolean& boolean) const
{
if (value.is<Boolean>()) {
return value.as<Boolean>().value == boolean.value;
}
return false;
}
bool operator()(const Null&) const
{
return value.is<Null>();
}
private:
const Value& value;
};
inline bool operator==(const Value& lhs, const Value& rhs)
{
return boost::apply_visitor(Comparator(lhs), rhs);
}
inline bool operator!=(const Value& lhs, const Value& rhs)
{
return !(lhs == rhs);
}
// The following are implementation of `json` customization points in order to
// use `JSON::*` objects with `jsonify`. This also means that `JSON::*` objects
// can be used within other `json` customization points.
//
// For example, we can use `jsonify` directly like this:
//
// std::cout << jsonify(JSON::Boolean(true));
//
// or, for a user-defined class like this:
//
// struct S
// {
// std::string name;
// JSON::Value payload;
// };
//
// void json(ObjectWriter* writer, const S& s)
// {
// writer->field("name", s.name);
// writer->field("payload", s.payload); // Printing out a `JSON::Value`!
// }
//
// S s{"mpark", JSON::Boolean(true)};
// std::cout << jsonify(s); // prints: {"name":"mpark","payload",true}
inline void json(BooleanWriter* writer, const Boolean& boolean)
{
json(writer, boolean.value);
}
inline void json(StringWriter* writer, const String& string)
{
json(writer, string.value);
}
inline void json(NumberWriter* writer, const Number& number)
{
switch (number.type) {
case Number::FLOATING:
json(writer, number.value);
break;
case Number::SIGNED_INTEGER:
json(writer, number.signed_integer);
break;
case Number::UNSIGNED_INTEGER:
json(writer, number.unsigned_integer);
break;
}
}
inline void json(ObjectWriter* writer, const Object& object)
{
json(writer, object.values);
}
inline void json(ArrayWriter* writer, const Array& array)
{
json(writer, array.values);
}
inline void json(NullWriter*, const Null&)
{
// Do nothing here since `NullWriter` will always just print `null`.
}
// Since `JSON::Value` is a `boost::variant`, we don't know what type of writer
// is required until we visit it. Therefore, we need an implementation of `json`
// which takes a `WriterProxy&&` directly, and constructs the correct writer
// after visitation.
//
// We'd prefer to implement this function similar to the below:
//
// void json(WriterProxy&& writer, const Value& value)
// {
// struct {
// void operator()(const Number& value) const {
// json(std::move(writer), value);
// }
// void operator()(const String& value) const {
// json(std::move(writer), value);
// }
// /* ... */
// } visitor;
// boost::apply_visitor(visitor, value);
// }
//
// But, `json` is invoked with `WriterProxy` and something like `JSON::Boolean`.
// The version sketched above would be ambiguous with the
// `void json(BooleanWriter*, const Boolean&)` version because both overloads
// involve a single implicit conversion. The `JSON::Boolean` overload has
// a conversion from `WriterProxy` to `BoolWriter*` and the `JSON::Value`
// overload has a conversion from `JSON::Boolean` to `JSON::Value`. In order to
// prefer the overloads such as the one for `JSON::Boolean`, we disallow the
// implicit conversion to `JSON::Value` by declaring as a template.
//
// TODO(mpark): Properly introduce a notion of deferred choice of writers.
// For example, when trying to print a `variant<int, string>` as the value,
// we could take something like a `Writer<Number, String>` which can be turned
// into either a `NumberWriter*` or `StringWriter*`.
template <
typename T,
typename std::enable_if<std::is_same<T, Value>::value, int>::type = 0>
void json(WriterProxy&& writer, const T& value)
{
struct
{
using result_type = void;
void operator()(const Boolean& value) const
{
json(std::move(writer_), value);
}
void operator()(const String& value) const
{
json(std::move(writer_), value);
}
void operator()(const Number& value) const
{
json(std::move(writer_), value);
}
void operator()(const Object& value) const
{
json(std::move(writer_), value);
}
void operator()(const Array& value) const
{
json(std::move(writer_), value);
}
void operator()(const Null& value) const
{
json(std::move(writer_), value);
}
WriterProxy&& writer_;
} visitor{std::move(writer)};
boost::apply_visitor(visitor, value);
}
inline std::ostream& operator<<(std::ostream& stream, const Boolean& boolean)
{
return stream << jsonify(boolean);
}
inline std::ostream& operator<<(std::ostream& stream, const String& string)
{
return stream << jsonify(string);
}
inline std::ostream& operator<<(std::ostream& stream, const Number& number)
{
return stream << jsonify(number);
}
inline std::ostream& operator<<(std::ostream& stream, const Object& object)
{
return stream << jsonify(object);
}
inline std::ostream& operator<<(std::ostream& stream, const Array& array)
{
return stream << jsonify(array);
}
inline std::ostream& operator<<(std::ostream& stream, const Null& null)
{
return stream << jsonify(null);
}
namespace internal {
// "Depth" is counted downwards to stay closer to the analogous
// implementation in PicoJSON.
constexpr size_t STOUT_JSON_MAX_DEPTH = 200;
// Our implementation of picojson's parsing context that allows
// us to parse directly into our JSON::Value.
//
// https://github.com/kazuho/picojson/blob/v1.3.0/picojson.h#L820-L870
class ParseContext {
public:
ParseContext(Value* _value, size_t _depth = STOUT_JSON_MAX_DEPTH)
: value(_value), depth(_depth) {}
ParseContext(const ParseContext&) = delete;
ParseContext &operator=(const ParseContext&) = delete;
bool set_null() { *value = Null(); return true; }
bool set_bool(bool b) { *value = Boolean(b); return true; }
bool set_int64(int64_t i) { *value = Number(i); return true; }
bool set_number(double f) {
// We take a trip through picojson::value here because it
// is where the validation takes place (i.e. it throws):
// https://github.com/kazuho/picojson/issues/94
// https://github.com/kazuho/picojson/blob/v1.3.0/picojson.h#L195-L208
picojson::value v(f);
*value = Number(v.get<double>());
return true;
}
template <typename Iter>
bool parse_string(picojson::input<Iter>& in) {
*value = String();
return picojson::_parse_string(value->as<String>().value, in);
}
bool parse_array_start() {
if (depth <= 0) {
return false;
}
--depth;
*value = Array();
return true;
}
template <typename Iter>
bool parse_array_item(picojson::input<Iter>& in, size_t) {
Array& array = value->as<Array>();
array.values.push_back(Value());
ParseContext context(&array.values.back(), depth);
return picojson::_parse(context, in);
}
bool parse_array_stop(size_t) {
++depth;
return true;
}
bool parse_object_start() {
if (depth <= 0) {
return false;
}
--depth;
*value = Object();
return true;
}
template <typename Iter>
bool parse_object_item(picojson::input<Iter>& in, const std::string& key) {
Object& object = value->as<Object>();
ParseContext context(&object.values[key], depth);
return picojson::_parse(context, in);
}
bool parse_object_stop() {
++depth;
return true;
}
Value* value;
size_t depth;
};
} // namespace internal {
inline Try<Value> parse(const std::string& s)
{
const char* parseBegin = s.c_str();
Value value;
std::string error;
// Because PicoJson supports repeated parsing of multiple objects/arrays in a
// stream, it will quietly ignore trailing non-whitespace characters. We would
// rather throw an error, however, so use `last_char` to check for this.
//
// TODO(alexr): Address cases when `s` is empty or consists only of whitespace
// characters.
const char* lastVisibleChar =
parseBegin + s.find_last_not_of(strings::WHITESPACE);
// Parse the string, returning a pointer to the character immediately
// following the last one parsed. Convert exceptions to `Error`s.
//
// TODO(alexr): Remove `try-catch` wrapper once picojson stops throwing
// on parsing, see https://github.com/kazuho/picojson/issues/94
const char* parseEnd;
try {
internal::ParseContext context(&value);