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