forked from kohler/masstree-beta
-
Notifications
You must be signed in to change notification settings - Fork 1
/
json.hh
3210 lines (2874 loc) · 89.3 KB
/
json.hh
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
/* Masstree
* Eddie Kohler, Yandong Mao, Robert Morris
* Copyright (c) 2012-2014 President and Fellows of Harvard College
* Copyright (c) 2012-2014 Massachusetts Institute of Technology
*
* 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, subject to the conditions
* listed in the Masstree LICENSE file. These conditions include: you must
* preserve this copyright notice, and you cannot mention the copyright
* holders in advertising related to the Software without their permission.
* The Software is provided WITHOUT ANY WARRANTY, EXPRESS OR IMPLIED. This
* notice is a summary of the Masstree LICENSE file; the license in that file
* is legally binding.
*/
// -*- c-basic-offset: 4 -*-
#ifndef JSON_HH
#define JSON_HH
#include "straccum.hh"
#include "str.hh"
#include <vector>
#include <utility>
#include <stdlib.h>
namespace lcdf {
template <typename P> class Json_proxy_base;
template <typename T> class Json_object_proxy;
template <typename T> class Json_object_str_proxy;
template <typename T> class Json_array_proxy;
class Json_get_proxy;
template <typename T, size_t S = sizeof(String::rep_type) - sizeof(T)>
struct Json_rep_item;
template <typename T> struct Json_rep_item<T, 4> {
T x;
int type;
};
template <typename T> struct Json_rep_item<T, 8> {
T x;
int padding;
int type;
};
class Json {
enum json_type { // order matters
j_string = -1, j_null = 0,
j_array = 1, j_object = 2,
j_int = 3, j_unsigned = 4, j_double = 5, j_bool = 6
};
public:
struct null_t {
inline constexpr null_t() { }
};
static const null_t null;
static const Json null_json;
typedef int size_type;
typedef std::pair<const String, Json> object_value_type;
class object_iterator;
class const_object_iterator;
typedef Json array_value_type;
class array_iterator;
class const_array_iterator;
class iterator;
class const_iterator;
typedef bool (Json::*unspecified_bool_type)() const;
class unparse_manipulator;
class streaming_parser;
// Constructors
inline Json();
inline Json(const Json& x);
template <typename P> inline Json(const Json_proxy_base<P>& x);
#if HAVE_CXX_RVALUE_REFERENCES
inline Json(Json&& x);
#endif
inline Json(const null_t& x);
inline Json(int x);
inline Json(unsigned x);
inline Json(long x);
inline Json(unsigned long x);
inline Json(long long x);
inline Json(unsigned long long x);
inline Json(double x);
inline Json(bool x);
inline Json(const String& x);
inline Json(const std::string& x);
inline Json(Str x);
inline Json(const char* x);
template <typename T> inline Json(const std::vector<T>& x);
template <typename T> inline Json(T first, T last);
inline ~Json();
static inline const Json& make_null();
static inline Json make_array();
static inline Json make_array_reserve(int n);
template <typename... Args>
static inline Json array(Args&&... rest);
static inline Json make_object();
template <typename... Args>
static inline Json object(Args&&... rest);
static inline Json make_string(const String& x);
static inline Json make_string(const std::string& x);
static inline Json make_string(const char* s, int len);
// Type information
inline bool truthy() const;
inline bool falsy() const;
inline operator unspecified_bool_type() const;
inline bool operator!() const;
inline bool is_null() const;
inline bool is_int() const;
inline bool is_i() const;
inline bool is_unsigned() const;
inline bool is_u() const;
inline bool is_signed() const;
inline bool is_nonnegint() const;
inline bool is_double() const;
inline bool is_d() const;
inline bool is_number() const;
inline bool is_n() const;
inline bool is_bool() const;
inline bool is_b() const;
inline bool is_string() const;
inline bool is_s() const;
inline bool is_array() const;
inline bool is_a() const;
inline bool is_object() const;
inline bool is_o() const;
inline bool is_primitive() const;
inline bool empty() const;
inline size_type size() const;
inline bool shared() const;
void clear();
// Primitive extractors
inline int64_t to_i() const;
inline uint64_t to_u() const;
inline uint64_t to_u64() const;
inline bool to_i(int& x) const;
inline bool to_i(unsigned& x) const;
inline bool to_i(long& x) const;
inline bool to_i(unsigned long& x) const;
inline bool to_i(long long& x) const;
inline bool to_i(unsigned long long& x) const;
inline int64_t as_i() const;
inline int64_t as_i(int64_t default_value) const;
inline uint64_t as_u() const;
inline uint64_t as_u(uint64_t default_value) const;
inline double to_d() const;
inline bool to_d(double& x) const;
inline double as_d() const;
inline double as_d(double default_value) const;
inline bool to_b() const;
inline bool to_b(bool& x) const;
inline bool as_b() const;
inline bool as_b(bool default_value) const;
inline String to_s() const;
inline bool to_s(Str& x) const;
inline bool to_s(String& x) const;
inline String& as_s();
inline const String& as_s() const;
inline const String& as_s(const String& default_value) const;
// Object methods
inline size_type count(Str key) const;
inline const Json& get(Str key) const;
inline Json& get_insert(const String& key);
inline Json& get_insert(Str key);
inline Json& get_insert(const char* key);
inline long get_i(Str key) const;
inline double get_d(Str key) const;
inline bool get_b(Str key) const;
inline String get_s(Str key) const;
inline const Json_get_proxy get(Str key, Json& x) const;
inline const Json_get_proxy get(Str key, int& x) const;
inline const Json_get_proxy get(Str key, unsigned& x) const;
inline const Json_get_proxy get(Str key, long& x) const;
inline const Json_get_proxy get(Str key, unsigned long& x) const;
inline const Json_get_proxy get(Str key, long long& x) const;
inline const Json_get_proxy get(Str key, unsigned long long& x) const;
inline const Json_get_proxy get(Str key, double& x) const;
inline const Json_get_proxy get(Str key, bool& x) const;
inline const Json_get_proxy get(Str key, Str& x) const;
inline const Json_get_proxy get(Str key, String& x) const;
const Json& operator[](Str key) const;
inline Json_object_proxy<Json> operator[](const String& key);
inline Json_object_str_proxy<Json> operator[](const std::string& key);
inline Json_object_str_proxy<Json> operator[](Str key);
inline Json_object_str_proxy<Json> operator[](const char* key);
inline const Json& at(Str key) const;
inline Json& at_insert(const String& key);
inline Json& at_insert(Str key);
inline Json& at_insert(const char* key);
inline Json& set(const String& key, Json value);
template <typename P>
inline Json& set(const String& key, const Json_proxy_base<P>& value);
inline Json& unset(Str key);
inline Json& set_list();
template <typename T, typename... Args>
inline Json& set_list(const String& key, T value, Args&&... rest);
inline std::pair<object_iterator, bool> insert(const object_value_type& x);
inline object_iterator insert(object_iterator position,
const object_value_type& x);
inline object_iterator erase(object_iterator it);
inline size_type erase(Str key);
inline Json& merge(const Json& x);
template <typename P> inline Json& merge(const Json_proxy_base<P>& x);
// Array methods
inline const Json& get(size_type x) const;
inline Json& get_insert(size_type x);
inline const Json& operator[](size_type x) const;
inline Json_array_proxy<Json> operator[](size_type x);
inline const Json& at(size_type x) const;
inline Json& at_insert(size_type x);
inline const Json& back() const;
inline Json& back();
inline Json& push_back(Json x);
template <typename P> inline Json& push_back(const Json_proxy_base<P>& x);
inline void pop_back();
inline Json& push_back_list();
template <typename T, typename... Args>
inline Json& push_back_list(T first, Args&&... rest);
inline array_iterator insert(array_iterator position, Json x);
template <typename P>
inline array_iterator insert(array_iterator position, const Json_proxy_base<P>& x);
array_iterator erase(array_iterator first, array_iterator last);
inline array_iterator erase(array_iterator position);
void reserve(size_type n);
void resize(size_type n);
inline Json* array_data();
inline const Json* array_data() const;
inline const Json* array_cdata() const;
inline Json* end_array_data();
inline const Json* end_array_data() const;
inline const Json* end_array_cdata() const;
// Iteration
inline const_object_iterator obegin() const;
inline const_object_iterator oend() const;
inline object_iterator obegin();
inline object_iterator oend();
inline const_object_iterator cobegin() const;
inline const_object_iterator coend() const;
inline const_array_iterator abegin() const;
inline const_array_iterator aend() const;
inline array_iterator abegin();
inline array_iterator aend();
inline const_array_iterator cabegin() const;
inline const_array_iterator caend() const;
inline const_iterator begin() const;
inline const_iterator end() const;
inline iterator begin();
inline iterator end();
inline const_iterator cbegin() const;
inline const_iterator cend() const;
// Unparsing
static inline unparse_manipulator indent_depth(int x);
static inline unparse_manipulator tab_width(int x);
static inline unparse_manipulator newline_terminator(bool x);
static inline unparse_manipulator space_separator(bool x);
inline String unparse() const;
inline String unparse(const unparse_manipulator& m) const;
inline void unparse(StringAccum& sa) const;
inline void unparse(StringAccum& sa, const unparse_manipulator& m) const;
// Parsing
inline bool assign_parse(const String& str);
inline bool assign_parse(const char* first, const char* last);
static inline Json parse(const String& str);
static inline Json parse(const char* first, const char* last);
// Assignment
inline Json& operator=(const Json& x);
#if HAVE_CXX_RVALUE_REFERENCES
inline Json& operator=(Json&& x);
#endif
inline Json& operator=(int x);
inline Json& operator=(unsigned x);
inline Json& operator=(long x);
inline Json& operator=(unsigned long x);
inline Json& operator=(long long x);
inline Json& operator=(unsigned long long x);
inline Json& operator=(double x);
inline Json& operator=(bool x);
inline Json& operator=(const String& x);
template <typename P> inline Json& operator=(const Json_proxy_base<P>& x);
inline Json& operator++();
inline void operator++(int);
inline Json& operator--();
inline void operator--(int);
inline Json& operator+=(int x);
inline Json& operator+=(unsigned x);
inline Json& operator+=(long x);
inline Json& operator+=(unsigned long x);
inline Json& operator+=(long long x);
inline Json& operator+=(unsigned long long x);
inline Json& operator+=(double x);
inline Json& operator+=(const Json& x);
inline Json& operator-=(int x);
inline Json& operator-=(unsigned x);
inline Json& operator-=(long x);
inline Json& operator-=(unsigned long x);
inline Json& operator-=(long long x);
inline Json& operator-=(unsigned long long x);
inline Json& operator-=(double x);
inline Json& operator-=(const Json& x);
friend bool operator==(const Json& a, const Json& b);
inline void swap(Json& x);
private:
enum {
st_initial = 0, st_array_initial = 1, st_array_delim = 2,
st_array_value = 3, st_object_initial = 4, st_object_delim = 5,
st_object_key = 6, st_object_colon = 7, st_object_value = 8,
max_depth = 2048
};
struct ComplexJson {
int refcount;
int size;
ComplexJson()
: refcount(1) {
}
inline void ref();
inline void deref(json_type j);
private:
ComplexJson(const ComplexJson& x); // does not exist
};
struct ArrayJson;
struct ObjectItem;
struct ObjectJson;
union rep_type {
Json_rep_item<int64_t> i;
Json_rep_item<uint64_t> u;
Json_rep_item<double> d;
String::rep_type str;
Json_rep_item<ArrayJson*> a;
Json_rep_item<ObjectJson*> o;
Json_rep_item<ComplexJson*> x;
} u_;
inline void deref();
inline ObjectJson* ojson() const;
inline ArrayJson* ajson() const;
int64_t hard_to_i() const;
uint64_t hard_to_u() const;
double hard_to_d() const;
bool hard_to_b() const;
String hard_to_s() const;
inline void force_number();
inline void force_double();
inline Json& add(double x);
template <typename T> inline Json& add(T x);
inline Json& subtract(double x);
template <typename T> inline Json& subtract(T x);
const Json& hard_get(Str key) const;
const Json& hard_get(size_type x) const;
Json& hard_get_insert(size_type x);
inline void uniqueify_object(bool convert);
void hard_uniqueify_object(bool convert);
inline void uniqueify_array(bool convert, int ncap);
void hard_uniqueify_array(bool convert, int ncap);
void* uniqueify_array_insert(bool convert, size_type pos);
static unparse_manipulator default_manipulator;
bool unparse_is_complex() const;
static void unparse_indent(StringAccum &sa, const unparse_manipulator &m, int depth);
void hard_unparse(StringAccum &sa, const unparse_manipulator &m, int depth) const;
bool assign_parse(const char* first, const char* last, const String &str);
friend class object_iterator;
friend class const_object_iterator;
friend class array_iterator;
friend class const_array_iterator;
friend Json operator+(Json);
friend Json operator-(Json);
};
struct Json::ArrayJson : public ComplexJson {
int capacity;
Json a[0];
inline ArrayJson(int cap)
: capacity(cap) {
size = 0;
}
static ArrayJson* make(int n);
static void destroy(ArrayJson* a);
};
struct Json::ObjectItem {
std::pair<const String, Json> v_;
int next_;
explicit ObjectItem(const String &key, const Json& value, int next)
: v_(key, value), next_(next) {
}
};
struct Json::ObjectJson : public ComplexJson {
ObjectItem *os_;
int n_;
int capacity_;
std::vector<int> hash_;
ObjectJson()
: os_(), n_(0), capacity_(0) {
size = 0;
}
ObjectJson(const ObjectJson& x);
~ObjectJson();
void grow(bool copy);
int bucket(const char* s, int len) const {
return String::hashcode(s, s + len) & (hash_.size() - 1);
}
ObjectItem& item(int p) const {
return os_[p];
}
int find(const char* s, int len) const {
if (hash_.size()) {
int p = hash_[bucket(s, len)];
while (p >= 0) {
ObjectItem &oi = item(p);
if (oi.v_.first.equals(s, len))
return p;
p = oi.next_;
}
}
return -1;
}
int find_insert(const String& key, const Json& value);
inline Json& get_insert(const String& key) {
int p = find_insert(key, make_null());
return item(p).v_.second;
}
Json& get_insert(Str key);
void erase(int p);
size_type erase(Str key);
void rehash();
};
inline const Json& Json::make_null() {
return null_json;
}
inline void Json::ComplexJson::ref() {
if (refcount >= 0)
++refcount;
}
inline void Json::ComplexJson::deref(json_type j) {
if (refcount >= 1 && --refcount == 0) {
if (j == j_object)
delete static_cast<ObjectJson*>(this);
else
ArrayJson::destroy(static_cast<ArrayJson*>(this));
}
}
inline Json::ArrayJson* Json::ajson() const {
precondition(u_.x.type == j_null || u_.x.type == j_array);
return u_.a.x;
}
inline Json::ObjectJson* Json::ojson() const {
precondition(u_.x.type == j_null || u_.x.type == j_object);
return u_.o.x;
}
inline void Json::uniqueify_array(bool convert, int ncap) {
if (u_.x.type != j_array || !u_.a.x || u_.a.x->refcount != 1
|| (ncap > 0 && ncap > u_.a.x->capacity))
hard_uniqueify_array(convert, ncap);
}
inline void Json::uniqueify_object(bool convert) {
if (u_.x.type != j_object || !u_.o.x || u_.o.x->refcount != 1)
hard_uniqueify_object(convert);
}
class Json::const_object_iterator { public:
typedef std::pair<const String, Json> value_type;
typedef const value_type* pointer_type;
typedef const value_type& reference_type;
typedef std::forward_iterator_tag iterator_category;
const_object_iterator() {
}
typedef bool (const_object_iterator::*unspecified_bool_type)() const;
operator unspecified_bool_type() const {
return live() ? &const_object_iterator::live : 0;
}
bool live() const {
return i_ >= 0;
}
const value_type& operator*() const {
return j_->ojson()->item(i_).v_;
}
const value_type* operator->() const {
return &(**this);
}
const String& key() const {
return (**this).first;
}
const Json& value() const {
return (**this).second;
}
void operator++() {
++i_;
fix();
}
void operator++(int) {
++(*this);
}
private:
const Json* j_;
int i_;
const_object_iterator(const Json* j, int i)
: j_(j), i_(i) {
if (i_ >= 0)
fix();
}
void fix() {
ObjectJson* oj = j_->ojson();
retry:
if (!oj || i_ >= oj->n_)
i_ = -1;
else if (oj->item(i_).next_ == -2) {
++i_;
goto retry;
}
}
friend class Json;
friend bool operator==(const const_object_iterator&, const const_object_iterator&);
};
class Json::object_iterator : public const_object_iterator { public:
typedef value_type* pointer_type;
typedef value_type& reference_type;
object_iterator() {
}
value_type& operator*() const {
const_cast<Json*>(j_)->uniqueify_object(false);
return j_->ojson()->item(i_).v_;
}
value_type* operator->() const {
return &(**this);
}
Json& value() const {
return (**this).second;
}
private:
object_iterator(Json* j, int i)
: const_object_iterator(j, i) {
}
friend class Json;
};
inline bool operator==(const Json::const_object_iterator& a, const Json::const_object_iterator& b) {
return a.j_ == b.j_ && a.i_ == b.i_;
}
inline bool operator!=(const Json::const_object_iterator& a, const Json::const_object_iterator& b) {
return !(a == b);
}
class Json::const_array_iterator { public:
typedef Json::size_type difference_type;
typedef Json value_type;
typedef const Json* pointer;
typedef const Json& reference;
typedef std::random_access_iterator_tag iterator_category;
const_array_iterator() {
}
typedef bool (const_array_iterator::*unspecified_bool_type)() const;
operator unspecified_bool_type() const {
return live() ? &const_array_iterator::live : 0;
}
bool live() const {
ArrayJson* aj = j_->ajson();
return aj && i_ < aj->size;
}
const Json& operator*() const {
return j_->ajson()->a[i_];
}
const Json& operator[](difference_type i) const {
return j_->ajson()->a[i_ + i];
}
const Json* operator->() const {
return &(**this);
}
const Json& value() const {
return **this;
}
void operator++(int) {
++i_;
}
const_array_iterator& operator++() {
++i_;
return *this;
}
void operator--(int) {
--i_;
}
const_array_iterator& operator--() {
--i_;
return *this;
}
const_array_iterator& operator+=(difference_type x) {
i_ += x;
return *this;
}
const_array_iterator& operator-=(difference_type x) {
i_ -= x;
return *this;
}
private:
const Json* j_;
int i_;
const_array_iterator(const Json* j, int i)
: j_(j), i_(i) {
}
friend class Json;
friend class Json::array_iterator;
friend bool operator==(const const_array_iterator&, const const_array_iterator&);
friend bool operator<(const const_array_iterator&, const const_array_iterator&);
friend difference_type operator-(const const_array_iterator&, const const_array_iterator&);
};
class Json::array_iterator : public const_array_iterator { public:
typedef const Json* pointer;
typedef const Json& reference;
array_iterator() {
}
Json& operator*() const {
const_cast<Json*>(j_)->uniqueify_array(false, 0);
return j_->ajson()->a[i_];
}
Json& operator[](difference_type i) const {
const_cast<Json*>(j_)->uniqueify_array(false, 0);
return j_->ajson()->a[i_ + i];
}
Json* operator->() const {
return &(**this);
}
Json& value() const {
return **this;
}
array_iterator& operator++() {
++i_;
return *this;
}
array_iterator& operator--() {
--i_;
return *this;
}
array_iterator& operator+=(difference_type x) {
i_ += x;
return *this;
}
array_iterator& operator-=(difference_type x) {
i_ -= x;
return *this;
}
private:
array_iterator(Json* j, int i)
: const_array_iterator(j, i) {
}
friend class Json;
};
inline bool operator==(const Json::const_array_iterator& a, const Json::const_array_iterator& b) {
return a.j_ == b.j_ && a.i_ == b.i_;
}
inline bool operator<(const Json::const_array_iterator& a, const Json::const_array_iterator& b) {
return a.j_ < b.j_ || (a.j_ == b.j_ && a.i_ < b.i_);
}
inline bool operator!=(const Json::const_array_iterator& a, const Json::const_array_iterator& b) {
return !(a == b);
}
inline bool operator<=(const Json::const_array_iterator& a, const Json::const_array_iterator& b) {
return !(b < a);
}
inline bool operator>(const Json::const_array_iterator& a, const Json::const_array_iterator& b) {
return b < a;
}
inline bool operator>=(const Json::const_array_iterator& a, const Json::const_array_iterator& b) {
return !(a < b);
}
inline Json::const_array_iterator operator+(Json::const_array_iterator a, Json::const_array_iterator::difference_type i) {
return a += i;
}
inline Json::array_iterator operator+(Json::array_iterator a, Json::array_iterator::difference_type i) {
return a += i;
}
inline Json::const_array_iterator operator-(Json::const_array_iterator a, Json::const_array_iterator::difference_type i) {
return a -= i;
}
inline Json::array_iterator operator-(Json::array_iterator a, Json::array_iterator::difference_type i) {
return a -= i;
}
inline Json::const_array_iterator::difference_type operator-(const Json::const_array_iterator& a, const Json::const_array_iterator& b) {
precondition(a.j_ == b.j_);
return a.i_ - b.i_;
}
class Json::const_iterator { public:
typedef std::pair<const String, Json&> value_type;
typedef const value_type* pointer;
typedef const value_type& reference;
typedef std::forward_iterator_tag iterator_category;
const_iterator()
: value_(String(), *(Json*) 0) {
}
typedef bool (const_iterator::*unspecified_bool_type)() const;
operator unspecified_bool_type() const {
return live() ? &const_iterator::live : 0;
}
bool live() const {
return i_ >= 0;
}
const value_type& operator*() const {
return value_;
}
const value_type* operator->() const {
return &(**this);
}
const String& key() const {
return (**this).first;
}
const Json& value() const {
return (**this).second;
}
void operator++() {
++i_;
fix();
}
void operator++(int) {
++(*this);
}
private:
const Json* j_;
int i_;
value_type value_;
const_iterator(const Json* j, int i)
: j_(j), i_(i), value_(String(), *(Json*) 0) {
if (i_ >= 0)
fix();
}
void fix() {
if (j_->u_.x.type == j_object) {
ObjectJson* oj = j_->ojson();
retry:
if (!oj || i_ >= oj->n_)
i_ = -1;
else if (oj->item(i_).next_ == -2) {
++i_;
goto retry;
} else {
value_.~pair();
new((void *) &value_) value_type(oj->item(i_).v_.first,
oj->item(i_).v_.second);
}
} else {
ArrayJson *aj = j_->ajson();
if (!aj || unsigned(i_) >= unsigned(aj->size))
i_ = -1;
else {
value_.~pair();
new((void *) &value_) value_type(String(i_), aj->a[i_]);
}
}
}
friend class Json;
friend bool operator==(const const_iterator &, const const_iterator &);
};
class Json::iterator : public const_iterator { public:
typedef value_type* pointer;
typedef value_type& reference;
iterator() {
}
value_type& operator*() const {
if (j_->u_.x.x->refcount != 1)
uniqueify();
return const_cast<value_type&>(const_iterator::operator*());
}
value_type* operator->() const {
return &(**this);
}
Json& value() const {
return (**this).second;
}
private:
iterator(Json *j, int i)
: const_iterator(j, i) {
}
void uniqueify() const {
if (j_->u_.x.type == j_object)
const_cast<Json*>(j_)->hard_uniqueify_object(false);
else
const_cast<Json*>(j_)->hard_uniqueify_array(false, 0);
const_cast<iterator*>(this)->fix();
}
friend class Json;
};
inline bool operator==(const Json::const_iterator& a, const Json::const_iterator& b) {
return a.j_ == b.j_ && a.i_ == b.i_;
}
inline bool operator!=(const Json::const_iterator& a, const Json::const_iterator& b) {
return !(a == b);
}
template <typename P>
class Json_proxy_base {
public:
const Json& cvalue() const {
return static_cast<const P *>(this)->cvalue();
}
Json& value() {
return static_cast<P *>(this)->value();
}
operator const Json&() const {
return cvalue();
}
operator Json&() {
return value();
}
bool truthy() const {
return cvalue().truthy();
}
bool falsy() const {
return cvalue().falsy();
}
operator Json::unspecified_bool_type() const {
return cvalue();
}
bool operator!() const {
return !cvalue();
}
bool is_null() const {
return cvalue().is_null();
}
bool is_int() const {
return cvalue().is_int();
}
bool is_i() const {
return cvalue().is_i();
}
bool is_unsigned() const {
return cvalue().is_unsigned();
}
bool is_u() const {
return cvalue().is_u();
}
bool is_signed() const {
return cvalue().is_signed();
}
bool is_nonnegint() const {
return cvalue().is_nonnegint();
}
bool is_double() const {
return cvalue().is_double();
}
bool is_d() const {
return cvalue().is_d();
}
bool is_number() const {
return cvalue().is_number();
}
bool is_n() const {
return cvalue().is_n();
}
bool is_bool() const {
return cvalue().is_bool();
}
bool is_b() const {
return cvalue().is_b();
}
bool is_string() const {
return cvalue().is_string();
}
bool is_s() const {
return cvalue().is_s();
}
bool is_array() const {
return cvalue().is_array();
}
bool is_a() const {
return cvalue().is_a();
}
bool is_object() const {
return cvalue().is_object();
}
bool is_o() const {
return cvalue().is_o();
}
bool is_primitive() const {
return cvalue().is_primitive();
}
bool empty() const {
return cvalue().empty();
}
Json::size_type size() const {
return cvalue().size();
}
int64_t to_i() const {
return cvalue().to_i();
}
uint64_t to_u() const {
return cvalue().to_u();
}
uint64_t to_u64() const {
return cvalue().to_u64();
}
bool to_i(int& x) const {
return cvalue().to_i(x);
}
bool to_i(unsigned& x) const {
return cvalue().to_i(x);
}
bool to_i(long& x) const {
return cvalue().to_i(x);
}
bool to_i(unsigned long& x) const {
return cvalue().to_i(x);
}
bool to_i(long long& x) const {
return cvalue().to_i(x);
}
bool to_i(unsigned long long& x) const {
return cvalue().to_i(x);
}
int64_t as_i() const {
return cvalue().as_i();
}
int64_t as_i(int64_t default_value) const {
return cvalue().as_i(default_value);
}
uint64_t as_u() const {