xrpld
Loading...
Searching...
No Matches
json_value.cpp
1#include <xrpl/json/json_value.h>
2
3#include <xrpl/basics/Number.h>
4#include <xrpl/basics/contract.h> // IWYU pragma: keep
5#include <xrpl/beast/core/LexicalCast.h>
6#include <xrpl/beast/utility/instrumentation.h>
7#include <xrpl/json/detail/json_assert.h>
8#include <xrpl/json/json_errors.h> // IWYU pragma: keep
9#include <xrpl/json/json_forwards.h>
10#include <xrpl/json/json_writer.h>
11
12#include <cmath>
13#include <cstdint>
14#include <cstdlib>
15#include <cstring>
16#include <limits>
17#include <string>
18#include <string_view>
19#include <utility>
20
21namespace json {
22
24
26{
27public:
28 ~DefaultValueAllocator() override = default;
29
30 char*
31 makeMemberName(char const* memberName) override
32 {
33 return duplicateStringValue(memberName);
34 }
35
36 void
37 releaseMemberName(char* memberName) override
38 {
39 releaseStringValue(memberName);
40 }
41
42 char*
43 duplicateStringValue(char const* value, unsigned int length = kUnknown) override
44 {
45 //@todo investigate this old optimization
46 // if ( !value || value[0] == 0 )
47 // return 0;
48
49 if (length == kUnknown)
50 length = (value != nullptr) ? (unsigned int)strlen(value) : 0;
51
52 // NOLINTNEXTLINE(cppcoreguidelines-no-malloc)
53 char* newString = static_cast<char*>(malloc(length + 1));
54 if (value != nullptr)
55 memcpy(newString, value, length);
56 newString[length] = 0;
57 return newString;
58 }
59
60 void
61 releaseStringValue(char* value) override
62 {
63 if (value != nullptr)
64 {
65 // NOLINTNEXTLINE(cppcoreguidelines-no-malloc)
66 free(value);
67 }
68 }
69};
70
71static ValueAllocator*&
73{
74 static ValueAllocator* kValueAllocator = new DefaultValueAllocator; // NOLINT TODO
75 return kValueAllocator;
76}
77
79{
81 {
82 valueAllocator(); // ensure valueAllocator() statics are initialized
83 // before main().
84 }
86
87// //////////////////////////////////////////////////////////////////
88// //////////////////////////////////////////////////////////////////
89// //////////////////////////////////////////////////////////////////
90// class Value::CZString
91// //////////////////////////////////////////////////////////////////
92// //////////////////////////////////////////////////////////////////
93// //////////////////////////////////////////////////////////////////
94
95// Notes: index_ indicates if the string was allocated when
96// a string is stored.
97
101
103 : cstr_(
104 allocate == DuplicationPolicy::Duplicate ? valueAllocator()->makeMemberName(cstr) : cstr)
105 , index_(static_cast<int>(allocate))
106{
107}
108
110 : cstr_(
111 other.index_ != static_cast<int>(DuplicationPolicy::NoDuplication) &&
112 other.cstr_ != nullptr
113 ? valueAllocator()->makeMemberName(other.cstr_)
114 : other.cstr_)
115 , index_([&] -> int {
116 if (!other.cstr_)
117 return other.index_;
118 return other.index_ == static_cast<int>(DuplicationPolicy::NoDuplication)
119 ? static_cast<int>(DuplicationPolicy::NoDuplication)
120 : static_cast<int>(DuplicationPolicy::Duplicate);
121 }())
122{
123}
124
126{
127 if ((cstr_ != nullptr) && index_ == static_cast<int>(DuplicationPolicy::Duplicate))
128 {
129 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-const-cast)
130 valueAllocator()->releaseMemberName(const_cast<char*>(cstr_));
131 }
132}
133
134bool
135Value::CZString::operator<(CZString const& other) const
136{
137 if ((cstr_ != nullptr) && (other.cstr_ != nullptr))
138 return strcmp(cstr_, other.cstr_) < 0;
139
140 return index_ < other.index_;
141}
142
143bool
145{
146 if ((cstr_ != nullptr) && (other.cstr_ != nullptr))
147 return strcmp(cstr_, other.cstr_) == 0;
148
149 return index_ == other.index_;
150}
151
152int
154{
155 return index_;
156}
157
158char const*
160{
161 return cstr_;
162}
163
164bool
166{
167 return index_ == static_cast<int>(DuplicationPolicy::NoDuplication);
168}
169
170// //////////////////////////////////////////////////////////////////
171// //////////////////////////////////////////////////////////////////
172// //////////////////////////////////////////////////////////////////
173// class Value::Value
174// //////////////////////////////////////////////////////////////////
175// //////////////////////////////////////////////////////////////////
176// //////////////////////////////////////////////////////////////////
177
184{
185 switch (type)
186 {
187 case ValueType::Null:
188 break;
189
190 case ValueType::Int:
191 value_.intVal = 0;
192 break;
193
194 case ValueType::UInt:
195 value_.uintVal = 0;
196 break;
197
198 case ValueType::Real:
199 value_.realVal = 0.0;
200 break;
201
203 value_.stringVal = nullptr;
204 break;
205
206 case ValueType::Array:
208 value_.mapVal = new ObjectValues();
209 break;
210
212 value_.boolVal = false;
213 break;
214
215 // LCOV_EXCL_START
216 default:
217 UNREACHABLE("json::Value::Value(ValueType) : invalid type");
218 // LCOV_EXCL_STOP
219 }
220}
221
223{
224 value_.intVal = value;
225}
226
228{
229 value_.uintVal = value;
230}
231
233{
234 value_.realVal = value;
235}
236
237Value::Value(char const* value) : type_(ValueType::String), allocated_(true)
238{
239 value_.stringVal = valueAllocator()->duplicateStringValue(value);
240}
241
243{
244 auto const tmp = to_string(value);
245 value_.stringVal = valueAllocator()->duplicateStringValue(tmp.c_str(), tmp.length());
246}
247
248Value::Value(std::string const& value) : Value(std::string_view{value})
249{
250}
251
253{
255 value.data(), static_cast<unsigned int>(value.length()));
256}
257
259{
260 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-const-cast)
261 value_.stringVal = const_cast<char*>(value.cStr());
262}
263
265{
266 value_.boolVal = value;
267}
268
269Value::Value(Value const& other) : type_(other.type_)
270{
271 switch (type_)
272 {
273 case ValueType::Null:
274 case ValueType::Int:
275 case ValueType::UInt:
276 case ValueType::Real:
278 value_ = other.value_;
279 break;
280
282 if (other.value_.stringVal != nullptr)
283 {
285 allocated_ = true;
286 }
287 else
288 {
289 value_.stringVal = nullptr;
290 }
291
292 break;
293
294 case ValueType::Array:
296 value_.mapVal = new ObjectValues(*other.value_.mapVal);
297 break;
298
299 // LCOV_EXCL_START
300 default:
301 UNREACHABLE("json::Value::Value(Value const&) : invalid type");
302 // LCOV_EXCL_STOP
303 }
304}
305
307{
308 switch (type_)
309 {
310 case ValueType::Null:
311 case ValueType::Int:
312 case ValueType::UInt:
313 case ValueType::Real:
315 break;
316
318 if (allocated_)
320
321 break;
322
323 case ValueType::Array:
325 delete value_.mapVal;
326 break;
327
328 // LCOV_EXCL_START
329 default:
330 UNREACHABLE("json::Value::~Value : invalid type");
331 // LCOV_EXCL_STOP
332 }
333}
334
335Value&
337{
338 Value tmp(other);
339 swap(tmp);
340 return *this;
341}
342
343Value::Value(Value&& other) noexcept
344 : value_(other.value_), type_(other.type_), allocated_(other.allocated_)
345{
346 other.type_ = ValueType::Null;
347 other.allocated_ = 0;
348}
349
350Value&
352{
353 Value tmp(std::move(other));
354 swap(tmp);
355 return *this;
356}
357
358void
359Value::swap(Value& other) noexcept
360{
361 std::swap(value_, other.value_);
362
363 ValueType const temp = type_;
364 type_ = other.type_;
365 other.type_ = temp;
366
367 int const temp2 = allocated_;
368 allocated_ = other.allocated_;
369 other.allocated_ = temp2;
370}
371
374{
375 return type_;
376}
377
378static int
380{
381 // All negative numbers are less than all unsigned numbers.
382 if (i < 0)
383 return -1;
384
385 // Now we can safely compare.
386 if (i < ui)
387 return -1;
388 return (i == ui) ? 0 : 1;
389}
390
391bool
392operator<(Value const& x, Value const& y)
393{
394 if (auto signum = static_cast<int>(x.type_) - static_cast<int>(y.type_))
395 {
396 if (x.type_ == ValueType::Int && y.type_ == ValueType::UInt)
397 {
398 signum = integerCmp(x.value_.intVal, y.value_.uintVal);
399 }
400 else if (x.type_ == ValueType::UInt && y.type_ == ValueType::Int)
401 {
402 signum = -integerCmp(y.value_.intVal, x.value_.uintVal);
403 }
404 return signum < 0;
405 }
406
407 switch (x.type_)
408 {
409 case ValueType::Null:
410 return false;
411
412 case ValueType::Int:
413 return x.value_.intVal < y.value_.intVal;
414
415 case ValueType::UInt:
416 return x.value_.uintVal < y.value_.uintVal;
417
418 case ValueType::Real:
419 return x.value_.realVal < y.value_.realVal;
420
422 return static_cast<int>(x.value_.boolVal) < static_cast<int>(y.value_.boolVal);
423
425 return (x.value_.stringVal == nullptr && (y.value_.stringVal != nullptr)) ||
426 ((y.value_.stringVal != nullptr) && (x.value_.stringVal != nullptr) &&
427 strcmp(x.value_.stringVal, y.value_.stringVal) < 0);
428
429 case ValueType::Array:
430 case ValueType::Object: {
431 if (int const signum = int(x.value_.mapVal->size()) - y.value_.mapVal->size())
432 return signum < 0;
433
434 return *x.value_.mapVal < *y.value_.mapVal;
435 }
436
437 // LCOV_EXCL_START
438 default:
439 UNREACHABLE("json::operator<(Value, Value) : invalid type");
440 // LCOV_EXCL_STOP
441 }
442
443 return false; // unreachable
444}
445
446bool
447operator==(Value const& x, Value const& y)
448{
449 if (x.type_ != y.type_)
450 {
451 if (x.type_ == ValueType::Int && y.type_ == ValueType::UInt)
452 return integerCmp(x.value_.intVal, y.value_.uintVal) == 0;
453 if (x.type_ == ValueType::UInt && y.type_ == ValueType::Int)
454 return integerCmp(y.value_.intVal, x.value_.uintVal) == 0;
455 return false;
456 }
457
458 switch (x.type_)
459 {
460 case ValueType::Null:
461 return true;
462
463 case ValueType::Int:
464 return x.value_.intVal == y.value_.intVal;
465
466 case ValueType::UInt:
467 return x.value_.uintVal == y.value_.uintVal;
468
469 case ValueType::Real:
470 return x.value_.realVal == y.value_.realVal;
471
473 return x.value_.boolVal == y.value_.boolVal;
474
476 return x.value_.stringVal == y.value_.stringVal ||
477 ((y.value_.stringVal != nullptr) && (x.value_.stringVal != nullptr) &&
478 (strcmp(x.value_.stringVal, y.value_.stringVal) == 0));
479
480 case ValueType::Array:
482 return x.value_.mapVal->size() == y.value_.mapVal->size() &&
483 *x.value_.mapVal == *y.value_.mapVal;
484
485 // LCOV_EXCL_START
486 default:
487 UNREACHABLE("json::operator==(Value, Value) : invalid type");
488 // LCOV_EXCL_STOP
489 }
490
491 return false; // unreachable
492}
493
494char const*
496{
497 XRPL_ASSERT(type_ == ValueType::String, "json::Value::asCString : valid type");
498 return value_.stringVal;
499}
500
503{
504 switch (type_)
505 {
506 case ValueType::Null:
507 return "";
508
510 return (value_.stringVal != nullptr) ? value_.stringVal : "";
511
513 return value_.boolVal ? "true" : "false";
514
515 case ValueType::Int:
516 return std::to_string(value_.intVal);
517
518 case ValueType::UInt:
519 return std::to_string(value_.uintVal);
520
521 case ValueType::Real:
522 return std::to_string(value_.realVal);
523
524 case ValueType::Array:
526 JSON_ASSERT_MESSAGE(false, "Type is not convertible to string");
527
528 // LCOV_EXCL_START
529 default:
530 UNREACHABLE("json::Value::asString : invalid type");
531 // LCOV_EXCL_STOP
532 }
533
534 return ""; // unreachable
535}
536
539{
540 switch (type_)
541 {
542 case ValueType::Null:
543 return 0;
544
545 case ValueType::Int:
546 return value_.intVal;
547
548 case ValueType::UInt:
549 JSON_ASSERT_MESSAGE(
550 value_.uintVal < (unsigned)kMaxInt, "integer out of signed integer range");
551 return value_.uintVal;
552
553 case ValueType::Real:
554 JSON_ASSERT_MESSAGE(
555 (value_.realVal >= kMinInt && value_.realVal <= kMaxInt),
556 "Real out of signed integer range");
557 return Int(value_.realVal);
558
560 return value_.boolVal ? 1 : 0;
561
562 case ValueType::String: {
563 char const* const str{(value_.stringVal != nullptr) ? value_.stringVal : ""};
565 }
566
567 case ValueType::Array:
569 JSON_ASSERT_MESSAGE(false, "Type is not convertible to int");
570
571 // LCOV_EXCL_START
572 default:
573 UNREACHABLE("json::Value::asInt : invalid type");
574 // LCOV_EXCL_STOP
575 }
576
577 return 0; // unreachable;
578}
579
580UInt
582{
583 switch (type_)
584 {
585 case ValueType::Null:
586 return 0;
587
588 case ValueType::Int: {
589 // Doing this conversion through int64 avoids overflow error for
590 // value_.intVal = -1 * 2^31 i.e. numeric_limits<int>::min().
591 if (value_.intVal < 0)
592 return static_cast<std::int64_t>(value_.intVal) * -1;
593 return value_.intVal;
594 }
595
596 case ValueType::UInt:
597 return value_.uintVal;
598
599 case ValueType::Real: {
600 if (value_.realVal < 0)
601 {
602 JSON_ASSERT_MESSAGE(
603 -1 * value_.realVal <= kMaxUInt, "Real out of unsigned integer range");
604 return UInt(-1 * value_.realVal);
605 }
606 JSON_ASSERT_MESSAGE(value_.realVal <= kMaxUInt, "Real out of unsigned integer range");
607 return UInt(value_.realVal);
608 }
609
611 return value_.boolVal ? 1 : 0;
612
613 case ValueType::String: {
614 char const* const str{(value_.stringVal != nullptr) ? value_.stringVal : ""};
615 auto const temp = beast::lexicalCastThrow<std::int64_t>(str);
616 if (temp < 0)
617 {
618 JSON_ASSERT_MESSAGE(-1 * temp <= kMaxUInt, "String out of unsigned integer range");
619 return -1 * temp;
620 }
621 JSON_ASSERT_MESSAGE(temp <= kMaxUInt, "String out of unsigned integer range");
622 return temp;
623 }
624
625 case ValueType::Array:
627 JSON_ASSERT_MESSAGE(false, "Type is not convertible to int");
628
629 // LCOV_EXCL_START
630 default:
631 UNREACHABLE("json::Value::asAbsInt : invalid type");
632 // LCOV_EXCL_STOP
633 }
634
635 return 0; // unreachable;
636}
637
640{
641 switch (type_)
642 {
643 case ValueType::Null:
644 return 0;
645
646 case ValueType::Int:
647 JSON_ASSERT_MESSAGE(
648 value_.intVal >= 0, "Negative integer can not be converted to unsigned integer");
649 return value_.intVal;
650
651 case ValueType::UInt:
652 return value_.uintVal;
653
654 case ValueType::Real:
655 JSON_ASSERT_MESSAGE(
656 (value_.realVal >= 0 && value_.realVal <= kMaxUInt),
657 "Real out of unsigned integer range");
658 return UInt(value_.realVal);
659
661 return value_.boolVal ? 1 : 0;
662
663 case ValueType::String: {
664 char const* const str{(value_.stringVal != nullptr) ? value_.stringVal : ""};
666 }
667
668 case ValueType::Array:
670 JSON_ASSERT_MESSAGE(false, "Type is not convertible to uint");
671
672 // LCOV_EXCL_START
673 default:
674 UNREACHABLE("json::Value::asUInt : invalid type");
675 // LCOV_EXCL_STOP
676 }
677
678 return 0; // unreachable;
679}
680
681double
683{
684 switch (type_)
685 {
686 case ValueType::Null:
687 return 0.0;
688
689 case ValueType::Int:
690 return value_.intVal;
691
692 case ValueType::UInt:
693 return value_.uintVal;
694
695 case ValueType::Real:
696 return value_.realVal;
697
699 return value_.boolVal ? 1.0 : 0.0;
700
702 case ValueType::Array:
704 JSON_ASSERT_MESSAGE(false, "Type is not convertible to double");
705
706 // LCOV_EXCL_START
707 default:
708 UNREACHABLE("json::Value::asDouble : invalid type");
709 // LCOV_EXCL_STOP
710 }
711
712 return 0; // unreachable;
713}
714
715bool
717{
718 switch (type_)
719 {
720 case ValueType::Null:
721 return false;
722
723 case ValueType::Int:
724 return value_.intVal != 0;
725
726 case ValueType::UInt:
727 return value_.uintVal != 0;
728
729 case ValueType::Real:
730 return value_.realVal != 0.0;
731
733 return value_.boolVal;
734
736 return (value_.stringVal != nullptr) && value_.stringVal[0] != 0;
737
738 case ValueType::Array:
740 return !value_.mapVal->empty();
741
742 // LCOV_EXCL_START
743 default:
744 UNREACHABLE("json::Value::asBool : invalid type");
745 // LCOV_EXCL_STOP
746 }
747
748 return false; // unreachable;
749}
750
751bool
753{
754 switch (type_)
755 {
756 case ValueType::Null:
757 return true;
758
759 case ValueType::Int:
760 return (other == ValueType::Null && value_.intVal == 0) || other == ValueType::Int ||
761 (other == ValueType::UInt && value_.intVal >= 0) || other == ValueType::Real ||
762 other == ValueType::String || other == ValueType::Boolean;
763
764 case ValueType::UInt:
765 return (other == ValueType::Null && value_.uintVal == 0) ||
766 (other == ValueType::Int && value_.uintVal <= (unsigned)kMaxInt) ||
767 other == ValueType::UInt || other == ValueType::Real ||
768 other == ValueType::String || other == ValueType::Boolean;
769
770 case ValueType::Real:
771 return (other == ValueType::Null && value_.realVal == 0.0) ||
772 (other == ValueType::Int && value_.realVal >= kMinInt &&
773 value_.realVal <= kMaxInt) ||
774 (other == ValueType::UInt && value_.realVal >= 0 && value_.realVal <= kMaxUInt &&
775 std::fabs(round(value_.realVal) - value_.realVal) <
777 other == ValueType::Real || other == ValueType::String ||
778 other == ValueType::Boolean;
779
781 return (other == ValueType::Null && !value_.boolVal) || other == ValueType::Int ||
782 other == ValueType::UInt || other == ValueType::Real ||
783 other == ValueType::String || other == ValueType::Boolean;
784
786 return other == ValueType::String ||
787 (other == ValueType::Null &&
788 ((value_.stringVal == nullptr) || value_.stringVal[0] == 0));
789
790 case ValueType::Array:
791 return other == ValueType::Array ||
792 (other == ValueType::Null && value_.mapVal->empty());
793
795 return other == ValueType::Object ||
796 (other == ValueType::Null && value_.mapVal->empty());
797
798 // LCOV_EXCL_START
799 default:
800 UNREACHABLE("json::Value::isConvertible : invalid type");
801 // LCOV_EXCL_STOP
802 }
803
804 return false; // unreachable;
805}
806
812{
813 switch (type_)
814 {
815 case ValueType::Null:
816 case ValueType::Int:
817 case ValueType::UInt:
818 case ValueType::Real:
821 return 0;
822
823 case ValueType::Array: // size of the array is highest index + 1
824 if (!value_.mapVal->empty())
825 {
826 auto itLast = value_.mapVal->end();
827 --itLast;
828 return (*itLast).first.index() + 1;
829 }
830
831 return 0;
832
834 return Int(value_.mapVal->size());
835
836 // LCOV_EXCL_START
837 default:
838 UNREACHABLE("json::Value::size : invalid type");
839 // LCOV_EXCL_STOP
840 }
841
842 return 0; // unreachable;
843}
844
845Value::
846operator bool() const
847{
848 if (isNull())
849 return false;
850
851 if (isString())
852 {
853 auto s = asCString();
854 return (s != nullptr) && (s[0] != 0);
855 }
856
857 return !(isArray() || isObject()) || (size() != 0u);
858}
859
860void
862{
863 XRPL_ASSERT(
865 "json::Value::clear : valid type");
866
867 switch (type_)
868 {
869 case ValueType::Array:
871 value_.mapVal->clear();
872 break;
873
874 default:
875 break;
876 }
877}
878
879Value&
881{
882 XRPL_ASSERT(
884 "json::Value::operator[](UInt) : valid type");
885
886 if (type_ == ValueType::Null)
887 *this = Value(ValueType::Array);
888
889 CZString const key(index);
890 auto it = value_.mapVal->lower_bound(key);
891
892 if (it != value_.mapVal->end() && (*it).first == key)
893 return (*it).second;
894
895 ObjectValues::value_type const defaultValue(key, kNull);
896 it = value_.mapVal->insert(it, defaultValue);
897 return (*it).second;
898}
899
900Value const&
902{
903 XRPL_ASSERT(
905 "json::Value::operator[](UInt) const : valid type");
906
907 if (type_ == ValueType::Null)
908 return kNull;
909
910 CZString const key(index);
911 auto const it = value_.mapVal->find(key);
912
913 if (it == value_.mapVal->end())
914 return kNull;
915
916 return (*it).second;
917}
918
919Value&
920Value::operator[](char const* key)
921{
922 return resolveReference(key, false);
923}
924
925Value&
926Value::resolveReference(char const* key, bool isStatic)
927{
928 XRPL_ASSERT(
930 "json::Value::resolveReference : valid type");
931
932 if (type_ == ValueType::Null)
933 *this = Value(ValueType::Object);
934
935 CZString const actualKey(
936 key,
939 auto it = value_.mapVal->lower_bound(actualKey);
940
941 if (it != value_.mapVal->end() && (*it).first == actualKey)
942 return (*it).second;
943
944 ObjectValues::value_type const defaultValue(actualKey, kNull);
945 it = value_.mapVal->insert(it, defaultValue);
946 Value& value = (*it).second;
947 return value;
948}
949
950Value
951Value::get(UInt index, Value const& defaultValue) const
952{
953 Value const* value = &(*this)[index];
954 return value == &kNull ? defaultValue : *value;
955}
956
957bool
959{
960 return index < size();
961}
962
963Value const&
964Value::operator[](char const* key) const
965{
966 XRPL_ASSERT(
968 "json::Value::operator[](const char*) const : valid type");
969
970 if (type_ == ValueType::Null)
971 return kNull;
972
974 auto const it = value_.mapVal->find(actualKey);
975
976 if (it == value_.mapVal->end())
977 return kNull;
978
979 return (*it).second;
980}
981
982Value&
984{
985 return (*this)[key.c_str()];
986}
987
988Value const&
990{
991 return (*this)[key.c_str()];
992}
993
994Value&
996{
997 return resolveReference(key, true);
998}
999
1000Value const&
1002{
1003 return (*this)[key.cStr()];
1004}
1005
1006Value&
1008{
1009 return (*this)[size()] = value;
1010}
1011
1012Value&
1014{
1015 return (*this)[size()] = std::move(value);
1016}
1017
1018Value
1019Value::get(char const* key, Value const& defaultValue) const
1020{
1021 Value const* value = &(*this)[key];
1022 return value == &kNull ? defaultValue : *value;
1023}
1024
1025Value
1026Value::get(std::string const& key, Value const& defaultValue) const
1027{
1028 return get(key.c_str(), defaultValue);
1029}
1030
1031Value
1032Value::removeMember(char const* key)
1033{
1034 XRPL_ASSERT(
1036 "json::Value::removeMember : valid type");
1037
1038 if (type_ == ValueType::Null)
1039 return kNull;
1040
1042 auto const it = value_.mapVal->find(actualKey);
1043
1044 if (it == value_.mapVal->end())
1045 return kNull;
1046
1047 Value old(it->second);
1048 value_.mapVal->erase(it);
1049 return old;
1050}
1051
1052Value
1054{
1055 return removeMember(key.c_str());
1056}
1057
1058bool
1059Value::isMember(char const* key) const
1060{
1061 if (type_ != ValueType::Object)
1062 return false;
1063
1064 Value const* value = &(*this)[key];
1065 return value != &kNull;
1066}
1067
1068bool
1070{
1071 return isMember(key.c_str());
1072}
1073
1074bool
1076{
1077 return isMember(key.cStr());
1078}
1079
1082{
1083 XRPL_ASSERT(
1085 "json::Value::getMemberNames : valid type");
1086
1087 if (type_ == ValueType::Null)
1088 return Value::Members();
1089
1090 Members members;
1091 members.reserve(value_.mapVal->size());
1092 auto it = value_.mapVal->begin();
1093 auto const itEnd = value_.mapVal->end();
1094
1095 for (; it != itEnd; ++it)
1096 members.emplace_back((*it).first.cStr());
1097
1098 return members;
1099}
1100
1101bool
1103{
1104 return type_ == ValueType::Null;
1105}
1106
1107bool
1109{
1110 return type_ == ValueType::Boolean;
1111}
1112
1113bool
1115{
1116 return type_ == ValueType::Int;
1117}
1118
1119bool
1121{
1122 return type_ == ValueType::UInt;
1123}
1124
1125bool
1130
1131bool
1133{
1134 return type_ == ValueType::Real;
1135}
1136
1137bool
1139{
1140 return isIntegral() || isDouble();
1141}
1142
1143bool
1145{
1146 return type_ == ValueType::String;
1147}
1148
1149bool
1151{
1152 return type_ == ValueType::Array;
1153}
1154
1155bool
1157{
1159}
1160
1161bool
1163{
1164 return type_ == ValueType::Object;
1165}
1166
1167bool
1169{
1171}
1172
1175{
1176 StyledWriter writer;
1177 return writer.write(*this);
1178}
1179
1182{
1183 switch (type_)
1184 {
1185 case ValueType::Array:
1186 case ValueType::Object:
1187 if (value_.mapVal != nullptr)
1188 return const_iterator(value_.mapVal->begin());
1189
1190 break;
1191 default:
1192 break;
1193 }
1194
1195 return const_iterator();
1196}
1197
1200{
1201 switch (type_)
1202 {
1203 case ValueType::Array:
1204 case ValueType::Object:
1205 if (value_.mapVal != nullptr)
1206 return const_iterator(value_.mapVal->end());
1207
1208 break;
1209 default:
1210 break;
1211 }
1212
1213 return const_iterator();
1214}
1215
1218{
1219 switch (type_)
1220 {
1221 case ValueType::Array:
1222 case ValueType::Object:
1223 if (value_.mapVal != nullptr)
1224 return iterator(value_.mapVal->begin());
1225 break;
1226 default:
1227 break;
1228 }
1229
1230 return iterator();
1231}
1232
1235{
1236 switch (type_)
1237 {
1238 case ValueType::Array:
1239 case ValueType::Object:
1240 if (value_.mapVal != nullptr)
1241 return iterator(value_.mapVal->end());
1242 break;
1243 default:
1244 break;
1245 }
1246
1247 return iterator();
1248}
1249
1250} // namespace json
T c_str(T... args)
void releaseMemberName(char *memberName) override
~DefaultValueAllocator() override=default
char * duplicateStringValue(char const *value, unsigned int length=kUnknown) override
void releaseStringValue(char *value) override
char * makeMemberName(char const *memberName) override
Lightweight wrapper to tag static string.
Definition json_value.h:52
constexpr char const * cStr() const
Definition json_value.h:65
Writes a Value in JSON format in a human friendly way.
Definition json_writer.h:73
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:569
virtual void releaseMemberName(char *memberName)=0
virtual void releaseStringValue(char *value)=0
virtual char * duplicateStringValue(char const *value, unsigned int length=kUnknown)=0
static constexpr auto kUnknown
Definition json_value.h:571
bool isStaticString() const
bool operator<(CZString const &other) const
char const * cStr() const
bool operator==(CZString const &other) const
Represents a JSON value.
Definition json_value.h:121
static constexpr Int kMaxInt
Definition json_value.h:134
const_iterator begin() const
bool isIntegral() const
bool isDouble() const
Value removeMember(char const *key)
Remove and return the named member.
bool isNull() const
isNull() tests to see if this field is null.
json::Int Int
Definition json_value.h:129
bool isObject() const
bool asBool() 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...
ValueConstIterator const_iterator
Definition json_value.h:127
std::vector< std::string > Members
Definition json_value.h:125
bool isBool() const
union json::Value::ValueHolder value_
bool isValidIndex(UInt index) const
Return true if index < size().
Value & resolveReference(char const *key, bool isStatic)
std::string toStyledString() const
bool isArray() const
UInt asAbsUInt() const
Correct absolute value from int or unsigned int.
bool isString() const
json::UInt UInt
Definition json_value.h:128
Value & append(Value const &value)
Append value to array at the end.
UInt size() const
Number of values in array or object.
ValueType type_
Definition json_value.h:486
Members getMemberNames() const
Return a list of the member names.
bool isNumeric() const
static constexpr Int kMinInt
Definition json_value.h:133
bool isArrayOrNull() const
const_iterator end() const
bool isUInt() const
ValueIterator iterator
Definition json_value.h:126
double asDouble() const
Value & operator=(Value const &other)
bool isInt() const
UInt asUInt() const
ValueType type() const
bool isConvertibleTo(ValueType other) const
std::string asString() const
Returns the unquoted string value.
void swap(Value &other) noexcept
Swap values.
static constexpr UInt kMaxUInt
Definition json_value.h:135
Value & operator[](UInt index)
Access an array element (zero based index ).
static Value const kNull
Definition json_value.h:132
Value(ValueType type=ValueType::Null)
Create a default Value of the given type.
char const * asCString() const
std::map< CZString, Value > ObjectValues
Definition json_value.h:166
bool isObjectOrNull() const
bool isMember(char const *key) const
Return true if the object has a member named key.
Int asInt() const
void clear()
Remove all object members and array elements.
Number is a floating point type that can represent a wide range of values.
Definition Number.h:351
T emplace_back(T... args)
T epsilon(T... args)
T fabs(T... args)
constexpr Out lexicalCastThrow(In in)
Convert from one type to another, throw on error.
JSON (JavaScript Object Notation).
Definition json_errors.h:5
static ValueAllocator *& valueAllocator()
int Int
std::string to_string(Value const &)
Writes a json::Value to an std::string.
Definition to_string.cpp:10
unsigned int UInt
bool operator==(StaticString x, StaticString y)
Definition json_value.h:75
ValueType
Type of the value held by a Value object.
Definition json_value.h:25
@ UInt
unsigned integer value
Definition json_value.h:28
@ Int
signed integer value
Definition json_value.h:27
@ String
UTF-8 string value.
Definition json_value.h:30
@ Boolean
bool value
Definition json_value.h:31
@ Array
array value (ordered list)
Definition json_value.h:32
@ Object
object value (collection of name/value pairs).
Definition json_value.h:33
@ Real
double value
Definition json_value.h:29
@ Null
'null' value
Definition json_value.h:26
static int integerCmp(Int i, UInt ui)
bool operator<(Value const &, Value const &)
static struct json::DummyValueAllocatorInitializer gDummyValueAllocatorInitializer
STL namespace.
T reserve(T... args)
T size(T... args)
T swap(T... args)
T to_string(T... args)
ObjectValues * mapVal
Definition json_value.h:484