update scripts
[c11concurrency-benchmarks.git] / silo / masstree / json.hh
1 /* Masstree
2  * Eddie Kohler, Yandong Mao, Robert Morris
3  * Copyright (c) 2012-2014 President and Fellows of Harvard College
4  * Copyright (c) 2012-2014 Massachusetts Institute of Technology
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a
7  * copy of this software and associated documentation files (the "Software"),
8  * to deal in the Software without restriction, subject to the conditions
9  * listed in the Masstree LICENSE file. These conditions include: you must
10  * preserve this copyright notice, and you cannot mention the copyright
11  * holders in advertising related to the Software without their permission.
12  * The Software is provided WITHOUT ANY WARRANTY, EXPRESS OR IMPLIED. This
13  * notice is a summary of the Masstree LICENSE file; the license in that file
14  * is legally binding.
15  */
16 // -*- c-basic-offset: 4 -*-
17 #ifndef JSON_HH
18 #define JSON_HH
19 #include "straccum.hh"
20 #include "str.hh"
21 #include <vector>
22 #include <utility>
23 #include <stdlib.h>
24 namespace lcdf {
25
26 template <typename P> class Json_proxy_base;
27 template <typename T> class Json_object_proxy;
28 template <typename T> class Json_object_str_proxy;
29 template <typename T> class Json_array_proxy;
30 class Json_get_proxy;
31
32 template <typename T, size_t S = sizeof(String::rep_type) - sizeof(T)>
33 struct Json_rep_item;
34 template <typename T> struct Json_rep_item<T, 4> {
35     T x;
36     int type;
37 };
38 template <typename T> struct Json_rep_item<T, 8> {
39     T x;
40     int padding;
41     int type;
42 };
43
44 class Json {
45     enum json_type { // order matters
46         j_string = -1, j_null = 0,
47         j_array = 1, j_object = 2,
48         j_int = 3, j_unsigned = 4, j_double = 5, j_bool = 6
49     };
50
51   public:
52     struct null_t {
53         inline constexpr null_t() { }
54     };
55     static const null_t null;
56     static const Json null_json;
57
58     typedef int size_type;
59
60     typedef std::pair<const String, Json> object_value_type;
61     class object_iterator;
62     class const_object_iterator;
63
64     typedef Json array_value_type;
65     class array_iterator;
66     class const_array_iterator;
67
68     class iterator;
69     class const_iterator;
70
71     typedef bool (Json::*unspecified_bool_type)() const;
72     class unparse_manipulator;
73     class streaming_parser;
74
75     // Constructors
76     inline Json();
77     inline Json(const Json& x);
78     template <typename P> inline Json(const Json_proxy_base<P>& x);
79 #if HAVE_CXX_RVALUE_REFERENCES
80     inline Json(Json&& x);
81 #endif
82     inline Json(const null_t& x);
83     inline Json(int x);
84     inline Json(unsigned x);
85     inline Json(long x);
86     inline Json(unsigned long x);
87     inline Json(long long x);
88     inline Json(unsigned long long x);
89     inline Json(double x);
90     inline Json(bool x);
91     inline Json(const String& x);
92     inline Json(Str x);
93     inline Json(const char* x);
94     template <typename T> inline Json(const std::vector<T>& x);
95     template <typename T> inline Json(T first, T last);
96     inline ~Json();
97
98     static inline const Json& make_null();
99     static inline Json make_array();
100     static inline Json make_array_reserve(int n);
101     template <typename... Args>
102     static inline Json array(Args&&... rest);
103     static inline Json make_object();
104     template <typename... Args>
105     static inline Json object(Args&&... rest);
106     static inline Json make_string(const String& x);
107     static inline Json make_string(const char* s, int len);
108
109     // Type information
110     inline bool truthy() const;
111     inline bool falsy() const;
112     inline operator unspecified_bool_type() const;
113     inline bool operator!() const;
114
115     inline bool is_null() const;
116     inline bool is_int() const;
117     inline bool is_i() const;
118     inline bool is_unsigned() const;
119     inline bool is_u() const;
120     inline bool is_signed() const;
121     inline bool is_nonnegint() const;
122     inline bool is_double() const;
123     inline bool is_d() const;
124     inline bool is_number() const;
125     inline bool is_n() const;
126     inline bool is_bool() const;
127     inline bool is_b() const;
128     inline bool is_string() const;
129     inline bool is_s() const;
130     inline bool is_array() const;
131     inline bool is_a() const;
132     inline bool is_object() const;
133     inline bool is_o() const;
134     inline bool is_primitive() const;
135
136     inline bool empty() const;
137     inline size_type size() const;
138     inline bool shared() const;
139
140     void clear();
141
142     // Primitive extractors
143     inline int64_t to_i() const;
144     inline uint64_t to_u() const;
145     inline uint64_t to_u64() const;
146     inline bool to_i(int& x) const;
147     inline bool to_i(unsigned& x) const;
148     inline bool to_i(long& x) const;
149     inline bool to_i(unsigned long& x) const;
150     inline bool to_i(long long& x) const;
151     inline bool to_i(unsigned long long& x) const;
152     inline int64_t as_i() const;
153     inline int64_t as_i(int64_t default_value) const;
154     inline uint64_t as_u() const;
155     inline uint64_t as_u(uint64_t default_value) const;
156
157     inline double to_d() const;
158     inline bool to_d(double& x) const;
159     inline double as_d() const;
160     inline double as_d(double default_value) const;
161
162     inline bool to_b() const;
163     inline bool to_b(bool& x) const;
164     inline bool as_b() const;
165     inline bool as_b(bool default_value) const;
166
167     inline String to_s() const;
168     inline bool to_s(Str& x) const;
169     inline bool to_s(String& x) const;
170     inline String& as_s();
171     inline const String& as_s() const;
172     inline const String& as_s(const String& default_value) const;
173
174     // Object methods
175     inline size_type count(Str key) const;
176     inline const Json& get(Str key) const;
177     inline Json& get_insert(const String& key);
178     inline Json& get_insert(Str key);
179     inline Json& get_insert(const char* key);
180
181     inline long get_i(Str key) const;
182     inline double get_d(Str key) const;
183     inline bool get_b(Str key) const;
184     inline String get_s(Str key) const;
185
186     inline const Json_get_proxy get(Str key, Json& x) const;
187     inline const Json_get_proxy get(Str key, int& x) const;
188     inline const Json_get_proxy get(Str key, unsigned& x) const;
189     inline const Json_get_proxy get(Str key, long& x) const;
190     inline const Json_get_proxy get(Str key, unsigned long& x) const;
191     inline const Json_get_proxy get(Str key, long long& x) const;
192     inline const Json_get_proxy get(Str key, unsigned long long& x) const;
193     inline const Json_get_proxy get(Str key, double& x) const;
194     inline const Json_get_proxy get(Str key, bool& x) const;
195     inline const Json_get_proxy get(Str key, Str& x) const;
196     inline const Json_get_proxy get(Str key, String& x) const;
197
198     const Json& operator[](Str key) const;
199     inline Json_object_proxy<Json> operator[](const String& key);
200     inline Json_object_str_proxy<Json> operator[](Str key);
201     inline Json_object_str_proxy<Json> operator[](const char* key);
202
203     inline const Json& at(Str key) const;
204     inline Json& at_insert(const String& key);
205     inline Json& at_insert(Str key);
206     inline Json& at_insert(const char* key);
207
208     inline Json& set(const String& key, Json value);
209     template <typename P>
210     inline Json& set(const String& key, const Json_proxy_base<P>& value);
211     inline Json& unset(Str key);
212
213     inline Json& set_list();
214     template <typename T, typename... Args>
215     inline Json& set_list(const String& key, T value, Args&&... rest);
216
217     inline std::pair<object_iterator, bool> insert(const object_value_type& x);
218     inline object_iterator insert(object_iterator position,
219                                   const object_value_type& x);
220     inline object_iterator erase(object_iterator it);
221     inline size_type erase(Str key);
222
223     inline Json& merge(const Json& x);
224     template <typename P> inline Json& merge(const Json_proxy_base<P>& x);
225
226     // Array methods
227     inline const Json& get(size_type x) const;
228     inline Json& get_insert(size_type x);
229
230     inline const Json& operator[](size_type x) const;
231     inline Json_array_proxy<Json> operator[](size_type x);
232
233     inline const Json& at(size_type x) const;
234     inline Json& at_insert(size_type x);
235
236     inline const Json& back() const;
237     inline Json& back();
238
239     inline Json& push_back(Json x);
240     template <typename P> inline Json& push_back(const Json_proxy_base<P>& x);
241     inline void pop_back();
242
243     inline Json& push_back_list();
244     template <typename T, typename... Args>
245     inline Json& push_back_list(T first, Args&&... rest);
246
247     inline array_iterator insert(array_iterator position, Json x);
248     template <typename P>
249     inline array_iterator insert(array_iterator position, const Json_proxy_base<P>& x);
250     array_iterator erase(array_iterator first, array_iterator last);
251     inline array_iterator erase(array_iterator position);
252
253     void reserve(size_type n);
254     void resize(size_type n);
255
256     inline Json* array_data();
257     inline const Json* array_data() const;
258     inline const Json* array_cdata() const;
259     inline Json* end_array_data();
260     inline const Json* end_array_data() const;
261     inline const Json* end_array_cdata() const;
262
263     // Iteration
264     inline const_object_iterator obegin() const;
265     inline const_object_iterator oend() const;
266     inline object_iterator obegin();
267     inline object_iterator oend();
268     inline const_object_iterator cobegin() const;
269     inline const_object_iterator coend() const;
270
271     inline const_array_iterator abegin() const;
272     inline const_array_iterator aend() const;
273     inline array_iterator abegin();
274     inline array_iterator aend();
275     inline const_array_iterator cabegin() const;
276     inline const_array_iterator caend() const;
277
278     inline const_iterator begin() const;
279     inline const_iterator end() const;
280     inline iterator begin();
281     inline iterator end();
282     inline const_iterator cbegin() const;
283     inline const_iterator cend() const;
284
285     // Unparsing
286     static inline unparse_manipulator indent_depth(int x);
287     static inline unparse_manipulator tab_width(int x);
288     static inline unparse_manipulator newline_terminator(bool x);
289     static inline unparse_manipulator space_separator(bool x);
290
291     inline String unparse() const;
292     inline String unparse(const unparse_manipulator& m) const;
293     inline void unparse(StringAccum& sa) const;
294     inline void unparse(StringAccum& sa, const unparse_manipulator& m) const;
295
296     // Parsing
297     inline bool assign_parse(const String& str);
298     inline bool assign_parse(const char* first, const char* last);
299
300     static inline Json parse(const String& str);
301     static inline Json parse(const char* first, const char* last);
302
303     // Assignment
304     inline Json& operator=(const Json& x);
305 #if HAVE_CXX_RVALUE_REFERENCES
306     inline Json& operator=(Json&& x);
307 #endif
308     inline Json& operator=(int x);
309     inline Json& operator=(unsigned x);
310     inline Json& operator=(long x);
311     inline Json& operator=(unsigned long x);
312     inline Json& operator=(long long x);
313     inline Json& operator=(unsigned long long x);
314     inline Json& operator=(double x);
315     inline Json& operator=(bool x);
316     inline Json& operator=(const String& x);
317     template <typename P> inline Json& operator=(const Json_proxy_base<P>& x);
318
319     inline Json& operator++();
320     inline void operator++(int);
321     inline Json& operator--();
322     inline void operator--(int);
323     inline Json& operator+=(int x);
324     inline Json& operator+=(unsigned x);
325     inline Json& operator+=(long x);
326     inline Json& operator+=(unsigned long x);
327     inline Json& operator+=(long long x);
328     inline Json& operator+=(unsigned long long x);
329     inline Json& operator+=(double x);
330     inline Json& operator+=(const Json& x);
331     inline Json& operator-=(int x);
332     inline Json& operator-=(unsigned x);
333     inline Json& operator-=(long x);
334     inline Json& operator-=(unsigned long x);
335     inline Json& operator-=(long long x);
336     inline Json& operator-=(unsigned long long x);
337     inline Json& operator-=(double x);
338     inline Json& operator-=(const Json& x);
339
340     friend bool operator==(const Json& a, const Json& b);
341
342     inline void swap(Json& x);
343
344   private:
345     enum {
346         st_initial = 0, st_array_initial = 1, st_array_delim = 2,
347         st_array_value = 3, st_object_initial = 4, st_object_delim = 5,
348         st_object_key = 6, st_object_colon = 7, st_object_value = 8,
349         max_depth = 2048
350     };
351
352     struct ComplexJson {
353         int refcount;
354         int size;
355         ComplexJson()
356             : refcount(1) {
357         }
358         inline void ref();
359         inline void deref(json_type j);
360       private:
361         ComplexJson(const ComplexJson& x); // does not exist
362     };
363
364     struct ArrayJson;
365     struct ObjectItem;
366     struct ObjectJson;
367
368     union rep_type {
369         Json_rep_item<int64_t> i;
370         Json_rep_item<uint64_t> u;
371         Json_rep_item<double> d;
372         String::rep_type str;
373         Json_rep_item<ArrayJson*> a;
374         Json_rep_item<ObjectJson*> o;
375         Json_rep_item<ComplexJson*> x;
376     } u_;
377
378     inline void deref();
379
380     inline ObjectJson* ojson() const;
381     inline ArrayJson* ajson() const;
382
383     int64_t hard_to_i() const;
384     uint64_t hard_to_u() const;
385     double hard_to_d() const;
386     bool hard_to_b() const;
387     String hard_to_s() const;
388     inline void force_number();
389     inline void force_double();
390     inline Json& add(double x);
391     template <typename T> inline Json& add(T x);
392     inline Json& subtract(double x);
393     template <typename T> inline Json& subtract(T x);
394
395     const Json& hard_get(Str key) const;
396     const Json& hard_get(size_type x) const;
397     Json& hard_get_insert(size_type x);
398
399     inline void uniqueify_object(bool convert);
400     void hard_uniqueify_object(bool convert);
401     inline void uniqueify_array(bool convert, int ncap);
402     void hard_uniqueify_array(bool convert, int ncap);
403     void* uniqueify_array_insert(bool convert, size_type pos);
404
405     static unparse_manipulator default_manipulator;
406     bool unparse_is_complex() const;
407     static void unparse_indent(StringAccum &sa, const unparse_manipulator &m, int depth);
408     void hard_unparse(StringAccum &sa, const unparse_manipulator &m, int depth) const;
409
410     bool assign_parse(const char* first, const char* last, const String &str);
411
412     friend class object_iterator;
413     friend class const_object_iterator;
414     friend class array_iterator;
415     friend class const_array_iterator;
416     friend Json operator+(Json);
417     friend Json operator-(Json);
418 };
419
420
421 struct Json::ArrayJson : public ComplexJson {
422     int capacity;
423     Json a[0];
424
425     inline ArrayJson(int cap)
426         : capacity(cap) {
427         size = 0;
428     }
429     static ArrayJson* make(int n);
430     static void destroy(ArrayJson* a);
431 };
432
433 struct Json::ObjectItem {
434     std::pair<const String, Json> v_;
435     int next_;
436     explicit ObjectItem(const String &key, const Json& value, int next)
437         : v_(key, value), next_(next) {
438     }
439 };
440
441 struct Json::ObjectJson : public ComplexJson {
442     ObjectItem *os_;
443     int n_;
444     int capacity_;
445     std::vector<int> hash_;
446     ObjectJson()
447         : os_(), n_(0), capacity_(0) {
448         size = 0;
449     }
450     ObjectJson(const ObjectJson& x);
451     ~ObjectJson();
452     void grow(bool copy);
453     int bucket(const char* s, int len) const {
454         return String::hashcode(s, s + len) & (hash_.size() - 1);
455     }
456     ObjectItem& item(int p) const {
457         return os_[p];
458     }
459     int find(const char* s, int len) const {
460         if (hash_.size()) {
461             int p = hash_[bucket(s, len)];
462             while (p >= 0) {
463                 ObjectItem &oi = item(p);
464                 if (oi.v_.first.equals(s, len))
465                     return p;
466                 p = oi.next_;
467             }
468         }
469         return -1;
470     }
471     int find_insert(const String& key, const Json& value);
472     inline Json& get_insert(const String& key) {
473         int p = find_insert(key, make_null());
474         return item(p).v_.second;
475     }
476     Json& get_insert(Str key);
477     void erase(int p);
478     size_type erase(Str key);
479     void rehash();
480 };
481
482 inline const Json& Json::make_null() {
483     return null_json;
484 }
485
486 inline void Json::ComplexJson::ref() {
487     if (refcount >= 0)
488         ++refcount;
489 }
490
491 inline void Json::ComplexJson::deref(json_type j) {
492     if (refcount >= 1 && --refcount == 0) {
493         if (j == j_object)
494             delete static_cast<ObjectJson*>(this);
495         else
496             ArrayJson::destroy(static_cast<ArrayJson*>(this));
497     }
498 }
499
500 inline Json::ArrayJson* Json::ajson() const {
501     precondition(u_.x.type == j_null || u_.x.type == j_array);
502     return u_.a.x;
503 }
504
505 inline Json::ObjectJson* Json::ojson() const {
506     precondition(u_.x.type == j_null || u_.x.type == j_object);
507     return u_.o.x;
508 }
509
510 inline void Json::uniqueify_array(bool convert, int ncap) {
511     if (u_.x.type != j_array || !u_.a.x || u_.a.x->refcount != 1
512         || (ncap > 0 && ncap > u_.a.x->capacity))
513         hard_uniqueify_array(convert, ncap);
514 }
515
516 inline void Json::uniqueify_object(bool convert) {
517     if (u_.x.type != j_object || !u_.o.x || u_.o.x->refcount != 1)
518         hard_uniqueify_object(convert);
519 }
520
521
522 class Json::const_object_iterator { public:
523     typedef std::pair<const String, Json> value_type;
524     typedef const value_type* pointer_type;
525     typedef const value_type& reference_type;
526     typedef std::forward_iterator_tag iterator_category;
527
528     const_object_iterator() {
529     }
530     typedef bool (const_object_iterator::*unspecified_bool_type)() const;
531     operator unspecified_bool_type() const {
532         return live() ? &const_object_iterator::live : 0;
533     }
534     bool live() const {
535         return i_ >= 0;
536     }
537     const value_type& operator*() const {
538         return j_->ojson()->item(i_).v_;
539     }
540     const value_type* operator->() const {
541         return &(**this);
542     }
543     const String& key() const {
544         return (**this).first;
545     }
546     const Json& value() const {
547         return (**this).second;
548     }
549     void operator++() {
550         ++i_;
551         fix();
552     }
553     void operator++(int) {
554         ++(*this);
555     }
556   private:
557     const Json* j_;
558     int i_;
559     const_object_iterator(const Json* j, int i)
560         : j_(j), i_(i) {
561         if (i_ >= 0)
562             fix();
563     }
564     void fix() {
565         ObjectJson* oj = j_->ojson();
566     retry:
567         if (!oj || i_ >= oj->n_)
568             i_ = -1;
569         else if (oj->item(i_).next_ == -2) {
570             ++i_;
571             goto retry;
572         }
573     }
574     friend class Json;
575     friend bool operator==(const const_object_iterator&, const const_object_iterator&);
576 };
577
578 class Json::object_iterator : public const_object_iterator { public:
579     typedef value_type* pointer_type;
580     typedef value_type& reference_type;
581
582     object_iterator() {
583     }
584     value_type& operator*() const {
585         const_cast<Json*>(j_)->uniqueify_object(false);
586         return j_->ojson()->item(i_).v_;
587     }
588     value_type* operator->() const {
589         return &(**this);
590     }
591     Json& value() const {
592         return (**this).second;
593     }
594   private:
595     object_iterator(Json* j, int i)
596         : const_object_iterator(j, i) {
597     }
598     friend class Json;
599 };
600
601 inline bool operator==(const Json::const_object_iterator& a, const Json::const_object_iterator& b) {
602     return a.j_ == b.j_ && a.i_ == b.i_;
603 }
604
605 inline bool operator!=(const Json::const_object_iterator& a, const Json::const_object_iterator& b) {
606     return !(a == b);
607 }
608
609 class Json::const_array_iterator { public:
610     typedef Json::size_type difference_type;
611     typedef Json value_type;
612     typedef const Json* pointer_type;
613     typedef const Json& reference_type;
614     typedef std::random_access_iterator_tag iterator_category;
615
616     const_array_iterator() {
617     }
618     typedef bool (const_array_iterator::*unspecified_bool_type)() const;
619     operator unspecified_bool_type() const {
620         return live() ? &const_array_iterator::live : 0;
621     }
622     bool live() const {
623         ArrayJson* aj = j_->ajson();
624         return aj && i_ < aj->size;
625     }
626     const Json& operator*() const {
627         return j_->ajson()->a[i_];
628     }
629     const Json& operator[](difference_type i) const {
630         return j_->ajson()->a[i_ + i];
631     }
632     const Json* operator->() const {
633         return &(**this);
634     }
635     const Json& value() const {
636         return **this;
637     }
638     void operator++(int) {
639         ++i_;
640     }
641     void operator++() {
642         ++i_;
643     }
644     void operator--(int) {
645         --i_;
646     }
647     void operator--() {
648         --i_;
649     }
650     const_array_iterator& operator+=(difference_type x) {
651         i_ += x;
652         return *this;
653     }
654     const_array_iterator& operator-=(difference_type x) {
655         i_ -= x;
656         return *this;
657     }
658   private:
659     const Json* j_;
660     int i_;
661     const_array_iterator(const Json* j, int i)
662         : j_(j), i_(i) {
663     }
664     friend class Json;
665     friend class Json::array_iterator;
666     friend bool operator==(const const_array_iterator&, const const_array_iterator&);
667     friend bool operator<(const const_array_iterator&, const const_array_iterator&);
668     friend difference_type operator-(const const_array_iterator&, const const_array_iterator&);
669 };
670
671 class Json::array_iterator : public const_array_iterator { public:
672     typedef const Json* pointer_type;
673     typedef const Json& reference_type;
674
675     array_iterator() {
676     }
677     Json& operator*() const {
678         const_cast<Json*>(j_)->uniqueify_array(false, 0);
679         return j_->ajson()->a[i_];
680     }
681     Json& operator[](difference_type i) const {
682         const_cast<Json*>(j_)->uniqueify_array(false, 0);
683         return j_->ajson()->a[i_ + i];
684     }
685     Json* operator->() const {
686         return &(**this);
687     }
688     Json& value() const {
689         return **this;
690     }
691     array_iterator& operator+=(difference_type x) {
692         i_ += x;
693         return *this;
694     }
695     array_iterator& operator-=(difference_type x) {
696         i_ -= x;
697         return *this;
698     }
699   private:
700     array_iterator(Json* j, int i)
701         : const_array_iterator(j, i) {
702     }
703     friend class Json;
704 };
705
706 inline bool operator==(const Json::const_array_iterator& a, const Json::const_array_iterator& b) {
707     return a.j_ == b.j_ && a.i_ == b.i_;
708 }
709
710 inline bool operator<(const Json::const_array_iterator& a, const Json::const_array_iterator& b) {
711     return a.j_ < b.j_ || (a.j_ == b.j_ && a.i_ < b.i_);
712 }
713
714 inline bool operator!=(const Json::const_array_iterator& a, const Json::const_array_iterator& b) {
715     return !(a == b);
716 }
717
718 inline bool operator<=(const Json::const_array_iterator& a, const Json::const_array_iterator& b) {
719     return !(b < a);
720 }
721
722 inline bool operator>(const Json::const_array_iterator& a, const Json::const_array_iterator& b) {
723     return b < a;
724 }
725
726 inline bool operator>=(const Json::const_array_iterator& a, const Json::const_array_iterator& b) {
727     return !(a < b);
728 }
729
730 inline Json::const_array_iterator operator+(Json::const_array_iterator a, Json::const_array_iterator::difference_type i) {
731     return a += i;
732 }
733 inline Json::array_iterator operator+(Json::array_iterator a, Json::array_iterator::difference_type i) {
734     return a += i;
735 }
736
737 inline Json::const_array_iterator operator-(Json::const_array_iterator a, Json::const_array_iterator::difference_type i) {
738     return a -= i;
739 }
740 inline Json::array_iterator operator-(Json::array_iterator a, Json::array_iterator::difference_type i) {
741     return a -= i;
742 }
743
744 inline Json::const_array_iterator::difference_type operator-(const Json::const_array_iterator& a, const Json::const_array_iterator& b) {
745     precondition(a.j_ == b.j_);
746     return a.i_ - b.i_;
747 }
748
749 class Json::const_iterator { public:
750     typedef std::pair<const String, Json&> value_type;
751     typedef const value_type* pointer_type;
752     typedef const value_type& reference_type;
753     typedef std::forward_iterator_tag iterator_category;
754
755     const_iterator()
756         : value_(String(), *(Json*) 0) {
757     }
758     typedef bool (const_iterator::*unspecified_bool_type)() const;
759     operator unspecified_bool_type() const {
760         return live() ? &const_iterator::live : 0;
761     }
762     bool live() const {
763         return i_ >= 0;
764     }
765     const value_type& operator*() const {
766         return value_;
767     }
768     const value_type* operator->() const {
769         return &(**this);
770     }
771     const String& key() const {
772         return (**this).first;
773     }
774     const Json& value() const {
775         return (**this).second;
776     }
777     void operator++() {
778         ++i_;
779         fix();
780     }
781     void operator++(int) {
782         ++(*this);
783     }
784   private:
785     const Json* j_;
786     int i_;
787     value_type value_;
788     const_iterator(const Json* j, int i)
789         : j_(j), i_(i), value_(String(), *(Json*) 0) {
790         if (i_ >= 0)
791             fix();
792     }
793     void fix() {
794         if (j_->u_.x.type == j_object) {
795             ObjectJson* oj = j_->ojson();
796         retry:
797             if (!oj || i_ >= oj->n_)
798                 i_ = -1;
799             else if (oj->item(i_).next_ == -2) {
800                 ++i_;
801                 goto retry;
802             } else {
803                 value_.~pair();
804                 new((void *) &value_) value_type(oj->item(i_).v_.first,
805                                                  oj->item(i_).v_.second);
806             }
807         } else {
808             ArrayJson *aj = j_->ajson();
809             if (!aj || unsigned(i_) >= unsigned(aj->size))
810                 i_ = -1;
811             else {
812                 value_.~pair();
813                 new((void *) &value_) value_type(String(i_), aj->a[i_]);
814             }
815         }
816     }
817     friend class Json;
818     friend bool operator==(const const_iterator &, const const_iterator &);
819 };
820
821 class Json::iterator : public const_iterator { public:
822     typedef value_type* pointer_type;
823     typedef value_type& reference_type;
824
825     iterator() {
826     }
827     value_type& operator*() const {
828         if (j_->u_.x.x->refcount != 1)
829             uniqueify();
830         return const_cast<value_type&>(const_iterator::operator*());
831     }
832     value_type* operator->() const {
833         return &(**this);
834     }
835     Json& value() const {
836         return (**this).second;
837     }
838   private:
839     iterator(Json *j, int i)
840         : const_iterator(j, i) {
841     }
842     void uniqueify() const {
843         if (j_->u_.x.type == j_object)
844             const_cast<Json*>(j_)->hard_uniqueify_object(false);
845         else
846             const_cast<Json*>(j_)->hard_uniqueify_array(false, 0);
847         const_cast<iterator*>(this)->fix();
848     }
849     friend class Json;
850 };
851
852 inline bool operator==(const Json::const_iterator& a, const Json::const_iterator& b) {
853     return a.j_ == b.j_ && a.i_ == b.i_;
854 }
855
856 inline bool operator!=(const Json::const_iterator& a, const Json::const_iterator& b) {
857     return !(a == b);
858 }
859
860
861 template <typename P>
862 class Json_proxy_base {
863   public:
864     const Json& cvalue() const {
865         return static_cast<const P *>(this)->cvalue();
866     }
867     Json& value() {
868         return static_cast<P *>(this)->value();
869     }
870     operator const Json&() const {
871         return cvalue();
872     }
873     operator Json&() {
874         return value();
875     }
876     bool truthy() const {
877         return cvalue().truthy();
878     }
879     bool falsy() const {
880         return cvalue().falsy();
881     }
882     operator Json::unspecified_bool_type() const {
883         return cvalue();
884     }
885     bool operator!() const {
886         return !cvalue();
887     }
888     bool is_null() const {
889         return cvalue().is_null();
890     }
891     bool is_int() const {
892         return cvalue().is_int();
893     }
894     bool is_i() const {
895         return cvalue().is_i();
896     }
897     bool is_unsigned() const {
898         return cvalue().is_unsigned();
899     }
900     bool is_u() const {
901         return cvalue().is_u();
902     }
903     bool is_signed() const {
904         return cvalue().is_signed();
905     }
906     bool is_nonnegint() const {
907         return cvalue().is_nonnegint();
908     }
909     bool is_double() const {
910         return cvalue().is_double();
911     }
912     bool is_d() const {
913         return cvalue().is_d();
914     }
915     bool is_number() const {
916         return cvalue().is_number();
917     }
918     bool is_n() const {
919         return cvalue().is_n();
920     }
921     bool is_bool() const {
922         return cvalue().is_bool();
923     }
924     bool is_b() const {
925         return cvalue().is_b();
926     }
927     bool is_string() const {
928         return cvalue().is_string();
929     }
930     bool is_s() const {
931         return cvalue().is_s();
932     }
933     bool is_array() const {
934         return cvalue().is_array();
935     }
936     bool is_a() const {
937         return cvalue().is_a();
938     }
939     bool is_object() const {
940         return cvalue().is_object();
941     }
942     bool is_o() const {
943         return cvalue().is_o();
944     }
945     bool is_primitive() const {
946         return cvalue().is_primitive();
947     }
948     bool empty() const {
949         return cvalue().empty();
950     }
951     Json::size_type size() const {
952         return cvalue().size();
953     }
954     int64_t to_i() const {
955         return cvalue().to_i();
956     }
957     uint64_t to_u() const {
958         return cvalue().to_u();
959     }
960     uint64_t to_u64() const {
961         return cvalue().to_u64();
962     }
963     bool to_i(int& x) const {
964         return cvalue().to_i(x);
965     }
966     bool to_i(unsigned& x) const {
967         return cvalue().to_i(x);
968     }
969     bool to_i(long& x) const {
970         return cvalue().to_i(x);
971     }
972     bool to_i(unsigned long& x) const {
973         return cvalue().to_i(x);
974     }
975     bool to_i(long long& x) const {
976         return cvalue().to_i(x);
977     }
978     bool to_i(unsigned long long& x) const {
979         return cvalue().to_i(x);
980     }
981     int64_t as_i() const {
982         return cvalue().as_i();
983     }
984     int64_t as_i(int64_t default_value) const {
985         return cvalue().as_i(default_value);
986     }
987     uint64_t as_u() const {
988         return cvalue().as_u();
989     }
990     uint64_t as_u(uint64_t default_value) const {
991         return cvalue().as_u(default_value);
992     }
993     double to_d() const {
994         return cvalue().to_d();
995     }
996     bool to_d(double& x) const {
997         return cvalue().to_d(x);
998     }
999     double as_d() const {
1000         return cvalue().as_d();
1001     }
1002     double as_d(double default_value) const {
1003         return cvalue().as_d(default_value);
1004     }
1005     bool to_b() const {
1006         return cvalue().to_b();
1007     }
1008     bool to_b(bool& x) const {
1009         return cvalue().to_b(x);
1010     }
1011     bool as_b() const {
1012         return cvalue().as_b();
1013     }
1014     bool as_b(bool default_value) const {
1015         return cvalue().as_b(default_value);
1016     }
1017     String to_s() const {
1018         return cvalue().to_s();
1019     }
1020     bool to_s(Str& x) const {
1021         return cvalue().to_s(x);
1022     }
1023     bool to_s(String& x) const {
1024         return cvalue().to_s(x);
1025     }
1026     const String& as_s() const {
1027         return cvalue().as_s();
1028     }
1029     const String& as_s(const String& default_value) const {
1030         return cvalue().as_s(default_value);
1031     }
1032     Json::size_type count(Str key) const {
1033         return cvalue().count(key);
1034     }
1035     const Json& get(Str key) const {
1036         return cvalue().get(key);
1037     }
1038     Json& get_insert(const String& key) {
1039         return value().get_insert(key);
1040     }
1041     Json& get_insert(Str key) {
1042         return value().get_insert(key);
1043     }
1044     Json& get_insert(const char* key) {
1045         return value().get_insert(key);
1046     }
1047     long get_i(Str key) const {
1048         return cvalue().get_i(key);
1049     }
1050     double get_d(Str key) const {
1051         return cvalue().get_d(key);
1052     }
1053     bool get_b(Str key) const {
1054         return cvalue().get_b(key);
1055     }
1056     String get_s(Str key) const {
1057         return cvalue().get_s(key);
1058     }
1059     inline const Json_get_proxy get(Str key, Json& x) const;
1060     inline const Json_get_proxy get(Str key, int& x) const;
1061     inline const Json_get_proxy get(Str key, unsigned& x) const;
1062     inline const Json_get_proxy get(Str key, long& x) const;
1063     inline const Json_get_proxy get(Str key, unsigned long& x) const;
1064     inline const Json_get_proxy get(Str key, long long& x) const;
1065     inline const Json_get_proxy get(Str key, unsigned long long& x) const;
1066     inline const Json_get_proxy get(Str key, double& x) const;
1067     inline const Json_get_proxy get(Str key, bool& x) const;
1068     inline const Json_get_proxy get(Str key, Str& x) const;
1069     inline const Json_get_proxy get(Str key, String& x) const;
1070     const Json& operator[](Str key) const {
1071         return cvalue().get(key);
1072     }
1073     Json_object_proxy<P> operator[](const String& key) {
1074         return Json_object_proxy<P>(*static_cast<P*>(this), key);
1075     }
1076     Json_object_str_proxy<P> operator[](Str key) {
1077         return Json_object_str_proxy<P>(*static_cast<P*>(this), key);
1078     }
1079     Json_object_str_proxy<P> operator[](const char* key) {
1080         return Json_object_str_proxy<P>(*static_cast<P*>(this), key);
1081     }
1082     const Json& at(Str key) const {
1083         return cvalue().at(key);
1084     }
1085     Json& at_insert(const String& key) {
1086         return value().at_insert(key);
1087     }
1088     Json& at_insert(Str key) {
1089         return value().at_insert(key);
1090     }
1091     Json& at_insert(const char* key) {
1092         return value().at_insert(key);
1093     }
1094     inline Json& set(const String& key, Json value) {
1095         return this->value().set(key, value);
1096     }
1097     template <typename Q>
1098     inline Json& set(const String& key, const Json_proxy_base<Q>& value) {
1099         return this->value().set(key, value);
1100     }
1101     Json& unset(Str key) {
1102         return value().unset(key);
1103     }
1104     template <typename... Args>
1105     inline Json& set_list(Args&&... args) {
1106         return value().set_list(std::forward<Args>(args)...);
1107     }
1108     std::pair<Json::object_iterator, bool> insert(const Json::object_value_type &x) {
1109         return value().insert(x);
1110     }
1111     Json::object_iterator insert(Json::object_iterator position, const Json::object_value_type &x) {
1112         return value().insert(position, x);
1113     }
1114     Json::object_iterator erase(Json::object_iterator it) {
1115         return value().erase(it);
1116     }
1117     Json::size_type erase(Str key) {
1118         return value().erase(key);
1119     }
1120     Json& merge(const Json& x) {
1121         return value().merge(x);
1122     }
1123     template <typename P2> Json& merge(const Json_proxy_base<P2>& x) {
1124         return value().merge(x.cvalue());
1125     }
1126     const Json& get(Json::size_type x) const {
1127         return cvalue().get(x);
1128     }
1129     Json& get_insert(Json::size_type x) {
1130         return value().get_insert(x);
1131     }
1132     const Json& operator[](int key) const {
1133         return cvalue().at(key);
1134     }
1135     Json_array_proxy<P> operator[](int key) {
1136         return Json_array_proxy<P>(*static_cast<P*>(this), key);
1137     }
1138     const Json& at(Json::size_type x) const {
1139         return cvalue().at(x);
1140     }
1141     Json& at_insert(Json::size_type x) {
1142         return value().at_insert(x);
1143     }
1144     const Json& back() const {
1145         return cvalue().back();
1146     }
1147     Json& back() {
1148         return value().back();
1149     }
1150     Json& push_back(Json x) {
1151         return value().push_back(std::move(x));
1152     }
1153     template <typename Q> Json& push_back(const Json_proxy_base<Q>& x) {
1154         return value().push_back(x);
1155     }
1156     void pop_back() {
1157         value().pop_back();
1158     }
1159     template <typename... Args> Json& push_back_list(Args&&... args) {
1160         return value().push_back_list(std::forward<Args>(args)...);
1161     }
1162     Json::array_iterator insert(Json::array_iterator position, Json x) {
1163         return value().insert(position, std::move(x));
1164     }
1165     template <typename Q> Json::array_iterator insert(Json::array_iterator position, const Json_proxy_base<Q>& x) {
1166         return value().insert(position, x);
1167     }
1168     Json::array_iterator erase(Json::array_iterator first, Json::array_iterator last) {
1169         return value().erase(first, last);
1170     }
1171     Json::array_iterator erase(Json::array_iterator position) {
1172         return value().erase(position);
1173     }
1174     void resize(Json::size_type n) {
1175         value().resize(n);
1176     }
1177     void unparse(StringAccum& sa) const {
1178         return cvalue().unparse(sa);
1179     }
1180     void unparse(StringAccum& sa, const Json::unparse_manipulator& m) const {
1181         return cvalue().unparse(sa, m);
1182     }
1183     String unparse() const {
1184         return cvalue().unparse();
1185     }
1186     String unparse(const Json::unparse_manipulator& m) const {
1187         return cvalue().unparse(m);
1188     }
1189     bool assign_parse(const String& str) {
1190         return value().assign_parse(str);
1191     }
1192     bool assign_parse(const char* first, const char* last) {
1193         return value().assign_parse(first, last);
1194     }
1195     void swap(Json& x) {
1196         value().swap(x);
1197     }
1198     Json& operator++() {
1199         return ++value();
1200     }
1201     void operator++(int) {
1202         value()++;
1203     }
1204     Json& operator--() {
1205         return --value();
1206     }
1207     void operator--(int) {
1208         value()--;
1209     }
1210     Json& operator+=(int x) {
1211         return value() += x;
1212     }
1213     Json& operator+=(unsigned x) {
1214         return value() += x;
1215     }
1216     Json& operator+=(long x) {
1217         return value() += x;
1218     }
1219     Json& operator+=(unsigned long x) {
1220         return value() += x;
1221     }
1222     Json& operator+=(long long x) {
1223         return value() += x;
1224     }
1225     Json& operator+=(unsigned long long x) {
1226         return value() += x;
1227     }
1228     Json& operator+=(double x) {
1229         return value() += x;
1230     }
1231     Json& operator+=(const Json& x) {
1232         return value() += x;
1233     }
1234     Json& operator-=(int x) {
1235         return value() -= x;
1236     }
1237     Json& operator-=(unsigned x) {
1238         return value() -= x;
1239     }
1240     Json& operator-=(long x) {
1241         return value() -= x;
1242     }
1243     Json& operator-=(unsigned long x) {
1244         return value() -= x;
1245     }
1246     Json& operator-=(long long x) {
1247         return value() -= x;
1248     }
1249     Json& operator-=(unsigned long long x) {
1250         return value() -= x;
1251     }
1252     Json& operator-=(double x) {
1253         return value() -= x;
1254     }
1255     Json& operator-=(const Json& x) {
1256         return value() -= x;
1257     }
1258     Json::const_object_iterator obegin() const {
1259         return cvalue().obegin();
1260     }
1261     Json::const_object_iterator oend() const {
1262         return cvalue().oend();
1263     }
1264     Json::object_iterator obegin() {
1265         return value().obegin();
1266     }
1267     Json::object_iterator oend() {
1268         return value().oend();
1269     }
1270     Json::const_object_iterator cobegin() const {
1271         return cvalue().cobegin();
1272     }
1273     Json::const_object_iterator coend() const {
1274         return cvalue().coend();
1275     }
1276     Json::const_array_iterator abegin() const {
1277         return cvalue().abegin();
1278     }
1279     Json::const_array_iterator aend() const {
1280         return cvalue().aend();
1281     }
1282     Json::array_iterator abegin() {
1283         return value().abegin();
1284     }
1285     Json::array_iterator aend() {
1286         return value().aend();
1287     }
1288     Json::const_array_iterator cabegin() const {
1289         return cvalue().cabegin();
1290     }
1291     Json::const_array_iterator caend() const {
1292         return cvalue().caend();
1293     }
1294     Json::const_iterator begin() const {
1295         return cvalue().begin();
1296     }
1297     Json::const_iterator end() const {
1298         return cvalue().end();
1299     }
1300     Json::iterator begin() {
1301         return value().begin();
1302     }
1303     Json::iterator end() {
1304         return value().end();
1305     }
1306     Json::const_iterator cbegin() const {
1307         return cvalue().cbegin();
1308     }
1309     Json::const_iterator cend() const {
1310         return cvalue().cend();
1311     }
1312 };
1313
1314 template <typename T>
1315 class Json_object_proxy : public Json_proxy_base<Json_object_proxy<T> > {
1316   public:
1317     const Json& cvalue() const {
1318         return base_.get(key_);
1319     }
1320     Json& value() {
1321         return base_.get_insert(key_);
1322     }
1323     Json& operator=(const Json& x) {
1324         return value() = x;
1325     }
1326 #if HAVE_CXX_RVALUE_REFERENCES
1327     Json& operator=(Json&& x) {
1328         return value() = std::move(x);
1329     }
1330 #endif
1331     Json& operator=(const Json_object_proxy<T>& x) {
1332         return value() = x.cvalue();
1333     }
1334     template <typename P> Json& operator=(const Json_proxy_base<P>& x) {
1335         return value() = x.cvalue();
1336     }
1337     Json_object_proxy(T& ref, const String& key)
1338         : base_(ref), key_(key) {
1339     }
1340     T &base_;
1341     String key_;
1342 };
1343
1344 template <typename T>
1345 class Json_object_str_proxy : public Json_proxy_base<Json_object_str_proxy<T> > {
1346   public:
1347     const Json& cvalue() const {
1348         return base_.get(key_);
1349     }
1350     Json& value() {
1351         return base_.get_insert(key_);
1352     }
1353     Json& operator=(const Json& x) {
1354         return value() = x;
1355     }
1356 #if HAVE_CXX_RVALUE_REFERENCES
1357     Json& operator=(Json&& x) {
1358         return value() = std::move(x);
1359     }
1360 #endif
1361     Json& operator=(const Json_object_str_proxy<T>& x) {
1362         return value() = x.cvalue();
1363     }
1364     template <typename P> Json& operator=(const Json_proxy_base<P>& x) {
1365         return value() = x.cvalue();
1366     }
1367     Json_object_str_proxy(T& ref, Str key)
1368         : base_(ref), key_(key) {
1369     }
1370     T &base_;
1371     Str key_;
1372 };
1373
1374 template <typename T>
1375 class Json_array_proxy : public Json_proxy_base<Json_array_proxy<T> > {
1376   public:
1377     const Json& cvalue() const {
1378         return base_.get(key_);
1379     }
1380     Json& value() {
1381         return base_.get_insert(key_);
1382     }
1383     Json& operator=(const Json& x) {
1384         return value() = x;
1385     }
1386 #if HAVE_CXX_RVALUE_REFERENCES
1387     Json& operator=(Json&& x) {
1388         return value() = std::move(x);
1389     }
1390 #endif
1391     Json& operator=(const Json_array_proxy<T>& x) {
1392         return value() = x.cvalue();
1393     }
1394     template <typename P> Json& operator=(const Json_proxy_base<P>& x) {
1395         return value() = x.cvalue();
1396     }
1397     Json_array_proxy(T& ref, int key)
1398         : base_(ref), key_(key) {
1399     }
1400     T &base_;
1401     int key_;
1402 };
1403
1404 class Json_get_proxy : public Json_proxy_base<Json_get_proxy> {
1405   public:
1406     const Json& cvalue() const {
1407         return base_;
1408     }
1409     operator Json::unspecified_bool_type() const {
1410         return status_ ? &Json::is_null : 0;
1411     }
1412     bool operator!() const {
1413         return !status_;
1414     }
1415     bool status() const {
1416         return status_;
1417     }
1418     const Json_get_proxy& status(bool& x) const {
1419         x = status_;
1420         return *this;
1421     }
1422     Json_get_proxy(const Json& ref, bool status)
1423         : base_(ref), status_(status) {
1424     }
1425     const Json& base_;
1426     bool status_;
1427   private:
1428     Json_get_proxy& operator=(const Json_get_proxy& x);
1429 };
1430
1431 template <typename T>
1432 inline const Json_get_proxy Json_proxy_base<T>::get(Str key, Json& x) const {
1433     return cvalue().get(key, x);
1434 }
1435 template <typename T>
1436 inline const Json_get_proxy Json_proxy_base<T>::get(Str key, int& x) const {
1437     return cvalue().get(key, x);
1438 }
1439 template <typename T>
1440 inline const Json_get_proxy Json_proxy_base<T>::get(Str key, unsigned& x) const {
1441     return cvalue().get(key, x);
1442 }
1443 template <typename T>
1444 inline const Json_get_proxy Json_proxy_base<T>::get(Str key, long& x) const {
1445     return cvalue().get(key, x);
1446 }
1447 template <typename T>
1448 inline const Json_get_proxy Json_proxy_base<T>::get(Str key, unsigned long& x) const {
1449     return cvalue().get(key, x);
1450 }
1451 template <typename T>
1452 inline const Json_get_proxy Json_proxy_base<T>::get(Str key, long long& x) const {
1453     return cvalue().get(key, x);
1454 }
1455 template <typename T>
1456 inline const Json_get_proxy Json_proxy_base<T>::get(Str key, unsigned long long& x) const {
1457     return cvalue().get(key, x);
1458 }
1459 template <typename T>
1460 inline const Json_get_proxy Json_proxy_base<T>::get(Str key, double& x) const {
1461     return cvalue().get(key, x);
1462 }
1463 template <typename T>
1464 inline const Json_get_proxy Json_proxy_base<T>::get(Str key, bool& x) const {
1465     return cvalue().get(key, x);
1466 }
1467 template <typename T>
1468 inline const Json_get_proxy Json_proxy_base<T>::get(Str key, Str& x) const {
1469     return cvalue().get(key, x);
1470 }
1471 template <typename T>
1472 inline const Json_get_proxy Json_proxy_base<T>::get(Str key, String& x) const {
1473     return cvalue().get(key, x);
1474 }
1475
1476
1477 /** @brief Construct a null Json. */
1478 inline Json::Json() {
1479     memset(&u_, 0, sizeof(u_));
1480 }
1481 /** @brief Construct a null Json. */
1482 inline Json::Json(const null_t&) {
1483     memset(&u_, 0, sizeof(u_));
1484 }
1485 /** @brief Construct a Json copy of @a x. */
1486 inline Json::Json(const Json& x)
1487     : u_(x.u_) {
1488     if (u_.x.type < 0)
1489         u_.str.ref();
1490     if (u_.x.x && (u_.x.type == j_array || u_.x.type == j_object))
1491         u_.x.x->ref();
1492 }
1493 /** @overload */
1494 template <typename P> inline Json::Json(const Json_proxy_base<P>& x)
1495     : u_(x.cvalue().u_) {
1496     if (u_.x.type < 0)
1497         u_.str.ref();
1498     if (u_.x.x && (u_.x.type == j_array || u_.x.type == j_object))
1499         u_.x.x->ref();
1500 }
1501 #if HAVE_CXX_RVALUE_REFERENCES
1502 /** @overload */
1503 inline Json::Json(Json&& x)
1504     : u_(std::move(x.u_)) {
1505     memset(&x, 0, sizeof(x));
1506 }
1507 #endif
1508 /** @brief Construct simple Json values. */
1509 inline Json::Json(int x) {
1510     u_.i.x = x;
1511     u_.i.type = j_int;
1512 }
1513 inline Json::Json(unsigned x) {
1514     u_.u.x = x;
1515     u_.u.type = j_unsigned;
1516 }
1517 inline Json::Json(long x) {
1518     u_.i.x = x;
1519     u_.i.type = j_int;
1520 }
1521 inline Json::Json(unsigned long x) {
1522     u_.u.x = x;
1523     u_.u.type = j_unsigned;
1524 }
1525 inline Json::Json(long long x) {
1526     u_.i.x = x;
1527     u_.i.type = j_int;
1528 }
1529 inline Json::Json(unsigned long long x) {
1530     u_.u.x = x;
1531     u_.u.type = j_unsigned;
1532 }
1533 inline Json::Json(double x) {
1534     u_.d.x = x;
1535     u_.d.type = j_double;
1536 }
1537 inline Json::Json(bool x) {
1538     u_.i.x = x;
1539     u_.i.type = j_bool;
1540 }
1541 inline Json::Json(const String& x) {
1542     u_.str = x.internal_rep();
1543     u_.str.ref();
1544 }
1545 inline Json::Json(Str x) {
1546     u_.str.reset_ref();
1547     String str(x);
1548     str.swap(u_.str);
1549 }
1550 inline Json::Json(const char* x) {
1551     u_.str.reset_ref();
1552     String str(x);
1553     str.swap(u_.str);
1554 }
1555 /** @brief Construct an array Json containing the elements of @a x. */
1556 template <typename T>
1557 inline Json::Json(const std::vector<T> &x) {
1558     u_.a.type = j_array;
1559     u_.a.x = ArrayJson::make(int(x.size()));
1560     for (typename std::vector<T>::const_iterator it = x.begin();
1561          it != x.end(); ++it) {
1562         new((void*) &u_.a.x->a[u_.a.x->size]) Json(*it);
1563         ++u_.a.x->size;
1564     }
1565 }
1566
1567 template <typename T> struct Json_iterator_initializer {
1568     template <typename I>
1569     static inline void run(Json& j, I first, I last) {
1570         for (j = Json::make_array(); first != last; ++first)
1571             j.push_back(Json(*first));
1572     }
1573 };
1574 template <typename T, typename U>
1575 struct Json_iterator_initializer<std::pair<T, U> > {
1576     template <typename I>
1577     static inline void run(Json& j, I first, I last) {
1578         for (j = Json::make_object(); first != last; ++first)
1579             j.set((*first).first, (*first).second);
1580     }
1581 };
1582
1583 /** @brief Construct an array Json containing the elements in [@a first,
1584     @a last). */
1585 template <typename T>
1586 inline Json::Json(T first, T last) {
1587     u_.a.type = 0;
1588     Json_iterator_initializer<typename std::iterator_traits<T>::value_type>::run(*this, first, last);
1589 }
1590
1591 /** @cond never */
1592 inline void Json::deref() {
1593     if (u_.x.type < 0)
1594         u_.str.deref();
1595     else if (u_.x.x && unsigned(u_.x.type - j_array) < 2)
1596         u_.x.x->deref(json_type(u_.x.type));
1597 }
1598 /** @endcond never */
1599
1600 inline Json::~Json() {
1601     deref();
1602 }
1603
1604 /** @brief Return an empty array-valued Json. */
1605 inline Json Json::make_array() {
1606     Json j;
1607     j.u_.x.type = j_array;
1608     return j;
1609 }
1610 /** @brief Return an array-valued Json containing @a args. */
1611 template <typename... Args>
1612 inline Json Json::array(Args&&... args) {
1613     Json j;
1614     j.u_.x.type = j_array;
1615     j.reserve(sizeof...(args));
1616     j.push_back_list(std::forward<Args>(args)...);
1617     return j;
1618 }
1619 /** @brief Return an empty array-valued Json with reserved space for @a n items. */
1620 inline Json Json::make_array_reserve(int n) {
1621     Json j;
1622     j.u_.a.type = j_array;
1623     j.u_.a.x = n ? ArrayJson::make(n) : 0;
1624     return j;
1625 }
1626 /** @brief Return an empty object-valued Json. */
1627 inline Json Json::make_object() {
1628     Json j;
1629     j.u_.o.type = j_object;
1630     return j;
1631 }
1632 /** @brief Return an empty object-valued Json. */
1633 template <typename... Args>
1634 inline Json Json::object(Args&&... rest) {
1635     Json j;
1636     j.u_.o.type = j_object;
1637     j.set_list(std::forward<Args>(rest)...);
1638     return j;
1639 }
1640 /** @brief Return a string-valued Json. */
1641 inline Json Json::make_string(const String &x) {
1642     return Json(x);
1643 }
1644 /** @overload */
1645 inline Json Json::make_string(const char *s, int len) {
1646     return Json(String(s, len));
1647 }
1648
1649 /** @brief Test if this Json is truthy. */
1650 inline bool Json::truthy() const {
1651     return (u_.x.x ? u_.x.type >= 0 || u_.str.length
1652             : (unsigned) (u_.x.type - 1) < (unsigned) (j_int - 1));
1653 }
1654 /** @brief Test if this Json is falsy. */
1655 inline bool Json::falsy() const {
1656     return !truthy();
1657 }
1658 /** @brief Test if this Json is truthy.
1659     @sa empty() */
1660 inline Json::operator unspecified_bool_type() const {
1661     return truthy() ? &Json::is_null : 0;
1662 }
1663 /** @brief Return true if this Json is falsy. */
1664 inline bool Json::operator!() const {
1665     return !truthy();
1666 }
1667
1668 /** @brief Test this Json's type. */
1669 inline bool Json::is_null() const {
1670     return !u_.x.x && u_.x.type == j_null;
1671 }
1672 inline bool Json::is_int() const {
1673     return unsigned(u_.x.type - j_int) <= unsigned(j_unsigned - j_int);
1674 }
1675 inline bool Json::is_i() const {
1676     return is_int();
1677 }
1678 inline bool Json::is_unsigned() const {
1679     return u_.x.type == j_unsigned;
1680 }
1681 inline bool Json::is_u() const {
1682     return is_unsigned();
1683 }
1684 inline bool Json::is_signed() const {
1685     return u_.x.type == j_int;
1686 }
1687 inline bool Json::is_nonnegint() const {
1688     return u_.x.type == j_unsigned
1689         || (u_.x.type == j_int && u_.i.x >= 0);
1690 }
1691 inline bool Json::is_double() const {
1692     return u_.x.type == j_double;
1693 }
1694 inline bool Json::is_d() const {
1695     return is_double();
1696 }
1697 inline bool Json::is_number() const {
1698     return unsigned(u_.x.type - j_int) <= unsigned(j_double - j_int);
1699 }
1700 inline bool Json::is_n() const {
1701     return is_number();
1702 }
1703 inline bool Json::is_bool() const {
1704     return u_.x.type == j_bool;
1705 }
1706 inline bool Json::is_b() const {
1707     return is_bool();
1708 }
1709 inline bool Json::is_string() const {
1710     return u_.x.x && u_.x.type <= 0;
1711 }
1712 inline bool Json::is_s() const {
1713     return is_string();
1714 }
1715 inline bool Json::is_array() const {
1716     return u_.x.type == j_array;
1717 }
1718 inline bool Json::is_a() const {
1719     return is_array();
1720 }
1721 inline bool Json::is_object() const {
1722     return u_.x.type == j_object;
1723 }
1724 inline bool Json::is_o() const {
1725     return is_object();
1726 }
1727 /** @brief Test if this Json is a primitive value, not including null. */
1728 inline bool Json::is_primitive() const {
1729     return u_.x.type >= j_int || (u_.x.x && u_.x.type <= 0);
1730 }
1731
1732 /** @brief Return true if this Json is null, an empty array, or an empty
1733     object. */
1734 inline bool Json::empty() const {
1735     return unsigned(u_.x.type) < unsigned(j_int)
1736         && (!u_.x.x || u_.x.x->size == 0);
1737 }
1738 /** @brief Return the number of elements in this complex Json.
1739     @pre is_array() || is_object() || is_null() */
1740 inline Json::size_type Json::size() const {
1741     precondition(unsigned(u_.x.type) < unsigned(j_int));
1742     return u_.x.x ? u_.x.x->size : 0;
1743 }
1744 /** @brief Test if this complex Json is shared. */
1745 inline bool Json::shared() const {
1746     return u_.x.x && (u_.x.type == j_array || u_.x.type == j_object)
1747         && u_.x.x->refcount != 1;
1748 }
1749
1750 // Primitive methods
1751
1752 /** @brief Return this Json converted to an integer.
1753
1754     Converts any Json to an integer value. Numeric Jsons convert as you'd
1755     expect. Null Jsons convert to 0; false boolean Jsons to 0 and true
1756     boolean Jsons to 1; string Jsons to a number parsed from their initial
1757     portions; and array and object Jsons to size().
1758     @sa as_i() */
1759 inline int64_t Json::to_i() const {
1760     if (is_int())
1761         return u_.i.x;
1762     else
1763         return hard_to_i();
1764 }
1765
1766 inline uint64_t Json::to_u() const {
1767     if (is_int())
1768         return u_.u.x;
1769     else
1770         return hard_to_u();
1771 }
1772
1773 /** @brief Extract this integer Json's value into @a x.
1774     @param[out] x value storage
1775     @return True iff this Json stores an integer value.
1776
1777     If false is returned (!is_number() or the number is not parseable as a
1778     pure integer), @a x remains unchanged. */
1779 inline bool Json::to_i(int& x) const {
1780     if (is_int()) {
1781         x = u_.i.x;
1782         return true;
1783     } else if (is_double() && u_.d.x == double(int(u_.d.x))) {
1784         x = int(u_.d.x);
1785         return true;
1786     } else
1787         return false;
1788 }
1789
1790 /** @overload */
1791 inline bool Json::to_i(unsigned& x) const {
1792     if (is_int()) {
1793         x = u_.u.x;
1794         return true;
1795     } else if (is_double() && u_.d.x == double(unsigned(u_.d.x))) {
1796         x = unsigned(u_.d.x);
1797         return true;
1798     } else
1799         return false;
1800 }
1801
1802 /** @overload */
1803 inline bool Json::to_i(long& x) const {
1804     if (is_int()) {
1805         x = u_.i.x;
1806         return true;
1807     } else if (is_double() && u_.d.x == double(long(u_.d.x))) {
1808         x = long(u_.d.x);
1809         return true;
1810     } else
1811         return false;
1812 }
1813
1814 /** @overload */
1815 inline bool Json::to_i(unsigned long& x) const {
1816     if (is_int()) {
1817         x = u_.u.x;
1818         return true;
1819     } else if (is_double() && u_.d.x == double((unsigned long) u_.d.x)) {
1820         x = (unsigned long) u_.d.x;
1821         return true;
1822     } else
1823         return false;
1824 }
1825
1826 /** @overload */
1827 inline bool Json::to_i(long long& x) const {
1828     if (is_int()) {
1829         x = u_.i.x;
1830         return true;
1831     } else if (is_double() && u_.d.x == double((long long) u_.d.x)) {
1832         x = (long long) u_.d.x;
1833         return true;
1834     } else
1835         return false;
1836 }
1837
1838 /** @overload */
1839 inline bool Json::to_i(unsigned long long& x) const {
1840     if (is_int()) {
1841         x = u_.u.x;
1842         return true;
1843     } else if (is_double() && u_.d.x == double((unsigned long long) u_.d.x)) {
1844         x = (unsigned long long) u_.d.x;
1845         return true;
1846     } else
1847         return false;
1848 }
1849
1850 /** @brief Return this Json converted to a 64-bit unsigned integer.
1851
1852     See to_i() for the conversion rules. */
1853 inline uint64_t Json::to_u64() const {
1854     return to_u();
1855 }
1856
1857 /** @brief Return the integer value of this numeric Json.
1858     @pre is_number()
1859     @sa to_i() */
1860 inline int64_t Json::as_i() const {
1861     precondition(is_int() || is_double());
1862     return is_int() ? u_.i.x : int64_t(u_.d.x);
1863 }
1864
1865 /** @brief Return the integer value of this numeric Json or @a default_value. */
1866 inline int64_t Json::as_i(int64_t default_value) const {
1867     if (is_int() || is_double())
1868         return as_i();
1869     else
1870         return default_value;
1871 }
1872
1873 /** @brief Return the unsigned integer value of this numeric Json.
1874     @pre is_number()
1875     @sa to_i() */
1876 inline uint64_t Json::as_u() const {
1877     precondition(is_int() || is_double());
1878     return is_int() ? u_.u.x : uint64_t(u_.d.x);
1879 }
1880
1881 /** @brief Return the integer value of this numeric Json or @a default_value. */
1882 inline uint64_t Json::as_u(uint64_t default_value) const {
1883     if (is_int() || is_double())
1884         return as_u();
1885     else
1886         return default_value;
1887 }
1888
1889 /** @brief Return this Json converted to a double.
1890
1891     Converts any Json to an integer value. Numeric Jsons convert as you'd
1892     expect. Null Jsons convert to 0; false boolean Jsons to 0 and true
1893     boolean Jsons to 1; string Jsons to a number parsed from their initial
1894     portions; and array and object Jsons to size().
1895     @sa as_d() */
1896 inline double Json::to_d() const {
1897     if (is_double())
1898         return u_.d.x;
1899     else
1900         return hard_to_d();
1901 }
1902
1903 /** @brief Extract this numeric Json's value into @a x.
1904     @param[out] x value storage
1905     @return True iff is_number().
1906
1907     If !is_number(), @a x remains unchanged. */
1908 inline bool Json::to_d(double& x) const {
1909     if (is_double() || is_int()) {
1910         x = to_d();
1911         return true;
1912     } else
1913         return false;
1914 }
1915
1916 /** @brief Return the double value of this numeric Json.
1917     @pre is_number()
1918     @sa to_d() */
1919 inline double Json::as_d() const {
1920     precondition(is_double() || is_int());
1921     if (is_double())
1922         return u_.d.x;
1923     else if (u_.x.type == j_int)
1924         return u_.i.x;
1925     else
1926         return u_.u.x;
1927 }
1928
1929 /** @brief Return the double value of this numeric Json or @a default_value. */
1930 inline double Json::as_d(double default_value) const {
1931     if (!is_double() && !is_int())
1932         return default_value;
1933     else
1934         return as_d();
1935 }
1936
1937 /** @brief Return this Json converted to a boolean.
1938
1939     Converts any Json to a boolean value. Boolean Jsons convert as you'd
1940     expect. Null Jsons convert to false; zero-valued numeric Jsons to false,
1941     and other numeric Jsons to true; empty string Jsons to false, and other
1942     string Jsons to true; and array and object Jsons to !empty().
1943     @sa as_b() */
1944 inline bool Json::to_b() const {
1945     if (is_bool())
1946         return u_.i.x;
1947     else
1948         return hard_to_b();
1949 }
1950
1951 /** @brief Extract this boolean Json's value into @a x.
1952     @param[out] x value storage
1953     @return True iff is_bool().
1954
1955     If !is_bool(), @a x remains unchanged. */
1956 inline bool Json::to_b(bool& x) const {
1957     if (is_bool()) {
1958         x = u_.i.x;
1959         return true;
1960     } else
1961         return false;
1962 }
1963
1964 /** @brief Return the value of this boolean Json.
1965     @pre is_bool()
1966     @sa to_b() */
1967 inline bool Json::as_b() const {
1968     precondition(is_bool());
1969     return u_.i.x;
1970 }
1971
1972 /** @brief Return the boolean value of this numeric Json or @a default_value. */
1973 inline bool Json::as_b(bool default_value) const {
1974     if (is_bool())
1975         return as_b();
1976     else
1977         return default_value;
1978 }
1979
1980 /** @brief Return this Json converted to a string.
1981
1982     Converts any Json to a string value. String Jsons convert as you'd expect.
1983     Null Jsons convert to the empty string; numeric Jsons to their string
1984     values; boolean Jsons to "false" or "true"; and array and object Jsons to
1985     "[Array]" and "[Object]", respectively.
1986     @sa as_s() */
1987 inline String Json::to_s() const {
1988     if (u_.x.type <= 0 && u_.x.x)
1989         return String(u_.str);
1990     else
1991         return hard_to_s();
1992 }
1993
1994 /** @brief Extract this string Json's value into @a x.
1995     @param[out] x value storage
1996     @return True iff is_string().
1997
1998     If !is_string(), @a x remains unchanged. */
1999 inline bool Json::to_s(Str& x) const {
2000     if (u_.x.type <= 0 && u_.x.x) {
2001         x.assign(u_.str.data, u_.str.length);
2002         return true;
2003     } else
2004         return false;
2005 }
2006
2007 /** @brief Extract this string Json's value into @a x.
2008     @param[out] x value storage
2009     @return True iff is_string().
2010
2011     If !is_string(), @a x remains unchanged. */
2012 inline bool Json::to_s(String& x) const {
2013     if (u_.x.type <= 0 && u_.x.x) {
2014         x.assign(u_.str);
2015         return true;
2016     } else
2017         return false;
2018 }
2019
2020 /** @brief Return the value of this string Json.
2021     @pre is_string()
2022     @sa to_s() */
2023 inline const String& Json::as_s() const {
2024     precondition(u_.x.type <= 0 && u_.x.x);
2025     return reinterpret_cast<const String&>(u_.str);
2026 }
2027
2028 /** @overload */
2029 inline String& Json::as_s() {
2030     precondition(u_.x.type <= 0 && u_.x.x);
2031     return reinterpret_cast<String&>(u_.str);
2032 }
2033
2034 /** @brief Return the value of this string Json or @a default_value. */
2035 inline const String& Json::as_s(const String& default_value) const {
2036     if (u_.x.type > 0 || !u_.x.x)
2037         return default_value;
2038     else
2039         return as_s();
2040 }
2041
2042 inline void Json::force_number() {
2043     precondition((u_.x.type == j_null && !u_.x.x) || u_.x.type == j_int || u_.x.type == j_double);
2044     if (u_.x.type == j_null && !u_.x.x)
2045         u_.x.type = j_int;
2046 }
2047
2048 inline void Json::force_double() {
2049     precondition((u_.x.type == j_null && !u_.x.x) || u_.x.type == j_int || u_.x.type == j_double);
2050     if (u_.x.type == j_null && !u_.x.x)
2051         u_.x.type = j_double;
2052     else if (u_.x.type == j_int) {
2053         u_.d.x = u_.i.x;
2054         u_.d.type = j_double;
2055     } else if (u_.x.type == j_unsigned) {
2056         u_.d.x = u_.u.x;
2057         u_.d.type = j_double;
2058     }
2059 }
2060
2061
2062 // Object methods
2063
2064 /** @brief Return 1 if this object Json contains @a key, 0 otherwise.
2065
2066     Returns 0 if this is not an object Json. */
2067 inline Json::size_type Json::count(Str key) const {
2068     precondition(u_.x.type == j_null || u_.x.type == j_object);
2069     return u_.o.x ? ojson()->find(key.data(), key.length()) >= 0 : 0;
2070 }
2071
2072 /** @brief Return the value at @a key in an object or array Json.
2073
2074     If this is an array Json, and @a key is the simplest base-10
2075     representation of an integer <em>i</em>, then returns get(<em>i</em>). If
2076     this is neither an array nor an object, returns a null Json. */
2077 inline const Json& Json::get(Str key) const {
2078     int i;
2079     ObjectJson *oj;
2080     if (is_object() && (oj = ojson())
2081         && (i = oj->find(key.data(), key.length())) >= 0)
2082         return oj->item(i).v_.second;
2083     else
2084         return hard_get(key);
2085 }
2086
2087 /** @brief Return a reference to the value of @a key in an object Json.
2088
2089     This Json is first converted to an object. Arrays are converted to objects
2090     with numeric keys. Other types of Json are converted to empty objects.
2091     If !count(@a key), then a null Json is inserted at @a key. */
2092 inline Json& Json::get_insert(const String &key) {
2093     uniqueify_object(true);
2094     return ojson()->get_insert(key);
2095 }
2096
2097 /** @overload */
2098 inline Json& Json::get_insert(Str key) {
2099     uniqueify_object(true);
2100     return ojson()->get_insert(key);
2101 }
2102
2103 /** @overload */
2104 inline Json& Json::get_insert(const char *key) {
2105     uniqueify_object(true);
2106     return ojson()->get_insert(Str(key));
2107 }
2108
2109 /** @brief Return get(@a key).to_i(). */
2110 inline long Json::get_i(Str key) const {
2111     return get(key).to_i();
2112 }
2113
2114 /** @brief Return get(@a key).to_d(). */
2115 inline double Json::get_d(Str key) const {
2116     return get(key).to_d();
2117 }
2118
2119 /** @brief Return get(@a key).to_b(). */
2120 inline bool Json::get_b(Str key) const {
2121     return get(key).to_b();
2122 }
2123
2124 /** @brief Return get(@a key).to_s(). */
2125 inline String Json::get_s(Str key) const {
2126     return get(key).to_s();
2127 }
2128
2129 /** @brief Extract this object Json's value at @a key into @a x.
2130     @param[out] x value storage
2131     @return proxy for *this
2132
2133     @a x is assigned iff count(@a key). The return value is a proxy
2134     object that mostly behaves like *this. However, the proxy is "truthy"
2135     iff count(@a key) and @a x was assigned. The proxy also has status()
2136     methods that return the extraction status. For example:
2137
2138     <code>
2139     Json j = Json::parse("{\"a\":1,\"b\":2}"), x;
2140     assert(j.get("a", x));            // extraction succeeded
2141     assert(x == Json(1));
2142     assert(!j.get("c", x));           // no "c" key
2143     assert(x == Json(1));             // x remains unchanged
2144     assert(!j.get("c", x).status());  // can use ".status()" for clarity
2145
2146     // Can chain .get() methods to extract multiple values
2147     Json a, b, c;
2148     j.get("a", a).get("b", b);
2149     assert(a == Json(1) && b == Json(2));
2150
2151     // Use .status() to return or assign extraction status
2152     bool a_status, b_status, c_status;
2153     j.get("a", a).status(a_status)
2154      .get("b", b).status(b_status)
2155      .get("c", c).status(c_status);
2156     assert(a_status && b_status && !c_status);
2157     </code>
2158
2159     Overloaded versions of @a get() can extract integer, double, boolean,
2160     and string values for specific keys. These versions succeed iff
2161     count(@a key) and the corresponding value has the expected type. For
2162     example:
2163
2164     <code>
2165     Json j = Json::parse("{\"a\":1,\"b\":\"\"}");
2166     int a, b;
2167     bool a_status, b_status;
2168     j.get("a", a).status(a_status).get("b", b).status(b_status);
2169     assert(a_status && a == 1 && !b_status);
2170     </code> */
2171 inline const Json_get_proxy Json::get(Str key, Json& x) const {
2172     int i;
2173     ObjectJson *oj;
2174     if (is_object() && (oj = ojson())
2175         && (i = oj->find(key.data(), key.length())) >= 0) {
2176         x = oj->item(i).v_.second;
2177         return Json_get_proxy(*this, true);
2178     } else
2179         return Json_get_proxy(*this, false);
2180 }
2181
2182 /** @overload */
2183 inline const Json_get_proxy Json::get(Str key, int &x) const {
2184     return Json_get_proxy(*this, get(key).to_i(x));
2185 }
2186
2187 /** @overload */
2188 inline const Json_get_proxy Json::get(Str key, unsigned& x) const {
2189     return Json_get_proxy(*this, get(key).to_i(x));
2190 }
2191
2192 /** @overload */
2193 inline const Json_get_proxy Json::get(Str key, long& x) const {
2194     return Json_get_proxy(*this, get(key).to_i(x));
2195 }
2196
2197 /** @overload */
2198 inline const Json_get_proxy Json::get(Str key, unsigned long& x) const {
2199     return Json_get_proxy(*this, get(key).to_i(x));
2200 }
2201
2202 /** @overload */
2203 inline const Json_get_proxy Json::get(Str key, long long& x) const {
2204     return Json_get_proxy(*this, get(key).to_i(x));
2205 }
2206
2207 /** @overload */
2208 inline const Json_get_proxy Json::get(Str key, unsigned long long& x) const {
2209     return Json_get_proxy(*this, get(key).to_i(x));
2210 }
2211
2212 /** @overload */
2213 inline const Json_get_proxy Json::get(Str key, double& x) const {
2214     return Json_get_proxy(*this, get(key).to_d(x));
2215 }
2216
2217 /** @overload */
2218 inline const Json_get_proxy Json::get(Str key, bool& x) const {
2219     return Json_get_proxy(*this, get(key).to_b(x));
2220 }
2221
2222 /** @overload */
2223 inline const Json_get_proxy Json::get(Str key, Str& x) const {
2224     return Json_get_proxy(*this, get(key).to_s(x));
2225 }
2226
2227 /** @overload */
2228 inline const Json_get_proxy Json::get(Str key, String& x) const {
2229     return Json_get_proxy(*this, get(key).to_s(x));
2230 }
2231
2232
2233 /** @brief Return the value at @a key in an object or array Json.
2234     @sa Json::get() */
2235 inline const Json& Json::operator[](Str key) const {
2236     return get(key);
2237 }
2238
2239 /** @brief Return a proxy reference to the value at @a key in an object Json.
2240
2241     Returns the current @a key value if it exists. Otherwise, returns a proxy
2242     that acts like a null Json. If this proxy is assigned, this Json is
2243     converted to an object as by get_insert(), and then extended as necessary
2244     to contain the new value. */
2245 inline Json_object_proxy<Json> Json::operator[](const String& key) {
2246     return Json_object_proxy<Json>(*this, key);
2247 }
2248
2249 /** @overload */
2250 inline Json_object_str_proxy<Json> Json::operator[](Str key) {
2251     return Json_object_str_proxy<Json>(*this, key);
2252 }
2253
2254 /** @overload */
2255 inline Json_object_str_proxy<Json> Json::operator[](const char* key) {
2256     return Json_object_str_proxy<Json>(*this, key);
2257 }
2258
2259 /** @brief Return the value at @a key in an object Json.
2260     @pre is_object() && count(@a key) */
2261 inline const Json& Json::at(Str key) const {
2262     precondition(is_object() && u_.o.x);
2263     ObjectJson *oj = ojson();
2264     int i = oj->find(key.data(), key.length());
2265     precondition(i >= 0);
2266     return oj->item(i).v_.second;
2267 }
2268
2269 /** @brief Return a reference to the value at @a key in an object Json.
2270     @pre is_object()
2271
2272     Returns a newly-inserted null Json if !count(@a key). */
2273 inline Json& Json::at_insert(const String &key) {
2274     precondition(is_object());
2275     return get_insert(key);
2276 }
2277
2278 /** @overload */
2279 inline Json& Json::at_insert(Str key) {
2280     precondition(is_object());
2281     return get_insert(key);
2282 }
2283
2284 /** @overload */
2285 inline Json& Json::at_insert(const char *key) {
2286     precondition(is_object());
2287     return get_insert(Str(key));
2288 }
2289
2290 /** @brief Set the value of @a key to @a value in this object Json.
2291     @return this Json
2292
2293     An array Json is converted to an object Json with numeric keys. Other
2294     non-object Jsons are converted to empty objects. */
2295 inline Json& Json::set(const String& key, Json value) {
2296     uniqueify_object(true);
2297     ojson()->get_insert(key) = std::move(value);
2298     return *this;
2299 }
2300
2301 /** @overload */
2302 template <typename P>
2303 inline Json& Json::set(const String& key, const Json_proxy_base<P>& value) {
2304     uniqueify_object(true);
2305     ojson()->get_insert(key) = value.cvalue();
2306     return *this;
2307 }
2308
2309 /** @brief Remove the value of @a key from an object Json.
2310     @return this Json
2311     @sa erase() */
2312 inline Json& Json::unset(Str key) {
2313     if (is_object()) {
2314         uniqueify_object(true);
2315         ojson()->erase(key);
2316     }
2317     return *this;
2318 }
2319
2320 /** @brief Add the key-value pairs [key, value, ...] to the object.
2321     @return this Json
2322
2323     An array Json is converted to an object Json with numeric keys. Other
2324     non-object Jsons are converted to empty objects. */
2325 template <typename T, typename... Args>
2326 inline Json& Json::set_list(const String& key, T value, Args&&... rest) {
2327     set(key, std::move(value));
2328     set_list(std::forward<Args>(rest)...);
2329     return *this;
2330 }
2331
2332 /** @overload */
2333 inline Json& Json::set_list() {
2334     return *this;
2335 }
2336
2337 /** @brief Insert element @a x in this object Json.
2338     @param x Pair of key and value.
2339     @return Pair of iterator pointing to key's value and bool indicating
2340     whether the value is newly inserted.
2341     @pre is_object()
2342
2343     An existing element with key @a x.first is not replaced. */
2344 inline std::pair<Json::object_iterator, bool> Json::insert(const object_value_type& x) {
2345     precondition(is_object());
2346     uniqueify_object(false);
2347     ObjectJson *oj = ojson();
2348     int n = oj->n_, i = oj->find_insert(x.first, x.second);
2349     return std::make_pair(object_iterator(this, i), i == n);
2350 }
2351
2352 /** @brief Insert element @a x in this object Json.
2353     @param position Ignored.
2354     @param x Pair of key and value.
2355     @return Pair of iterator pointing to key's value and bool indicating
2356     whether the value is newly inserted.
2357     @pre is_object()
2358
2359     An existing element with key @a x.first is not replaced. */
2360 inline Json::object_iterator Json::insert(object_iterator position, const object_value_type& x) {
2361     (void) position;
2362     return insert(x).first;
2363 }
2364
2365 /** @brief Remove the value pointed to by @a it from an object Json.
2366     @pre is_object()
2367     @return Next iterator */
2368 inline Json::object_iterator Json::erase(Json::object_iterator it) {
2369     precondition(is_object() && it.live() && it.j_ == this);
2370     uniqueify_object(false);
2371     ojson()->erase(it.i_);
2372     ++it;
2373     return it;
2374 }
2375
2376 /** @brief Remove the value of @a key from an object Json.
2377     @pre is_object()
2378     @return Number of items removed */
2379 inline Json::size_type Json::erase(Str key) {
2380     precondition(is_object());
2381     uniqueify_object(false);
2382     return ojson()->erase(key);
2383 }
2384
2385 /** @brief Merge the values of object Json @a x into this object Json.
2386     @pre (is_object() || is_null()) && (x.is_object() || x.is_null())
2387     @return this Json
2388
2389     The key-value pairs in @a x are assigned to this Json. Null Jsons are
2390     silently converted to empty objects, except that if @a x and this Json are
2391     both null, then this Json is left as null. */
2392 inline Json& Json::merge(const Json& x) {
2393     precondition(is_object() || is_null());
2394     precondition(x.is_object() || x.is_null());
2395     if (x.u_.o.x) {
2396         uniqueify_object(false);
2397         ObjectJson *oj = ojson(), *xoj = x.ojson();
2398         const ObjectItem *xb = xoj->os_, *xe = xb + xoj->n_;
2399         for (; xb != xe; ++xb)
2400             if (xb->next_ > -2)
2401                 oj->get_insert(xb->v_.first) = xb->v_.second;
2402     }
2403     return *this;
2404 }
2405
2406 /** @cond never */
2407 template <typename U>
2408 inline Json& Json::merge(const Json_proxy_base<U>& x) {
2409     return merge(x.cvalue());
2410 }
2411 /** @endcond never */
2412
2413
2414 // ARRAY METHODS
2415
2416 /** @brief Return the @a x th array element.
2417
2418     If @a x is out of range of this array, returns a null Json. If this is an
2419     object Json, then returns get(String(@a x)). If this is neither an object
2420     nor an array, returns a null Json. */
2421 inline const Json& Json::get(size_type x) const {
2422     ArrayJson *aj;
2423     if (u_.x.type == j_array && (aj = ajson()) && unsigned(x) < unsigned(aj->size))
2424         return aj->a[x];
2425     else
2426         return hard_get(x);
2427 }
2428
2429 /** @brief Return a reference to the @a x th array element.
2430
2431     If this Json is an object, returns get_insert(String(x)). Otherwise this
2432     Json is first converted to an array; non-arrays are converted to empty
2433     arrays. The array is extended if @a x is out of range. */
2434 inline Json& Json::get_insert(size_type x) {
2435     ArrayJson *aj;
2436     if (u_.x.type == j_array && (aj = ajson()) && aj->refcount == 1
2437         && unsigned(x) < unsigned(aj->size))
2438         return aj->a[x];
2439     else
2440         return hard_get_insert(x);
2441 }
2442
2443 /** @brief Return the @a x th element in an array Json.
2444     @pre is_array()
2445
2446     A null Json is treated like an empty array. */
2447 inline const Json& Json::at(size_type x) const {
2448     precondition(is_array());
2449     return get(x);
2450 }
2451
2452 /** @brief Return a reference to the @a x th element in an array Json.
2453     @pre is_array()
2454
2455     The array is extended if @a x is out of range. */
2456 inline Json& Json::at_insert(size_type x) {
2457     precondition(is_array());
2458     return get_insert(x);
2459 }
2460
2461 /** @brief Return the @a x th array element.
2462
2463     If @a x is out of range of this array, returns a null Json. If this is an
2464     object Json, then returns get(String(@a x)). If this is neither an object
2465     nor an array, returns a null Json. */
2466 inline const Json& Json::operator[](size_type x) const {
2467     return get(x);
2468 }
2469
2470 /** @brief Return a proxy reference to the @a x th array element.
2471
2472     If this Json is an object, returns operator[](String(x)). If this Json is
2473     an array and @a x is in range, returns that element. Otherwise, returns a
2474     proxy that acts like a null Json. If this proxy is assigned, this Json is
2475     converted to an array, and then extended as necessary to contain the new
2476     value. */
2477 inline Json_array_proxy<Json> Json::operator[](size_type x) {
2478     return Json_array_proxy<Json>(*this, x);
2479 }
2480
2481 /** @brief Return the last array element.
2482     @pre is_array() && !empty() */
2483 inline const Json& Json::back() const {
2484     precondition(is_array() && u_.a.x && u_.a.x->size > 0);
2485     return u_.a.x->a[u_.a.x->size - 1];
2486 }
2487
2488 /** @brief Return a reference to the last array element.
2489     @pre is_array() && !empty() */
2490 inline Json& Json::back() {
2491     precondition(is_array() && u_.a.x && u_.a.x->size > 0);
2492     uniqueify_array(false, 0);
2493     return u_.a.x->a[u_.a.x->size - 1];
2494 }
2495
2496 /** @brief Push an element onto the back of the array.
2497     @pre is_array() || is_null()
2498     @return this Json
2499
2500     A null Json is promoted to an array. */
2501 inline Json& Json::push_back(Json x) {
2502     new(uniqueify_array_insert(false, -1)) Json(std::move(x));
2503     return *this;
2504 }
2505
2506 /** @overload */
2507 template <typename P> inline Json& Json::push_back(const Json_proxy_base<P>& x) {
2508     new(uniqueify_array_insert(false, -1)) Json(x.cvalue());
2509     return *this;
2510 }
2511
2512 /** @brief Remove the last element from an array.
2513     @pre is_array() && !empty() */
2514 inline void Json::pop_back() {
2515     precondition(is_array() && u_.a.x && u_.a.x->size > 0);
2516     uniqueify_array(false, 0);
2517     --u_.a.x->size;
2518     u_.a.x->a[u_.a.x->size].~Json();
2519 }
2520
2521 inline Json& Json::push_back_list() {
2522     return *this;
2523 }
2524
2525 /** @brief Insert the items [first, rest...] onto the back of this array.
2526     @pre is_array() || is_null()
2527     @return this Json
2528
2529     A null Json is promoted to an array. */
2530 template <typename T, typename... Args>
2531 inline Json& Json::push_back_list(T first, Args&&... rest) {
2532     push_back(std::move(first));
2533     push_back_list(std::forward<Args>(rest)...);
2534     return *this;
2535 }
2536
2537
2538 /** @brief Insert an element into the array.
2539     @pre is_array() || is_null()
2540     @return this Json
2541
2542     A null Json is promoted to an array. */
2543 inline Json::array_iterator Json::insert(array_iterator position, Json x) {
2544     precondition(position >= abegin() && position <= aend());
2545     size_type pos = position - abegin();
2546     new(uniqueify_array_insert(false, pos)) Json(std::move(x));
2547     return abegin() + pos;
2548 }
2549
2550 /** @overload */
2551 template <typename P> inline Json::array_iterator Json::insert(array_iterator position, const Json_proxy_base<P>& x) {
2552     precondition(position >= abegin() && position <= aend());
2553     size_type pos = position - abegin();
2554     new(uniqueify_array_insert(false, pos)) Json(x.cvalue());
2555     return abegin() + pos;
2556 }
2557
2558 inline Json::array_iterator Json::erase(array_iterator position) {
2559     return erase(position, position + 1);
2560 }
2561
2562
2563 inline Json* Json::array_data() {
2564     precondition(is_null() || is_array());
2565     uniqueify_array(false, 0);
2566     return u_.a.x ? u_.a.x->a : 0;
2567 }
2568
2569 inline const Json* Json::array_data() const {
2570     precondition(is_null() || is_array());
2571     return u_.a.x ? u_.a.x->a : 0;
2572 }
2573
2574 inline const Json* Json::array_cdata() const {
2575     precondition(is_null() || is_array());
2576     return u_.a.x ? u_.a.x->a : 0;
2577 }
2578
2579 inline Json* Json::end_array_data() {
2580     precondition(is_null() || is_array());
2581     uniqueify_array(false, 0);
2582     return u_.a.x ? u_.a.x->a + u_.a.x->size : 0;
2583 }
2584
2585 inline const Json* Json::end_array_data() const {
2586     precondition(is_null() || is_array());
2587     return u_.a.x ? u_.a.x->a + u_.a.x->size : 0;
2588 }
2589
2590 inline const Json* Json::end_array_cdata() const {
2591     precondition(is_null() || is_array());
2592     return u_.a.x ? u_.a.x->a + u_.a.x->size : 0;
2593 }
2594
2595
2596 inline Json::const_object_iterator Json::cobegin() const {
2597     precondition(is_null() || is_object());
2598     return const_object_iterator(this, 0);
2599 }
2600
2601 inline Json::const_object_iterator Json::coend() const {
2602     precondition(is_null() || is_object());
2603     return const_object_iterator(this, -1);
2604 }
2605
2606 inline Json::const_object_iterator Json::obegin() const {
2607     return cobegin();
2608 }
2609
2610 inline Json::const_object_iterator Json::oend() const {
2611     return coend();
2612 }
2613
2614 inline Json::object_iterator Json::obegin() {
2615     precondition(is_null() || is_object());
2616     return object_iterator(this, 0);
2617 }
2618
2619 inline Json::object_iterator Json::oend() {
2620     precondition(is_null() || is_object());
2621     return object_iterator(this, -1);
2622 }
2623
2624 inline Json::const_array_iterator Json::cabegin() const {
2625     precondition(is_null() || is_array());
2626     return const_array_iterator(this, 0);
2627 }
2628
2629 inline Json::const_array_iterator Json::caend() const {
2630     precondition(is_null() || is_array());
2631     ArrayJson *aj = ajson();
2632     return const_array_iterator(this, aj ? aj->size : 0);
2633 }
2634
2635 inline Json::const_array_iterator Json::abegin() const {
2636     return cabegin();
2637 }
2638
2639 inline Json::const_array_iterator Json::aend() const {
2640     return caend();
2641 }
2642
2643 inline Json::array_iterator Json::abegin() {
2644     precondition(is_null() || is_array());
2645     return array_iterator(this, 0);
2646 }
2647
2648 inline Json::array_iterator Json::aend() {
2649     precondition(is_null() || is_array());
2650     ArrayJson *aj = ajson();
2651     return array_iterator(this, aj ? aj->size : 0);
2652 }
2653
2654 inline Json::const_iterator Json::cbegin() const {
2655     return const_iterator(this, 0);
2656 }
2657
2658 inline Json::const_iterator Json::cend() const {
2659     return const_iterator(this, -1);
2660 }
2661
2662 inline Json::iterator Json::begin() {
2663     return iterator(this, 0);
2664 }
2665
2666 inline Json::iterator Json::end() {
2667     return iterator(this, -1);
2668 }
2669
2670 inline Json::const_iterator Json::begin() const {
2671     return cbegin();
2672 }
2673
2674 inline Json::const_iterator Json::end() const {
2675     return cend();
2676 }
2677
2678
2679 // Unparsing
2680 class Json::unparse_manipulator {
2681   public:
2682     unparse_manipulator()
2683         : indent_depth_(0), tab_width_(0), newline_terminator_(false),
2684           space_separator_(false) {
2685     }
2686     int indent_depth() const {
2687         return indent_depth_;
2688     }
2689     unparse_manipulator indent_depth(int x) const {
2690         unparse_manipulator m(*this);
2691         m.indent_depth_ = x;
2692         return m;
2693     }
2694     int tab_width() const {
2695         return tab_width_;
2696     }
2697     unparse_manipulator tab_width(int x) const {
2698         unparse_manipulator m(*this);
2699         m.tab_width_ = x;
2700         return m;
2701     }
2702     bool newline_terminator() const {
2703         return newline_terminator_;
2704     }
2705     unparse_manipulator newline_terminator(bool x) const {
2706         unparse_manipulator m(*this);
2707         m.newline_terminator_ = x;
2708         return m;
2709     }
2710     bool space_separator() const {
2711         return space_separator_;
2712     }
2713     unparse_manipulator space_separator(bool x) const {
2714         unparse_manipulator m(*this);
2715         m.space_separator_ = x;
2716         return m;
2717     }
2718     bool empty() const {
2719         return !indent_depth_ && !newline_terminator_ && !space_separator_;
2720     }
2721   private:
2722     int indent_depth_;
2723     int tab_width_;
2724     bool newline_terminator_;
2725     bool space_separator_;
2726 };
2727
2728 inline Json::unparse_manipulator Json::indent_depth(int x) {
2729     return unparse_manipulator().indent_depth(x);
2730 }
2731 inline Json::unparse_manipulator Json::tab_width(int x) {
2732     return unparse_manipulator().tab_width(x);
2733 }
2734 inline Json::unparse_manipulator Json::newline_terminator(bool x) {
2735     return unparse_manipulator().newline_terminator(x);
2736 }
2737 inline Json::unparse_manipulator Json::space_separator(bool x) {
2738     return unparse_manipulator().space_separator(x);
2739 }
2740
2741 /** @brief Return the string representation of this Json. */
2742 inline String Json::unparse() const {
2743     StringAccum sa;
2744     hard_unparse(sa, default_manipulator, 0);
2745     return sa.take_string();
2746 }
2747
2748 /** @brief Return the string representation of this Json.
2749     @param add_newline If true, add a final newline. */
2750 inline String Json::unparse(const unparse_manipulator &m) const {
2751     StringAccum sa;
2752     hard_unparse(sa, m, 0);
2753     return sa.take_string();
2754 }
2755
2756 /** @brief Unparse the string representation of this Json into @a sa. */
2757 inline void Json::unparse(StringAccum &sa) const {
2758     hard_unparse(sa, default_manipulator, 0);
2759 }
2760
2761 /** @brief Unparse the string representation of this Json into @a sa. */
2762 inline void Json::unparse(StringAccum &sa, const unparse_manipulator &m) const {
2763     hard_unparse(sa, m, 0);
2764 }
2765
2766
2767 // Parsing
2768
2769 class Json::streaming_parser {
2770   public:
2771     inline streaming_parser();
2772     inline void reset();
2773
2774     inline bool done() const;
2775     inline bool success() const;
2776     inline bool error() const;
2777
2778     inline size_t consume(const char* first, size_t length,
2779                           const String& str = String(),
2780                           bool complete = false);
2781     inline const char* consume(const char* first, const char* last,
2782                                const String& str = String(),
2783                                bool complete = false);
2784     const uint8_t* consume(const uint8_t* first, const uint8_t* last,
2785                            const String& str = String(),
2786                            bool complete = false);
2787
2788     inline Json& result();
2789     inline const Json& result() const;
2790
2791   private:
2792     enum {
2793         st_final = -2, st_error = -1,
2794         st_partlenmask = 0x0F, st_partmask = 0xFF,
2795         st_stringpart = 0x10, st_primitivepart = 0x20, st_numberpart = 0x40,
2796
2797         st_initial = 0x000,
2798         st_array_initial = 0x100,
2799         st_array_value = 0x200,
2800         st_object_value = 0x300,
2801
2802         st_array_delim = 0x400,
2803         st_object_delim = 0x500,
2804         st_object_initial = 0x600,
2805         st_object_key = 0x700,
2806         st_object_colon = 0x800,
2807
2808         max_depth = 2048
2809     };
2810
2811     int state_;
2812     std::vector<Json*> stack_;
2813     String str_;
2814     Json json_;
2815
2816     inline Json* current();
2817     const uint8_t* error_at(const uint8_t* here);
2818     const uint8_t* consume_string(const uint8_t* first, const uint8_t* last, const String& str);
2819     const uint8_t* consume_backslash(StringAccum& sa, const uint8_t* first, const uint8_t* last);
2820     const uint8_t* consume_stringpart(StringAccum& sa, const uint8_t* first, const uint8_t* last);
2821     const uint8_t* consume_primitive(const uint8_t* first, const uint8_t* last, Json& j);
2822     const uint8_t* consume_number(const uint8_t* first, const uint8_t* last, const String& str, bool complete, Json& j);
2823 };
2824
2825 inline Json::streaming_parser::streaming_parser()
2826     : state_(st_initial) {
2827 }
2828
2829 inline void Json::streaming_parser::reset() {
2830     state_ = st_initial;
2831     stack_.clear();
2832 }
2833
2834 inline bool Json::streaming_parser::done() const {
2835     return state_ < 0;
2836 }
2837
2838 inline bool Json::streaming_parser::success() const {
2839     return state_ == st_final;
2840 }
2841
2842 inline bool Json::streaming_parser::error() const {
2843     return state_ == st_error;
2844 }
2845
2846 inline size_t Json::streaming_parser::consume(const char* first, size_t length,
2847                                               const String& str,
2848                                               bool complete) {
2849     const uint8_t* ufirst = reinterpret_cast<const uint8_t*>(first);
2850     return consume(ufirst, ufirst + length, str, complete) - ufirst;
2851 }
2852
2853 inline const char* Json::streaming_parser::consume(const char* first,
2854                                                    const char* last,
2855                                                    const String& str,
2856                                                    bool complete) {
2857     return reinterpret_cast<const char*>
2858         (consume(reinterpret_cast<const uint8_t*>(first),
2859                  reinterpret_cast<const uint8_t*>(last), str, complete));
2860 }
2861
2862 inline Json& Json::streaming_parser::result() {
2863     return json_;
2864 }
2865
2866 inline const Json& Json::streaming_parser::result() const {
2867     return json_;
2868 }
2869
2870
2871 /** @brief Parse @a str as UTF-8 JSON into this Json object.
2872     @return true iff the parse succeeded.
2873
2874     An unsuccessful parse does not modify *this. */
2875 inline bool Json::assign_parse(const String &str) {
2876     return assign_parse(str.begin(), str.end(), str);
2877 }
2878
2879 /** @brief Parse [@a first, @a last) as UTF-8 JSON into this Json object.
2880     @return true iff the parse succeeded.
2881
2882     An unsuccessful parse does not modify *this. */
2883 inline bool Json::assign_parse(const char *first, const char *last) {
2884     return assign_parse(first, last, String());
2885 }
2886
2887 /** @brief Return @a str parsed as UTF-8 JSON.
2888
2889     Returns a null JSON object if the parse fails. */
2890 inline Json Json::parse(const String &str) {
2891     Json j;
2892     (void) j.assign_parse(str);
2893     return j;
2894 }
2895
2896 /** @brief Return [@a first, @a last) parsed as UTF-8 JSON.
2897
2898     Returns a null JSON object if the parse fails. */
2899 inline Json Json::parse(const char *first, const char *last) {
2900     Json j;
2901     (void) j.assign_parse(first, last);
2902     return j;
2903 }
2904
2905
2906 // Assignment
2907
2908 inline Json& Json::operator=(const Json& x) {
2909     if (x.u_.x.type < 0)
2910         x.u_.str.ref();
2911     else if (x.u_.x.x && (x.u_.x.type == j_array || x.u_.x.type == j_object))
2912         x.u_.x.x->ref();
2913     deref();
2914     u_ = x.u_;
2915     return *this;
2916 }
2917
2918 #if HAVE_CXX_RVALUE_REFERENCES
2919 inline Json& Json::operator=(Json&& x) {
2920     using std::swap;
2921     swap(u_, x.u_);
2922     return *this;
2923 }
2924 #endif
2925
2926 /** @cond never */
2927 template <typename U>
2928 inline Json& Json::operator=(const Json_proxy_base<U> &x) {
2929     return *this = x.cvalue();
2930 }
2931
2932 inline Json& Json::operator=(int x) {
2933     deref();
2934     u_.i.x = x;
2935     u_.i.type = j_int;
2936     return *this;
2937 }
2938
2939 inline Json& Json::operator=(unsigned x) {
2940     deref();
2941     u_.u.x = x;
2942     u_.u.type = j_unsigned;
2943     return *this;
2944 }
2945
2946 inline Json& Json::operator=(long x) {
2947     deref();
2948     u_.i.x = x;
2949     u_.i.type = j_int;
2950     return *this;
2951 }
2952
2953 inline Json& Json::operator=(unsigned long x) {
2954     deref();
2955     u_.u.x = x;
2956     u_.u.type = j_unsigned;
2957     return *this;
2958 }
2959
2960 inline Json& Json::operator=(long long x) {
2961     deref();
2962     u_.i.x = x;
2963     u_.i.type = j_int;
2964     return *this;
2965 }
2966
2967 inline Json& Json::operator=(unsigned long long x) {
2968     deref();
2969     u_.u.x = x;
2970     u_.u.type = j_unsigned;
2971     return *this;
2972 }
2973
2974 inline Json& Json::operator=(double x) {
2975     deref();
2976     u_.d.x = x;
2977     u_.d.type = j_double;
2978     return *this;
2979 }
2980
2981 inline Json& Json::operator=(bool x) {
2982     deref();
2983     u_.i.x = x;
2984     u_.i.type = j_bool;
2985     return *this;
2986 }
2987
2988 inline Json& Json::operator=(const String& x) {
2989     deref();
2990     u_.str = x.internal_rep();
2991     u_.str.ref();
2992     return *this;
2993 }
2994 /** @endcond never */
2995
2996 inline Json& Json::operator++() {
2997     return *this += 1;
2998 }
2999 inline void Json::operator++(int) {
3000     ++(*this);
3001 }
3002 inline Json& Json::operator--() {
3003     return *this += -1;
3004 }
3005 inline void Json::operator--(int) {
3006     --(*this);
3007 }
3008 inline Json& Json::add(double x) {
3009     force_double();
3010     u_.d.x += x;
3011     return *this;
3012 }
3013 template <typename T>
3014 inline Json& Json::add(T x) {
3015     force_number();
3016     if (is_int())
3017         u_.u.x += x;
3018     else
3019         u_.d.x += x;
3020     return *this;
3021 }
3022 inline Json& Json::subtract(double x) {
3023     force_double();
3024     u_.d.x -= x;
3025     return *this;
3026 }
3027 template <typename T>
3028 inline Json& Json::subtract(T x) {
3029     force_number();
3030     if (is_int())
3031         u_.u.x -= x;
3032     else
3033         u_.d.x -= x;
3034     return *this;
3035 }
3036 inline Json& Json::operator+=(int x) {
3037     return add(x);
3038 }
3039 inline Json& Json::operator+=(unsigned x) {
3040     return add(x);
3041 }
3042 inline Json& Json::operator+=(long x) {
3043     return add(x);
3044 }
3045 inline Json& Json::operator+=(unsigned long x) {
3046     return add(x);
3047 }
3048 inline Json& Json::operator+=(long long x) {
3049     return add(x);
3050 }
3051 inline Json& Json::operator+=(unsigned long long x) {
3052     return add(x);
3053 }
3054 inline Json& Json::operator+=(double x) {
3055     return add(x);
3056 }
3057 inline Json& Json::operator-=(int x) {
3058     return subtract(x);
3059 }
3060 inline Json& Json::operator-=(unsigned x) {
3061     return subtract(x);
3062 }
3063 inline Json& Json::operator-=(long x) {
3064     return subtract(x);
3065 }
3066 inline Json& Json::operator-=(unsigned long x) {
3067     return subtract(x);
3068 }
3069 inline Json& Json::operator-=(long long x) {
3070     return subtract(x);
3071 }
3072 inline Json& Json::operator-=(unsigned long long x) {
3073     return subtract(x);
3074 }
3075 inline Json& Json::operator-=(double x) {
3076     return subtract(x);
3077 }
3078 inline Json& Json::operator+=(const Json& x) {
3079     if (!x.is_null()) {
3080         // XXX what if both are integers
3081         force_number();
3082         u_.d.x = as_d() + x.as_d();
3083         u_.d.type = j_double;
3084     }
3085     return *this;
3086 }
3087 inline Json& Json::operator-=(const Json& x) {
3088     if (!x.is_null()) {
3089         // XXX what if both are integers
3090         force_number();
3091         u_.d.x = as_d() - x.as_d();
3092         u_.d.type = j_double;
3093     }
3094     return *this;
3095 }
3096 inline Json operator+(Json x) {
3097     x.force_number();
3098     return x;
3099 }
3100 inline Json operator-(Json x) {
3101     x.force_number();
3102     if (x.is_int())
3103         x.u_.u.x = -x.u_.u.x;
3104     else
3105         x.u_.d.x = -x.u_.d.x;
3106     return x;
3107 }
3108
3109 /** @brief Swap this Json with @a x. */
3110 inline void Json::swap(Json& x) {
3111     using std::swap;
3112     swap(u_, x.u_);
3113 }
3114
3115
3116 inline StringAccum &operator<<(StringAccum &sa, const Json& json) {
3117     json.unparse(sa);
3118     return sa;
3119 }
3120
3121 template <typename P>
3122 inline StringAccum &operator<<(StringAccum &sa, const Json_proxy_base<P> &json) {
3123     return (sa << json.cvalue());
3124 }
3125
3126 inline std::ostream &operator<<(std::ostream& f, const Json& json) {
3127     StringAccum sa;
3128     json.unparse(sa);
3129     return f.write(sa.data(), sa.length());
3130 }
3131
3132 template <typename P>
3133 inline std::ostream &operator<<(std::ostream& f, const Json_proxy_base<P>& json) {
3134     return (f << json.cvalue());
3135 }
3136
3137 bool operator==(const Json& a, const Json& b);
3138
3139 template <typename T>
3140 inline bool operator==(const Json_proxy_base<T>& a, const Json& b) {
3141     return a.cvalue() == b;
3142 }
3143
3144 template <typename T>
3145 inline bool operator==(const Json& a, const Json_proxy_base<T>& b) {
3146     return a == b.cvalue();
3147 }
3148
3149 template <typename T, typename U>
3150 inline bool operator==(const Json_proxy_base<T>& a,
3151                        const Json_proxy_base<U>& b) {
3152     return a.cvalue() == b.cvalue();
3153 }
3154
3155 inline bool operator!=(const Json& a, const Json& b) {
3156     return !(a == b);
3157 }
3158
3159 template <typename T>
3160 inline bool operator!=(const Json_proxy_base<T>& a, const Json& b) {
3161     return !(a == b);
3162 }
3163
3164 template <typename T>
3165 inline bool operator!=(const Json& a, const Json_proxy_base<T>& b) {
3166     return !(a == b);
3167 }
3168
3169 template <typename T, typename U>
3170 inline bool operator!=(const Json_proxy_base<T>& a,
3171                        const Json_proxy_base<U>& b) {
3172     return !(a == b);
3173 }
3174
3175 inline void swap(Json& a, Json& b) {
3176     a.swap(b);
3177 }
3178
3179 } // namespace lcdf
3180 #endif