JsonCpp 1.10.0
JSON data format manipulation library
Loading...
Searching...
No Matches
value.h
Go to the documentation of this file.
1// Copyright 2007-2010 Baptiste Lepilleur and The JsonCpp Authors
2// Distributed under MIT license, or public domain if desired and
3// recognized in your jurisdiction.
4// See file LICENSE for detail or copy at http://jsoncpp.sourceforge.net/LICENSE
5
6#ifndef JSON_VALUE_H_INCLUDED
7#define JSON_VALUE_H_INCLUDED
8
9#if !defined(JSON_IS_AMALGAMATION)
10#include "forwards.h"
11#endif // if !defined(JSON_IS_AMALGAMATION)
12
13// Conditional NORETURN attribute on the throw functions would:
14// a) suppress false positives from static code analysis
15// b) possibly improve optimization opportunities.
16#if !defined(JSONCPP_NORETURN)
17#if defined(_MSC_VER) && _MSC_VER == 1800
18#define JSONCPP_NORETURN __declspec(noreturn)
19#else
20#define JSONCPP_NORETURN [[noreturn]]
21#endif
22#endif
23
24// Support for '= delete' with template declarations was a late addition
25// to the c++11 standard and is rejected by clang 3.8 and Apple clang 8.2
26// even though these declare themselves to be c++11 compilers.
27#if !defined(JSONCPP_TEMPLATE_DELETE)
28#if defined(__clang__) && defined(__apple_build_version__)
29#if __apple_build_version__ <= 8000042
30#define JSONCPP_TEMPLATE_DELETE
31#endif
32#elif defined(__clang__)
33#if __clang_major__ == 3 && __clang_minor__ <= 8
34#define JSONCPP_TEMPLATE_DELETE
35#endif
36#endif
37#if !defined(JSONCPP_TEMPLATE_DELETE)
38#define JSONCPP_TEMPLATE_DELETE = delete
39#endif
40#endif
41
42#ifndef JSONCPP_HAS_STRING_VIEW
43#if __cplusplus >= 201703L
44#define JSONCPP_HAS_STRING_VIEW 1
45#endif
46#endif
47
48#include <array>
49#include <exception>
50#include <map>
51#include <memory>
52#include <string>
53#include <vector>
54
55// Forward declaration for testing.
56struct ValueTest;
57
58#ifdef JSONCPP_HAS_STRING_VIEW
59#include <string_view>
60#endif
61
62// Disable warning C4251: <data member>: <type> needs to have dll-interface to
63// be used by...
64#if defined(JSONCPP_DISABLE_DLL_INTERFACE_WARNING)
65#pragma warning(push)
66#pragma warning(disable : 4251 4275)
67#endif // if defined(JSONCPP_DISABLE_DLL_INTERFACE_WARNING)
68
69#pragma pack(push)
70#pragma pack()
71
74namespace Json {
75
76#if JSON_USE_EXCEPTION
81class JSON_API Exception : public std::exception {
82public:
83 Exception(String msg);
84 ~Exception() noexcept override;
85 char const* what() const noexcept override;
86
87protected:
89};
90
97class JSON_API RuntimeError : public Exception {
98public:
99 RuntimeError(String const& msg);
100};
101
108class JSON_API LogicError : public Exception {
109public:
110 LogicError(String const& msg);
111};
112#endif
113
115JSONCPP_NORETURN void throwRuntimeError(String const& msg);
117JSONCPP_NORETURN void throwLogicError(String const& msg);
118
131
139
146
161class JSON_API StaticString {
162public:
163 explicit StaticString(const char* czstring) : c_str_(czstring) {}
164
165 operator const char*() const { return c_str_; }
166
167 const char* c_str() const { return c_str_; }
168
169private:
170 const char* c_str_;
171};
172
207class JSON_API Value {
208 friend class ValueIteratorBase;
209 friend struct ::ValueTest;
210
211public:
212 using Members = std::vector<String>;
216 using Int = Json::Int;
217#if defined(JSON_HAS_INT64)
220#endif // defined(JSON_HAS_INT64)
224
225 // Required for boost integration, e. g. BOOST_TEST
226 using value_type = std::string;
227
228#if JSON_USE_NULLREF
229 // Binary compatibility kludges, do not use.
230 static const Value& null;
231 static const Value& nullRef;
232#endif
233
234 // null and nullRef are deprecated, use this instead.
235 static Value const& nullSingleton();
236
238 static constexpr LargestInt minLargestInt =
239 LargestInt(~(LargestUInt(-1) / 2));
241 static constexpr LargestInt maxLargestInt = LargestInt(LargestUInt(-1) / 2);
243 static constexpr LargestUInt maxLargestUInt = LargestUInt(-1);
244
246 static constexpr Int minInt = Int(~(UInt(-1) / 2));
248 static constexpr Int maxInt = Int(UInt(-1) / 2);
250 static constexpr UInt maxUInt = UInt(-1);
251
252#if defined(JSON_HAS_INT64)
254 static constexpr Int64 minInt64 = Int64(~(UInt64(-1) / 2));
256 static constexpr Int64 maxInt64 = Int64(UInt64(-1) / 2);
258 static constexpr UInt64 maxUInt64 = UInt64(-1);
259#endif // defined(JSON_HAS_INT64)
261 static constexpr UInt defaultRealPrecision = 17;
262 // The constant is hard-coded because some compiler have trouble
263 // converting Value::maxUInt64 to a double correctly (AIX/xlC).
264 // Assumes that UInt64 is a 64 bits integer.
265 static constexpr double maxUInt64AsDouble = 18446744073709551615.0;
266// Workaround for bug in the NVIDIAs CUDA 9.1 nvcc compiler
267// when using gcc and clang backend compilers. CZString
268// cannot be defined as private. See issue #486
269#ifdef __NVCC__
270public:
271#else
272private:
273#endif
274#ifndef JSONCPP_DOC_EXCLUDE_IMPLEMENTATION
275 class JSON_API CZString {
276 public:
277 enum DuplicationPolicy { noDuplication = 0, duplicate, duplicateOnCopy };
278 CZString(ArrayIndex index);
279 CZString(char const* str, unsigned length, DuplicationPolicy allocate);
280 CZString(CZString const& other);
281 CZString(CZString&& other) noexcept;
282 ~CZString();
283 CZString& operator=(const CZString& other);
284 CZString& operator=(CZString&& other) noexcept;
285
286 bool operator<(CZString const& other) const;
287 bool operator==(CZString const& other) const;
288 ArrayIndex index() const;
289 // const char* c_str() const; ///< \deprecated
290 char const* data() const;
291 unsigned length() const;
292 bool isStaticString() const;
293
294 private:
295 void swap(CZString& other);
296
297 struct StringStorage {
298 unsigned policy_ : 2;
299 unsigned length_ : 30; // 1GB max
300 };
301
302 char const* cstr_; // actually, a prefixed string, unless policy is noDup
303 union {
304 ArrayIndex index_;
305 StringStorage storage_;
306 };
307 };
308
309public:
310 typedef std::map<CZString, Value> ObjectValues;
311#endif // ifndef JSONCPP_DOC_EXCLUDE_IMPLEMENTATION
312
313public:
330 Value(ValueType type = nullValue);
331 Value(Int value);
332 Value(UInt value);
333#if defined(JSON_HAS_INT64)
334 Value(Int64 value);
335 Value(UInt64 value);
336#endif // if defined(JSON_HAS_INT64)
337 Value(double value);
338 Value(const char* value);
339 Value(const char* begin, const char* end);
357 Value(const StaticString& value);
358 Value(const String& value);
359#ifdef JSONCPP_HAS_STRING_VIEW
360 inline Value(std::string_view value)
361 : Value(value.data(), value.data() + value.length()) {}
362#endif
363 Value(bool value);
364 Value(std::nullptr_t ptr) = delete;
365 Value(const Value& other);
366 Value(Value&& other) noexcept;
367 ~Value();
368
371 Value& operator=(const Value& other);
372 Value& operator=(Value&& other) noexcept;
373
375 void swap(Value& other);
377 void swapPayload(Value& other);
378
380 void copy(const Value& other);
382 void copyPayload(const Value& other);
383
384 ValueType type() const;
385
387 bool operator<(const Value& other) const;
388 bool operator<=(const Value& other) const;
389 bool operator>=(const Value& other) const;
390 bool operator>(const Value& other) const;
391 bool operator==(const Value& other) const;
392 bool operator!=(const Value& other) const;
393 int compare(const Value& other) const;
394
395 const char* asCString() const;
396#if JSONCPP_USE_SECURE_MEMORY
397 unsigned getCStringLength() const; // Allows you to understand the length of
398 // the CString
399#endif
400 String asString() const;
404 bool getString(char const** begin, char const** end) const;
405#ifdef JSONCPP_HAS_STRING_VIEW
409 inline bool getString(std::string_view* str) const {
410 char const* begin;
411 char const* end;
412 if (!getString(&begin, &end))
413 return false;
414 *str = std::string_view(begin, static_cast<size_t>(end - begin));
415 return true;
416 }
417#endif
418 Int asInt() const;
419 UInt asUInt() const;
420#if defined(JSON_HAS_INT64)
421 Int64 asInt64() const;
422 UInt64 asUInt64() const;
423#endif // if defined(JSON_HAS_INT64)
424 LargestInt asLargestInt() const;
425 LargestUInt asLargestUInt() const;
426 float asFloat() const;
427 double asDouble() const;
428 bool asBool() const;
429
430 bool isNull() const;
431 bool isBool() const;
432 bool isInt() const;
433 bool isInt64() const;
434 bool isUInt() const;
435 bool isUInt64() const;
436 bool isIntegral() const;
437 bool isDouble() const;
438 bool isNumeric() const;
439 bool isString() const;
440 bool isArray() const;
441 bool isObject() const;
442
444 template <typename T> T as() const JSONCPP_TEMPLATE_DELETE;
445 template <typename T> bool is() const JSONCPP_TEMPLATE_DELETE;
446
447 bool isConvertibleTo(ValueType other) const;
448
450 ArrayIndex size() const;
451
454 bool empty() const;
455
457 explicit operator bool() const;
458
462 void clear();
463
469 void resize(ArrayIndex newSize);
470
477 Value& operator[](ArrayIndex index);
478 Value& operator[](int index);
480
485 const Value& operator[](ArrayIndex index) const;
486 const Value& operator[](int index) const;
488
491 Value get(ArrayIndex index, const Value& defaultValue) const;
493 bool isValidIndex(ArrayIndex index) const;
497 Value& append(const Value& value);
498 Value& append(Value&& value);
499
501 bool insert(ArrayIndex index, const Value& newValue);
502 bool insert(ArrayIndex index, Value&& newValue);
503
504#ifdef JSONCPP_HAS_STRING_VIEW
507 inline Value& operator[](std::string_view key) {
508 return resolveReference(key.data(), key.data() + key.length());
509 }
510
513 inline const Value& operator[](std::string_view key) const {
514 Value const* found = find(key.data(), key.data() + key.length());
515 if (!found)
516 return nullSingleton();
517 return *found;
518 }
519#endif
523 Value& operator[](const char* key);
526 const Value& operator[](const char* key) const;
529 Value& operator[](const String& key);
533 const Value& operator[](const String& key) const;
546 Value& operator[](const StaticString& key);
547#ifdef JSONCPP_HAS_STRING_VIEW
550 inline Value get(std::string_view key, const Value& defaultValue) const {
551 return get(key.data(), key.data() + key.length(), defaultValue);
552 }
553#endif
556 Value get(const char* key, const Value& defaultValue) const;
561 Value get(const String& key, const Value& defaultValue) const;
565 Value get(const char* begin, const char* end,
566 const Value& defaultValue) const;
570 Value const* find(char const* begin, char const* end) const;
573 Value const* find(const String& key) const;
574
576 template <typename T, bool (T::*TMemFn)() const>
577 Value const* findValue(const String& key) const {
578 Value const* found = find(key);
579 if (!found || !(found->*TMemFn)())
580 return nullptr;
581 return found;
582 }
583
584 Value const* findNull(const String& key) const;
585 Value const* findBool(const String& key) const;
586 Value const* findInt(const String& key) const;
587 Value const* findInt64(const String& key) const;
588 Value const* findUInt(const String& key) const;
589 Value const* findUInt64(const String& key) const;
590 Value const* findIntegral(const String& key) const;
591 Value const* findDouble(const String& key) const;
592 Value const* findNumeric(const String& key) const;
593 Value const* findString(const String& key) const;
594 Value const* findArray(const String& key) const;
595 Value const* findObject(const String& key) const;
596
600 Value* demand(char const* begin, char const* end);
606#if JSONCPP_HAS_STRING_VIEW
607 inline void removeMember(std::string_view key) {
608 removeMember(key.data(), key.data() + key.length(), nullptr);
609 }
610#endif
611 void removeMember(const char* key);
614 void removeMember(const String& key);
622#if JSONCPP_HAS_STRING_VIEW
623 inline bool removeMember(std::string_view key, Value* removed) {
624 return removeMember(key.data(), key.data() + key.length(), removed);
625 }
626#endif
627 bool removeMember(String const& key, Value* removed);
630 bool removeMember(const char* key, Value* removed);
632 bool removeMember(const char* begin, const char* end, Value* removed);
639 bool removeIndex(ArrayIndex index, Value* removed);
640
641#ifdef JSONCPP_HAS_STRING_VIEW
644 inline bool isMember(std::string_view key) const {
645 return isMember(key.data(), key.data() + key.length());
646 }
647#endif
650 bool isMember(const char* key) const;
653 bool isMember(const String& key) const;
655 bool isMember(const char* begin, const char* end) const;
656
662 Members getMemberNames() const;
663
665 JSONCPP_DEPRECATED("Use setComment(String const&) instead.")
666 void setComment(const char* comment, CommentPlacement placement) {
667 setComment(String(comment, strlen(comment)), placement);
668 }
669
670 void setComment(const char* comment, size_t len, CommentPlacement placement) {
671 setComment(String(comment, len), placement);
672 }
673
674 void setComment(String comment, CommentPlacement placement);
675 bool hasComment(CommentPlacement placement) const;
677 String getComment(CommentPlacement placement) const;
678
679 String toStyledString() const;
680
681 const_iterator begin() const;
682 const_iterator end() const;
683
684 iterator begin();
685 iterator end();
686
687 // \brief Returns a view of member pairs for range-based for loops.
688 ValueMembersView members();
689 // \brief Returns a view of member pairs for range-based for loops.
690 ValueConstMembersView members() const;
691
695 const Value& front() const;
696
700 Value& front();
701
705 const Value& back() const;
706
710 Value& back();
711
712 // Accessors for the [start, limit) range of bytes within the JSON text from
713 // which this value was parsed, if any.
714 void setOffsetStart(ptrdiff_t start);
715 void setOffsetLimit(ptrdiff_t limit);
716 ptrdiff_t getOffsetStart() const;
717 ptrdiff_t getOffsetLimit() const;
718
719private:
720 void setType(ValueType v) {
721 bits_.value_type_ = static_cast<unsigned char>(v);
722 }
723 bool isAllocated() const { return bits_.allocated_; }
724 void setIsAllocated(bool v) { bits_.allocated_ = v; }
725
726 void initBasic(ValueType type, bool allocated = false);
727 void dupPayload(const Value& other);
728 void releasePayload();
729 void dupMeta(const Value& other);
730
731 Value& resolveReference(const char* key);
732 Value& resolveReference(const char* key, const char* end);
733
734 // struct MemberNamesTransform
735 //{
736 // typedef const char *result_type;
737 // const char *operator()( const CZString &name ) const
738 // {
739 // return name.c_str();
740 // }
741 //};
742
743 union ValueHolder {
744 LargestInt int_;
745 LargestUInt uint_;
746 double real_;
747 bool bool_;
748 char* string_; // if allocated_, ptr to { unsigned, char[] }.
749 ObjectValues* map_;
750 } value_;
751
752 struct {
753 // Really a ValueType, but types should agree for bitfield packing.
754 unsigned int value_type_ : 8;
755 // Unless allocated_, string_ must be null-terminated.
756 unsigned int allocated_ : 1;
757 } bits_;
758
759 class Comments {
760 public:
761 Comments() = default;
762 Comments(const Comments& that);
763 Comments(Comments&& that) noexcept;
764 Comments& operator=(const Comments& that);
765 Comments& operator=(Comments&& that) noexcept;
766 bool has(CommentPlacement slot) const;
767 String get(CommentPlacement slot) const;
768 void set(CommentPlacement slot, String comment);
769
770 private:
771 using Array = std::array<String, numberOfCommentPlacement>;
772 std::unique_ptr<Array> ptr_;
773 };
774 Comments comments_;
775
776 // [start, limit) byte offsets in the source JSON text from which this Value
777 // was extracted.
778 ptrdiff_t start_;
779 ptrdiff_t limit_;
780};
781
782template <> inline bool Value::as<bool>() const { return asBool(); }
783template <> inline bool Value::is<bool>() const { return isBool(); }
784
785template <> inline Int Value::as<Int>() const { return asInt(); }
786template <> inline bool Value::is<Int>() const { return isInt(); }
787
788template <> inline UInt Value::as<UInt>() const { return asUInt(); }
789template <> inline bool Value::is<UInt>() const { return isUInt(); }
790
791#if defined(JSON_HAS_INT64)
792template <> inline Int64 Value::as<Int64>() const { return asInt64(); }
793template <> inline bool Value::is<Int64>() const { return isInt64(); }
794
795template <> inline UInt64 Value::as<UInt64>() const { return asUInt64(); }
796template <> inline bool Value::is<UInt64>() const { return isUInt64(); }
797#endif
798
799template <> inline double Value::as<double>() const { return asDouble(); }
800template <> inline bool Value::is<double>() const { return isDouble(); }
801
802template <> inline String Value::as<String>() const { return asString(); }
803template <> inline bool Value::is<String>() const { return isString(); }
804
807template <> inline float Value::as<float>() const { return asFloat(); }
808template <> inline const char* Value::as<const char*>() const {
809 return asCString();
810}
811
815class JSON_API PathArgument {
816public:
817 friend class Path;
818
821 PathArgument(const char* key);
822 PathArgument(String key);
823
824private:
825 enum Kind { kindNone = 0, kindIndex, kindKey };
826 String key_;
827 ArrayIndex index_{};
828 Kind kind_{kindNone};
829};
830
842class JSON_API Path {
843public:
844 Path(const String& path, const PathArgument& a1 = PathArgument(),
845 const PathArgument& a2 = PathArgument(),
846 const PathArgument& a3 = PathArgument(),
847 const PathArgument& a4 = PathArgument(),
848 const PathArgument& a5 = PathArgument());
849
850 const Value& resolve(const Value& root) const;
851 Value resolve(const Value& root, const Value& defaultValue) const;
854 Value& make(Value& root) const;
855
856private:
857 using InArgs = std::vector<const PathArgument*>;
858 using Args = std::vector<PathArgument>;
859
860 void makePath(const String& path, const InArgs& in);
861 void addPathInArg(const String& path, const InArgs& in,
862 InArgs::const_iterator& itInArg, PathArgument::Kind kind);
863 static void invalidPath(const String& path, int location);
864
865 Args args_;
866};
867
871class JSON_API ValueIteratorBase {
872public:
873 using iterator_category = std::bidirectional_iterator_tag;
874 using size_t = unsigned int;
875 using difference_type = int;
877
878 bool operator==(const SelfType& other) const { return isEqual(other); }
879
880 bool operator!=(const SelfType& other) const { return !isEqual(other); }
881
882 difference_type operator-(const SelfType& other) const {
883 return other.computeDistance(*this);
884 }
885
888 Value key() const;
889
892 UInt index() const;
893
897 String name() const;
898
903 JSONCPP_DEPRECATED("Use `key = name();` instead.")
904 char const* memberName() const;
908 char const* memberName(char const** end) const;
909
910protected:
917 const Value& deref() const;
918 Value& deref();
919
920 void increment();
921
922 void decrement();
923
924 difference_type computeDistance(const SelfType& other) const;
925
926 bool isEqual(const SelfType& other) const;
927
928 void copy(const SelfType& other);
929
930private:
931 Value::ObjectValues::iterator current_;
932 // Indicates that iterator is for a null value.
933 bool isNull_{true};
934
935public:
936 // For some reason, BORLAND needs these at the end, rather
937 // than earlier. No idea why.
938 ValueIteratorBase();
939 explicit ValueIteratorBase(const Value::ObjectValues::iterator& current);
940};
941
945class JSON_API ValueConstIterator : public ValueIteratorBase {
946 friend class Value;
947
948public:
949 using value_type = const Value;
950 // typedef unsigned int size_t;
951 // typedef int difference_type;
952 using reference = const Value&;
953 using pointer = const Value*;
955
957 ValueConstIterator(ValueIterator const& other);
958
959private:
962 explicit ValueConstIterator(const Value::ObjectValues::iterator& current);
963
964public:
965 SelfType& operator=(const ValueIteratorBase& other);
966
968 SelfType temp(*this);
969 ++*this;
970 return temp;
971 }
972
974 SelfType temp(*this);
975 --*this;
976 return temp;
977 }
978
980 decrement();
981 return *this;
982 }
983
985 increment();
986 return *this;
987 }
988
989 reference operator*() const { return deref(); }
990
991 pointer operator->() const { return &deref(); }
992};
993
996class JSON_API ValueIterator : public ValueIteratorBase {
997 friend class Value;
998
999public:
1001 using size_t = unsigned int;
1002 using difference_type = int;
1004 using pointer = Value*;
1006
1008 explicit ValueIterator(const ValueConstIterator& other);
1010
1011private:
1014 explicit ValueIterator(const Value::ObjectValues::iterator& current);
1015
1016public:
1017 SelfType& operator=(const SelfType& other);
1018
1020 SelfType temp(*this);
1021 ++*this;
1022 return temp;
1023 }
1024
1026 SelfType temp(*this);
1027 --*this;
1028 return temp;
1029 }
1030
1032 decrement();
1033 return *this;
1034 }
1035
1037 increment();
1038 return *this;
1039 }
1040
1046 reference operator*() const { return const_cast<reference>(deref()); }
1047 pointer operator->() const { return const_cast<pointer>(&deref()); }
1048};
1049
1056
1062 const Value& value;
1063};
1064
1068public:
1069 using iterator_category = std::forward_iterator_tag;
1071 using difference_type = int;
1074
1076 explicit ValueMembersIterator(ValueIterator const& iter) : it_(iter) {}
1077
1079 ++it_;
1080 return *this;
1081 }
1083 ValueMembersIterator temp(*this);
1084 ++*this;
1085 return temp;
1086 }
1087 bool operator==(ValueMembersIterator const& other) const {
1088 return it_ == other.it_;
1089 }
1090 bool operator!=(ValueMembersIterator const& other) const {
1091 return it_ != other.it_;
1092 }
1093 MemberProxy operator*() const { return MemberProxy{it_.name(), *it_}; }
1094
1095private:
1096 ValueIterator it_;
1097};
1098
1102public:
1103 using iterator_category = std::forward_iterator_tag;
1105 using difference_type = int;
1108
1111 : it_(iter) {}
1112
1114 ++it_;
1115 return *this;
1116 }
1118 ValueConstMembersIterator temp(*this);
1119 ++*this;
1120 return temp;
1121 }
1122 bool operator==(ValueConstMembersIterator const& other) const {
1123 return it_ == other.it_;
1124 }
1125 bool operator!=(ValueConstMembersIterator const& other) const {
1126 return it_ != other.it_;
1127 }
1129 return ConstMemberProxy{it_.name(), *it_};
1130 }
1131
1132private:
1134};
1135
1139public:
1144
1145private:
1146 ValueIterator begin_;
1147 ValueIterator end_;
1148};
1149
1153public:
1157 return ValueConstMembersIterator(begin_);
1158 }
1160 return ValueConstMembersIterator(end_);
1161 }
1162
1163private:
1164 ValueConstIterator begin_;
1165 ValueConstIterator end_;
1166};
1167
1169 return ValueMembersView(begin(), end());
1170}
1172 return ValueConstMembersView(begin(), end());
1173}
1174
1175inline void swap(Value& a, Value& b) { a.swap(b); }
1176
1177inline const Value& Value::front() const { return *begin(); }
1178
1179inline Value& Value::front() { return *begin(); }
1180
1181inline const Value& Value::back() const { return *(--end()); }
1182
1183inline Value& Value::back() { return *(--end()); }
1184
1185} // namespace Json
1186
1187#pragma pack(pop)
1188
1189#if defined(JSONCPP_DISABLE_DLL_INTERFACE_WARNING)
1190#pragma warning(pop)
1191#endif // if defined(JSONCPP_DISABLE_DLL_INTERFACE_WARNING)
1192
1193#endif // JSON_H_INCLUDED
char const * what() const noexcept override
~Exception() noexcept override
Exception(String msg)
String msg_
Definition value.h:88
LogicError(String const &msg)
Experimental and untested: represents an element of the "path" to access a node.
Definition value.h:815
friend class Path
Definition value.h:817
Path(const String &path, const PathArgument &a1=PathArgument(), const PathArgument &a2=PathArgument(), const PathArgument &a3=PathArgument(), const PathArgument &a4=PathArgument(), const PathArgument &a5=PathArgument())
Value & make(Value &root) const
Creates the "path" to access the specified node and returns a reference on the node.
const Value & resolve(const Value &root) const
RuntimeError(String const &msg)
Lightweight wrapper to tag static string.
Definition value.h:161
const char * c_str() const
Definition value.h:167
StaticString(const char *czstring)
Definition value.h:163
const iterator for object and array value.
Definition value.h:945
SelfType & operator--()
Definition value.h:979
pointer operator->() const
Definition value.h:991
const Value & reference
Definition value.h:952
ValueConstIterator SelfType
Definition value.h:954
SelfType operator--(int)
Definition value.h:973
SelfType & operator++()
Definition value.h:984
SelfType operator++(int)
Definition value.h:967
const Value value_type
Definition value.h:949
SelfType & operator=(const ValueIteratorBase &other)
reference operator*() const
Definition value.h:989
const Value * pointer
Definition value.h:953
friend class Value
Definition value.h:946
Iterator adapter for range-based for loops.
Definition value.h:1101
ConstMemberProxy * pointer
Definition value.h:1106
bool operator!=(ValueConstMembersIterator const &other) const
Definition value.h:1125
std::forward_iterator_tag iterator_category
Definition value.h:1103
ValueConstMembersIterator(ValueConstIterator const &iter)
Definition value.h:1110
ConstMemberProxy reference
Definition value.h:1107
bool operator==(ValueConstMembersIterator const &other) const
Definition value.h:1122
ValueConstMembersIterator operator++(int)
Definition value.h:1117
ConstMemberProxy value_type
Definition value.h:1104
ValueConstMembersIterator & operator++()
Definition value.h:1113
ConstMemberProxy operator*() const
Definition value.h:1128
Range-based for loop adapter for object members.
Definition value.h:1152
ValueConstMembersView(ValueConstIterator begin, ValueConstIterator end)
Definition value.h:1154
ValueConstMembersIterator begin() const
Definition value.h:1156
ValueConstMembersIterator end() const
Definition value.h:1159
Represents a JSON value.
Definition value.h:207
const_iterator begin() const
Value get(ArrayIndex index, const Value &defaultValue) const
If the array contains at least index+1 elements, returns the element value, otherwise returns default...
bool empty() const
Return true if empty array, empty object, or null; otherwise, false.
Value(std::nullptr_t ptr)=delete
static constexpr LargestInt maxLargestInt
Maximum signed integer value that can be stored in a Json::Value.
Definition value.h:241
Value(std::string_view value)
Definition value.h:360
Json::ArrayIndex ArrayIndex
Definition value.h:223
UInt64 asUInt64() const
ArrayIndex size() const
Number of values in array or object.
Json::UInt UInt
Definition value.h:215
const char * asCString() const
Embedded zeroes could cause you trouble!
bool operator==(const Value &other) const
static constexpr Int64 maxInt64
Maximum signed 64 bits int value that can be stored in a Json::Value.
Definition value.h:256
void copy(const Value &other)
copy everything.
static const Value & null
Definition value.h:230
void setComment(const char *comment, size_t len, CommentPlacement placement)
Comments must be //... or /* ... *‍/.
Definition value.h:670
void setComment(const char *comment, CommentPlacement placement)
Definition value.h:666
const Value & operator[](std::string_view key) const
Access an object value by name, returns null if there is no member with that name.
Definition value.h:513
bool getString(char const **begin, char const **end) const
Get raw char* of string-value.
T as() const =delete
The as<T> and is<T> member function templates and specializations.
static constexpr double maxUInt64AsDouble
Definition value.h:265
std::vector< String > Members
Definition value.h:212
const_iterator end() const
bool operator<=(const Value &other) const
const Value & front() const
Returns a reference to the first element in the Value.
Definition value.h:1177
bool operator>(const Value &other) const
bool isDouble() const
bool isInt64() const
void clear()
Remove all object members and array elements.
String asString() const
Embedded zeroes are possible.
void swapPayload(Value &other)
Swap values but leave comments and source offsets in place.
bool removeMember(std::string_view key, Value *removed)
Remove the named map member.
Definition value.h:623
Int asInt() const
ValueIterator iterator
Definition value.h:213
Json::LargestInt LargestInt
Definition value.h:221
Json::LargestUInt LargestUInt
Definition value.h:222
ValueConstIterator const_iterator
Definition value.h:214
bool isString() const
void removeMember(std::string_view key)
Remove and return the named member.
Definition value.h:607
UInt asUInt() const
bool isMember(std::string_view key) const
Return true if the object has a member named key.
Definition value.h:644
Json::UInt64 UInt64
Definition value.h:218
void resize(ArrayIndex newSize)
Resize the array to newSize elements.
Value & append(const Value &value)
Append value to array at the end.
Value & operator[](std::string_view key)
Access an object value by name, create a null member if it does not exist.
Definition value.h:507
bool operator!=(const Value &other) const
bool getString(std::string_view *str) const
Get string_view of string-value.
Definition value.h:409
bool isUInt64() const
Value const * findValue(const String &key) const
Calls find and only returns a valid pointer if the type is found.
Definition value.h:577
ValueType type() const
ValueMembersView members()
Definition value.h:1168
const Value & back() const
Returns a reference to the last element in the Value.
Definition value.h:1181
Int64 asInt64() const
void swap(Value &other)
Swap everything.
bool operator<(const Value &other) const
Compare payload only, not comments etc.
Json::Int Int
Definition value.h:216
static const Value & nullRef
Definition value.h:231
bool isBool() const
void copyPayload(const Value &other)
copy values but leave comments and source offsets in place.
static constexpr Int maxInt
Maximum signed int value that can be stored in a Json::Value.
Definition value.h:248
bool asBool() const
bool isUInt() const
static constexpr LargestUInt maxLargestUInt
Maximum unsigned integer value that can be stored in a Json::Value.
Definition value.h:243
bool isValidIndex(ArrayIndex index) const
Return true if index < size().
friend class ValueIteratorBase
Definition value.h:208
Json::Int64 Int64
Definition value.h:219
Value(ValueType type=nullValue)
Create a default Value of the given type.
static constexpr UInt64 maxUInt64
Maximum unsigned 64 bits int value that can be stored in a Json::Value.
Definition value.h:258
Value & operator=(const Value &other)
static constexpr Int minInt
Minimum signed int value that can be stored in a Json::Value.
Definition value.h:246
Value get(std::string_view key, const Value &defaultValue) const
Return the member named key if it exist, defaultValue otherwise.
Definition value.h:550
bool insert(ArrayIndex index, const Value &newValue)
Insert value in array at specific index.
static constexpr Int64 minInt64
Minimum signed 64 bits int value that can be stored in a Json::Value.
Definition value.h:254
std::string value_type
Definition value.h:226
bool is() const =delete
int compare(const Value &other) const
static constexpr LargestInt minLargestInt
Minimum signed integer value that can be stored in a Json::Value.
Definition value.h:238
bool isConvertibleTo(ValueType other) const
static Value const & nullSingleton()
float asFloat() const
Value const * find(char const *begin, char const *end) const
Most general and efficient version of isMember()const, get()const, and operator[]const.
double asDouble() const
bool operator>=(const Value &other) const
static constexpr UInt maxUInt
Maximum unsigned int value that can be stored in a Json::Value.
Definition value.h:250
bool isInt() const
bool isEqual(const SelfType &other) const
bool operator==(const SelfType &other) const
Definition value.h:878
unsigned int size_t
Definition value.h:874
std::bidirectional_iterator_tag iterator_category
Definition value.h:873
difference_type operator-(const SelfType &other) const
Definition value.h:882
bool operator!=(const SelfType &other) const
Definition value.h:880
const Value & deref() const
ValueIteratorBase SelfType
Definition value.h:876
difference_type computeDistance(const SelfType &other) const
Iterator for object and array value.
Definition value.h:996
SelfType operator--(int)
Definition value.h:1025
SelfType & operator--()
Definition value.h:1031
reference operator*() const
Definition value.h:1046
SelfType & operator++()
Definition value.h:1036
ValueIterator SelfType
Definition value.h:1005
unsigned int size_t
Definition value.h:1001
Value & reference
Definition value.h:1003
ValueIterator(const ValueIterator &other)
pointer operator->() const
Definition value.h:1047
SelfType & operator=(const SelfType &other)
SelfType operator++(int)
Definition value.h:1019
friend class Value
Definition value.h:997
Iterator adapter for range-based for loops.
Definition value.h:1067
bool operator!=(ValueMembersIterator const &other) const
Definition value.h:1090
ValueMembersIterator & operator++()
Definition value.h:1078
MemberProxy * pointer
Definition value.h:1072
ValueMembersIterator operator++(int)
Definition value.h:1082
bool operator==(ValueMembersIterator const &other) const
Definition value.h:1087
std::forward_iterator_tag iterator_category
Definition value.h:1069
ValueMembersIterator(ValueIterator const &iter)
Definition value.h:1076
MemberProxy operator*() const
Definition value.h:1093
Range-based for loop adapter for object members.
Definition value.h:1138
ValueMembersIterator begin() const
Definition value.h:1142
ValueMembersView(ValueIterator begin, ValueIterator end)
Definition value.h:1140
ValueMembersIterator end() const
Definition value.h:1143
JSON (JavaScript Object Notation).
Definition allocator.h:16
void throwLogicError(String const &msg)
used internally
int64_t Int64
Definition config.h:121
unsigned int ArrayIndex
Definition forwards.h:32
Int64 LargestInt
Definition config.h:124
CommentPlacement
Definition value.h:132
@ commentAfterOnSameLine
a comment just after a value on the same line
Definition value.h:134
@ commentBefore
a comment placed on the line before a value
Definition value.h:133
@ numberOfCommentPlacement
root value)
Definition value.h:137
@ commentAfter
a comment on the line after a value (only make sense for
Definition value.h:135
unsigned int UInt
Definition config.h:110
ValueType
Type of the value held by a Value object.
Definition value.h:121
@ booleanValue
bool value
Definition value.h:127
@ nullValue
'null' value
Definition value.h:122
@ stringValue
UTF-8 string value.
Definition value.h:126
@ realValue
double value
Definition value.h:125
@ arrayValue
array value (ordered list)
Definition value.h:128
@ intValue
signed integer value
Definition value.h:123
@ objectValue
object value (collection of name/value pairs).
Definition value.h:129
@ uintValue
unsigned integer value
Definition value.h:124
void throwRuntimeError(String const &msg)
used internally
int Int
Definition config.h:109
std::basic_string< char, std::char_traits< char >, Allocator< char > > String
Definition config.h:135
uint64_t UInt64
Definition config.h:122
UInt64 LargestUInt
Definition config.h:125
PrecisionType
Type of precision for formatting of real values.
Definition value.h:142
@ decimalPlaces
we set max number of digits after "." in string
Definition value.h:144
@ significantDigits
we set max number of significant digits in string
Definition value.h:143
void swap(Value &a, Value &b)
Definition value.h:1175
Proxy struct to enable range-based for loops over const object members.
Definition value.h:1060
const String name
Definition value.h:1061
const Value & value
Definition value.h:1062
Proxy struct to enable range-based for loops over object members.
Definition value.h:1052
const String name
Definition value.h:1053
Value & value
Definition value.h:1054
#define JSONCPP_TEMPLATE_DELETE
Definition value.h:38