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
34Value const Value::null;
35Int const Value::minInt = Int(~(UInt(-1) / 2));
36Int const Value::maxInt = Int(UInt(-1) / 2);
37UInt const Value::maxUInt = UInt(-1);
38
40{
41public:
42 virtual ~DefaultValueAllocator() = default;
43
44 char*
45 makeMemberName(char const* 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(char const* 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*&
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<(CZString const& 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
162char const*
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(char const* value) : type_(stringValue), allocated_(true)
236{
238}
239
240Value::Value(ripple::Number const& value) : type_(stringValue), allocated_(true)
241{
242 auto const tmp = to_string(value);
244 valueAllocator()->duplicateStringValue(tmp.c_str(), tmp.length());
245}
246
247Value::Value(std::string const& value) : type_(stringValue), allocated_(true)
248{
250 value.c_str(), (unsigned int)value.length());
251}
252
253Value::Value(StaticString const& value) : type_(stringValue), allocated_(false)
254{
255 value_.string_ = const_cast<char*>(value.c_str());
256}
257
258Value::Value(bool value) : type_(booleanValue)
259{
260 value_.bool_ = value;
261}
262
263Value::Value(Value const& other) : type_(other.type_)
264{
265 switch (type_)
266 {
267 case nullValue:
268 case intValue:
269 case uintValue:
270 case realValue:
271 case booleanValue:
272 value_ = other.value_;
273 break;
274
275 case stringValue:
276 if (other.value_.string_)
277 {
279 other.value_.string_);
280 allocated_ = true;
281 }
282 else
283 value_.string_ = 0;
284
285 break;
286
287 case arrayValue:
288 case objectValue:
289 value_.map_ = new ObjectValues(*other.value_.map_);
290 break;
291
292 default:
293 UNREACHABLE("Json::Value::Value(Value const&) : invalid type");
294 }
295}
296
298{
299 switch (type_)
300 {
301 case nullValue:
302 case intValue:
303 case uintValue:
304 case realValue:
305 case booleanValue:
306 break;
307
308 case stringValue:
309 if (allocated_)
311
312 break;
313
314 case arrayValue:
315 case objectValue:
316 if (value_.map_)
317 delete value_.map_;
318 break;
319
320 default:
321 UNREACHABLE("Json::Value::~Value : invalid type");
322 }
323}
324
325Value&
327{
328 Value tmp(other);
329 swap(tmp);
330 return *this;
331}
332
333Value::Value(Value&& other) noexcept
334 : value_(other.value_), type_(other.type_), allocated_(other.allocated_)
335{
336 other.type_ = nullValue;
337 other.allocated_ = 0;
338}
339
340Value&
342{
343 Value tmp(std::move(other));
344 swap(tmp);
345 return *this;
346}
347
348void
349Value::swap(Value& other) noexcept
350{
351 std::swap(value_, other.value_);
352
353 ValueType temp = type_;
354 type_ = other.type_;
355 other.type_ = temp;
356
357 int temp2 = allocated_;
358 allocated_ = other.allocated_;
359 other.allocated_ = temp2;
360}
361
364{
365 return type_;
366}
367
368static int
370{
371 // All negative numbers are less than all unsigned numbers.
372 if (i < 0)
373 return -1;
374
375 // Now we can safely compare.
376 return (i < ui) ? -1 : (i == ui) ? 0 : 1;
377}
378
379bool
380operator<(Value const& x, Value const& y)
381{
382 if (auto signum = x.type_ - y.type_)
383 {
384 if (x.type_ == intValue && y.type_ == uintValue)
385 signum = integerCmp(x.value_.int_, y.value_.uint_);
386 else if (x.type_ == uintValue && y.type_ == intValue)
387 signum = -integerCmp(y.value_.int_, x.value_.uint_);
388 return signum < 0;
389 }
390
391 switch (x.type_)
392 {
393 case nullValue:
394 return false;
395
396 case intValue:
397 return x.value_.int_ < y.value_.int_;
398
399 case uintValue:
400 return x.value_.uint_ < y.value_.uint_;
401
402 case realValue:
403 return x.value_.real_ < y.value_.real_;
404
405 case booleanValue:
406 return x.value_.bool_ < y.value_.bool_;
407
408 case stringValue:
409 return (x.value_.string_ == 0 && y.value_.string_) ||
410 (y.value_.string_ && x.value_.string_ &&
411 strcmp(x.value_.string_, y.value_.string_) < 0);
412
413 case arrayValue:
414 case objectValue: {
415 if (int signum = int(x.value_.map_->size()) - y.value_.map_->size())
416 return signum < 0;
417
418 return *x.value_.map_ < *y.value_.map_;
419 }
420
421 default:
422 UNREACHABLE("Json::operator<(Value, Value) : invalid type");
423 }
424
425 return 0; // unreachable
426}
427
428bool
429operator==(Value const& x, Value const& y)
430{
431 if (x.type_ != y.type_)
432 {
433 if (x.type_ == intValue && y.type_ == uintValue)
434 return !integerCmp(x.value_.int_, y.value_.uint_);
435 if (x.type_ == uintValue && y.type_ == intValue)
436 return !integerCmp(y.value_.int_, x.value_.uint_);
437 return false;
438 }
439
440 switch (x.type_)
441 {
442 case nullValue:
443 return true;
444
445 case intValue:
446 return x.value_.int_ == y.value_.int_;
447
448 case uintValue:
449 return x.value_.uint_ == y.value_.uint_;
450
451 case realValue:
452 return x.value_.real_ == y.value_.real_;
453
454 case booleanValue:
455 return x.value_.bool_ == y.value_.bool_;
456
457 case stringValue:
458 return x.value_.string_ == y.value_.string_ ||
459 (y.value_.string_ && x.value_.string_ &&
460 !strcmp(x.value_.string_, y.value_.string_));
461
462 case arrayValue:
463 case objectValue:
464 return x.value_.map_->size() == y.value_.map_->size() &&
465 *x.value_.map_ == *y.value_.map_;
466
467 default:
468 UNREACHABLE("Json::operator==(Value, Value) : invalid type");
469 }
470
471 return 0; // unreachable
472}
473
474char const*
476{
477 XRPL_ASSERT(type_ == stringValue, "Json::Value::asCString : valid type");
478 return value_.string_;
479}
480
483{
484 switch (type_)
485 {
486 case nullValue:
487 return "";
488
489 case stringValue:
490 return value_.string_ ? value_.string_ : "";
491
492 case booleanValue:
493 return value_.bool_ ? "true" : "false";
494
495 case intValue:
496 return std::to_string(value_.int_);
497
498 case uintValue:
500
501 case realValue:
503
504 case arrayValue:
505 case objectValue:
506 JSON_ASSERT_MESSAGE(false, "Type is not convertible to string");
507
508 default:
509 UNREACHABLE("Json::Value::asString : invalid type");
510 }
511
512 return ""; // unreachable
513}
514
517{
518 switch (type_)
519 {
520 case nullValue:
521 return 0;
522
523 case intValue:
524 return value_.int_;
525
526 case uintValue:
527 JSON_ASSERT_MESSAGE(
528 value_.uint_ < (unsigned)maxInt,
529 "integer out of signed integer range");
530 return value_.uint_;
531
532 case realValue:
533 JSON_ASSERT_MESSAGE(
535 "Real out of signed integer range");
536 return Int(value_.real_);
537
538 case booleanValue:
539 return value_.bool_ ? 1 : 0;
540
541 case stringValue: {
542 char const* const str{value_.string_ ? value_.string_ : ""};
543 return beast::lexicalCastThrow<int>(str);
544 }
545
546 case arrayValue:
547 case objectValue:
548 JSON_ASSERT_MESSAGE(false, "Type is not convertible to int");
549
550 default:
551 UNREACHABLE("Json::Value::asInt : invalid type");
552 }
553
554 return 0; // unreachable;
555}
556
559{
560 switch (type_)
561 {
562 case nullValue:
563 return 0;
564
565 case intValue:
566 JSON_ASSERT_MESSAGE(
567 value_.int_ >= 0,
568 "Negative integer can not be converted to unsigned integer");
569 return value_.int_;
570
571 case uintValue:
572 return value_.uint_;
573
574 case realValue:
575 JSON_ASSERT_MESSAGE(
576 value_.real_ >= 0 && value_.real_ <= maxUInt,
577 "Real out of unsigned integer range");
578 return UInt(value_.real_);
579
580 case booleanValue:
581 return value_.bool_ ? 1 : 0;
582
583 case stringValue: {
584 char const* const str{value_.string_ ? value_.string_ : ""};
585 return beast::lexicalCastThrow<unsigned int>(str);
586 }
587
588 case arrayValue:
589 case objectValue:
590 JSON_ASSERT_MESSAGE(false, "Type is not convertible to uint");
591
592 default:
593 UNREACHABLE("Json::Value::asUInt : invalid type");
594 }
595
596 return 0; // unreachable;
597}
598
599double
601{
602 switch (type_)
603 {
604 case nullValue:
605 return 0.0;
606
607 case intValue:
608 return value_.int_;
609
610 case uintValue:
611 return value_.uint_;
612
613 case realValue:
614 return value_.real_;
615
616 case booleanValue:
617 return value_.bool_ ? 1.0 : 0.0;
618
619 case stringValue:
620 case arrayValue:
621 case objectValue:
622 JSON_ASSERT_MESSAGE(false, "Type is not convertible to double");
623
624 default:
625 UNREACHABLE("Json::Value::asDouble : invalid type");
626 }
627
628 return 0; // unreachable;
629}
630
631bool
633{
634 switch (type_)
635 {
636 case nullValue:
637 return false;
638
639 case intValue:
640 case uintValue:
641 return value_.int_ != 0;
642
643 case realValue:
644 return value_.real_ != 0.0;
645
646 case booleanValue:
647 return value_.bool_;
648
649 case stringValue:
650 return value_.string_ && value_.string_[0] != 0;
651
652 case arrayValue:
653 case objectValue:
654 return value_.map_->size() != 0;
655
656 default:
657 UNREACHABLE("Json::Value::asBool : invalid type");
658 }
659
660 return false; // unreachable;
661}
662
663bool
665{
666 switch (type_)
667 {
668 case nullValue:
669 return true;
670
671 case intValue:
672 return (other == nullValue && value_.int_ == 0) ||
673 other == intValue || (other == uintValue && value_.int_ >= 0) ||
674 other == realValue || other == stringValue ||
675 other == booleanValue;
676
677 case uintValue:
678 return (other == nullValue && value_.uint_ == 0) ||
679 (other == intValue && value_.uint_ <= (unsigned)maxInt) ||
680 other == uintValue || other == realValue ||
681 other == stringValue || other == booleanValue;
682
683 case realValue:
684 return (other == nullValue && value_.real_ == 0.0) ||
685 (other == intValue && value_.real_ >= minInt &&
686 value_.real_ <= maxInt) ||
687 (other == uintValue && value_.real_ >= 0 &&
688 value_.real_ <= maxUInt) ||
689 other == realValue || other == stringValue ||
690 other == booleanValue;
691
692 case booleanValue:
693 return (other == nullValue && value_.bool_ == false) ||
694 other == intValue || other == uintValue || other == realValue ||
695 other == stringValue || other == booleanValue;
696
697 case stringValue:
698 return other == stringValue ||
699 (other == nullValue &&
700 (!value_.string_ || value_.string_[0] == 0));
701
702 case arrayValue:
703 return other == arrayValue ||
704 (other == nullValue && value_.map_->size() == 0);
705
706 case objectValue:
707 return other == objectValue ||
708 (other == nullValue && value_.map_->size() == 0);
709
710 default:
711 UNREACHABLE("Json::Value::isConvertible : invalid type");
712 }
713
714 return false; // unreachable;
715}
716
720{
721 switch (type_)
722 {
723 case nullValue:
724 case intValue:
725 case uintValue:
726 case realValue:
727 case booleanValue:
728 case stringValue:
729 return 0;
730
731 case arrayValue: // size of the array is highest index + 1
732 if (!value_.map_->empty())
733 {
734 ObjectValues::const_iterator itLast = value_.map_->end();
735 --itLast;
736 return (*itLast).first.index() + 1;
737 }
738
739 return 0;
740
741 case objectValue:
742 return Int(value_.map_->size());
743
744 default:
745 UNREACHABLE("Json::Value::size : invalid type");
746 }
747
748 return 0; // unreachable;
749}
750
751Value::operator bool() const
752{
753 if (isNull())
754 return false;
755
756 if (isString())
757 {
758 auto s = asCString();
759 return s && s[0];
760 }
761
762 return !(isArray() || isObject()) || size();
763}
764
765void
767{
768 XRPL_ASSERT(
770 "Json::Value::clear : valid type");
771
772 switch (type_)
773 {
774 case arrayValue:
775 case objectValue:
776 value_.map_->clear();
777 break;
778
779 default:
780 break;
781 }
782}
783
784Value&
786{
787 XRPL_ASSERT(
789 "Json::Value::operator[](UInt) : valid type");
790
791 if (type_ == nullValue)
792 *this = Value(arrayValue);
793
794 CZString key(index);
795 ObjectValues::iterator it = value_.map_->lower_bound(key);
796
797 if (it != value_.map_->end() && (*it).first == key)
798 return (*it).second;
799
800 ObjectValues::value_type defaultValue(key, null);
801 it = value_.map_->insert(it, defaultValue);
802 return (*it).second;
803}
804
805Value const&
807{
808 XRPL_ASSERT(
810 "Json::Value::operator[](UInt) const : valid type");
811
812 if (type_ == nullValue)
813 return null;
814
815 CZString key(index);
816 ObjectValues::const_iterator it = value_.map_->find(key);
817
818 if (it == value_.map_->end())
819 return null;
820
821 return (*it).second;
822}
823
824Value&
825Value::operator[](char const* key)
826{
827 return resolveReference(key, false);
828}
829
830Value&
831Value::resolveReference(char const* key, bool isStatic)
832{
833 XRPL_ASSERT(
835 "Json::Value::resolveReference : valid type");
836
837 if (type_ == nullValue)
838 *this = Value(objectValue);
839
840 CZString actualKey(
842 ObjectValues::iterator it = value_.map_->lower_bound(actualKey);
843
844 if (it != value_.map_->end() && (*it).first == actualKey)
845 return (*it).second;
846
847 ObjectValues::value_type defaultValue(actualKey, null);
848 it = value_.map_->insert(it, defaultValue);
849 Value& value = (*it).second;
850 return value;
851}
852
853Value
854Value::get(UInt index, Value const& defaultValue) const
855{
856 Value const* value = &((*this)[index]);
857 return value == &null ? defaultValue : *value;
858}
859
860bool
862{
863 return index < size();
864}
865
866Value const&
867Value::operator[](char const* key) const
868{
869 XRPL_ASSERT(
871 "Json::Value::operator[](const char*) const : valid type");
872
873 if (type_ == nullValue)
874 return null;
875
876 CZString actualKey(key, CZString::noDuplication);
877 ObjectValues::const_iterator it = value_.map_->find(actualKey);
878
879 if (it == value_.map_->end())
880 return null;
881
882 return (*it).second;
883}
884
885Value&
887{
888 return (*this)[key.c_str()];
889}
890
891Value const&
893{
894 return (*this)[key.c_str()];
895}
896
897Value&
899{
900 return resolveReference(key, true);
901}
902
903Value const&
905{
906 return (*this)[key.c_str()];
907}
908
909Value&
910Value::append(Value const& value)
911{
912 return (*this)[size()] = value;
913}
914
915Value&
917{
918 return (*this)[size()] = std::move(value);
919}
920
921Value
922Value::get(char const* key, Value const& defaultValue) const
923{
924 Value const* value = &((*this)[key]);
925 return value == &null ? defaultValue : *value;
926}
927
928Value
929Value::get(std::string const& key, Value const& defaultValue) const
930{
931 return get(key.c_str(), defaultValue);
932}
933
934Value
935Value::removeMember(char const* key)
936{
937 XRPL_ASSERT(
939 "Json::Value::removeMember : valid type");
940
941 if (type_ == nullValue)
942 return null;
943
944 CZString actualKey(key, CZString::noDuplication);
945 ObjectValues::iterator it = value_.map_->find(actualKey);
946
947 if (it == value_.map_->end())
948 return null;
949
950 Value old(it->second);
951 value_.map_->erase(it);
952 return old;
953}
954
955Value
957{
958 return removeMember(key.c_str());
959}
960
961bool
962Value::isMember(char const* key) const
963{
964 if (type_ != objectValue)
965 return false;
966
967 Value const* value = &((*this)[key]);
968 return value != &null;
969}
970
971bool
973{
974 return isMember(key.c_str());
975}
976
979{
980 XRPL_ASSERT(
982 "Json::Value::getMemberNames : valid type");
983
984 if (type_ == nullValue)
985 return Value::Members();
986
987 Members members;
988 members.reserve(value_.map_->size());
989 ObjectValues::const_iterator it = value_.map_->begin();
990 ObjectValues::const_iterator itEnd = value_.map_->end();
991
992 for (; it != itEnd; ++it)
993 members.push_back(std::string((*it).first.c_str()));
994
995 return members;
996}
997
998bool
1000{
1001 return type_ == nullValue;
1002}
1003
1004bool
1006{
1007 return type_ == booleanValue;
1008}
1009
1010bool
1012{
1013 return type_ == intValue;
1014}
1015
1016bool
1018{
1019 return type_ == uintValue;
1020}
1021
1022bool
1024{
1025 return type_ == intValue || type_ == uintValue || type_ == booleanValue;
1026}
1027
1028bool
1030{
1031 return type_ == realValue;
1032}
1033
1034bool
1036{
1037 return isIntegral() || isDouble();
1038}
1039
1040bool
1042{
1043 return type_ == stringValue;
1044}
1045
1046bool
1048{
1049 return type_ == arrayValue;
1050}
1051
1052bool
1054{
1055 return type_ == nullValue || type_ == arrayValue;
1056}
1057
1058bool
1060{
1061 return type_ == objectValue;
1062}
1063
1064bool
1066{
1067 return type_ == nullValue || type_ == objectValue;
1068}
1069
1072{
1073 StyledWriter writer;
1074 return writer.write(*this);
1075}
1076
1079{
1080 switch (type_)
1081 {
1082 case arrayValue:
1083 case objectValue:
1084 if (value_.map_)
1085 return const_iterator(value_.map_->begin());
1086
1087 break;
1088 default:
1089 break;
1090 }
1091
1092 return const_iterator();
1093}
1094
1097{
1098 switch (type_)
1099 {
1100 case arrayValue:
1101 case objectValue:
1102 if (value_.map_)
1103 return const_iterator(value_.map_->end());
1104
1105 break;
1106 default:
1107 break;
1108 }
1109
1110 return const_iterator();
1111}
1112
1115{
1116 switch (type_)
1117 {
1118 case arrayValue:
1119 case objectValue:
1120 if (value_.map_)
1121 return iterator(value_.map_->begin());
1122 break;
1123 default:
1124 break;
1125 }
1126
1127 return iterator();
1128}
1129
1132{
1133 switch (type_)
1134 {
1135 case arrayValue:
1136 case objectValue:
1137 if (value_.map_)
1138 return iterator(value_.map_->end());
1139 break;
1140 default:
1141 break;
1142 }
1143
1144 return iterator();
1145}
1146
1147} // 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 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)