rippled
Loading...
Searching...
No Matches
json_value.cpp
1//------------------------------------------------------------------------------
2/*
3 This file is part of rippled: https://github.com/ripple/rippled
4 Copyright (c) 2012, 2013 Ripple Labs Inc.
5
6 Permission to use, copy, modify, and/or distribute this software for any
7 purpose with or without fee is hereby granted, provided that the above
8 copyright notice and this permission notice appear in all copies.
9
10 THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 ANY SPECIAL , DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17*/
18//==============================================================================
19
20#include <xrpl/beast/core/LexicalCast.h>
21#include <xrpl/beast/utility/instrumentation.h>
22#include <xrpl/json/detail/json_assert.h>
23#include <xrpl/json/json_forwards.h>
24#include <xrpl/json/json_value.h>
25#include <xrpl/json/json_writer.h>
26
27#include <cstdlib>
28#include <cstring>
29#include <string>
30#include <utility>
31
32namespace Json {
33
34const Value Value::null;
35const Int Value::minInt = Int(~(UInt(-1) / 2));
36const Int Value::maxInt = Int(UInt(-1) / 2);
37const UInt Value::maxUInt = UInt(-1);
38
40{
41public:
42 virtual ~DefaultValueAllocator() = default;
43
44 char*
45 makeMemberName(const char* memberName) override
46 {
47 return duplicateStringValue(memberName);
48 }
49
50 void
51 releaseMemberName(char* memberName) override
52 {
53 releaseStringValue(memberName);
54 }
55
56 char*
57 duplicateStringValue(const char* value, unsigned int length = unknown)
58 override
59 {
60 //@todo investigate this old optimization
61 // if ( !value || value[0] == 0 )
62 // return 0;
63
64 if (length == unknown)
65 length = value ? (unsigned int)strlen(value) : 0;
66
67 char* newString = static_cast<char*>(malloc(length + 1));
68 if (value)
69 memcpy(newString, value, length);
70 newString[length] = 0;
71 return newString;
72 }
73
74 void
75 releaseStringValue(char* value) override
76 {
77 if (value)
78 free(value);
79 }
80};
81
82static ValueAllocator*&
84{
86 return valueAllocator;
87}
88
90{
92 {
93 valueAllocator(); // ensure valueAllocator() statics are initialized
94 // before main().
95 }
97
98// //////////////////////////////////////////////////////////////////
99// //////////////////////////////////////////////////////////////////
100// //////////////////////////////////////////////////////////////////
101// class Value::CZString
102// //////////////////////////////////////////////////////////////////
103// //////////////////////////////////////////////////////////////////
104// //////////////////////////////////////////////////////////////////
105
106// Notes: index_ indicates if the string was allocated when
107// a string is stored.
108
109Value::CZString::CZString(int index) : cstr_(0), index_(index)
110{
111}
112
114 : cstr_(
115 allocate == duplicate ? valueAllocator()->makeMemberName(cstr) : cstr)
116 , index_(allocate)
117{
118}
119
121 : cstr_(
122 other.index_ != noDuplication && other.cstr_ != 0
123 ? valueAllocator()->makeMemberName(other.cstr_)
124 : other.cstr_)
125 , index_(
126 other.cstr_
127 ? (other.index_ == noDuplication ? noDuplication : duplicate)
128 : other.index_)
129{
130}
131
133{
134 if (cstr_ && index_ == duplicate)
135 valueAllocator()->releaseMemberName(const_cast<char*>(cstr_));
136}
137
138bool
139Value::CZString::operator<(const CZString& other) const
140{
141 if (cstr_ && other.cstr_)
142 return strcmp(cstr_, other.cstr_) < 0;
143
144 return index_ < other.index_;
145}
146
147bool
149{
150 if (cstr_ && other.cstr_)
151 return strcmp(cstr_, other.cstr_) == 0;
152
153 return index_ == other.index_;
154}
155
156int
158{
159 return index_;
160}
161
162const char*
164{
165 return cstr_;
166}
167
168bool
170{
171 return index_ == noDuplication;
172}
173
174// //////////////////////////////////////////////////////////////////
175// //////////////////////////////////////////////////////////////////
176// //////////////////////////////////////////////////////////////////
177// class Value::Value
178// //////////////////////////////////////////////////////////////////
179// //////////////////////////////////////////////////////////////////
180// //////////////////////////////////////////////////////////////////
181
187{
188 switch (type)
189 {
190 case nullValue:
191 break;
192
193 case intValue:
194 case uintValue:
195 value_.int_ = 0;
196 break;
197
198 case realValue:
199 value_.real_ = 0.0;
200 break;
201
202 case stringValue:
203 value_.string_ = 0;
204 break;
205
206 case arrayValue:
207 case objectValue:
208 value_.map_ = new ObjectValues();
209 break;
210
211 case booleanValue:
212 value_.bool_ = false;
213 break;
214
215 default:
216 UNREACHABLE("Json::Value::Value(ValueType) : invalid type");
217 }
218}
219
220Value::Value(Int value) : type_(intValue)
221{
222 value_.int_ = value;
223}
224
226{
227 value_.uint_ = value;
228}
229
230Value::Value(double value) : type_(realValue)
231{
232 value_.real_ = value;
233}
234
235Value::Value(const char* value) : type_(stringValue), allocated_(true)
236{
238}
239
240Value::Value(std::string const& value) : type_(stringValue), allocated_(true)
241{
243 value.c_str(), (unsigned int)value.length());
244}
245
246Value::Value(const StaticString& value) : type_(stringValue), allocated_(false)
247{
248 value_.string_ = const_cast<char*>(value.c_str());
249}
250
251Value::Value(bool value) : type_(booleanValue)
252{
253 value_.bool_ = value;
254}
255
256Value::Value(const Value& other) : type_(other.type_)
257{
258 switch (type_)
259 {
260 case nullValue:
261 case intValue:
262 case uintValue:
263 case realValue:
264 case booleanValue:
265 value_ = other.value_;
266 break;
267
268 case stringValue:
269 if (other.value_.string_)
270 {
272 other.value_.string_);
273 allocated_ = true;
274 }
275 else
276 value_.string_ = 0;
277
278 break;
279
280 case arrayValue:
281 case objectValue:
282 value_.map_ = new ObjectValues(*other.value_.map_);
283 break;
284
285 default:
286 UNREACHABLE("Json::Value::Value(Value const&) : invalid type");
287 }
288}
289
291{
292 switch (type_)
293 {
294 case nullValue:
295 case intValue:
296 case uintValue:
297 case realValue:
298 case booleanValue:
299 break;
300
301 case stringValue:
302 if (allocated_)
304
305 break;
306
307 case arrayValue:
308 case objectValue:
309 if (value_.map_)
310 delete value_.map_;
311 break;
312
313 default:
314 UNREACHABLE("Json::Value::~Value : invalid type");
315 }
316}
317
318Value&
320{
321 Value tmp(other);
322 swap(tmp);
323 return *this;
324}
325
326Value::Value(Value&& other) noexcept
327 : value_(other.value_), type_(other.type_), allocated_(other.allocated_)
328{
329 other.type_ = nullValue;
330 other.allocated_ = 0;
331}
332
333Value&
335{
336 Value tmp(std::move(other));
337 swap(tmp);
338 return *this;
339}
340
341void
342Value::swap(Value& other) noexcept
343{
344 std::swap(value_, other.value_);
345
346 ValueType temp = type_;
347 type_ = other.type_;
348 other.type_ = temp;
349
350 int temp2 = allocated_;
351 allocated_ = other.allocated_;
352 other.allocated_ = temp2;
353}
354
357{
358 return type_;
359}
360
361static int
363{
364 // All negative numbers are less than all unsigned numbers.
365 if (i < 0)
366 return -1;
367
368 // Now we can safely compare.
369 return (i < ui) ? -1 : (i == ui) ? 0 : 1;
370}
371
372bool
373operator<(const Value& x, const Value& y)
374{
375 if (auto signum = x.type_ - y.type_)
376 {
377 if (x.type_ == intValue && y.type_ == uintValue)
378 signum = integerCmp(x.value_.int_, y.value_.uint_);
379 else if (x.type_ == uintValue && y.type_ == intValue)
380 signum = -integerCmp(y.value_.int_, x.value_.uint_);
381 return signum < 0;
382 }
383
384 switch (x.type_)
385 {
386 case nullValue:
387 return false;
388
389 case intValue:
390 return x.value_.int_ < y.value_.int_;
391
392 case uintValue:
393 return x.value_.uint_ < y.value_.uint_;
394
395 case realValue:
396 return x.value_.real_ < y.value_.real_;
397
398 case booleanValue:
399 return x.value_.bool_ < y.value_.bool_;
400
401 case stringValue:
402 return (x.value_.string_ == 0 && y.value_.string_) ||
403 (y.value_.string_ && x.value_.string_ &&
404 strcmp(x.value_.string_, y.value_.string_) < 0);
405
406 case arrayValue:
407 case objectValue: {
408 if (int signum = int(x.value_.map_->size()) - y.value_.map_->size())
409 return signum < 0;
410
411 return *x.value_.map_ < *y.value_.map_;
412 }
413
414 default:
415 UNREACHABLE("Json::operator<(Value, Value) : invalid type");
416 }
417
418 return 0; // unreachable
419}
420
421bool
422operator==(const Value& x, const Value& y)
423{
424 if (x.type_ != y.type_)
425 {
426 if (x.type_ == intValue && y.type_ == uintValue)
427 return !integerCmp(x.value_.int_, y.value_.uint_);
428 if (x.type_ == uintValue && y.type_ == intValue)
429 return !integerCmp(y.value_.int_, x.value_.uint_);
430 return false;
431 }
432
433 switch (x.type_)
434 {
435 case nullValue:
436 return true;
437
438 case intValue:
439 return x.value_.int_ == y.value_.int_;
440
441 case uintValue:
442 return x.value_.uint_ == y.value_.uint_;
443
444 case realValue:
445 return x.value_.real_ == y.value_.real_;
446
447 case booleanValue:
448 return x.value_.bool_ == y.value_.bool_;
449
450 case stringValue:
451 return x.value_.string_ == y.value_.string_ ||
452 (y.value_.string_ && x.value_.string_ &&
453 !strcmp(x.value_.string_, y.value_.string_));
454
455 case arrayValue:
456 case objectValue:
457 return x.value_.map_->size() == y.value_.map_->size() &&
458 *x.value_.map_ == *y.value_.map_;
459
460 default:
461 UNREACHABLE("Json::operator==(Value, Value) : invalid type");
462 }
463
464 return 0; // unreachable
465}
466
467const char*
469{
470 XRPL_ASSERT(type_ == stringValue, "Json::Value::asCString : valid type");
471 return value_.string_;
472}
473
476{
477 switch (type_)
478 {
479 case nullValue:
480 return "";
481
482 case stringValue:
483 return value_.string_ ? value_.string_ : "";
484
485 case booleanValue:
486 return value_.bool_ ? "true" : "false";
487
488 case intValue:
489 return std::to_string(value_.int_);
490
491 case uintValue:
493
494 case realValue:
496
497 case arrayValue:
498 case objectValue:
499 JSON_ASSERT_MESSAGE(false, "Type is not convertible to string");
500
501 default:
502 UNREACHABLE("Json::Value::asString : invalid type");
503 }
504
505 return ""; // unreachable
506}
507
510{
511 switch (type_)
512 {
513 case nullValue:
514 return 0;
515
516 case intValue:
517 return value_.int_;
518
519 case uintValue:
520 JSON_ASSERT_MESSAGE(
521 value_.uint_ < (unsigned)maxInt,
522 "integer out of signed integer range");
523 return value_.uint_;
524
525 case realValue:
526 JSON_ASSERT_MESSAGE(
528 "Real out of signed integer range");
529 return Int(value_.real_);
530
531 case booleanValue:
532 return value_.bool_ ? 1 : 0;
533
534 case stringValue: {
535 char const* const str{value_.string_ ? value_.string_ : ""};
536 return beast::lexicalCastThrow<int>(str);
537 }
538
539 case arrayValue:
540 case objectValue:
541 JSON_ASSERT_MESSAGE(false, "Type is not convertible to int");
542
543 default:
544 UNREACHABLE("Json::Value::asInt : invalid type");
545 }
546
547 return 0; // unreachable;
548}
549
552{
553 switch (type_)
554 {
555 case nullValue:
556 return 0;
557
558 case intValue:
559 JSON_ASSERT_MESSAGE(
560 value_.int_ >= 0,
561 "Negative integer can not be converted to unsigned integer");
562 return value_.int_;
563
564 case uintValue:
565 return value_.uint_;
566
567 case realValue:
568 JSON_ASSERT_MESSAGE(
569 value_.real_ >= 0 && value_.real_ <= maxUInt,
570 "Real out of unsigned integer range");
571 return UInt(value_.real_);
572
573 case booleanValue:
574 return value_.bool_ ? 1 : 0;
575
576 case stringValue: {
577 char const* const str{value_.string_ ? value_.string_ : ""};
578 return beast::lexicalCastThrow<unsigned int>(str);
579 }
580
581 case arrayValue:
582 case objectValue:
583 JSON_ASSERT_MESSAGE(false, "Type is not convertible to uint");
584
585 default:
586 UNREACHABLE("Json::Value::asUInt : invalid type");
587 }
588
589 return 0; // unreachable;
590}
591
592double
594{
595 switch (type_)
596 {
597 case nullValue:
598 return 0.0;
599
600 case intValue:
601 return value_.int_;
602
603 case uintValue:
604 return value_.uint_;
605
606 case realValue:
607 return value_.real_;
608
609 case booleanValue:
610 return value_.bool_ ? 1.0 : 0.0;
611
612 case stringValue:
613 case arrayValue:
614 case objectValue:
615 JSON_ASSERT_MESSAGE(false, "Type is not convertible to double");
616
617 default:
618 UNREACHABLE("Json::Value::asDouble : invalid type");
619 }
620
621 return 0; // unreachable;
622}
623
624bool
626{
627 switch (type_)
628 {
629 case nullValue:
630 return false;
631
632 case intValue:
633 case uintValue:
634 return value_.int_ != 0;
635
636 case realValue:
637 return value_.real_ != 0.0;
638
639 case booleanValue:
640 return value_.bool_;
641
642 case stringValue:
643 return value_.string_ && value_.string_[0] != 0;
644
645 case arrayValue:
646 case objectValue:
647 return value_.map_->size() != 0;
648
649 default:
650 UNREACHABLE("Json::Value::asBool : invalid type");
651 }
652
653 return false; // unreachable;
654}
655
656bool
658{
659 switch (type_)
660 {
661 case nullValue:
662 return true;
663
664 case intValue:
665 return (other == nullValue && value_.int_ == 0) ||
666 other == intValue || (other == uintValue && value_.int_ >= 0) ||
667 other == realValue || other == stringValue ||
668 other == booleanValue;
669
670 case uintValue:
671 return (other == nullValue && value_.uint_ == 0) ||
672 (other == intValue && value_.uint_ <= (unsigned)maxInt) ||
673 other == uintValue || other == realValue ||
674 other == stringValue || other == booleanValue;
675
676 case realValue:
677 return (other == nullValue && value_.real_ == 0.0) ||
678 (other == intValue && value_.real_ >= minInt &&
679 value_.real_ <= maxInt) ||
680 (other == uintValue && value_.real_ >= 0 &&
681 value_.real_ <= maxUInt) ||
682 other == realValue || other == stringValue ||
683 other == booleanValue;
684
685 case booleanValue:
686 return (other == nullValue && value_.bool_ == false) ||
687 other == intValue || other == uintValue || other == realValue ||
688 other == stringValue || other == booleanValue;
689
690 case stringValue:
691 return other == stringValue ||
692 (other == nullValue &&
693 (!value_.string_ || value_.string_[0] == 0));
694
695 case arrayValue:
696 return other == arrayValue ||
697 (other == nullValue && value_.map_->size() == 0);
698
699 case objectValue:
700 return other == objectValue ||
701 (other == nullValue && value_.map_->size() == 0);
702
703 default:
704 UNREACHABLE("Json::Value::isConvertible : invalid type");
705 }
706
707 return false; // unreachable;
708}
709
713{
714 switch (type_)
715 {
716 case nullValue:
717 case intValue:
718 case uintValue:
719 case realValue:
720 case booleanValue:
721 case stringValue:
722 return 0;
723
724 case arrayValue: // size of the array is highest index + 1
725 if (!value_.map_->empty())
726 {
727 ObjectValues::const_iterator itLast = value_.map_->end();
728 --itLast;
729 return (*itLast).first.index() + 1;
730 }
731
732 return 0;
733
734 case objectValue:
735 return Int(value_.map_->size());
736
737 default:
738 UNREACHABLE("Json::Value::size : invalid type");
739 }
740
741 return 0; // unreachable;
742}
743
744Value::operator bool() const
745{
746 if (isNull())
747 return false;
748
749 if (isString())
750 {
751 auto s = asCString();
752 return s && s[0];
753 }
754
755 return !(isArray() || isObject()) || size();
756}
757
758void
760{
761 XRPL_ASSERT(
763 "Json::Value::clear : valid type");
764
765 switch (type_)
766 {
767 case arrayValue:
768 case objectValue:
769 value_.map_->clear();
770 break;
771
772 default:
773 break;
774 }
775}
776
777Value&
779{
780 XRPL_ASSERT(
782 "Json::Value::operator[](UInt) : valid type");
783
784 if (type_ == nullValue)
785 *this = Value(arrayValue);
786
787 CZString key(index);
788 ObjectValues::iterator it = value_.map_->lower_bound(key);
789
790 if (it != value_.map_->end() && (*it).first == key)
791 return (*it).second;
792
793 ObjectValues::value_type defaultValue(key, null);
794 it = value_.map_->insert(it, defaultValue);
795 return (*it).second;
796}
797
798const Value&
800{
801 XRPL_ASSERT(
803 "Json::Value::operator[](UInt) const : valid type");
804
805 if (type_ == nullValue)
806 return null;
807
808 CZString key(index);
809 ObjectValues::const_iterator it = value_.map_->find(key);
810
811 if (it == value_.map_->end())
812 return null;
813
814 return (*it).second;
815}
816
817Value&
818Value::operator[](const char* key)
819{
820 return resolveReference(key, false);
821}
822
823Value&
824Value::resolveReference(const char* key, bool isStatic)
825{
826 XRPL_ASSERT(
828 "Json::Value::resolveReference : valid type");
829
830 if (type_ == nullValue)
831 *this = Value(objectValue);
832
833 CZString actualKey(
835 ObjectValues::iterator it = value_.map_->lower_bound(actualKey);
836
837 if (it != value_.map_->end() && (*it).first == actualKey)
838 return (*it).second;
839
840 ObjectValues::value_type defaultValue(actualKey, null);
841 it = value_.map_->insert(it, defaultValue);
842 Value& value = (*it).second;
843 return value;
844}
845
846Value
847Value::get(UInt index, const Value& defaultValue) const
848{
849 const Value* value = &((*this)[index]);
850 return value == &null ? defaultValue : *value;
851}
852
853bool
855{
856 return index < size();
857}
858
859const Value&
860Value::operator[](const char* key) const
861{
862 XRPL_ASSERT(
864 "Json::Value::operator[](const char*) const : valid type");
865
866 if (type_ == nullValue)
867 return null;
868
869 CZString actualKey(key, CZString::noDuplication);
870 ObjectValues::const_iterator it = value_.map_->find(actualKey);
871
872 if (it == value_.map_->end())
873 return null;
874
875 return (*it).second;
876}
877
878Value&
880{
881 return (*this)[key.c_str()];
882}
883
884const Value&
886{
887 return (*this)[key.c_str()];
888}
889
890Value&
892{
893 return resolveReference(key, true);
894}
895
896Value&
897Value::append(const Value& value)
898{
899 return (*this)[size()] = value;
900}
901
902Value&
904{
905 return (*this)[size()] = std::move(value);
906}
907
908Value
909Value::get(const char* key, const Value& defaultValue) const
910{
911 const Value* value = &((*this)[key]);
912 return value == &null ? defaultValue : *value;
913}
914
915Value
916Value::get(std::string const& key, const Value& defaultValue) const
917{
918 return get(key.c_str(), defaultValue);
919}
920
921Value
922Value::removeMember(const char* key)
923{
924 XRPL_ASSERT(
926 "Json::Value::removeMember : valid type");
927
928 if (type_ == nullValue)
929 return null;
930
931 CZString actualKey(key, CZString::noDuplication);
932 ObjectValues::iterator it = value_.map_->find(actualKey);
933
934 if (it == value_.map_->end())
935 return null;
936
937 Value old(it->second);
938 value_.map_->erase(it);
939 return old;
940}
941
942Value
944{
945 return removeMember(key.c_str());
946}
947
948bool
949Value::isMember(const char* key) const
950{
951 if (type_ != objectValue)
952 return false;
953
954 const Value* value = &((*this)[key]);
955 return value != &null;
956}
957
958bool
960{
961 return isMember(key.c_str());
962}
963
966{
967 XRPL_ASSERT(
969 "Json::Value::getMemberNames : valid type");
970
971 if (type_ == nullValue)
972 return Value::Members();
973
974 Members members;
975 members.reserve(value_.map_->size());
976 ObjectValues::const_iterator it = value_.map_->begin();
977 ObjectValues::const_iterator itEnd = value_.map_->end();
978
979 for (; it != itEnd; ++it)
980 members.push_back(std::string((*it).first.c_str()));
981
982 return members;
983}
984
985bool
987{
988 return type_ == nullValue;
989}
990
991bool
993{
994 return type_ == booleanValue;
995}
996
997bool
999{
1000 return type_ == intValue;
1001}
1002
1003bool
1005{
1006 return type_ == uintValue;
1007}
1008
1009bool
1011{
1012 return type_ == intValue || type_ == uintValue || type_ == booleanValue;
1013}
1014
1015bool
1017{
1018 return type_ == realValue;
1019}
1020
1021bool
1023{
1024 return isIntegral() || isDouble();
1025}
1026
1027bool
1029{
1030 return type_ == stringValue;
1031}
1032
1033bool
1035{
1036 return type_ == arrayValue;
1037}
1038
1039bool
1041{
1042 return type_ == nullValue || type_ == arrayValue;
1043}
1044
1045bool
1047{
1048 return type_ == objectValue;
1049}
1050
1051bool
1053{
1054 return type_ == nullValue || type_ == objectValue;
1055}
1056
1059{
1060 StyledWriter writer;
1061 return writer.write(*this);
1062}
1063
1066{
1067 switch (type_)
1068 {
1069 case arrayValue:
1070 case objectValue:
1071 if (value_.map_)
1072 return const_iterator(value_.map_->begin());
1073
1074 break;
1075 default:
1076 break;
1077 }
1078
1079 return const_iterator();
1080}
1081
1084{
1085 switch (type_)
1086 {
1087 case arrayValue:
1088 case objectValue:
1089 if (value_.map_)
1090 return const_iterator(value_.map_->end());
1091
1092 break;
1093 default:
1094 break;
1095 }
1096
1097 return const_iterator();
1098}
1099
1102{
1103 switch (type_)
1104 {
1105 case arrayValue:
1106 case objectValue:
1107 if (value_.map_)
1108 return iterator(value_.map_->begin());
1109 break;
1110 default:
1111 break;
1112 }
1113
1114 return iterator();
1115}
1116
1119{
1120 switch (type_)
1121 {
1122 case arrayValue:
1123 case objectValue:
1124 if (value_.map_)
1125 return iterator(value_.map_->end());
1126 break;
1127 default:
1128 break;
1129 }
1130
1131 return iterator();
1132}
1133
1134} // namespace Json
T begin(T... args)
T c_str(T... args)
virtual ~DefaultValueAllocator()=default
void releaseMemberName(char *memberName) override
Definition: json_value.cpp:51
char * makeMemberName(const char *memberName) override
Definition: json_value.cpp:45
char * duplicateStringValue(const char *value, unsigned int length=unknown) override
Definition: json_value.cpp:57
void releaseStringValue(char *value) override
Definition: json_value.cpp:75
Lightweight wrapper to tag static string.
Definition: json_value.h:62
constexpr const char * c_str() const
Definition: json_value.h:75
Writes a Value in JSON format in a human friendly way.
Definition: json_writer.h:91
std::string write(const Value &root) override
Serialize a Value in JSON format.
Experimental do not use: Allocator to customize member name and string value memory management done b...
Definition: json_value.h:478
virtual void releaseStringValue(char *value)=0
virtual void releaseMemberName(char *memberName)=0
virtual char * duplicateStringValue(const char *value, unsigned int length=unknown)=0
const iterator for object and array value.
Definition: json_value.h:563
Iterator for object and array value.
Definition: json_value.h:624
bool isStaticString() const
Definition: json_value.cpp:169
bool operator<(const CZString &other) const
Definition: json_value.cpp:139
const char * c_str() const
Definition: json_value.cpp:163
const char * cstr_
Definition: json_value.h:191
bool operator==(const CZString &other) const
Definition: json_value.cpp:148
Represents a JSON value.
Definition: json_value.h:148
const_iterator begin() const
Json::UInt UInt
Definition: json_value.h:155
Value & resolveReference(const char *key, bool isStatic)
Definition: json_value.cpp:824
bool isArray() const
const char * asCString() const
Definition: json_value.cpp:468
Value get(UInt index, const Value &defaultValue) const
If the array contains at least index+1 elements, returns the element value, otherwise returns default...
Definition: json_value.cpp:847
UInt size() const
Number of values in array or object.
Definition: json_value.cpp:712
std::string toStyledString() const
const_iterator end() const
bool isObjectOrNull() const
bool isDouble() const
void clear()
Remove all object members and array elements.
Definition: json_value.cpp:759
static const Value null
Definition: json_value.h:159
Int asInt() const
Definition: json_value.cpp:509
union Json::Value::ValueHolder value_
ValueIterator iterator
Definition: json_value.h:153
ValueConstIterator const_iterator
Definition: json_value.h:154
bool isString() const
UInt asUInt() const
Definition: json_value.cpp:551
Members getMemberNames() const
Return a list of the member names.
Definition: json_value.cpp:965
static const Int minInt
Definition: json_value.h:160
Value & append(const Value &value)
Append value to array at the end.
Definition: json_value.cpp:897
std::vector< std::string > Members
Definition: json_value.h:152
ValueType type() const
Definition: json_value.cpp:356
bool isObject() const
Value & operator=(Value const &other)
Definition: json_value.cpp:319
static const Int maxInt
Definition: json_value.h:161
void swap(Value &other) noexcept
Swap values.
Definition: json_value.cpp:342
Value removeMember(const char *key)
Remove and return the named member.
Definition: json_value.cpp:922
Json::Int Int
Definition: json_value.h:156
bool isValidIndex(UInt index) const
Return true if index < size().
Definition: json_value.cpp:854
std::string asString() const
Returns the unquoted string value.
Definition: json_value.cpp:475
bool isBool() const
Definition: json_value.cpp:992
bool isIntegral() const
bool asBool() const
Definition: json_value.cpp:625
ValueType type_
Definition: json_value.h:435
bool isUInt() const
bool isNull() const
isNull() tests to see if this field is null.
Definition: json_value.cpp:986
bool isArrayOrNull() const
static const UInt maxUInt
Definition: json_value.h:162
bool isMember(const char *key) const
Return true if the object has a member named key.
Definition: json_value.cpp:949
Value(ValueType type=nullValue)
Create a default Value of the given type.
Definition: json_value.cpp:186
std::map< CZString, Value > ObjectValues
Definition: json_value.h:196
bool isConvertibleTo(ValueType other) const
Definition: json_value.cpp:657
int allocated_
Definition: json_value.h:436
bool isNumeric() const
double asDouble() const
Definition: json_value.cpp:593
Value & operator[](UInt index)
Access an array element (zero based index ).
Definition: json_value.cpp:778
bool isInt() const
Definition: json_value.cpp:998
T clear(T... args)
T empty(T... args)
T end(T... args)
T erase(T... args)
T find(T... args)
T insert(T... args)
T lower_bound(T... args)
JSON (JavaScript Object Notation).
Definition: json_errors.h:25
bool operator<(const Value &, const Value &)
Definition: json_value.cpp:373
static struct Json::DummyValueAllocatorInitializer dummyValueAllocatorInitializer
ValueType
Type of the value held by a Value object.
Definition: json_value.h:36
@ booleanValue
bool value
Definition: json_value.h:42
@ nullValue
'null' value
Definition: json_value.h:37
@ stringValue
UTF-8 string value.
Definition: json_value.h:41
@ realValue
double value
Definition: json_value.h:40
@ arrayValue
array value (ordered list)
Definition: json_value.h:43
@ intValue
signed integer value
Definition: json_value.h:38
@ objectValue
object value (collection of name/value pairs).
Definition: json_value.h:44
@ uintValue
unsigned integer value
Definition: json_value.h:39
bool operator==(StaticString x, StaticString y)
Definition: json_value.h:85
int Int
Definition: json_forwards.h:26
unsigned int UInt
Definition: json_forwards.h:27
static ValueAllocator *& valueAllocator()
Definition: json_value.cpp:83
static int integerCmp(Int i, UInt ui)
Definition: json_value.cpp:362
T push_back(T... args)
T reserve(T... args)
T length(T... args)
T swap(T... args)
T to_string(T... args)
ObjectValues * map_
Definition: json_value.h:433