1/**
2 * Copyright 2021 Alibaba, Inc. and its affiliates. All Rights Reserved.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15
16 * \author Rainvan (Yunfeng.Xiao)
17 * \date Dev 2012
18 * \brief Interface of JSON Parser/Generator (C++)
19 */
20
21#ifndef __AILEGO_ENCODING_JSON_MOD_JSON_PLUS_H__
22#define __AILEGO_ENCODING_JSON_MOD_JSON_PLUS_H__
23
24#include <cfloat>
25#include <cstring>
26#include <stdexcept>
27#include <string>
28#include "mod_json.h"
29
30namespace ailego {
31
32/*! JSON String
33 */
34class JsonString {
35 public:
36 typedef mod_json_size_t size_type;
37 typedef mod_json_ssize_t ssize_type;
38 typedef mod_json_float_t float_type;
39 typedef mod_json_integer_t integer_type;
40
41 //! Constructor
42 JsonString(void) : str_(0) {}
43
44 //! Constructor
45 JsonString(const JsonString &rhs) : str_(0) {
46 if (rhs.str_) {
47 str_ = mod_json_string_grab(rhs.str_);
48 }
49 }
50
51#if __cplusplus >= 201103L
52 //! Constructor
53 JsonString(JsonString &&rhs) : str_(rhs.str_) {
54 rhs.str_ = 0;
55 }
56#endif
57
58 //! Constructor
59 JsonString(const char *cstr) {
60 str_ = cstr ? mod_json_string_set(cstr, (mod_json_size_t)std::strlen(cstr))
61 : 0;
62 }
63
64 //! Constructor
65 JsonString(const char *cstr, size_type len) {
66 str_ = mod_json_string_set(cstr, len);
67 }
68
69 //! Constructor
70 JsonString(const std::string &str) {
71 str_ = mod_json_string_set(str.c_str(), (mod_json_size_t)str.size());
72 }
73
74 //! Destructor
75 ~JsonString(void) {
76 mod_json_string_unset(str_);
77 }
78
79 //! Assign new contents to the string, replacing its current content
80 JsonString &operator=(const JsonString &rhs) {
81 this->assign(rhs);
82 return *this;
83 }
84
85#if __cplusplus >= 201103L
86 //! Assign new contents to the string, replacing its current content
87 JsonString &operator=(JsonString &&rhs) {
88 this->assign(std::move(rhs));
89 return *this;
90 }
91#endif
92
93 //! Assign new contents to the string, replacing its current content
94 JsonString &operator=(const char *cstr) {
95 this->assign(cstr);
96 return *this;
97 }
98
99 //! Assign new contents to the string, replacing its current content
100 JsonString &operator=(const std::string &rhs) {
101 this->assign(rhs);
102 return *this;
103 }
104
105 //! Append a JSON string
106 JsonString &operator+=(const JsonString &str) {
107 this->append(str);
108 return *this;
109 }
110
111 //! Append a c-style string
112 JsonString &operator+=(const char *cstr) {
113 this->append(cstr);
114 return *this;
115 }
116
117 //! Append a character to string
118 JsonString &operator+=(char c) {
119 this->append(c);
120 return *this;
121 }
122
123 //! Equality
124 bool operator==(const JsonString &rhs) const {
125 return (mod_json_string_compare(str_, rhs.str_) == 0);
126 }
127
128 //! No equality
129 bool operator!=(const JsonString &rhs) const {
130 return !(*this == rhs);
131 }
132
133 //! Retrieve the character at index n
134 char &operator[](size_type n) {
135 if (!copy_and_leak()) {
136 throw std::runtime_error("JsonString::operator[]");
137 }
138 return *(str_->first + n);
139 }
140
141 //! Retrieve the character at index n
142 const char &operator[](size_type n) const {
143 return *(str_->first + n);
144 }
145
146 //! Retrieve non-zero if the string is valid
147 bool is_valid(void) const {
148 return (str_ != (mod_json_string_t *)0);
149 }
150
151 //! Retrieve non-zero if the string is empty
152 bool empty(void) const {
153 return mod_json_string_empty(str_);
154 }
155
156 //! Assign a JSON string
157 void assign(const JsonString &rhs) {
158 mod_json_string_unset(str_);
159 str_ = rhs.str_ ? mod_json_string_grab(rhs.str_) : 0;
160 }
161
162#if __cplusplus >= 201103L
163 //! Assign a JSON string
164 void assign(JsonString &&rhs) {
165 mod_json_string_unset(str_);
166 str_ = rhs.str_;
167 rhs.str_ = 0;
168 }
169#endif
170
171 //! Assign a c-style string
172 void assign(const char *cstr) {
173 if (cstr) {
174 if (!copy_on_write() ||
175 mod_json_string_assign(str_, cstr,
176 (mod_json_size_t)std::strlen(cstr)) != 0) {
177 throw std::runtime_error("JsonString::assign");
178 }
179 }
180 }
181
182 //! Assign a c-style string
183 void assign(const char *cstr, size_type len) {
184 if (!copy_on_write() || mod_json_string_assign(str_, cstr, len) != 0) {
185 throw std::runtime_error("JsonString::assign");
186 }
187 }
188
189 //! Assign a STL-style string
190 void assign(const std::string &str) {
191 if (!copy_on_write() ||
192 mod_json_string_assign(str_, str.c_str(),
193 (mod_json_size_t)str.size()) != 0) {
194 throw std::runtime_error("JsonString::assign");
195 }
196 }
197
198 //! Append a JSON string
199 void append(const JsonString &str) {
200 if (str.str_) {
201 if (!copy_on_write() || mod_json_string_add(str_, str.str_) != 0) {
202 throw std::runtime_error("JsonString::append");
203 }
204 }
205 }
206
207 //! Append a c-style string
208 void append(const char *cstr) {
209 if (cstr) {
210 if (!copy_on_write() ||
211 mod_json_string_append(str_, cstr,
212 (mod_json_size_t)std::strlen(cstr)) != 0) {
213 throw std::runtime_error("JsonString::append");
214 }
215 }
216 }
217
218 //! Append a c-style string
219 void append(const char *cstr, size_type len) {
220 if (!copy_on_write() || mod_json_string_append(str_, cstr, len) != 0) {
221 throw std::runtime_error("JsonString::append");
222 }
223 }
224
225 //! Append a STL-style string
226 void append(const std::string &str) {
227 if (!copy_on_write() ||
228 mod_json_string_append(str_, str.c_str(),
229 (mod_json_size_t)str.size()) != 0) {
230 throw std::runtime_error("JsonString::append");
231 }
232 }
233
234 //! Append a character to string
235 void append(char c) {
236 if (!copy_on_write() || mod_json_string_append(str_, &c, 1) != 0) {
237 throw std::runtime_error("JsonString::append");
238 }
239 }
240
241 //! Retrieve the character at index n
242 char &at(size_type n) {
243 if (this->size() <= n) {
244 throw std::out_of_range("JsonString::at");
245 }
246 if (!copy_and_leak()) {
247 throw std::runtime_error("JsonString::at");
248 }
249 return *(str_->first + n);
250 }
251
252 //! Retrieve the character at index n
253 const char &at(size_type n) const {
254 if (this->size() <= n) {
255 throw std::out_of_range("JsonString::at");
256 }
257 return *(str_->first + n);
258 }
259
260 //! Request a change in capacity
261 void reserve(size_type n) {
262 if (!copy_on_write() || mod_json_string_reserve(str_, n) != 0) {
263 throw std::runtime_error("JsonString::reserve");
264 }
265 }
266
267 //! Clear the JSON string
268 void clear(void) {
269 mod_json_string_unset(str_);
270 str_ = 0;
271 }
272
273 //! Exchange the content with another JSON string
274 void swap(JsonString &rhs) {
275 mod_json_string_t *str = str_;
276 str_ = rhs.str_;
277 rhs.str_ = str;
278 }
279
280 //! Retrieve the data pointer
281 char *data(void) {
282 return mod_json_string_data(str_);
283 }
284
285 //! Retrieve the data pointer
286 const char *data(void) const {
287 return mod_json_string_data(str_);
288 }
289
290 //! Retrieve HASH of a JSON string
291 size_type hash(void) const {
292 return mod_json_string_hash(str_);
293 }
294
295 //! Compare two JSON strings (case sensitive)
296 int compare(const JsonString &rhs) const {
297 return mod_json_string_compare(str_, rhs.str_);
298 }
299
300 //! Compare two strings (case sensitive)
301 int compare(const char *cstr) const {
302 const char *self = this->c_str();
303 if (self && cstr) {
304 return std::strcmp(self, cstr);
305 }
306
307 // particular case
308 if (!self && cstr) {
309 return -1;
310 } else if (self && !cstr) {
311 return 1;
312 }
313 return 0;
314 }
315
316 // Encode a JSON string
317 JsonString encode(void) const {
318 JsonString ret;
319 ret.str_ = mod_json_string_encode(str_);
320 return ret;
321 }
322
323 // Decode a JSON string
324 JsonString decode(void) const {
325 JsonString ret;
326 ret.str_ = mod_json_string_decode(str_);
327 return ret;
328 }
329
330 //! Retrieve the capacity of string
331 size_type capacity(void) const {
332 return mod_json_string_capacity(str_);
333 }
334
335 //! Retrieve the length of string
336 size_type size(void) const {
337 return mod_json_string_length(str_);
338 }
339
340 //! Retrieve the length of string
341 size_type length(void) const {
342 return mod_json_string_length(str_);
343 }
344
345 //! Retrieve refer-counter of string
346 ssize_type refer(void) const {
347 return mod_json_string_refer(str_);
348 }
349
350 //! Retrieve the c-style string
351 const char *c_str(void) const {
352 return mod_json_string_cstr(str_);
353 }
354
355 //! Convert string to float
356 float_type as_float(void) const {
357 return mod_json_string_float(str_);
358 }
359
360 //! Convert string to integer
361 integer_type as_integer(void) const {
362 return mod_json_string_integer(str_);
363 }
364
365 //! Retrieve string as a STL string
366 std::string as_stl_string(void) const {
367 if (!this->empty()) {
368 return std::string(this->data(), this->size());
369 }
370 return std::string();
371 }
372
373 protected:
374 //! Clone the string for writing
375 bool copy_on_write(void) {
376 if (str_) {
377 if (mod_json_string_is_shared(str_)) {
378 mod_json_string_put(str_);
379 str_ = mod_json_string_clone(str_);
380 }
381 } else {
382 str_ = mod_json_string_set("", 0);
383 }
384 return (str_ != 0);
385 }
386
387 //! Clone the value and leak it
388 bool copy_and_leak(void) {
389 if (copy_on_write()) {
390 mod_json_string_set_leaked(str_);
391 return true;
392 }
393 return false;
394 }
395
396 private:
397 mod_json_string_t *str_;
398};
399
400class JsonArray;
401class JsonObject;
402
403/*! JSON Value
404 */
405class JsonValue {
406 public:
407 typedef mod_json_size_t size_type;
408 typedef mod_json_ssize_t ssize_type;
409 typedef mod_json_float_t float_type;
410 typedef mod_json_integer_t integer_type;
411
412 //! Constructor
413 JsonValue(void) : val_(0) {}
414
415 //! Constructor
416 explicit JsonValue(const bool &val) {
417 val_ = mod_json_value_set_boolean((mod_json_boolean_t)val);
418 }
419
420 //! Constructor
421 explicit JsonValue(const signed char &val) {
422 val_ = mod_json_value_set_integer((mod_json_integer_t)val);
423 }
424
425 //! Constructor
426 explicit JsonValue(const char &val) {
427 val_ = mod_json_value_set_integer((mod_json_integer_t)val);
428 }
429
430 //! Constructor
431 explicit JsonValue(const short int &val) {
432 val_ = mod_json_value_set_integer((mod_json_integer_t)val);
433 }
434
435 //! Constructor
436 explicit JsonValue(const int &val) {
437 val_ = mod_json_value_set_integer((mod_json_integer_t)val);
438 }
439
440 //! Constructor
441 explicit JsonValue(const long int &val) {
442 val_ = mod_json_value_set_integer((mod_json_integer_t)val);
443 }
444
445 //! Constructor
446 explicit JsonValue(const long long int &val) {
447 val_ = mod_json_value_set_integer((mod_json_integer_t)val);
448 }
449
450 //! Constructor
451 explicit JsonValue(const float &val) {
452 val_ = mod_json_value_set_float((mod_json_float_t)val);
453 }
454
455 //! Constructor
456 explicit JsonValue(const double &val) {
457 val_ = mod_json_value_set_float((mod_json_float_t)val);
458 }
459
460 //! Constructor
461 explicit JsonValue(const long double &val) {
462 val_ = mod_json_value_set_float((mod_json_float_t)val);
463 }
464
465 //! Constructor
466 explicit JsonValue(const unsigned char &val) {
467 val_ = mod_json_value_set_integer((mod_json_integer_t)val);
468 }
469
470 //! Constructor
471 explicit JsonValue(const unsigned short int &val) {
472 val_ = mod_json_value_set_integer((mod_json_integer_t)val);
473 }
474
475 //! Constructor
476 explicit JsonValue(const unsigned int &val) {
477 val_ = mod_json_value_set_integer((mod_json_integer_t)val);
478 }
479
480 //! Constructor
481 explicit JsonValue(const unsigned long int &val) {
482 val_ = mod_json_value_set_integer((mod_json_integer_t)val);
483 }
484
485 //! Constructor
486 explicit JsonValue(const unsigned long long int &val) {
487 val_ = mod_json_value_set_integer((mod_json_integer_t)val);
488 }
489
490 //! Constructor
491 JsonValue(const JsonString &val) {
492 val_ = mod_json_value_set_string(*(mod_json_string_t **)&val);
493 }
494
495 //! Constructor
496 JsonValue(const char *val) {
497 val_ = mod_json_value_set_buffer(
498 val, val ? (mod_json_size_t)std::strlen(val) : 0);
499 }
500
501 //! Constructor
502 JsonValue(const char *val, size_type len) {
503 val_ = mod_json_value_set_buffer(val, len);
504 }
505
506 //! Constructor
507 JsonValue(const std::string &val) {
508 val_ = mod_json_value_set_buffer(val.data(), (mod_json_size_t)val.size());
509 }
510
511 //! Constructor
512 JsonValue(const JsonArray &val) {
513 val_ = mod_json_value_set_array(*(mod_json_array_t **)&val);
514 }
515
516 //! Constructor
517 JsonValue(const JsonObject &val) {
518 val_ = mod_json_value_set_object(*(mod_json_object_t **)&val);
519 }
520
521 //! Constructor
522 JsonValue(const JsonValue &rhs) : val_(0) {
523 if (rhs.val_) {
524 val_ = mod_json_value_grab(rhs.val_);
525 }
526 }
527
528#if __cplusplus >= 201103L
529 //! Constructor
530 JsonValue(JsonValue &&rhs) : val_(rhs.val_) {
531 rhs.val_ = 0;
532 }
533#endif
534
535 //! Destructor
536 ~JsonValue(void) {
537 mod_json_value_unset(val_);
538 }
539
540 //! Assign new contents to the value, replacing its current content
541 JsonValue &operator=(const JsonValue &rhs) {
542 this->assign(rhs);
543 return *this;
544 }
545
546#if __cplusplus >= 201103L
547 //! Assign new contents to the value, replacing its current content
548 JsonValue &operator=(JsonValue &&rhs) {
549 this->assign(std::move(rhs));
550 return *this;
551 }
552#endif
553
554 //! Assign new contents to the value, replacing its current content
555 JsonValue &operator=(const bool &val) {
556 this->assign(val);
557 return *this;
558 }
559
560 //! Assign new contents to the value, replacing its current content
561 JsonValue &operator=(const signed char &val) {
562 this->assign(val);
563 return *this;
564 }
565
566 //! Assign new contents to the value, replacing its current content
567 JsonValue &operator=(const char &val) {
568 this->assign(val);
569 return *this;
570 }
571
572 //! Assign new contents to the value, replacing its current content
573 JsonValue &operator=(const short int &val) {
574 this->assign(val);
575 return *this;
576 }
577
578 //! Assign new contents to the value, replacing its current content
579 JsonValue &operator=(const int &val) {
580 this->assign(val);
581 return *this;
582 }
583
584 //! Assign new contents to the value, replacing its current content
585 JsonValue &operator=(const long int &val) {
586 this->assign(val);
587 return *this;
588 }
589
590 //! Assign new contents to the value, replacing its current content
591 JsonValue &operator=(const long long int &val) {
592 this->assign(val);
593 return *this;
594 }
595
596 //! Assign new contents to the value, replacing its current content
597 JsonValue &operator=(const float &val) {
598 this->assign(val);
599 return *this;
600 }
601
602 //! Assign new contents to the value, replacing its current content
603 JsonValue &operator=(const double &val) {
604 this->assign(val);
605 return *this;
606 }
607
608 //! Assign new contents to the value, replacing its current content
609 JsonValue &operator=(const long double &val) {
610 this->assign(val);
611 return *this;
612 }
613
614 //! Assign new contents to the value, replacing its current content
615 JsonValue &operator=(const unsigned char &val) {
616 this->assign(val);
617 return *this;
618 }
619
620 //! Assign new contents to the value, replacing its current content
621 JsonValue &operator=(const unsigned short int &val) {
622 this->assign(val);
623 return *this;
624 }
625
626 //! Assign new contents to the value, replacing its current content
627 JsonValue &operator=(const unsigned int &val) {
628 this->assign(val);
629 return *this;
630 }
631
632 //! Assign new contents to the value, replacing its current content
633 JsonValue &operator=(const unsigned long int &val) {
634 this->assign(val);
635 return *this;
636 }
637
638 //! Assign new contents to the value, replacing its current content
639 JsonValue &operator=(const unsigned long long int &val) {
640 this->assign(val);
641 return *this;
642 }
643
644 //! Assign new contents to the value, replacing its current content
645 JsonValue &operator=(const JsonString &val) {
646 this->assign(val);
647 return *this;
648 }
649
650 //! Assign new contents to the value, replacing its current content
651 JsonValue &operator=(const char *val) {
652 this->assign(val);
653 return *this;
654 }
655
656 //! Assign new contents to the value, replacing its current content
657 JsonValue &operator=(const std::string &val) {
658 this->assign(val);
659 return *this;
660 }
661
662 //! Assign new contents to the value, replacing its current content
663 JsonValue &operator=(const JsonArray &arr) {
664 this->assign(arr);
665 return *this;
666 }
667
668 //! Assign new contents to the value, replacing its current content
669 JsonValue &operator=(const JsonObject &obj) {
670 this->assign(obj);
671 return *this;
672 }
673
674 //! Equality
675 bool operator==(const JsonValue &rhs) const {
676 return mod_json_value_is_equal(val_, rhs.val_);
677 }
678
679 //! No equality
680 bool operator!=(const JsonValue &rhs) const {
681 return !(*this == rhs);
682 }
683
684 //! Treat self value as object by force, retrieving value of a key
685 JsonValue &operator[](const char *key) {
686 return this->get_value(key);
687 }
688
689 //! Retrieve a reference of value by a key
690 JsonValue operator[](const char *key) const {
691 return this->get_value(key);
692 }
693
694 //! Treat self value as object by force, retrieving value of a key
695 JsonValue &operator[](const JsonString &key) {
696 return this->get_value(key.c_str());
697 }
698
699 //! Retrieve a reference of value by a key
700 JsonValue operator[](const JsonString &key) const {
701 return this->get_value(key.c_str());
702 }
703
704 //! Treat self value as object by force, retrieving value of a key
705 JsonValue &operator[](const std::string &key) {
706 return this->get_value(key.c_str());
707 }
708
709 //! Retrieve a reference of value by a key
710 JsonValue operator[](const std::string &key) const {
711 return this->get_value(key.c_str());
712 }
713
714 //! Treat self value as array by force, retrieving value at index n
715 JsonValue &operator[](size_type n) {
716 return this->get_value(n);
717 }
718
719 //! Retrieve a reference of value at index n
720 JsonValue operator[](size_type n) const {
721 return this->get_value(n);
722 }
723
724 //! Retrieve non-zero if the value is valid
725 bool is_valid(void) const {
726 return (val_ != (mod_json_value_t *)0);
727 }
728
729 //! Retrieve non-zero if the value is a object
730 bool is_object(void) const {
731 return mod_json_value_is_object(val_);
732 }
733
734 //! Retrieve non-zero if the value is an array
735 bool is_array(void) const {
736 return mod_json_value_is_array(val_);
737 }
738
739 //! Retrieve non-zero if the value is a string
740 bool is_string(void) const {
741 return mod_json_value_is_string(val_);
742 }
743
744 //! Retrieve non-zero if the value is null
745 bool is_null(void) const {
746 return mod_json_value_is_null(val_);
747 }
748
749 //! Retrieve non-zero if the value is a float
750 bool is_float(void) const {
751 return mod_json_value_is_float(val_);
752 }
753
754 //! Retrieve non-zero if the value is an integer
755 bool is_integer(void) const {
756 return mod_json_value_is_integer(val_);
757 }
758
759 //! Retrieve non-zero if the value is a boolean
760 bool is_boolean(void) const {
761 return mod_json_value_is_boolean(val_);
762 }
763
764 //! Assign new contents to the value, replacing its current content
765 void assign(const JsonValue &rhs) {
766 mod_json_value_unset(val_);
767 val_ = rhs.val_ ? mod_json_value_grab(rhs.val_) : 0;
768 }
769
770#if __cplusplus >= 201103L
771 //! Assign new contents to the value, replacing its current content
772 void assign(JsonValue &&rhs) {
773 mod_json_value_unset(val_);
774 val_ = rhs.val_;
775 rhs.val_ = 0;
776 }
777#endif
778
779 //! Assign new contents to the value, replacing its current content
780 void assign(const bool &val) {
781 if (!copy_on_write()) {
782 throw std::runtime_error("JsonValue::assign");
783 }
784 mod_json_value_assign_boolean(val_, (mod_json_boolean_t)val);
785 }
786
787 //! Assign new contents to the value, replacing its current content
788 void assign(const signed char &val) {
789 if (!copy_on_write()) {
790 throw std::runtime_error("JsonValue::assign");
791 }
792 mod_json_value_assign_integer(val_, (mod_json_integer_t)val);
793 }
794
795 //! Assign new contents to the value, replacing its current content
796 void assign(const char &val) {
797 if (!copy_on_write()) {
798 throw std::runtime_error("JsonValue::assign");
799 }
800 mod_json_value_assign_integer(val_, (mod_json_integer_t)val);
801 }
802
803 //! Assign new contents to the value, replacing its current content
804 void assign(const short int &val) {
805 if (!copy_on_write()) {
806 throw std::runtime_error("JsonValue::assign");
807 }
808 mod_json_value_assign_integer(val_, (mod_json_integer_t)val);
809 }
810
811 //! Assign new contents to the value, replacing its current content
812 void assign(const int &val) {
813 if (!copy_on_write()) {
814 throw std::runtime_error("JsonValue::assign");
815 }
816 mod_json_value_assign_integer(val_, (mod_json_integer_t)val);
817 }
818
819 //! Assign new contents to the value, replacing its current content
820 void assign(const long int &val) {
821 if (!copy_on_write()) {
822 throw std::runtime_error("JsonValue::assign");
823 }
824 mod_json_value_assign_integer(val_, (mod_json_integer_t)val);
825 }
826
827 //! Assign new contents to the value, replacing its current content
828 void assign(const long long int &val) {
829 if (!copy_on_write()) {
830 throw std::runtime_error("JsonValue::assign");
831 }
832 mod_json_value_assign_integer(val_, (mod_json_integer_t)val);
833 }
834
835 //! Assign new contents to the value, replacing its current content
836 void assign(const float &val) {
837 if (!copy_on_write()) {
838 throw std::runtime_error("JsonValue::assign");
839 }
840 mod_json_value_assign_float(val_, (mod_json_float_t)val);
841 }
842
843 //! Assign new contents to the value, replacing its current content
844 void assign(const double &val) {
845 if (!copy_on_write()) {
846 throw std::runtime_error("JsonValue::assign");
847 }
848 mod_json_value_assign_float(val_, (mod_json_float_t)val);
849 }
850
851 //! Assign new contents to the value, replacing its current content
852 void assign(const long double &val) {
853 if (!copy_on_write()) {
854 throw std::runtime_error("JsonValue::assign");
855 }
856 mod_json_value_assign_float(val_, (mod_json_float_t)val);
857 }
858
859 //! Assign new contents to the value, replacing its current content
860 void assign(const unsigned char &val) {
861 if (!copy_on_write()) {
862 throw std::runtime_error("JsonValue::assign");
863 }
864 mod_json_value_assign_integer(val_, (mod_json_integer_t)val);
865 }
866
867 //! Assign new contents to the value, replacing its current content
868 void assign(const unsigned short int &val) {
869 if (!copy_on_write()) {
870 throw std::runtime_error("JsonValue::assign");
871 }
872 mod_json_value_assign_integer(val_, (mod_json_integer_t)val);
873 }
874
875 //! Assign new contents to the value, replacing its current content
876 void assign(const unsigned int &val) {
877 if (!copy_on_write()) {
878 throw std::runtime_error("JsonValue::assign");
879 }
880 mod_json_value_assign_integer(val_, (mod_json_integer_t)val);
881 }
882
883 //! Assign new contents to the value, replacing its current content
884 void assign(const unsigned long int &val) {
885 if (!copy_on_write()) {
886 throw std::runtime_error("JsonValue::assign");
887 }
888 mod_json_value_assign_integer(val_, (mod_json_integer_t)val);
889 }
890
891 //! Assign new contents to the value, replacing its current content
892 void assign(const unsigned long long int &val) {
893 if (!copy_on_write()) {
894 throw std::runtime_error("JsonValue::assign");
895 }
896 mod_json_value_assign_integer(val_, (mod_json_integer_t)val);
897 }
898
899 //! Assign new contents to the value, replacing its current content
900 void assign(const JsonString &val) {
901 if (!copy_on_write()) {
902 throw std::runtime_error("JsonValue::assign");
903 }
904 mod_json_value_assign_string(val_, *(mod_json_string_t **)&val);
905 }
906
907 //! Assign new contents to the value, replacing its current content
908 void assign(const char *val) {
909 JsonString str(val);
910 if (!str.is_valid() || !copy_on_write()) {
911 throw std::runtime_error("JsonValue::assign");
912 }
913 mod_json_value_assign_string(val_, *(mod_json_string_t **)&str);
914 }
915
916 //! Assign new contents to the value, replacing its current content
917 void assign(const char *val, size_type len) {
918 JsonString str(val, len);
919 if (!str.is_valid() || !copy_on_write()) {
920 throw std::runtime_error("JsonValue::assign");
921 }
922 mod_json_value_assign_string(val_, *(mod_json_string_t **)&str);
923 }
924
925 //! Assign new contents to the value, replacing its current content
926 void assign(const std::string &val) {
927 JsonString str(val);
928 if (!str.is_valid() || !copy_on_write()) {
929 throw std::runtime_error("JsonValue::assign");
930 }
931 mod_json_value_assign_string(val_, *(mod_json_string_t **)&str);
932 }
933
934 //! Assign new contents to the value, replacing its current content
935 void assign(const JsonArray &arr);
936
937 //! Assign new contents to the value, replacing its current content
938 void assign(const JsonObject &obj);
939
940 //! Retrieve refer-counter of JSON value
941 ssize_type refer(void) const {
942 return mod_json_value_refer(val_);
943 }
944
945 //! Retrieve value as JSON format string
946 JsonString as_json_string(void) const {
947 mod_json_string_t *tmp = mod_json_dump(val_);
948 JsonString ret = *reinterpret_cast<JsonString *>(&tmp);
949 if (tmp) {
950 mod_json_string_unset(tmp);
951 }
952 return ret;
953 }
954
955 //! Retrieve value as a STL string
956 std::string as_stl_string(void) const {
957 if (is_string()) {
958 return to_string().as_stl_string();
959 }
960 return std::string();
961 }
962
963 //! Retrieve value as JSON string
964 const JsonString &as_string(void) const {
965 if (!is_string()) {
966 throw std::logic_error("JsonValue::as_string");
967 }
968 return to_string();
969 }
970
971 //! Retrieve value as c-style string
972 const char *as_c_string(void) const {
973 return mod_json_value_cstring(val_);
974 }
975
976 //! Retrieve value as JSON string
977 JsonString &as_string(void) {
978 if (!is_string()) {
979 throw std::logic_error("JsonValue::as_string");
980 }
981 if (!copy_and_leak()) {
982 throw std::runtime_error("JsonValue::as_string");
983 }
984 return to_string();
985 }
986
987 //! Retrieve value as JSON array
988 const JsonArray &as_array(void) const {
989 if (!is_array()) {
990 throw std::logic_error("JsonValue::as_array");
991 }
992 return to_array();
993 }
994
995 //! Retrieve value as JSON array
996 JsonArray &as_array(void) {
997 if (!is_array()) {
998 throw std::logic_error("JsonValue::as_array");
999 }
1000 if (!copy_and_leak()) {
1001 throw std::runtime_error("JsonValue::as_array");
1002 }
1003 return to_array();
1004 }
1005
1006 //! Retrieve value as JSON object
1007 const JsonObject &as_object(void) const {
1008 if (!is_object()) {
1009 throw std::logic_error("JsonValue::as_object");
1010 }
1011 return to_object();
1012 }
1013
1014 //! Retrieve value as JSON object
1015 JsonObject &as_object(void) {
1016 if (!is_object()) {
1017 throw std::logic_error("JsonValue::as_object");
1018 }
1019 if (!copy_and_leak()) {
1020 throw std::runtime_error("JsonValue::as_object");
1021 }
1022 return to_object();
1023 }
1024
1025 //! Retrieve value as float
1026 float_type as_float(void) const {
1027 return mod_json_value_float(val_);
1028 }
1029
1030 //! Retrieve value as integer
1031 integer_type as_integer(void) const {
1032 return mod_json_value_integer(val_);
1033 }
1034
1035 //! Retrieve value as boolean
1036 bool as_bool(void) const {
1037 return mod_json_value_boolean(val_);
1038 }
1039
1040 //! Exchange the content with another JSON value
1041 void swap(JsonValue &rhs) {
1042 mod_json_value_t *val = val_;
1043 val_ = rhs.val_;
1044 rhs.val_ = val;
1045 }
1046
1047 //! Merge another JSON value
1048 void merge(const JsonValue &rhs) {
1049 if (!copy_on_write()) {
1050 throw std::runtime_error("JsonValue::merge");
1051 }
1052 mod_json_value_merge(val_, rhs.val_);
1053 }
1054
1055 //! Parse a sting as a JSON value
1056 bool parse(const char *str) {
1057 mod_json_token_t *tok = mod_json_token_create(NULL);
1058
1059 if (tok) {
1060 mod_json_value_t *jval = mod_json_parse(tok, str);
1061
1062 mod_json_token_destroy(tok);
1063 if (jval) {
1064 *this = *reinterpret_cast<JsonValue *>(&jval);
1065 mod_json_value_unset(jval);
1066 return is_valid();
1067 }
1068 }
1069 return false;
1070 }
1071
1072 //! Parse a sting as a JSON value
1073 bool parse(const JsonString &str) {
1074 return this->parse(str.c_str());
1075 }
1076
1077 //! Parse a sting as a JSON value
1078 bool parse(const std::string &str) {
1079 return this->parse(str.c_str());
1080 }
1081
1082 protected:
1083 //! Clone the value for writing
1084 bool copy_on_write(void) {
1085 if (val_) {
1086 if (mod_json_value_is_shared(val_)) {
1087 mod_json_value_put(val_);
1088 val_ = mod_json_value_clone(val_);
1089 }
1090 } else {
1091 val_ = mod_json_value_set_null();
1092 }
1093 return (val_ != 0);
1094 }
1095
1096 //! Clone the value and leak it
1097 bool copy_and_leak(void) {
1098 if (copy_on_write()) {
1099 mod_json_value_set_leaked(val_);
1100 return true;
1101 }
1102 return false;
1103 }
1104
1105 //! Convert value to JSON object
1106 JsonObject &to_object(void);
1107
1108 //! Convert value to JSON object
1109 const JsonObject &to_object(void) const;
1110
1111 //! Convert value to JSON array
1112 JsonArray &to_array(void);
1113
1114 //! Convert value to JSON array
1115 const JsonArray &to_array(void) const;
1116
1117 //! Convert value to JSON string
1118 JsonString &to_string(void);
1119
1120 //! Convert value to JSON string
1121 const JsonString &to_string(void) const;
1122
1123 //! Treat self value as object by force, retrieving value of a key
1124 JsonValue &get_value(const char *key);
1125
1126 //! Retrieve a reference of value by a key
1127 JsonValue get_value(const char *key) const;
1128
1129 //! Treat self value as array by force, retrieving value at index n
1130 JsonValue &get_value(size_type n);
1131
1132 //! Retrieve a reference of value at index n
1133 JsonValue get_value(size_type n) const;
1134
1135 //! Set the new array to the value, replacing its current content
1136 void set_value(const JsonArray &val);
1137
1138 //! Set the new object to the value, replacing its current content
1139 void set_value(const JsonObject &val);
1140
1141 private:
1142 mod_json_value_t *val_;
1143};
1144
1145/*! JSON Array
1146 */
1147class JsonArray {
1148 public:
1149 typedef mod_json_size_t size_type;
1150 typedef mod_json_ssize_t ssize_type;
1151
1152 class iterator;
1153 class const_iterator;
1154 class reverse_iterator;
1155 class const_reverse_iterator;
1156
1157 /*! Const iterator of JSON Array
1158 */
1159 class const_iterator {
1160 public:
1161 //! Constructor
1162 const_iterator(void) : iter_(0) {}
1163
1164 //! Equality
1165 bool operator==(const const_iterator &rhs) const {
1166 return (iter_ == rhs.iter_);
1167 }
1168
1169 //! No equality
1170 bool operator!=(const const_iterator &rhs) const {
1171 return (iter_ != rhs.iter_);
1172 }
1173
1174 //! Increment (Prefix)
1175 const_iterator &operator++() {
1176 ++iter_;
1177 return *this;
1178 }
1179
1180 //! Increment (Suffix)
1181 const_iterator operator++(int) {
1182 const_iterator tmp = *this;
1183 ++iter_;
1184 return tmp;
1185 }
1186
1187 //! Decrement (Prefix)
1188 const_iterator &operator--() {
1189 --iter_;
1190 return *this;
1191 }
1192
1193 //! Decrement (Suffix)
1194 const_iterator operator--(int) {
1195 const_iterator tmp = *this;
1196 --iter_;
1197 return tmp;
1198 }
1199
1200 //! Indirection (eg. *iter)
1201 const JsonValue &operator*() const {
1202 return *reinterpret_cast<const JsonValue *>(iter_);
1203 }
1204
1205 //! Structure dereference (eg. iter->)
1206 const JsonValue *operator->() const {
1207 return reinterpret_cast<const JsonValue *>(iter_);
1208 }
1209
1210 //! Retrieve as const reverse iterator
1211 operator const_reverse_iterator() const {
1212 return const_reverse_iterator(iter_);
1213 }
1214
1215 protected:
1216 friend class JsonArray;
1217 friend class JsonArray::iterator;
1218 friend class JsonArray::reverse_iterator;
1219 friend class JsonArray::const_reverse_iterator;
1220
1221 //! Constructor for friends
1222 const_iterator(mod_json_value_t *const *iter) : iter_(iter) {}
1223
1224 private:
1225 mod_json_value_t *const *iter_;
1226 };
1227
1228 /*! iterator of JSON Array
1229 */
1230 class iterator {
1231 public:
1232 //! Constructor
1233 iterator(void) : iter_(0) {}
1234
1235 //! Equality
1236 bool operator==(const iterator &rhs) const {
1237 return (iter_ == rhs.iter_);
1238 }
1239
1240 //! No equality
1241 bool operator!=(const iterator &rhs) const {
1242 return (iter_ != rhs.iter_);
1243 }
1244
1245 //! Increment (Prefix)
1246 iterator &operator++() {
1247 ++iter_;
1248 return *this;
1249 }
1250
1251 //! Increment (Suffix)
1252 iterator operator++(int) {
1253 iterator tmp = *this;
1254 ++iter_;
1255 return tmp;
1256 }
1257
1258 //! Decrement (Prefix)
1259 iterator &operator--() {
1260 --iter_;
1261 return *this;
1262 }
1263
1264 //! Decrement (Suffix)
1265 iterator operator--(int) {
1266 iterator tmp = *this;
1267 --iter_;
1268 return tmp;
1269 }
1270
1271 //! Indirection (eg. *iter)
1272 JsonValue &operator*() const {
1273 return *reinterpret_cast<JsonValue *>(iter_);
1274 }
1275
1276 //! Structure dereference (eg. iter->)
1277 JsonValue *operator->() const {
1278 return reinterpret_cast<JsonValue *>(iter_);
1279 }
1280
1281 //! Retrieve as const iterator
1282 operator const_iterator() const {
1283 return const_iterator(iter_);
1284 }
1285
1286 //! Retrieve as reverse iterator
1287 operator reverse_iterator() const {
1288 return reverse_iterator(iter_);
1289 }
1290
1291 //! Retrieve as const reverse iterator
1292 operator const_reverse_iterator() const {
1293 return const_reverse_iterator(iter_);
1294 }
1295
1296 protected:
1297 friend class JsonArray;
1298 friend class JsonArray::reverse_iterator;
1299
1300 //! Constructor for friends
1301 iterator(mod_json_value_t **iter) : iter_(iter) {}
1302
1303 private:
1304 mod_json_value_t **iter_;
1305 };
1306
1307 /*! Const Reverse iterator of JSON Array
1308 */
1309 class const_reverse_iterator {
1310 public:
1311 //! Constructor
1312 const_reverse_iterator(void) : iter_(0) {}
1313
1314 //! Equality
1315 bool operator==(const const_reverse_iterator &rhs) const {
1316 return (iter_ == rhs.iter_);
1317 }
1318
1319 //! No equality
1320 bool operator!=(const const_reverse_iterator &rhs) const {
1321 return (iter_ != rhs.iter_);
1322 }
1323
1324 //! Increment (Prefix)
1325 const_reverse_iterator &operator++() {
1326 --iter_;
1327 return *this;
1328 }
1329
1330 //! Increment (Suffix)
1331 const_reverse_iterator operator++(int) {
1332 const_reverse_iterator tmp = *this;
1333 --iter_;
1334 return tmp;
1335 }
1336
1337 //! Decrement (Prefix)
1338 const_reverse_iterator &operator--() {
1339 ++iter_;
1340 return *this;
1341 }
1342
1343 //! Decrement (Suffix)
1344 const_reverse_iterator operator--(int) {
1345 const_reverse_iterator tmp = *this;
1346 ++iter_;
1347 return tmp;
1348 }
1349
1350 //! Indirection (eg. *iter)
1351 const JsonValue &operator*() const {
1352 return *reinterpret_cast<const JsonValue *>(iter_);
1353 }
1354
1355 //! Structure dereference (eg. iter->)
1356 const JsonValue *operator->() const {
1357 return reinterpret_cast<const JsonValue *>(iter_);
1358 }
1359
1360 //! Retrieve as const iterator
1361 operator const_iterator() const {
1362 return const_iterator(iter_);
1363 }
1364
1365 protected:
1366 friend class JsonArray;
1367 friend class JsonArray::iterator;
1368 friend class JsonArray::const_iterator;
1369 friend class JsonArray::reverse_iterator;
1370
1371 //! Constructor for friends
1372 const_reverse_iterator(mod_json_value_t *const *iter) : iter_(iter) {}
1373
1374 private:
1375 mod_json_value_t *const *iter_;
1376 };
1377
1378 /*! Reverse iterator of JSON Array
1379 */
1380 class reverse_iterator {
1381 public:
1382 //! Constructor
1383 reverse_iterator(void) : iter_(0) {}
1384
1385 //! Equality
1386 bool operator==(const reverse_iterator &rhs) const {
1387 return (iter_ == rhs.iter_);
1388 }
1389
1390 //! No equality
1391 bool operator!=(const reverse_iterator &rhs) const {
1392 return (iter_ != rhs.iter_);
1393 }
1394
1395 //! Increment (Prefix)
1396 reverse_iterator &operator++() {
1397 --iter_;
1398 return *this;
1399 }
1400
1401 //! Increment (Suffix)
1402 reverse_iterator operator++(int) {
1403 reverse_iterator tmp = *this;
1404 --iter_;
1405 return tmp;
1406 }
1407
1408 //! Decrement (Prefix)
1409 reverse_iterator &operator--() {
1410 ++iter_;
1411 return *this;
1412 }
1413
1414 //! Decrement (Suffix)
1415 reverse_iterator operator--(int) {
1416 reverse_iterator tmp = *this;
1417 ++iter_;
1418 return tmp;
1419 }
1420
1421 //! Indirection (eg. *iter)
1422 JsonValue &operator*() const {
1423 return *reinterpret_cast<JsonValue *>(iter_);
1424 }
1425
1426 //! Structure dereference (eg. iter->)
1427 JsonValue *operator->() const {
1428 return reinterpret_cast<JsonValue *>(iter_);
1429 }
1430
1431 //! Retrieve as iterator
1432 operator iterator() const {
1433 return iterator(iter_);
1434 }
1435
1436 //! Retrieve as const iterator
1437 operator const_iterator() const {
1438 return const_iterator(iter_);
1439 }
1440
1441 //! Retrieve as const reverse iterator
1442 operator const_reverse_iterator() const {
1443 return const_reverse_iterator(iter_);
1444 }
1445
1446 protected:
1447 friend class JsonArray;
1448 friend class JsonArray::iterator;
1449
1450 //! Constructor for friends
1451 reverse_iterator(mod_json_value_t **iter) : iter_(iter) {}
1452
1453 private:
1454 mod_json_value_t **iter_;
1455 };
1456
1457 //! Constructor
1458 JsonArray(void) : arr_(0) {}
1459
1460 //! Constructor
1461 JsonArray(const JsonArray &rhs) : arr_(0) {
1462 if (rhs.arr_) {
1463 arr_ = mod_json_array_grab(rhs.arr_);
1464 }
1465 }
1466
1467#if __cplusplus >= 201103L
1468 //! Constructor
1469 JsonArray(JsonArray &&rhs) : arr_(rhs.arr_) {
1470 rhs.arr_ = 0;
1471 }
1472#endif
1473
1474 //! Destructor
1475 ~JsonArray(void) {
1476 mod_json_array_unset(arr_);
1477 }
1478
1479 //! Assign new contents to the array, replacing its current content
1480 JsonArray &operator=(const JsonArray &rhs) {
1481 this->assign(rhs);
1482 return *this;
1483 }
1484
1485#if __cplusplus >= 201103L
1486 //! Assign new contents to the array, replacing its current content
1487 JsonArray &operator=(JsonArray &&rhs) {
1488 this->assign(std::move(rhs));
1489 return *this;
1490 }
1491#endif
1492
1493 //! Equality
1494 bool operator==(const JsonArray &rhs) const {
1495 return mod_json_array_is_equal(arr_, rhs.arr_);
1496 }
1497
1498 //! No equality
1499 bool operator!=(const JsonArray &rhs) const {
1500 return !(*this == rhs);
1501 }
1502
1503 //! Retrieve the value at index n, if no one exists, throw an exception.
1504 JsonValue &operator[](size_type n) {
1505 return this->at(n);
1506 }
1507
1508 //! Retrieve the value at index n, if no one exists, return a null value.
1509 JsonValue operator[](size_type n) const {
1510 return ((n < this->size()) ? this->get_value(n) : JsonValue());
1511 }
1512
1513 //! Retrieve non-zero if the array is valid
1514 bool is_valid(void) const {
1515 return (arr_ != (mod_json_array_t *)0);
1516 }
1517
1518 //! Retrieve non-zero if the array is empty
1519 bool empty(void) const {
1520 return mod_json_array_empty(arr_);
1521 }
1522
1523 //! Retrieve the size of JSON array
1524 size_type size(void) const {
1525 return mod_json_array_count(arr_);
1526 }
1527
1528 //! Retrieve the capacity of JSON array
1529 size_type capacity(void) const {
1530 return mod_json_array_capacity(arr_);
1531 }
1532
1533 //! Retrieve refer-counter of JSON array
1534 ssize_type refer(void) const {
1535 return mod_json_array_refer(arr_);
1536 }
1537
1538 //! Assign new contents to the array, replacing its current content
1539 void assign(const JsonArray &rhs) {
1540 mod_json_array_unset(arr_);
1541 arr_ = rhs.arr_ ? mod_json_array_grab(rhs.arr_) : 0;
1542 }
1543
1544#if __cplusplus >= 201103L
1545 //! Assign new contents to the array, replacing its current content
1546 void assign(JsonArray &&rhs) {
1547 mod_json_array_unset(arr_);
1548 arr_ = rhs.arr_;
1549 rhs.arr_ = 0;
1550 }
1551#endif
1552
1553 //! Request a change in capacity
1554 void reserve(size_type n) {
1555 if (!copy_on_write() || mod_json_array_reserve(arr_, n) != 0) {
1556 throw std::runtime_error("JsonArray::reserve");
1557 }
1558 }
1559
1560 //! Reverse the order of the elements
1561 void reverse(void) {
1562 if (arr_ && copy_on_write()) {
1563 mod_json_array_reverse(arr_);
1564 }
1565 }
1566
1567 //! Push a value to array
1568 void push(const JsonValue &val) {
1569 JsonValue tmp(val);
1570
1571 if (!copy_on_write() ||
1572 mod_json_array_push(arr_, *((mod_json_value_t **)&tmp)) != 0) {
1573 throw std::runtime_error("JsonArray::push");
1574 }
1575 }
1576
1577 //! Pop the last element from array
1578 void pop(void) {
1579 if (arr_) {
1580 if (!copy_on_write()) {
1581 throw std::runtime_error("JsonArray::pop");
1582 }
1583 mod_json_array_pop(arr_);
1584 }
1585 }
1586
1587 //! Remove the first element of array
1588 void shift(void) {
1589 if (arr_) {
1590 if (!copy_on_write()) {
1591 throw std::runtime_error("JsonArray::shift");
1592 }
1593 mod_json_array_shift(arr_);
1594 }
1595 }
1596
1597 //! Retrieve the value at index n
1598 JsonValue &at(size_type n) {
1599 if (this->size() <= n) {
1600 throw std::out_of_range("JsonArray::at");
1601 }
1602 if (!copy_and_leak()) {
1603 throw std::runtime_error("JsonArray::at");
1604 }
1605 return this->get_value(n);
1606 }
1607
1608 //! Retrieve the value at index n
1609 const JsonValue &at(size_type n) const {
1610 if (this->size() <= n) {
1611 throw std::out_of_range("JsonArray::at");
1612 }
1613 return this->get_value(n);
1614 }
1615
1616 //! Retrieve a reference to the first element
1617 JsonValue &front(void) {
1618 if (this->size() <= 0) {
1619 throw std::out_of_range("JsonArray::front");
1620 }
1621 if (!copy_and_leak()) {
1622 throw std::runtime_error("JsonArray::front");
1623 }
1624 return this->get_value(0);
1625 }
1626
1627 //! Retrieve a reference to the first element
1628 const JsonValue &front(void) const {
1629 if (this->size() <= 0) {
1630 throw std::out_of_range("JsonArray::front");
1631 }
1632 return this->get_value(0);
1633 }
1634
1635 //! Retrieve a reference to the last element
1636 JsonValue &back(void) {
1637 if (this->size() <= 0) {
1638 throw std::out_of_range("JsonArray::back");
1639 }
1640 if (!copy_and_leak()) {
1641 throw std::runtime_error("JsonArray::back");
1642 }
1643 return this->get_value(this->size() - 1);
1644 }
1645
1646 //! Retrieve a reference to the last element
1647 const JsonValue &back(void) const {
1648 if (this->size() <= 0) {
1649 throw std::out_of_range("JsonArray::back");
1650 }
1651 return this->get_value(this->size() - 1);
1652 }
1653
1654 //! Clear the JSON array
1655 void clear(void) {
1656 mod_json_array_unset(arr_);
1657 arr_ = 0;
1658 }
1659
1660 //! Exchange the content with another JSON array
1661 void swap(JsonArray &rhs) {
1662 mod_json_array_t *arr = arr_;
1663 arr_ = rhs.arr_;
1664 rhs.arr_ = arr;
1665 }
1666
1667 //! Merge another JSON array
1668 void merge(const JsonArray &rhs) {
1669 if (!copy_on_write()) {
1670 throw std::runtime_error("JsonArray::merge");
1671 }
1672 mod_json_array_merge(arr_, rhs.arr_);
1673 }
1674
1675 //! Resize a JSON array so that it contains n elements
1676 void resize(size_type n, const JsonValue &val = JsonValue()) {
1677 if (!copy_on_write() ||
1678 mod_json_array_resize(arr_, n, *((mod_json_value_t **)&val)) != 0) {
1679 throw std::runtime_error("JsonArray::resize");
1680 }
1681 }
1682
1683 //! Retrieve an iterator pointing to the first element
1684 iterator begin(void) {
1685 if (copy_and_leak()) {
1686 return iterator(mod_json_array_begin(arr_));
1687 }
1688 return iterator();
1689 }
1690
1691 //! Retrieve a const iterator pointing to the first element
1692 const_iterator begin(void) const {
1693 if (arr_) {
1694 return const_iterator(mod_json_array_begin(arr_));
1695 }
1696 return const_iterator();
1697 }
1698
1699 //! Retrieve a const iterator pointing to the first element
1700 const_iterator cbegin(void) const {
1701 if (arr_) {
1702 return const_iterator(mod_json_array_begin(arr_));
1703 }
1704 return const_iterator();
1705 }
1706
1707 //! Retrieve a reverse iterator pointing to the last element
1708 reverse_iterator rbegin(void) {
1709 if (copy_and_leak()) {
1710 return reverse_iterator(mod_json_array_rbegin(arr_));
1711 }
1712 return reverse_iterator();
1713 }
1714
1715 //! Retrieve a const reverse iterator pointing to the last element
1716 const_reverse_iterator rbegin(void) const {
1717 if (arr_) {
1718 return const_reverse_iterator(mod_json_array_rbegin(arr_));
1719 }
1720 return const_reverse_iterator();
1721 }
1722
1723 //! Retrieve a const reverse iterator pointing to the last element
1724 const_reverse_iterator crbegin(void) const {
1725 if (arr_) {
1726 return const_reverse_iterator(mod_json_array_rbegin(arr_));
1727 }
1728 return const_reverse_iterator();
1729 }
1730
1731 //! Retrieve an iterator pointing to the past-the-end element
1732 iterator end(void) {
1733 if (copy_and_leak()) {
1734 return iterator(mod_json_array_end(arr_));
1735 }
1736 return iterator();
1737 }
1738
1739 //! Retrieve a const iterator pointing to the past-the-end element
1740 const_iterator end(void) const {
1741 if (arr_) {
1742 return const_iterator(mod_json_array_end(arr_));
1743 }
1744 return const_iterator();
1745 }
1746
1747 //! Retrieve a const iterator pointing to the past-the-end element
1748 const_iterator cend(void) const {
1749 if (arr_) {
1750 return const_iterator(mod_json_array_end(arr_));
1751 }
1752 return const_iterator();
1753 }
1754
1755 //! Retrieve a reverse pointing to the past-the-end element
1756 reverse_iterator rend(void) {
1757 if (copy_and_leak()) {
1758 return reverse_iterator(mod_json_array_rend(arr_));
1759 }
1760 return reverse_iterator();
1761 }
1762
1763 //! Retrieve a const reverse pointing to the past-the-end element
1764 const_reverse_iterator rend(void) const {
1765 if (arr_) {
1766 return const_reverse_iterator(mod_json_array_rend(arr_));
1767 }
1768 return const_reverse_iterator();
1769 }
1770
1771 //! Retrieve a const reverse pointing to the past-the-end element
1772 const_reverse_iterator crend(void) const {
1773 if (arr_) {
1774 return const_reverse_iterator(mod_json_array_rend(arr_));
1775 }
1776 return const_reverse_iterator();
1777 }
1778
1779 protected:
1780 //! Clone the array for writing
1781 bool copy_on_write(void) {
1782 if (arr_) {
1783 if (mod_json_array_is_shared(arr_)) {
1784 mod_json_array_put(arr_);
1785 arr_ = mod_json_array_clone(arr_);
1786 }
1787 } else {
1788 arr_ = mod_json_array_set_default();
1789 }
1790 return (arr_ != 0);
1791 }
1792
1793 //! Clone the array and leak it
1794 bool copy_and_leak(void) {
1795 if (copy_on_write()) {
1796 mod_json_array_set_leaked(arr_);
1797 return true;
1798 }
1799 return false;
1800 }
1801
1802 //! Retrieve the value at index n
1803 JsonValue &get_value(size_type n) {
1804 return *reinterpret_cast<JsonValue *>(arr_->first + n);
1805 }
1806
1807 //! Retrieve the value at index n
1808 const JsonValue &get_value(size_type n) const {
1809 return *reinterpret_cast<JsonValue *>(arr_->first + n);
1810 }
1811
1812 private:
1813 mod_json_array_t *arr_;
1814};
1815
1816/*! JSON Pair
1817 */
1818class JsonPair {
1819 public:
1820 //! Constructor
1821 JsonPair(void) : pair_(0) {}
1822
1823 //! Retrieve non-zero if the pair is valid
1824 bool is_valid(void) const {
1825 return (pair_ != (mod_json_pair_t *)0);
1826 }
1827
1828 //! Retrieve the key of pair
1829 const JsonString &key(void) const {
1830 return *reinterpret_cast<JsonString *>(&pair_->key);
1831 }
1832
1833 //! Retrieve the value of pair
1834 JsonValue &value(void) {
1835 return *reinterpret_cast<JsonValue *>(&pair_->val);
1836 }
1837
1838 //! Retrieve the value of pair
1839 const JsonValue &value(void) const {
1840 return *reinterpret_cast<JsonValue *>(&pair_->val);
1841 }
1842
1843 protected:
1844 friend class JsonObject;
1845
1846 //! Constructor for friends
1847 JsonPair(mod_json_pair_t *pair) : pair_(pair) {}
1848
1849 //! Constructor for friends
1850 JsonPair(const JsonPair &rhs) : pair_(rhs.pair_) {}
1851
1852 private:
1853 mod_json_pair_t *pair_;
1854};
1855
1856/*! JSON Object
1857 */
1858class JsonObject {
1859 public:
1860 typedef mod_json_size_t size_type;
1861 typedef mod_json_ssize_t ssize_type;
1862
1863 class iterator;
1864 class const_iterator;
1865 class reverse_iterator;
1866 class const_reverse_iterator;
1867
1868 /*! Const iterator of JSON Object
1869 */
1870 class const_iterator {
1871 public:
1872 //! Constructor
1873 const_iterator(void) : iter_(0) {}
1874
1875 //! Equality
1876 bool operator==(const const_iterator &rhs) const {
1877 return (iter_ == rhs.iter_);
1878 }
1879
1880 //! No equality
1881 bool operator!=(const const_iterator &rhs) const {
1882 return (iter_ != rhs.iter_);
1883 }
1884
1885 //! Increment (Prefix)
1886 const_iterator &operator++() {
1887 ++iter_;
1888 return *this;
1889 }
1890
1891 //! Increment (Suffix)
1892 const_iterator operator++(int) {
1893 const_iterator tmp = *this;
1894 ++iter_;
1895 return tmp;
1896 }
1897
1898 //! Decrement (Prefix)
1899 const_iterator &operator--() {
1900 --iter_;
1901 return *this;
1902 }
1903
1904 //! Decrement (Suffix)
1905 const_iterator operator--(int) {
1906 const_iterator tmp = *this;
1907 --iter_;
1908 return tmp;
1909 }
1910
1911 //! Indirection (eg. *iter)
1912 const JsonPair &operator*() const {
1913 return *reinterpret_cast<const JsonPair *>(&iter_);
1914 }
1915
1916 //! Structure dereference (eg. iter->)
1917 const JsonPair *operator->() const {
1918 return reinterpret_cast<const JsonPair *>(&iter_);
1919 }
1920
1921 //! Retrieve as const reverse iterator
1922 operator const_reverse_iterator() const {
1923 return const_reverse_iterator(iter_);
1924 }
1925
1926 protected:
1927 friend class JsonObject;
1928 friend class JsonObject::iterator;
1929 friend class JsonObject::reverse_iterator;
1930 friend class JsonObject::const_reverse_iterator;
1931
1932 //! Constructor for friends
1933 const_iterator(const mod_json_pair_t *iter) : iter_(iter) {}
1934
1935 private:
1936 const mod_json_pair_t *iter_;
1937 };
1938
1939 /*! iterator of JSON Object
1940 */
1941 class iterator {
1942 public:
1943 //! Constructor
1944 iterator(void) : iter_(0) {}
1945
1946 //! Equality
1947 bool operator==(const iterator &rhs) const {
1948 return (iter_ == rhs.iter_);
1949 }
1950
1951 //! No equality
1952 bool operator!=(const iterator &rhs) const {
1953 return (iter_ != rhs.iter_);
1954 }
1955
1956 //! Increment (Prefix)
1957 iterator &operator++() {
1958 ++iter_;
1959 return *this;
1960 }
1961
1962 //! Increment (Suffix)
1963 iterator operator++(int) {
1964 iterator tmp = *this;
1965 ++iter_;
1966 return tmp;
1967 }
1968
1969 //! Decrement (Prefix)
1970 iterator &operator--() {
1971 --iter_;
1972 return *this;
1973 }
1974
1975 //! Decrement (Suffix)
1976 iterator operator--(int) {
1977 iterator tmp = *this;
1978 --iter_;
1979 return tmp;
1980 }
1981
1982 //! Indirection (eg. *iter)
1983 JsonPair &operator*() const {
1984 return *reinterpret_cast<JsonPair *>((mod_json_pair_t **)&iter_);
1985 }
1986
1987 //! Structure dereference (eg. iter->)
1988 JsonPair *operator->() const {
1989 return reinterpret_cast<JsonPair *>((mod_json_pair_t **)&iter_);
1990 }
1991
1992 //! Retrieve as const iterator
1993 operator const_iterator() const {
1994 return const_iterator(iter_);
1995 }
1996
1997 //! Retrieve as reverse iterator
1998 operator reverse_iterator() const {
1999 return reverse_iterator(iter_);
2000 }
2001
2002 //! Retrieve as const reverse iterator
2003 operator const_reverse_iterator() const {
2004 return const_reverse_iterator(iter_);
2005 }
2006
2007 protected:
2008 friend class JsonObject;
2009 friend class JsonObject::reverse_iterator;
2010
2011 //! Constructor for friends
2012 iterator(mod_json_pair_t *iter) : iter_(iter) {}
2013
2014 private:
2015 mod_json_pair_t *iter_;
2016 };
2017
2018 /*! Const Reverse iterator of JSON Object
2019 */
2020 class const_reverse_iterator {
2021 public:
2022 //! Constructor
2023 const_reverse_iterator(void) : iter_(0) {}
2024
2025 //! Equality
2026 bool operator==(const const_reverse_iterator &rhs) const {
2027 return (iter_ == rhs.iter_);
2028 }
2029
2030 //! No equality
2031 bool operator!=(const const_reverse_iterator &rhs) const {
2032 return (iter_ != rhs.iter_);
2033 }
2034
2035 //! Increment (Prefix)
2036 const_reverse_iterator &operator++() {
2037 --iter_;
2038 return *this;
2039 }
2040
2041 //! Increment (Suffix)
2042 const_reverse_iterator operator++(int) {
2043 const_reverse_iterator tmp = *this;
2044 --iter_;
2045 return tmp;
2046 }
2047
2048 //! Decrement (Prefix)
2049 const_reverse_iterator &operator--() {
2050 ++iter_;
2051 return *this;
2052 }
2053
2054 //! Decrement (Suffix)
2055 const_reverse_iterator operator--(int) {
2056 const_reverse_iterator tmp = *this;
2057 ++iter_;
2058 return tmp;
2059 }
2060
2061 //! Indirection (eg. *iter)
2062 const JsonPair &operator*() const {
2063 return *reinterpret_cast<const JsonPair *>(&iter_);
2064 }
2065
2066 //! Structure dereference (eg. iter->)
2067 const JsonPair *operator->() const {
2068 return reinterpret_cast<const JsonPair *>(&iter_);
2069 }
2070
2071 //! Retrieve as const iterator
2072 operator const_iterator() const {
2073 return const_iterator(iter_);
2074 }
2075
2076 protected:
2077 friend class JsonObject;
2078 friend class JsonObject::iterator;
2079 friend class JsonObject::const_iterator;
2080 friend class JsonObject::reverse_iterator;
2081
2082 //! Constructor for friends
2083 const_reverse_iterator(const mod_json_pair_t *iter) : iter_(iter) {}
2084
2085 private:
2086 const mod_json_pair_t *iter_;
2087 };
2088
2089 /*! iterator of JSON Object
2090 */
2091 class reverse_iterator {
2092 public:
2093 //! Constructor
2094 reverse_iterator(void) : iter_(0) {}
2095
2096 //! Equality
2097 bool operator==(const reverse_iterator &rhs) const {
2098 return (iter_ == rhs.iter_);
2099 }
2100
2101 //! No equality
2102 bool operator!=(const reverse_iterator &rhs) const {
2103 return (iter_ != rhs.iter_);
2104 }
2105
2106 //! Increment (Prefix)
2107 reverse_iterator &operator++() {
2108 --iter_;
2109 return *this;
2110 }
2111
2112 //! Increment (Suffix)
2113 reverse_iterator operator++(int) {
2114 reverse_iterator tmp = *this;
2115 --iter_;
2116 return tmp;
2117 }
2118
2119 //! Decrement (Prefix)
2120 reverse_iterator &operator--() {
2121 ++iter_;
2122 return *this;
2123 }
2124
2125 //! Decrement (Suffix)
2126 reverse_iterator operator--(int) {
2127 reverse_iterator tmp = *this;
2128 ++iter_;
2129 return tmp;
2130 }
2131
2132 //! Indirection (eg. *iter)
2133 JsonPair &operator*() const {
2134 return *reinterpret_cast<JsonPair *>((mod_json_pair_t **)&iter_);
2135 }
2136
2137 //! Structure dereference (eg. iter->)
2138 JsonPair *operator->() const {
2139 return reinterpret_cast<JsonPair *>((mod_json_pair_t **)&iter_);
2140 }
2141
2142 //! Retrieve as iterator
2143 operator iterator() const {
2144 return iterator(iter_);
2145 }
2146
2147 //! Retrieve as const iterator
2148 operator const_iterator() const {
2149 return const_iterator(iter_);
2150 }
2151
2152 //! Retrieve as const reverse iterator
2153 operator const_reverse_iterator() const {
2154 return const_reverse_iterator(iter_);
2155 }
2156
2157 protected:
2158 friend class JsonObject;
2159 friend class JsonArray::iterator;
2160
2161 //! Constructor for friends
2162 reverse_iterator(mod_json_pair_t *iter) : iter_(iter) {}
2163
2164 private:
2165 mod_json_pair_t *iter_;
2166 };
2167
2168 //! Constructor
2169 JsonObject(void) : obj_(0) {}
2170
2171 //! Constructor
2172 JsonObject(const JsonObject &rhs) : obj_(0) {
2173 if (rhs.obj_) {
2174 obj_ = mod_json_object_grab(rhs.obj_);
2175 }
2176 }
2177
2178#if __cplusplus >= 201103L
2179 //! Constructor
2180 JsonObject(JsonObject &&rhs) : obj_(rhs.obj_) {
2181 rhs.obj_ = 0;
2182 }
2183#endif
2184
2185 //! Destructor
2186 ~JsonObject(void) {
2187 mod_json_object_unset(obj_);
2188 }
2189
2190 //! Assign new contents to the object, replacing its current content
2191 JsonObject &operator=(const JsonObject &rhs) {
2192 this->assign(rhs);
2193 return *this;
2194 }
2195
2196#if __cplusplus >= 201103L
2197 //! Assign new contents to the object, replacing its current content
2198 JsonObject &operator=(JsonObject &&rhs) {
2199 this->assign(std::move(rhs));
2200 return *this;
2201 }
2202#endif
2203
2204 //! Equality
2205 bool operator==(const JsonObject &rhs) const {
2206 return mod_json_object_is_equal(obj_, rhs.obj_);
2207 }
2208
2209 //! No equality
2210 bool operator!=(const JsonObject &rhs) const {
2211 return !(*this == rhs);
2212 }
2213
2214 //! Retrieve the value of a key, if no one exists, create a new one.
2215 JsonValue &operator[](const char *key) {
2216 if (!key) {
2217 throw std::invalid_argument("JsonObject::operator[]");
2218 }
2219
2220 if (!copy_and_leak()) {
2221 throw std::runtime_error("JsonObject::operator[]");
2222 }
2223
2224 JsonPair pair(mod_json_object_touch(obj_, key));
2225 if (!pair.is_valid()) {
2226 throw std::runtime_error("JsonObject::operator[]");
2227 }
2228 return pair.value();
2229 }
2230
2231 //! Retrieve the value of a key, if no one exists, return a null value.
2232 JsonValue operator[](const char *key) const {
2233 if (!key) {
2234 throw std::invalid_argument("JsonObject::operator[]");
2235 }
2236
2237 JsonPair pair(mod_json_object_find(obj_, key));
2238 return (pair.is_valid() ? pair.value() : JsonValue());
2239 }
2240
2241 //! Retrieve the value of a key, if no one exists, create a new one.
2242 JsonValue &operator[](const JsonString &key) {
2243 return (*this)[key.c_str()];
2244 }
2245
2246 //! Retrieve the value of a key, if no one exists, return a null value.
2247 JsonValue operator[](const JsonString &key) const {
2248 return (*this)[key.c_str()];
2249 }
2250
2251 //! Retrieve non-zero if the object is valid
2252 bool is_valid(void) const {
2253 return (obj_ != (mod_json_object_t *)0);
2254 }
2255
2256 //! Retrieve non-zero if the object is empty
2257 bool empty(void) const {
2258 return mod_json_object_empty(obj_);
2259 }
2260
2261 //! Retrieve the size of JSON object
2262 size_type size(void) const {
2263 return mod_json_object_count(obj_);
2264 }
2265
2266 //! Retrieve refer-counter of JSON object
2267 ssize_type refer(void) const {
2268 return mod_json_object_refer(obj_);
2269 }
2270
2271 //! Assign new contents to the object, replacing its current content
2272 void assign(const JsonObject &rhs) {
2273 mod_json_object_unset(obj_);
2274 obj_ = rhs.obj_ ? mod_json_object_grab(rhs.obj_) : 0;
2275 }
2276
2277#if __cplusplus >= 201103L
2278 //! Assign new contents to the object, replacing its current content
2279 void assign(JsonObject &&rhs) {
2280 mod_json_object_unset(obj_);
2281 obj_ = rhs.obj_;
2282 rhs.obj_ = 0;
2283 }
2284#endif
2285
2286 //! Clear the JSON object
2287 void clear(void) {
2288 mod_json_object_unset(obj_);
2289 obj_ = 0;
2290 }
2291
2292 //! Set the value of a key
2293 bool set(const JsonString &key, const JsonValue &val) {
2294 JsonValue tmp(val);
2295 if (!copy_on_write()) {
2296 throw std::runtime_error("JsonObject::set");
2297 }
2298 return (mod_json_object_insert(obj_, *(mod_json_string_t **)&key,
2299 *(mod_json_value_t **)&tmp) !=
2300 (mod_json_pair_t *)0);
2301 }
2302
2303 //! Retrieve the value of a key
2304 bool get(const char *key, JsonValue *val) const {
2305 const JsonPair pair(mod_json_object_find(obj_, key));
2306 if (!pair.is_valid()) {
2307 return false;
2308 }
2309 *val = pair.value();
2310 return true;
2311 }
2312
2313 //! Retrieve the value of a key
2314 bool get(const char *key, JsonString *val) const {
2315 const JsonPair pair(mod_json_object_find(obj_, key));
2316 if (!pair.is_valid() || !pair.value().is_string()) {
2317 return false;
2318 }
2319 *val = pair.value().as_string();
2320 return true;
2321 }
2322
2323 //! Retrieve the value of a key
2324 bool get(const char *key, std::string *val) const {
2325 const JsonPair pair(mod_json_object_find(obj_, key));
2326 if (!pair.is_valid() || !pair.value().is_string()) {
2327 return false;
2328 }
2329 *val = pair.value().as_stl_string();
2330 return true;
2331 }
2332
2333 //! Retrieve the value of a key
2334 bool get(const char *key, JsonArray *val) const {
2335 const JsonPair pair(mod_json_object_find(obj_, key));
2336 if (!pair.is_valid() || !pair.value().is_array()) {
2337 return false;
2338 }
2339 *val = pair.value().as_array();
2340 return true;
2341 }
2342
2343 //! Retrieve the value of a key
2344 bool get(const char *key, JsonObject *val) const {
2345 const JsonPair pair(mod_json_object_find(obj_, key));
2346 if (!pair.is_valid() || !pair.value().is_object()) {
2347 return false;
2348 }
2349 *val = pair.value().as_object();
2350 return true;
2351 }
2352
2353 //! Retrieve the value of a key
2354 bool get(const char *key, bool *val) const {
2355 const JsonPair pair(mod_json_object_find(obj_, key));
2356 if (!pair.is_valid()) {
2357 return false;
2358 }
2359 *val = pair.value().as_bool();
2360 return true;
2361 }
2362
2363 //! Retrieve the value of a key
2364 bool get(const char *key, signed char *val) const {
2365 const JsonPair pair(mod_json_object_find(obj_, key));
2366 if (!pair.is_valid()) {
2367 return false;
2368 }
2369 *val = static_cast<signed char>(pair.value().as_integer());
2370 return true;
2371 }
2372
2373 //! Retrieve the value of a key
2374 bool get(const char *key, char *val) const {
2375 const JsonPair pair(mod_json_object_find(obj_, key));
2376 if (!pair.is_valid()) {
2377 return false;
2378 }
2379 *val = static_cast<char>(pair.value().as_integer());
2380 return true;
2381 }
2382
2383 //! Retrieve the value of a key
2384 bool get(const char *key, short int *val) const {
2385 const JsonPair pair(mod_json_object_find(obj_, key));
2386 if (!pair.is_valid()) {
2387 return false;
2388 }
2389 *val = static_cast<short int>(pair.value().as_integer());
2390 return true;
2391 }
2392
2393 //! Retrieve the value of a key
2394 bool get(const char *key, int *val) const {
2395 const JsonPair pair(mod_json_object_find(obj_, key));
2396 if (!pair.is_valid()) {
2397 return false;
2398 }
2399 *val = static_cast<int>(pair.value().as_integer());
2400 return true;
2401 }
2402
2403 //! Retrieve the value of a key
2404 bool get(const char *key, long int *val) const {
2405 const JsonPair pair(mod_json_object_find(obj_, key));
2406 if (!pair.is_valid()) {
2407 return false;
2408 }
2409 *val = static_cast<long int>(pair.value().as_integer());
2410 return true;
2411 }
2412
2413 //! Retrieve the value of a key
2414 bool get(const char *key, long long int *val) const {
2415 const JsonPair pair(mod_json_object_find(obj_, key));
2416 if (!pair.is_valid()) {
2417 return false;
2418 }
2419 *val = static_cast<long long int>(pair.value().as_integer());
2420 return true;
2421 }
2422
2423 //! Retrieve the value of a key
2424 bool get(const char *key, unsigned char *val) const {
2425 const JsonPair pair(mod_json_object_find(obj_, key));
2426 if (!pair.is_valid()) {
2427 return false;
2428 }
2429 *val = static_cast<unsigned char>(pair.value().as_integer());
2430 return true;
2431 }
2432
2433 //! Retrieve the value of a key
2434 bool get(const char *key, unsigned short int *val) const {
2435 const JsonPair pair(mod_json_object_find(obj_, key));
2436 if (!pair.is_valid()) {
2437 return false;
2438 }
2439 *val = static_cast<unsigned short int>(pair.value().as_integer());
2440 return true;
2441 }
2442
2443 //! Retrieve the value of a key
2444 bool get(const char *key, unsigned int *val) const {
2445 const JsonPair pair(mod_json_object_find(obj_, key));
2446 if (!pair.is_valid()) {
2447 return false;
2448 }
2449 *val = static_cast<unsigned int>(pair.value().as_integer());
2450 return true;
2451 }
2452
2453 //! Retrieve the value of a key
2454 bool get(const char *key, unsigned long int *val) const {
2455 const JsonPair pair(mod_json_object_find(obj_, key));
2456 if (!pair.is_valid()) {
2457 return false;
2458 }
2459 *val = static_cast<unsigned long int>(pair.value().as_integer());
2460 return true;
2461 }
2462
2463 //! Retrieve the value of a key
2464 bool get(const char *key, unsigned long long int *val) const {
2465 const JsonPair pair(mod_json_object_find(obj_, key));
2466 if (!pair.is_valid()) {
2467 return false;
2468 }
2469 *val = static_cast<unsigned long long int>(pair.value().as_integer());
2470 return true;
2471 }
2472
2473 //! Retrieve the value of a key
2474 bool get(const char *key, float *val) const {
2475 const JsonPair pair(mod_json_object_find(obj_, key));
2476 if (!pair.is_valid()) {
2477 return false;
2478 }
2479 *val = static_cast<float>(pair.value().as_float());
2480 return true;
2481 }
2482
2483 //! Retrieve the value of a key
2484 bool get(const char *key, double *val) const {
2485 const JsonPair pair(mod_json_object_find(obj_, key));
2486 if (!pair.is_valid()) {
2487 return false;
2488 }
2489 *val = static_cast<double>(pair.value().as_float());
2490 return true;
2491 }
2492
2493 //! Retrieve the value of a key
2494 bool get(const char *key, long double *val) const {
2495 const JsonPair pair(mod_json_object_find(obj_, key));
2496 if (!pair.is_valid()) {
2497 return false;
2498 }
2499 *val = static_cast<long double>(pair.value().as_float());
2500 return true;
2501 }
2502
2503 //! Retrieve the value of a key
2504 template <typename T>
2505 bool get(const JsonString &key, T *val) const {
2506 return this->get(key.c_str(), val);
2507 }
2508
2509 //! Retrieve the value of a key
2510 template <typename T>
2511 bool get(const std::string &key, T *val) const {
2512 return this->get(key.c_str(), val);
2513 }
2514
2515 //! Delete a key-value pair from JSON object
2516 void unset(const char *key) {
2517 if (obj_ && key) {
2518 if (!copy_on_write()) {
2519 throw std::runtime_error("JsonObject::unset");
2520 }
2521 mod_json_object_erase(obj_, key);
2522 }
2523 }
2524
2525 //! Retrieve non-zero if the key exists in JSON object
2526 bool has(const char *key) const {
2527 return (mod_json_object_find(obj_, key) != (mod_json_pair_t *)0);
2528 }
2529
2530 //! Exchange the content with another JSON object
2531 void swap(JsonObject &rhs) {
2532 mod_json_object_t *obj = obj_;
2533 obj_ = rhs.obj_;
2534 rhs.obj_ = obj;
2535 }
2536
2537 //! Merge another JSON object
2538 void merge(const JsonObject &rhs) {
2539 if (!copy_on_write()) {
2540 throw std::runtime_error("JsonObject::merge");
2541 }
2542 mod_json_object_merge(obj_, rhs.obj_);
2543 }
2544
2545 //! Retrieve an iterator pointing to the first element
2546 iterator begin(void) {
2547 if (copy_and_leak()) {
2548 return iterator(mod_json_object_begin(obj_));
2549 }
2550 return iterator();
2551 }
2552
2553 //! Retrieve a const iterator pointing to the first element
2554 const_iterator begin(void) const {
2555 if (obj_) {
2556 return const_iterator(mod_json_object_begin(obj_));
2557 }
2558 return const_iterator();
2559 }
2560
2561 //! Retrieve a const iterator pointing to the first element
2562 const_iterator cbegin(void) const {
2563 if (obj_) {
2564 return const_iterator(mod_json_object_begin(obj_));
2565 }
2566 return const_iterator();
2567 }
2568
2569 //! Retrieve a reverse iterator pointing to the last element
2570 reverse_iterator rbegin(void) {
2571 if (copy_and_leak()) {
2572 return reverse_iterator(mod_json_object_rbegin(obj_));
2573 }
2574 return reverse_iterator();
2575 }
2576
2577 //! Retrieve a const reverse iterator pointing to the last element
2578 const_reverse_iterator rbegin(void) const {
2579 if (obj_) {
2580 return const_reverse_iterator(mod_json_object_rbegin(obj_));
2581 }
2582 return const_reverse_iterator();
2583 }
2584
2585 //! Retrieve a const reverse iterator pointing to the last element
2586 const_reverse_iterator crbegin(void) const {
2587 if (obj_) {
2588 return const_reverse_iterator(mod_json_object_rbegin(obj_));
2589 }
2590 return const_reverse_iterator();
2591 }
2592
2593 //! Retrieve an iterator pointing to the past-the-end element
2594 iterator end(void) {
2595 if (copy_and_leak()) {
2596 return iterator(mod_json_object_end(obj_));
2597 }
2598 return iterator();
2599 }
2600
2601 //! Retrieve a const iterator pointing to the past-the-end element
2602 const_iterator end(void) const {
2603 if (obj_) {
2604 return const_iterator(mod_json_object_end(obj_));
2605 }
2606 return const_iterator();
2607 }
2608
2609 //! Retrieve a const iterator pointing to the past-the-end element
2610 const_iterator cend(void) const {
2611 if (obj_) {
2612 return const_iterator(mod_json_object_end(obj_));
2613 }
2614 return const_iterator();
2615 }
2616
2617 //! Retrieve a reverse pointing to the past-the-end element
2618 reverse_iterator rend(void) {
2619 if (copy_and_leak()) {
2620 return reverse_iterator(mod_json_object_rend(obj_));
2621 }
2622 return reverse_iterator();
2623 }
2624
2625 //! Retrieve a const reverse pointing to the past-the-end element
2626 const_reverse_iterator rend(void) const {
2627 if (obj_) {
2628 return const_reverse_iterator(mod_json_object_rend(obj_));
2629 }
2630 return const_reverse_iterator();
2631 }
2632
2633 //! Retrieve a const reverse pointing to the past-the-end element
2634 const_reverse_iterator crend(void) const {
2635 if (obj_) {
2636 return const_reverse_iterator(mod_json_object_rend(obj_));
2637 }
2638 return const_reverse_iterator();
2639 }
2640
2641 protected:
2642 //! Clone the object for writing
2643 bool copy_on_write(void) {
2644 if (obj_) {
2645 if (mod_json_object_is_shared(obj_)) {
2646 mod_json_object_put(obj_);
2647 obj_ = mod_json_object_clone(obj_);
2648 }
2649 } else {
2650 obj_ = mod_json_object_set_default();
2651 }
2652 return (obj_ != 0);
2653 }
2654
2655 //! Clone the object and leak it
2656 bool copy_and_leak(void) {
2657 if (copy_on_write()) {
2658 mod_json_object_set_leaked(obj_);
2659 return true;
2660 }
2661 return false;
2662 }
2663
2664 private:
2665 mod_json_object_t *obj_;
2666};
2667
2668//! Assign new contents to the value, replacing its current content
2669inline void JsonValue::assign(const JsonArray &arr) {
2670 this->set_value(arr);
2671}
2672
2673//! Assign new contents to the value, replacing its current content
2674inline void JsonValue::assign(const JsonObject &obj) {
2675 this->set_value(obj);
2676}
2677
2678//! Convert value to JSON object
2679inline JsonObject &JsonValue::to_object(void) {
2680 return *reinterpret_cast<JsonObject *>(&val_->data.c_obj);
2681}
2682
2683//! Convert value to JSON object
2684inline const JsonObject &JsonValue::to_object(void) const {
2685 return *reinterpret_cast<JsonObject *>(&val_->data.c_obj);
2686}
2687
2688//! Convert value to JSON array
2689inline JsonArray &JsonValue::to_array(void) {
2690 return *reinterpret_cast<JsonArray *>(&val_->data.c_arr);
2691}
2692
2693//! Convert value to JSON array
2694inline const JsonArray &JsonValue::to_array(void) const {
2695 return *reinterpret_cast<JsonArray *>(&val_->data.c_arr);
2696}
2697
2698//! Convert value to JSON string
2699inline JsonString &JsonValue::to_string(void) {
2700 return *reinterpret_cast<JsonString *>(&val_->data.c_str);
2701}
2702
2703//! Convert value to JSON string
2704inline const JsonString &JsonValue::to_string(void) const {
2705 return *reinterpret_cast<JsonString *>(&val_->data.c_str);
2706}
2707
2708//! Treat self value as object by force, retrieving value of a key
2709inline JsonValue &JsonValue::get_value(const char *key) {
2710 if (!is_object()) {
2711 *this = JsonObject();
2712 }
2713 if (!copy_and_leak()) {
2714 throw std::runtime_error("JsonValue::get_value");
2715 }
2716 return (to_object())[key];
2717}
2718
2719//! Retrieve a reference of value by a key
2720inline JsonValue JsonValue::get_value(const char *key) const {
2721 return (is_object() ? (to_object())[key] : JsonValue());
2722}
2723
2724//! Treat self value as array by force, retrieving value at index n
2725inline JsonValue &JsonValue::get_value(size_type n) {
2726 if (!is_array()) {
2727 throw std::logic_error("JsonValue::get_value");
2728 }
2729 if (!copy_and_leak()) {
2730 throw std::runtime_error("JsonValue::get_value");
2731 }
2732 return (to_array())[n];
2733}
2734
2735//! Retrieve a reference of value at index n
2736inline JsonValue JsonValue::get_value(size_type n) const {
2737 return (is_array() ? (to_array())[n] : JsonValue());
2738}
2739
2740//! Set the new array to the value, replacing its current content
2741inline void JsonValue::set_value(const JsonArray &val) {
2742 if (!copy_on_write()) {
2743 throw std::runtime_error("JsonValue::set_value");
2744 }
2745 mod_json_value_assign_array(val_, *(mod_json_array_t **)&val);
2746}
2747
2748//! Set the new object to the value, replacing its current content
2749inline void JsonValue::set_value(const JsonObject &val) {
2750 if (!copy_on_write()) {
2751 throw std::runtime_error("JsonValue::set_value");
2752 }
2753 mod_json_value_assign_object(val_, *(mod_json_object_t **)&val);
2754}
2755
2756/*! JSON Parser
2757 */
2758class JsonParser {
2759 public:
2760 typedef mod_json_size_t size_type;
2761
2762 //! Constructor
2763 JsonParser(void)
2764 : state_(mod_json_state_null), error_(mod_json_error_null), context_(0) {
2765 option_.options = 0;
2766 option_.object_depth = 0;
2767 option_.array_depth = 0;
2768 }
2769
2770 //! Destructor
2771 ~JsonParser(void) {}
2772
2773 //! Set the max object depth
2774 void set_object_depth(size_type depth) {
2775 option_.object_depth = depth;
2776 }
2777
2778 //! Set the max array depth
2779 void set_array_depth(size_type depth) {
2780 option_.array_depth = depth;
2781 }
2782
2783 //! Enable/Disable comments
2784 void set_comment(bool enable = true) {
2785 if (enable) {
2786 option_.options |= MOD_JSON_COMMENT;
2787 } else {
2788 option_.options &= ~MOD_JSON_COMMENT;
2789 }
2790 }
2791
2792 //! Enable/Disable loose strings
2793 void set_unstrict(bool enable = true) {
2794 if (enable) {
2795 option_.options |= MOD_JSON_UNSTRICT;
2796 } else {
2797 option_.options &= ~MOD_JSON_UNSTRICT;
2798 }
2799 }
2800
2801 //! Enable/Disable simple format
2802 void set_simple(bool enable = true) {
2803 if (enable) {
2804 option_.options |= MOD_JSON_SIMPLE;
2805 } else {
2806 option_.options &= ~MOD_JSON_SIMPLE;
2807 }
2808 }
2809
2810 //! Enable/Disable single quotes support
2811 void set_squote(bool enable = true) {
2812 if (enable) {
2813 option_.options |= MOD_JSON_SQUOTE;
2814 } else {
2815 option_.options &= ~MOD_JSON_SQUOTE;
2816 }
2817 }
2818
2819 //! Convert a sting to a JSON value
2820 bool parse(const char *str, JsonValue *out) {
2821 mod_json_token_t *tok;
2822
2823 state_ = mod_json_state_null;
2824 error_ = mod_json_error_null;
2825 context_ = str;
2826
2827 tok = mod_json_token_create(&option_);
2828 if (tok) {
2829 mod_json_value_t *jval;
2830
2831 jval = mod_json_parse(tok, str);
2832
2833 /* save information of token */
2834 state_ = mod_json_token_state(tok);
2835 error_ = mod_json_token_error(tok);
2836 context_ = mod_json_token_context(tok);
2837 mod_json_token_destroy(tok);
2838
2839 if (jval) {
2840 *out = *reinterpret_cast<JsonValue *>(&jval);
2841 mod_json_value_unset(jval);
2842
2843 return out->is_valid();
2844 }
2845 }
2846 return false;
2847 }
2848
2849 //! Retrieve the error code of parser
2850 int error(void) const {
2851 return (int)error_;
2852 }
2853
2854 //! Retrieve the state code of parser
2855 int state(void) const {
2856 return (int)state_;
2857 }
2858
2859 //! Retrieve the context of parser
2860 const char *context(void) const {
2861 return context_;
2862 }
2863
2864 private:
2865 mod_json_option_t option_;
2866 mod_json_state_t state_;
2867 mod_json_error_t error_;
2868 mod_json_cchar_t *context_;
2869};
2870
2871/*! JSON Dumper
2872 */
2873class JsonDumper {
2874 public:
2875 //! Constructor
2876 JsonDumper(void) : str_() {}
2877
2878 //! Destructor
2879 ~JsonDumper(void) {}
2880
2881 //! Dump a JSON value to string
2882 bool dump(const JsonValue &val) {
2883 mod_json_string_t *str;
2884
2885 str = mod_json_dump(*((mod_json_value_t **)&val));
2886 str_ = *reinterpret_cast<JsonString *>(&str);
2887 if (str) {
2888 mod_json_string_unset(str);
2889 return true;
2890 }
2891 return false;
2892 }
2893
2894 //! Retrieve result of dumper
2895 JsonString &result(void) {
2896 return str_;
2897 }
2898
2899 //! Retrieve result of dumper
2900 const JsonString &result(void) const {
2901 return str_;
2902 }
2903
2904 private:
2905 JsonString str_;
2906};
2907
2908//! Equality
2909static inline bool operator==(const ailego::JsonString &lhs, const char *rhs) {
2910 const char *self = lhs.c_str();
2911 if (self == rhs) {
2912 return true;
2913 }
2914
2915 if (self && rhs) {
2916 return (std::strcmp(self, rhs) == 0);
2917 }
2918 return false;
2919}
2920
2921//! Equality
2922static inline bool operator==(const char *lhs, const ailego::JsonString &rhs) {
2923 return (rhs == lhs);
2924}
2925
2926//! Equality
2927static inline bool operator==(const ailego::JsonString &lhs,
2928 const std::string &rhs) {
2929 std::size_t ls = lhs.size();
2930 std::size_t rs = rhs.size();
2931 if (ls == 0 && rs == 0) {
2932 return true;
2933 }
2934
2935 if (ls == rs) {
2936 const char *ld = lhs.data();
2937 const char *rd = rhs.data();
2938
2939 if (ld && rd) {
2940 return (std::memcmp(ld, rd, ls) == 0);
2941 }
2942 }
2943 return false;
2944}
2945
2946//! Equality
2947static inline bool operator==(const std::string &lhs, const JsonString &rhs) {
2948 return (rhs == lhs);
2949}
2950
2951//! Equality
2952static inline bool operator==(const JsonString &lhs, const JsonValue &rhs) {
2953 return (rhs.is_string() ? lhs == rhs.as_string() : false);
2954}
2955
2956//! Equality
2957static inline bool operator==(const JsonValue &lhs, const JsonString &rhs) {
2958 return (lhs.is_string() ? lhs.as_string() == rhs : false);
2959}
2960
2961//! Equality
2962static inline bool operator==(const JsonArray &lhs, const JsonValue &rhs) {
2963 return (rhs.is_array() ? lhs == rhs.as_array() : false);
2964}
2965
2966//! Equality
2967static inline bool operator==(const JsonValue &lhs, const JsonArray &rhs) {
2968 return (lhs.is_array() ? lhs.as_array() == rhs : false);
2969}
2970
2971//! Equality
2972static inline bool operator==(const JsonObject &lhs, const JsonValue &rhs) {
2973 return (rhs.is_object() ? lhs == rhs.as_object() : false);
2974}
2975
2976//! Equality
2977static inline bool operator==(const JsonValue &lhs, const JsonObject &rhs) {
2978 return (lhs.is_object() ? lhs.as_object() == rhs : false);
2979}
2980
2981//! Equality
2982static inline bool operator==(const JsonValue &lhs, const bool &rhs) {
2983 return (lhs.is_boolean() ? lhs.as_bool() == rhs : false);
2984}
2985
2986//! Equality
2987static inline bool operator==(const bool &lhs, const JsonValue &rhs) {
2988 return (rhs.is_boolean() ? lhs == rhs.as_bool() : false);
2989}
2990
2991//! Equality
2992static inline bool operator==(const JsonValue &lhs, const signed char &rhs) {
2993 return (lhs.is_integer()
2994 ? lhs.as_integer() == static_cast<JsonValue::integer_type>(rhs)
2995 : false);
2996}
2997
2998//! Equality
2999static inline bool operator==(const signed char &lhs, const JsonValue &rhs) {
3000 return (rhs.is_integer()
3001 ? static_cast<JsonValue::integer_type>(lhs) == rhs.as_integer()
3002 : false);
3003}
3004
3005//! Equality
3006static inline bool operator==(const JsonValue &lhs, const char &rhs) {
3007 return (lhs.is_integer()
3008 ? lhs.as_integer() == static_cast<JsonValue::integer_type>(rhs)
3009 : false);
3010}
3011
3012//! Equality
3013static inline bool operator==(const char &lhs, const JsonValue &rhs) {
3014 return (rhs.is_integer()
3015 ? static_cast<JsonValue::integer_type>(lhs) == rhs.as_integer()
3016 : false);
3017}
3018
3019//! Equality
3020static inline bool operator==(const JsonValue &lhs, const short int &rhs) {
3021 return (lhs.is_integer()
3022 ? lhs.as_integer() == static_cast<JsonValue::integer_type>(rhs)
3023 : false);
3024}
3025
3026//! Equality
3027static inline bool operator==(const short int &lhs, const JsonValue &rhs) {
3028 return (rhs.is_integer()
3029 ? static_cast<JsonValue::integer_type>(lhs) == rhs.as_integer()
3030 : false);
3031}
3032
3033//! Equality
3034static inline bool operator==(const JsonValue &lhs, const int &rhs) {
3035 return (lhs.is_integer()
3036 ? lhs.as_integer() == static_cast<JsonValue::integer_type>(rhs)
3037 : false);
3038}
3039
3040//! Equality
3041static inline bool operator==(const int &lhs, const JsonValue &rhs) {
3042 return (rhs.is_integer()
3043 ? static_cast<JsonValue::integer_type>(lhs) == rhs.as_integer()
3044 : false);
3045}
3046
3047//! Equality
3048static inline bool operator==(const JsonValue &lhs, const long int &rhs) {
3049 return (lhs.is_integer()
3050 ? lhs.as_integer() == static_cast<JsonValue::integer_type>(rhs)
3051 : false);
3052}
3053
3054//! Equality
3055static inline bool operator==(const long int &lhs, const JsonValue &rhs) {
3056 return (rhs.is_integer()
3057 ? static_cast<JsonValue::integer_type>(lhs) == rhs.as_integer()
3058 : false);
3059}
3060
3061//! Equality
3062static inline bool operator==(const JsonValue &lhs, const long long int &rhs) {
3063 return (lhs.is_integer()
3064 ? lhs.as_integer() == static_cast<JsonValue::integer_type>(rhs)
3065 : false);
3066}
3067
3068//! Equality
3069static inline bool operator==(const long long int &lhs, const JsonValue &rhs) {
3070 return (rhs.is_integer()
3071 ? static_cast<JsonValue::integer_type>(lhs) == rhs.as_integer()
3072 : false);
3073}
3074
3075//! Equality
3076static inline bool operator==(const JsonValue &lhs, const float &rhs) {
3077 if (lhs.is_float()) {
3078 double diff = static_cast<double>(lhs.as_float() - rhs);
3079 return ((diff < DBL_EPSILON) && (diff > -DBL_EPSILON));
3080 }
3081 return false;
3082}
3083
3084//! Equality
3085static inline bool operator==(const float &lhs, const JsonValue &rhs) {
3086 if (rhs.is_float()) {
3087 double diff = static_cast<double>(rhs.as_float() - lhs);
3088 return ((diff < DBL_EPSILON) && (diff > -DBL_EPSILON));
3089 }
3090 return false;
3091}
3092
3093//! Equality
3094static inline bool operator==(const JsonValue &lhs, const double &rhs) {
3095 if (lhs.is_float()) {
3096 double diff = static_cast<double>(lhs.as_float() - rhs);
3097 return ((diff < DBL_EPSILON) && (diff > -DBL_EPSILON));
3098 }
3099 return false;
3100}
3101
3102//! Equality
3103static inline bool operator==(const double &lhs, const JsonValue &rhs) {
3104 if (rhs.is_float()) {
3105 double diff = static_cast<double>(rhs.as_float() - lhs);
3106 return ((diff < DBL_EPSILON) && (diff > -DBL_EPSILON));
3107 }
3108 return false;
3109}
3110
3111//! Equality
3112static inline bool operator==(const JsonValue &lhs, const long double &rhs) {
3113 if (lhs.is_float()) {
3114 double diff = static_cast<double>(lhs.as_float() - rhs);
3115 return ((diff < DBL_EPSILON) && (diff > -DBL_EPSILON));
3116 }
3117 return false;
3118}
3119
3120//! Equality
3121static inline bool operator==(const long double &lhs, const JsonValue &rhs) {
3122 if (rhs.is_float()) {
3123 double diff = static_cast<double>(rhs.as_float() - lhs);
3124 return ((diff < DBL_EPSILON) && (diff > -DBL_EPSILON));
3125 }
3126 return false;
3127}
3128
3129//! Equality
3130static inline bool operator==(const JsonValue &lhs, const unsigned char &rhs) {
3131 return (lhs.is_integer()
3132 ? lhs.as_integer() == static_cast<JsonValue::integer_type>(rhs)
3133 : false);
3134}
3135
3136//! Equality
3137static inline bool operator==(const unsigned char &lhs, const JsonValue &rhs) {
3138 return (rhs.is_integer()
3139 ? static_cast<JsonValue::integer_type>(lhs) == rhs.as_integer()
3140 : false);
3141}
3142
3143//! Equality
3144static inline bool operator==(const JsonValue &lhs,
3145 const unsigned short int &rhs) {
3146 return (lhs.is_integer()
3147 ? lhs.as_integer() == static_cast<JsonValue::integer_type>(rhs)
3148 : false);
3149}
3150
3151//! Equality
3152static inline bool operator==(const unsigned short int &lhs,
3153 const JsonValue &rhs) {
3154 return (rhs.is_integer()
3155 ? static_cast<JsonValue::integer_type>(lhs) == rhs.as_integer()
3156 : false);
3157}
3158
3159//! Equality
3160static inline bool operator==(const JsonValue &lhs, const unsigned int &rhs) {
3161 return (lhs.is_integer()
3162 ? lhs.as_integer() == static_cast<JsonValue::integer_type>(rhs)
3163 : false);
3164}
3165
3166//! Equality
3167static inline bool operator==(const unsigned int &lhs, const JsonValue &rhs) {
3168 return (rhs.is_integer()
3169 ? static_cast<JsonValue::integer_type>(lhs) == rhs.as_integer()
3170 : false);
3171}
3172
3173//! Equality
3174static inline bool operator==(const JsonValue &lhs,
3175 const unsigned long int &rhs) {
3176 return (lhs.is_integer()
3177 ? lhs.as_integer() == static_cast<JsonValue::integer_type>(rhs)
3178 : false);
3179}
3180
3181//! Equality
3182static inline bool operator==(const unsigned long int &lhs,
3183 const JsonValue &rhs) {
3184 return (rhs.is_integer()
3185 ? static_cast<JsonValue::integer_type>(lhs) == rhs.as_integer()
3186 : false);
3187}
3188
3189//! Equality
3190static inline bool operator==(const JsonValue &lhs,
3191 const unsigned long long int &rhs) {
3192 return (lhs.is_integer()
3193 ? lhs.as_integer() == static_cast<JsonValue::integer_type>(rhs)
3194 : false);
3195}
3196
3197//! Equality
3198static inline bool operator==(const unsigned long long int &lhs,
3199 const JsonValue &rhs) {
3200 return (rhs.is_integer()
3201 ? static_cast<JsonValue::integer_type>(lhs) == rhs.as_integer()
3202 : false);
3203}
3204
3205//! Equality
3206static inline bool operator==(const JsonValue &lhs, const char *rhs) {
3207 return (lhs.is_string() ? lhs.as_string() == rhs : false);
3208}
3209
3210//! Equality
3211static inline bool operator==(const char *lhs, const JsonValue &rhs) {
3212 return (rhs.is_string() ? lhs == rhs.as_string() : false);
3213}
3214
3215//! Equality
3216static inline bool operator==(const JsonValue &lhs, const std::string &rhs) {
3217 return (lhs.is_string() ? lhs.as_string() == rhs : false);
3218}
3219
3220//! Equality
3221static inline bool operator==(const std::string &lhs, const JsonValue &rhs) {
3222 return (rhs.is_string() ? lhs == rhs.as_string() : false);
3223}
3224
3225//! No equality
3226static inline bool operator!=(const JsonString &lhs, const char *rhs) {
3227 return !(lhs == rhs);
3228}
3229
3230//! No equality
3231static inline bool operator!=(const char *lhs, const JsonString &rhs) {
3232 return !(lhs == rhs);
3233}
3234
3235//! No equality
3236static inline bool operator!=(const JsonString &lhs, const std::string &rhs) {
3237 return !(lhs == rhs);
3238}
3239
3240//! No equality
3241static inline bool operator!=(const std::string &lhs, const JsonString &rhs) {
3242 return !(lhs == rhs);
3243}
3244
3245//! No equality
3246static inline bool operator!=(const JsonString &lhs, const JsonValue &rhs) {
3247 return !(lhs == rhs);
3248}
3249
3250//! No equality
3251static inline bool operator!=(const JsonValue &lhs, const JsonString &rhs) {
3252 return !(lhs == rhs);
3253}
3254
3255//! No equality
3256static inline bool operator!=(const JsonArray &lhs, const JsonValue &rhs) {
3257 return !(lhs == rhs);
3258}
3259
3260//! No equality
3261static inline bool operator!=(const JsonValue &lhs, const JsonArray &rhs) {
3262 return !(lhs == rhs);
3263}
3264
3265//! No equality
3266static inline bool operator!=(const JsonObject &lhs, const JsonValue &rhs) {
3267 return !(lhs == rhs);
3268}
3269
3270//! No equality
3271static inline bool operator!=(const JsonValue &lhs, const JsonObject &rhs) {
3272 return !(lhs == rhs);
3273}
3274
3275//! No equality
3276static inline bool operator!=(const JsonValue &lhs, const bool &rhs) {
3277 return !(lhs == rhs);
3278}
3279
3280//! No equality
3281static inline bool operator!=(const bool &lhs, const JsonValue &rhs) {
3282 return !(lhs == rhs);
3283}
3284
3285//! No equality
3286static inline bool operator!=(const JsonValue &lhs, const signed char &rhs) {
3287 return !(lhs == rhs);
3288}
3289
3290//! No equality
3291static inline bool operator!=(const signed char &lhs, const JsonValue &rhs) {
3292 return !(lhs == rhs);
3293}
3294
3295//! No equality
3296static inline bool operator!=(const JsonValue &lhs, const char &rhs) {
3297 return !(lhs == rhs);
3298}
3299
3300//! No equality
3301static inline bool operator!=(const char &lhs, const JsonValue &rhs) {
3302 return !(lhs == rhs);
3303}
3304
3305//! No equality
3306static inline bool operator!=(const JsonValue &lhs, const short int &rhs) {
3307 return !(lhs == rhs);
3308}
3309
3310//! No equality
3311static inline bool operator!=(const short int &lhs, const JsonValue &rhs) {
3312 return !(lhs == rhs);
3313}
3314
3315//! No equality
3316static inline bool operator!=(const JsonValue &lhs, const int &rhs) {
3317 return !(lhs == rhs);
3318}
3319
3320//! No equality
3321static inline bool operator!=(const int &lhs, const JsonValue &rhs) {
3322 return !(lhs == rhs);
3323}
3324
3325//! No equality
3326static inline bool operator!=(const JsonValue &lhs, const long int &rhs) {
3327 return !(lhs == rhs);
3328}
3329
3330//! No equality
3331static inline bool operator!=(const long int &lhs, const JsonValue &rhs) {
3332 return !(lhs == rhs);
3333}
3334
3335//! No equality
3336static inline bool operator!=(const JsonValue &lhs, const long long int &rhs) {
3337 return !(lhs == rhs);
3338}
3339
3340//! No equality
3341static inline bool operator!=(const long long int &lhs, const JsonValue &rhs) {
3342 return !(lhs == rhs);
3343}
3344
3345//! No equality
3346static inline bool operator!=(const JsonValue &lhs, const float &rhs) {
3347 return !(lhs == rhs);
3348}
3349
3350//! No equality
3351static inline bool operator!=(const float &lhs, const JsonValue &rhs) {
3352 return !(lhs == rhs);
3353}
3354
3355//! No equality
3356static inline bool operator!=(const JsonValue &lhs, const double &rhs) {
3357 return !(lhs == rhs);
3358}
3359
3360//! No equality
3361static inline bool operator!=(const double &lhs, const JsonValue &rhs) {
3362 return !(lhs == rhs);
3363}
3364
3365//! No equality
3366static inline bool operator!=(const JsonValue &lhs, const long double &rhs) {
3367 return !(lhs == rhs);
3368}
3369
3370//! No equality
3371static inline bool operator!=(const long double &lhs, const JsonValue &rhs) {
3372 return !(lhs == rhs);
3373}
3374
3375//! No equality
3376static inline bool operator!=(const JsonValue &lhs, const unsigned char &rhs) {
3377 return !(lhs == rhs);
3378}
3379
3380//! No equality
3381static inline bool operator!=(const unsigned char &lhs, const JsonValue &rhs) {
3382 return !(lhs == rhs);
3383}
3384
3385//! No equality
3386static inline bool operator!=(const JsonValue &lhs,
3387 const unsigned short int &rhs) {
3388 return !(lhs == rhs);
3389}
3390
3391//! No equality
3392static inline bool operator!=(const unsigned short int &lhs,
3393 const JsonValue &rhs) {
3394 return !(lhs == rhs);
3395}
3396
3397//! No equality
3398static inline bool operator!=(const JsonValue &lhs, const unsigned int &rhs) {
3399 return !(lhs == rhs);
3400}
3401
3402//! No equality
3403static inline bool operator!=(const unsigned int &lhs, const JsonValue &rhs) {
3404 return !(lhs == rhs);
3405}
3406
3407//! No equality
3408static inline bool operator!=(const JsonValue &lhs,
3409 const unsigned long int &rhs) {
3410 return !(lhs == rhs);
3411}
3412
3413//! No equality
3414static inline bool operator!=(const unsigned long int &lhs,
3415 const JsonValue &rhs) {
3416 return !(lhs == rhs);
3417}
3418
3419//! No equality
3420static inline bool operator!=(const JsonValue &lhs,
3421 const unsigned long long int &rhs) {
3422 return !(lhs == rhs);
3423}
3424
3425//! No equality
3426static inline bool operator!=(const unsigned long long int &lhs,
3427 const JsonValue &rhs) {
3428 return !(lhs == rhs);
3429}
3430
3431//! No equality
3432static inline bool operator!=(const JsonValue &lhs, const char *rhs) {
3433 return !(lhs == rhs);
3434}
3435
3436//! No equality
3437static inline bool operator!=(const char *lhs, const JsonValue &rhs) {
3438 return !(lhs == rhs);
3439}
3440
3441//! No equality
3442static inline bool operator!=(const JsonValue &lhs, const std::string &rhs) {
3443 return !(lhs == rhs);
3444}
3445
3446//! No equality
3447static inline bool operator!=(const std::string &lhs, const JsonValue &rhs) {
3448 return !(lhs == rhs);
3449}
3450
3451} // namespace ailego
3452
3453#endif // __AILEGO_ENCODING_JSON_MOD_JSON_PLUS_H__
3454