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