JsonCpp project page | Classes | Namespace | JsonCpp home page

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.
56 struct 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
74 namespace Json {
75
76#if JSON_USE_EXCEPTION
81 class JSON_API Exception : public std::exception {
82 public:
83 Exception(String msg);
84 ~Exception() noexcept override;
85 char const* what() const noexcept override;
86
87protected:
88 String msg_;
89};
90
97class JSON_API RuntimeError : public Exception {
98 public:
99 RuntimeError(String const& msg);
100};
101
108 class JSON_API LogicError : public Exception {
109 public:
110 LogicError(String const& msg);
111};
112#endif
113
115 JSONCPP_NORETURN void throwRuntimeError(String const& msg);
117 JSONCPP_NORETURN void throwLogicError(String const& msg);
118
121 enum ValueType {
122 nullValue = 0,
123 intValue,
124 uintValue,
125 realValue,
126 stringValue,
127 booleanValue,
128 arrayValue,
129 objectValue
130};
131
132 enum CommentPlacement {
133 commentBefore = 0,
134 commentAfterOnSameLine,
135 commentAfter,
137 numberOfCommentPlacement
138};
139
142 enum PrecisionType {
143 significantDigits = 0,
144 decimalPlaces
145};
146
161 class JSON_API StaticString {
162 public:
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
169 private:
170 const char* c_str_;
171};
172
207 class JSON_API Value {
208 friend class ValueIteratorBase;
209 friend struct ::ValueTest;
210
211 public:
212 using Members = std::vector<String>;
213 using iterator = ValueIterator;
214 using const_iterator = ValueConstIterator;
215 using UInt = Json::UInt;
216 using Int = Json::Int;
217#if defined(JSON_HAS_INT64)
218 using UInt64 = Json::UInt64;
219 using Int64 = Json::Int64;
220#endif // defined(JSON_HAS_INT64)
221 using LargestInt = Json::LargestInt;
222 using LargestUInt = Json::LargestUInt;
223 using ArrayIndex = Json::ArrayIndex;
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__
270 public:
271#else
272 private:
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
309 public:
310 typedef std::map<CZString, Value> ObjectValues;
311#endif // ifndef JSONCPP_DOC_EXCLUDE_IMPLEMENTATION
312
313 public:
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 }
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;
560 Value get(const String& key, const Value& defaultValue) const;
564 Value get(const char* begin, const char* end,
565 const Value& defaultValue) const;
569 Value const* find(char const* begin, char const* end) const;
572 Value const* find(const String& key) const;
573
575 template <typename T, bool (T::*TMemFn)() const>
576 Value const* findValue(const String& key) const {
577 Value const* found = find(key);
578 if (!found || !(found->*TMemFn)())
579 return nullptr;
580 return found;
581 }
582
583 Value const* findNull(const String& key) const;
584 Value const* findBool(const String& key) const;
585 Value const* findInt(const String& key) const;
586 Value const* findInt64(const String& key) const;
587 Value const* findUInt(const String& key) const;
588 Value const* findUInt64(const String& key) const;
589 Value const* findIntegral(const String& key) const;
590 Value const* findDouble(const String& key) const;
591 Value const* findNumeric(const String& key) const;
592 Value const* findString(const String& key) const;
593 Value const* findArray(const String& key) const;
594 Value const* findObject(const String& key) const;
595
599 Value* demand(char const* begin, char const* end);
605#if JSONCPP_HAS_STRING_VIEW
606 inline void removeMember(std::string_view key) {
607 removeMember(key.data(), key.data() + key.length(), nullptr);
608 }
609#endif
610 void removeMember(const char* key);
613 void removeMember(const String& key);
620#if JSONCPP_HAS_STRING_VIEW
621 inline bool removeMember(std::string_view key, Value* removed) {
622 return removeMember(key.data(), key.data() + key.length(), removed);
623 }
624#endif
625 bool removeMember(String const& key, Value* removed);
628 bool removeMember(const char* key, Value* removed);
630 bool removeMember(const char* begin, const char* end, Value* removed);
637 bool removeIndex(ArrayIndex index, Value* removed);
638
639#ifdef JSONCPP_HAS_STRING_VIEW
642 inline bool isMember(std::string_view key) const {
643 return isMember(key.data(), key.data() + key.length());
644 }
645#endif
648 bool isMember(const char* key) const;
651 bool isMember(const String& key) const;
653 bool isMember(const char* begin, const char* end) const;
654
660 Members getMemberNames() const;
661
663 JSONCPP_DEPRECATED("Use setComment(String const&) instead.")
664 void setComment(const char* comment, CommentPlacement placement) {
665 setComment(String(comment, strlen(comment)), placement);
666 }
668 void setComment(const char* comment, size_t len, CommentPlacement placement) {
669 setComment(String(comment, len), placement);
670 }
671
672 void setComment(String comment, CommentPlacement placement);
673 bool hasComment(CommentPlacement placement) const;
675 String getComment(CommentPlacement placement) const;
676
677 String toStyledString() const;
678
679 const_iterator begin() const;
680 const_iterator end() const;
681
682 iterator begin();
683 iterator end();
684
685 // \brief Returns a view of member pairs for range-based for loops.
686 ValueMembersView members();
687 // \brief Returns a view of member pairs for range-based for loops.
688 ValueConstMembersView members() const;
689
693 const Value& front() const;
694
698 Value& front();
699
703 const Value& back() const;
704
708 Value& back();
709
710 // Accessors for the [start, limit) range of bytes within the JSON text from
711 // which this value was parsed, if any.
712 void setOffsetStart(ptrdiff_t start);
713 void setOffsetLimit(ptrdiff_t limit);
714 ptrdiff_t getOffsetStart() const;
715 ptrdiff_t getOffsetLimit() const;
716
717 private:
718 void setType(ValueType v) {
719 bits_.value_type_ = static_cast<unsigned char>(v);
720 }
721 bool isAllocated() const { return bits_.allocated_; }
722 void setIsAllocated(bool v) { bits_.allocated_ = v; }
723
724 void initBasic(ValueType type, bool allocated = false);
725 void dupPayload(const Value& other);
726 void releasePayload();
727 void dupMeta(const Value& other);
728
729 Value& resolveReference(const char* key);
730 Value& resolveReference(const char* key, const char* end);
731
732 // struct MemberNamesTransform
733 //{
734 // typedef const char *result_type;
735 // const char *operator()( const CZString &name ) const
736 // {
737 // return name.c_str();
738 // }
739 //};
740
741 union ValueHolder {
742 LargestInt int_;
743 LargestUInt uint_;
744 double real_;
745 bool bool_;
746 char* string_; // if allocated_, ptr to { unsigned, char[] }.
747 ObjectValues* map_;
748 } value_;
749
750 struct {
751 // Really a ValueType, but types should agree for bitfield packing.
752 unsigned int value_type_ : 8;
753 // Unless allocated_, string_ must be null-terminated.
754 unsigned int allocated_ : 1;
755 } bits_;
756
757 class Comments {
758 public:
759 Comments() = default;
760 Comments(const Comments& that);
761 Comments(Comments&& that) noexcept;
762 Comments& operator=(const Comments& that);
763 Comments& operator=(Comments&& that) noexcept;
764 bool has(CommentPlacement slot) const;
765 String get(CommentPlacement slot) const;
766 void set(CommentPlacement slot, String comment);
767
768 private:
769 using Array = std::array<String, numberOfCommentPlacement>;
770 std::unique_ptr<Array> ptr_;
771 };
772 Comments comments_;
773
774 // [start, limit) byte offsets in the source JSON text from which this Value
775 // was extracted.
776 ptrdiff_t start_;
777 ptrdiff_t limit_;
778};
779
780 template <> inline bool Value::as<bool>() const { return asBool(); }
781 template <> inline bool Value::is<bool>() const { return isBool(); }
782
783 template <> inline Int Value::as<Int>() const { return asInt(); }
784 template <> inline bool Value::is<Int>() const { return isInt(); }
785
786 template <> inline UInt Value::as<UInt>() const { return asUInt(); }
787 template <> inline bool Value::is<UInt>() const { return isUInt(); }
788
789#if defined(JSON_HAS_INT64)
790 template <> inline Int64 Value::as<Int64>() const { return asInt64(); }
791 template <> inline bool Value::is<Int64>() const { return isInt64(); }
792
793 template <> inline UInt64 Value::as<UInt64>() const { return asUInt64(); }
794 template <> inline bool Value::is<UInt64>() const { return isUInt64(); }
795#endif
796
797 template <> inline double Value::as<double>() const { return asDouble(); }
798 template <> inline bool Value::is<double>() const { return isDouble(); }
799
800 template <> inline String Value::as<String>() const { return asString(); }
801 template <> inline bool Value::is<String>() const { return isString(); }
802
805 template <> inline float Value::as<float>() const { return asFloat(); }
806 template <> inline const char* Value::as<const char*>() const {
807 return asCString();
808}
809
813 class JSON_API PathArgument {
814 public:
815 friend class Path;
816
817 PathArgument();
818 PathArgument(ArrayIndex index);
819 PathArgument(const char* key);
820 PathArgument(String key);
821
822 private:
823 enum Kind { kindNone = 0, kindIndex, kindKey };
824 String key_;
825 ArrayIndex index_{};
826 Kind kind_{kindNone};
827};
828
840 class JSON_API Path {
841 public:
842 Path(const String& path, const PathArgument& a1 = PathArgument(),
843 const PathArgument& a2 = PathArgument(),
844 const PathArgument& a3 = PathArgument(),
845 const PathArgument& a4 = PathArgument(),
846 const PathArgument& a5 = PathArgument());
847
848 const Value& resolve(const Value& root) const;
849 Value resolve(const Value& root, const Value& defaultValue) const;
852 Value& make(Value& root) const;
853
854 private:
855 using InArgs = std::vector<const PathArgument*>;
856 using Args = std::vector<PathArgument>;
857
858 void makePath(const String& path, const InArgs& in);
859 void addPathInArg(const String& path, const InArgs& in,
860 InArgs::const_iterator& itInArg, PathArgument::Kind kind);
861 static void invalidPath(const String& path, int location);
862
863 Args args_;
864};
865
869 class JSON_API ValueIteratorBase {
870 public:
871 using iterator_category = std::bidirectional_iterator_tag;
872 using size_t = unsigned int;
873 using difference_type = int;
874 using SelfType = ValueIteratorBase;
875
876 bool operator==(const SelfType& other) const { return isEqual(other); }
877
878 bool operator!=(const SelfType& other) const { return !isEqual(other); }
879
880 difference_type operator-(const SelfType& other) const {
881 return other.computeDistance(*this);
882 }
883
886 Value key() const;
887
890 UInt index() const;
891
895 String name() const;
896
901 JSONCPP_DEPRECATED("Use `key = name();` instead.")
902 char const* memberName() const;
906 char const* memberName(char const** end) const;
907
908protected:
915 const Value& deref() const;
916 Value& deref();
917
918 void increment();
919
920 void decrement();
921
922 difference_type computeDistance(const SelfType& other) const;
923
924 bool isEqual(const SelfType& other) const;
925
926 void copy(const SelfType& other);
927
928private:
929 Value::ObjectValues::iterator current_;
930 // Indicates that iterator is for a null value.
931 bool isNull_{true};
932
933 public:
934 // For some reason, BORLAND needs these at the end, rather
935 // than earlier. No idea why.
936 ValueIteratorBase();
937 explicit ValueIteratorBase(const Value::ObjectValues::iterator& current);
938};
939
943 class JSON_API ValueConstIterator : public ValueIteratorBase {
944 friend class Value;
945
946 public:
947 using value_type = const Value;
948 // typedef unsigned int size_t;
949 // typedef int difference_type;
950 using reference = const Value&;
951 using pointer = const Value*;
952 using SelfType = ValueConstIterator;
953
954 ValueConstIterator();
955 ValueConstIterator(ValueIterator const& other);
956
957 private:
960 explicit ValueConstIterator(const Value::ObjectValues::iterator& current);
961
962 public:
963 SelfType& operator=(const ValueIteratorBase& other);
964
965 SelfType operator++(int) {
966 SelfType temp(*this);
967 ++*this;
968 return temp;
969 }
970
971 SelfType operator--(int) {
972 SelfType temp(*this);
973 --*this;
974 return temp;
975 }
976
977 SelfType& operator--() {
978 decrement();
979 return *this;
980 }
981
982 SelfType& operator++() {
983 increment();
984 return *this;
985 }
986
987 reference operator*() const { return deref(); }
988
989 pointer operator->() const { return &deref(); }
990};
991
994 class JSON_API ValueIterator : public ValueIteratorBase {
995 friend class Value;
996
997 public:
998 using value_type = Value;
999 using size_t = unsigned int;
1000 using difference_type = int;
1001 using reference = Value&;
1002 using pointer = Value*;
1003 using SelfType = ValueIterator;
1004
1005 ValueIterator();
1006 explicit ValueIterator(const ValueConstIterator& other);
1007 ValueIterator(const ValueIterator& other);
1008
1009 private:
1012 explicit ValueIterator(const Value::ObjectValues::iterator& current);
1013
1014 public:
1015 SelfType& operator=(const SelfType& other);
1016
1017 SelfType operator++(int) {
1018 SelfType temp(*this);
1019 ++*this;
1020 return temp;
1021 }
1022
1023 SelfType operator--(int) {
1024 SelfType temp(*this);
1025 --*this;
1026 return temp;
1027 }
1028
1029 SelfType& operator--() {
1030 decrement();
1031 return *this;
1032 }
1033
1034 SelfType& operator++() {
1035 increment();
1036 return *this;
1037 }
1038
1044 reference operator*() const { return const_cast<reference>(deref()); }
1045 pointer operator->() const { return const_cast<pointer>(&deref()); }
1046};
1047
1050 struct MemberProxy {
1051 const String name;
1052 Value& value;
1053};
1054
1058 struct ConstMemberProxy {
1059 const String name;
1060 const Value& value;
1061};
1062
1065 class ValueMembersIterator {
1066 public:
1067 using iterator_category = std::forward_iterator_tag;
1068 using value_type = MemberProxy;
1069 using difference_type = int;
1070 using pointer = MemberProxy*;
1071 using reference = MemberProxy;
1072
1073 ValueMembersIterator() = default;
1074 explicit ValueMembersIterator(ValueIterator const& iter) : it_(iter) {}
1075
1076 ValueMembersIterator& operator++() {
1077 ++it_;
1078 return *this;
1079 }
1080 ValueMembersIterator operator++(int) {
1081 ValueMembersIterator temp(*this);
1082 ++*this;
1083 return temp;
1084 }
1085 bool operator==(ValueMembersIterator const& other) const {
1086 return it_ == other.it_;
1087 }
1088 bool operator!=(ValueMembersIterator const& other) const {
1089 return it_ != other.it_;
1090 }
1091 MemberProxy operator*() const { return MemberProxy{it_.name(), *it_}; }
1092
1093 private:
1094 ValueIterator it_;
1095};
1096
1099 class ValueConstMembersIterator {
1100 public:
1101 using iterator_category = std::forward_iterator_tag;
1102 using value_type = ConstMemberProxy;
1103 using difference_type = int;
1104 using pointer = ConstMemberProxy*;
1105 using reference = ConstMemberProxy;
1106
1107 ValueConstMembersIterator() = default;
1108 explicit ValueConstMembersIterator(ValueConstIterator const& iter)
1109 : it_(iter) {}
1110
1111 ValueConstMembersIterator& operator++() {
1112 ++it_;
1113 return *this;
1114 }
1115 ValueConstMembersIterator operator++(int) {
1116 ValueConstMembersIterator temp(*this);
1117 ++*this;
1118 return temp;
1119 }
1120 bool operator==(ValueConstMembersIterator const& other) const {
1121 return it_ == other.it_;
1122 }
1123 bool operator!=(ValueConstMembersIterator const& other) const {
1124 return it_ != other.it_;
1125 }
1126 ConstMemberProxy operator*() const {
1127 return ConstMemberProxy{it_.name(), *it_};
1128 }
1129
1130 private:
1131 ValueConstIterator it_;
1132};
1133
1136 class ValueMembersView {
1137 public:
1138 ValueMembersView(ValueIterator begin, ValueIterator end)
1139 : begin_(begin), end_(end) {}
1140 ValueMembersIterator begin() const { return ValueMembersIterator(begin_); }
1141 ValueMembersIterator end() const { return ValueMembersIterator(end_); }
1142
1143 private:
1144 ValueIterator begin_;
1145 ValueIterator end_;
1146};
1147
1150 class ValueConstMembersView {
1151 public:
1152 ValueConstMembersView(ValueConstIterator begin, ValueConstIterator end)
1153 : begin_(begin), end_(end) {}
1154 ValueConstMembersIterator begin() const {
1155 return ValueConstMembersIterator(begin_);
1156 }
1157 ValueConstMembersIterator end() const {
1158 return ValueConstMembersIterator(end_);
1159 }
1160
1161 private:
1162 ValueConstIterator begin_;
1163 ValueConstIterator end_;
1164};
1165
1166 inline ValueMembersView Value::members() {
1167 return ValueMembersView(begin(), end());
1168}
1169 inline ValueConstMembersView Value::members() const {
1170 return ValueConstMembersView(begin(), end());
1171}
1172
1173 inline void swap(Value& a, Value& b) { a.swap(b); }
1174
1175 inline const Value& Value::front() const { return *begin(); }
1176
1177 inline Value& Value::front() { return *begin(); }
1178
1179 inline const Value& Value::back() const { return *(--end()); }
1180
1181 inline Value& Value::back() { return *(--end()); }
1182
1183} // namespace Json
1184
1185#pragma pack(pop)
1186
1187#if defined(JSONCPP_DISABLE_DLL_INTERFACE_WARNING)
1188#pragma warning(pop)
1189#endif // if defined(JSONCPP_DISABLE_DLL_INTERFACE_WARNING)
1190
1191#endif // JSON_H_INCLUDED
Json::Exception::what
char const * what() const noexcept override
Definition json_value.cpp:233
Json::Exception::~Exception
~Exception() noexcept override
Json::Exception::Exception
Exception(String msg)
Definition json_value.cpp:231
Json::Exception::msg_
String msg_
Definition value.h:88
Json::LogicError::LogicError
LogicError(String const &msg)
Definition json_value.cpp:235
Json::PathArgument
Experimental and untested: represents an element of the "path" to access a node.
Definition value.h:813
Json::PathArgument::PathArgument
PathArgument()
Json::PathArgument::Path
friend class Path
Definition value.h:815
Json::Path::Path
Path(const String &path, const PathArgument &a1=PathArgument(), const PathArgument &a2=PathArgument(), const PathArgument &a3=PathArgument(), const PathArgument &a4=PathArgument(), const PathArgument &a5=PathArgument())
Definition json_value.cpp:1619
Json::Path::make
Value & make(Value &root) const
Creates the "path" to access the specified node and returns a reference on the node.
Definition json_value.cpp:1722
Json::Path::resolve
const Value & resolve(const Value &root) const
Definition json_value.cpp:1679
Json::RuntimeError::RuntimeError
RuntimeError(String const &msg)
Definition json_value.cpp:234
Json::StaticString
Lightweight wrapper to tag static string.
Definition value.h:161
Json::StaticString::c_str
const char * c_str() const
Definition value.h:167
Json::StaticString::StaticString
StaticString(const char *czstring)
Definition value.h:163
Json::ValueConstIterator
const iterator for object and array value.
Definition value.h:943
Json::ValueConstIterator::ValueConstIterator
ValueConstIterator()
Json::ValueConstIterator::operator--
SelfType & operator--()
Definition value.h:977
Json::ValueConstIterator::operator->
pointer operator->() const
Definition value.h:989
Json::ValueConstIterator::reference
const Value & reference
Definition value.h:950
Json::ValueConstIterator::SelfType
ValueConstIterator SelfType
Definition value.h:952
Json::ValueConstIterator::operator--
SelfType operator--(int)
Definition value.h:971
Json::ValueConstIterator::operator++
SelfType & operator++()
Definition value.h:982
Json::ValueConstIterator::operator++
SelfType operator++(int)
Definition value.h:965
Json::ValueConstIterator::value_type
const Value value_type
Definition value.h:947
Json::ValueConstIterator::operator=
SelfType & operator=(const ValueIteratorBase &other)
Definition json_valueiterator.inl:126
Json::ValueConstIterator::operator*
reference operator*() const
Definition value.h:987
Json::ValueConstIterator::pointer
const Value * pointer
Definition value.h:951
Json::ValueConstIterator::Value
friend class Value
Definition value.h:944
Json::ValueConstMembersIterator
Iterator adapter for range-based for loops.
Definition value.h:1099
Json::ValueConstMembersIterator::pointer
ConstMemberProxy * pointer
Definition value.h:1104
Json::ValueConstMembersIterator::operator!=
bool operator!=(ValueConstMembersIterator const &other) const
Definition value.h:1123
Json::ValueConstMembersIterator::iterator_category
std::forward_iterator_tag iterator_category
Definition value.h:1101
Json::ValueConstMembersIterator::ValueConstMembersIterator
ValueConstMembersIterator(ValueConstIterator const &iter)
Definition value.h:1108
Json::ValueConstMembersIterator::reference
ConstMemberProxy reference
Definition value.h:1105
Json::ValueConstMembersIterator::operator==
bool operator==(ValueConstMembersIterator const &other) const
Definition value.h:1120
Json::ValueConstMembersIterator::operator++
ValueConstMembersIterator operator++(int)
Definition value.h:1115
Json::ValueConstMembersIterator::ValueConstMembersIterator
ValueConstMembersIterator()=default
Json::ValueConstMembersIterator::value_type
ConstMemberProxy value_type
Definition value.h:1102
Json::ValueConstMembersIterator::difference_type
int difference_type
Definition value.h:1103
Json::ValueConstMembersIterator::operator++
ValueConstMembersIterator & operator++()
Definition value.h:1111
Json::ValueConstMembersIterator::operator*
ConstMemberProxy operator*() const
Definition value.h:1126
Json::ValueConstMembersView
Range-based for loop adapter for object members.
Definition value.h:1150
Json::ValueConstMembersView::ValueConstMembersView
ValueConstMembersView(ValueConstIterator begin, ValueConstIterator end)
Definition value.h:1152
Json::ValueConstMembersView::begin
ValueConstMembersIterator begin() const
Definition value.h:1154
Json::ValueConstMembersView::end
ValueConstMembersIterator end() const
Definition value.h:1157
Json::Value
Represents a JSON value.
Definition value.h:207
Json::Value::begin
const_iterator begin() const
Definition json_value.cpp:1552
Json::Value::get
Value get(ArrayIndex 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:1129
Json::Value::empty
bool empty() const
Return true if empty array, empty object, or null; otherwise, false.
Definition json_value.cpp:932
Json::Value::Value
Value(std::nullptr_t ptr)=delete
Json::Value::maxLargestInt
static constexpr LargestInt maxLargestInt
Maximum signed integer value that can be stored in a Json::Value.
Definition value.h:241
Json::Value::ArrayIndex
Json::ArrayIndex ArrayIndex
Definition value.h:223
Json::Value::asUInt64
UInt64 asUInt64() const
Definition json_value.cpp:771
Json::Value::size
ArrayIndex size() const
Number of values in array or object.
Definition json_value.cpp:909
Json::Value::UInt
Json::UInt UInt
Definition value.h:215
Json::Value::asCString
const char * asCString() const
Embedded zeroes could cause you trouble!
Definition json_value.cpp:635
Json::Value::operator==
bool operator==(const Value &other) const
Definition json_value.cpp:591
Json::Value::maxInt64
static constexpr Int64 maxInt64
Maximum signed 64 bits int value that can be stored in a Json::Value.
Definition value.h:256
Json::Value::copy
void copy(const Value &other)
copy everything.
Definition json_value.cpp:518
Json::Value::null
static const Value & null
Definition value.h:230
Json::Value::setComment
void setComment(const char *comment, size_t len, CommentPlacement placement)
Comments must be //... or /* ... *‍/.
Definition value.h:668
Json::Value::getString
bool getString(char const **begin, char const **end) const
Get raw char* of string-value.
Definition json_value.cpp:661
Json::Value::as
T as() const =delete
The as<T> and is<T> member function templates and specializations.
Json::Value::maxUInt64AsDouble
static constexpr double maxUInt64AsDouble
Definition value.h:265
Json::Value::Members
std::vector< String > Members
Definition value.h:212
Json::Value::end
const_iterator end() const
Definition json_value.cpp:1565
Json::Value::operator<=
bool operator<=(const Value &other) const
Definition json_value.cpp:585
Json::Value::front
const Value & front() const
Returns a reference to the first element in the Value.
Definition value.h:1175
Json::Value::operator>
bool operator>(const Value &other) const
Definition json_value.cpp:589
Json::Value::isDouble
bool isDouble() const
Definition json_value.cpp:1469
Json::Value::isInt64
bool isInt64() const
Definition json_value.cpp:1399
Json::Value::clear
void clear()
Remove all object members and array elements.
Definition json_value.cpp:940
Json::Value::asString
String asString() const
Embedded zeroes are possible.
Definition json_value.cpp:673
Json::Value::swapPayload
void swapPayload(Value &other)
Swap values but leave comments and source offsets in place.
Definition json_value.cpp:501
Json::Value::placement
CommentPlacement placement
Definition value.h:664
Json::Value::asInt
Int asInt() const
Definition json_value.cpp:699
Json::Value::iterator
ValueIterator iterator
Definition value.h:213
Json::Value::LargestInt
Json::LargestInt LargestInt
Definition value.h:221
Json::Value::LargestUInt
Json::LargestUInt LargestUInt
Definition value.h:222
Json::Value::const_iterator
ValueConstIterator const_iterator
Definition value.h:214
Json::Value::isString
bool isString() const
Definition json_value.cpp:1475
Json::Value::asUInt
UInt asUInt() const
Definition json_value.cpp:721
Json::Value::UInt64
Json::UInt64 UInt64
Definition value.h:218
Json::Value::resize
void resize(ArrayIndex newSize)
Resize the array to newSize elements.
Definition json_value.cpp:956
Json::Value::operator[]
Value & operator[](ArrayIndex index)
Definition json_value.cpp:977
Json::Value::append
Value & append(const Value &value)
Append value to array at the end.
Definition json_value.cpp:1221
Json::Value::operator!=
bool operator!=(const Value &other) const
Definition json_value.cpp:633
Json::Value::isUInt64
bool isUInt64() const
Definition json_value.cpp:1423
Json::Value::findValue
Value const * findValue(const String &key) const
Calls find and only returns a valid pointer if the type is found.
Definition value.h:576
Json::Value::type
ValueType type() const
Definition json_value.cpp:523
Json::Value::members
ValueMembersView members()
Definition value.h:1166
Json::Value::back
const Value & back() const
Returns a reference to the last element in the Value.
Definition value.h:1179
Json::Value::asInt64
Int64 asInt64() const
Definition json_value.cpp:745
Json::Value::swap
void swap(Value &other)
Swap everything.
Definition json_value.cpp:511
Json::Value::operator<
bool operator<(const Value &other) const
Compare payload only, not comments etc.
Definition json_value.cpp:535
Json::Value::Int
Json::Int Int
Definition value.h:216
Json::Value::nullRef
static const Value & nullRef
Definition value.h:231
Json::Value::isBool
bool isBool() const
Definition json_value.cpp:1355
Json::Value::copyPayload
void copyPayload(const Value &other)
copy values but leave comments and source offsets in place.
Definition json_value.cpp:506
Json::Value::maxInt
static constexpr Int maxInt
Maximum signed int value that can be stored in a Json::Value.
Definition value.h:248
Json::Value::asBool
bool asBool() const
Definition json_value.cpp:854
Json::Value::isUInt
bool isUInt() const
Definition json_value.cpp:1376
Json::Value::maxLargestUInt
static constexpr LargestUInt maxLargestUInt
Maximum unsigned integer value that can be stored in a Json::Value.
Definition value.h:243
Json::Value::isValidIndex
bool isValidIndex(ArrayIndex index) const
Return true if index < size().
Definition json_value.cpp:1134
Json::Value::ValueIteratorBase
friend class ValueIteratorBase
Definition value.h:208
Json::Value::Int64
Json::Int64 Int64
Definition value.h:219
Json::Value::Value
Value(ValueType type=nullValue)
Create a default Value of the given type.
Definition json_value.cpp:392
Json::Value::maxUInt64
static constexpr UInt64 maxUInt64
Maximum unsigned 64 bits int value that can be stored in a Json::Value.
Definition value.h:258
Json::Value::operator=
Value & operator=(const Value &other)
Definition json_value.cpp:491
Json::Value::minInt
static constexpr Int minInt
Minimum signed int value that can be stored in a Json::Value.
Definition value.h:246
Json::Value::insert
bool insert(ArrayIndex index, const Value &newValue)
Insert value in array at specific index.
Definition json_value.cpp:1233
Json::Value::minInt64
static constexpr Int64 minInt64
Minimum signed 64 bits int value that can be stored in a Json::Value.
Definition value.h:254
Json::Value::value_type
std::string value_type
Definition value.h:226
Json::Value::is
bool is() const =delete
Json::Value::compare
int compare(const Value &other) const
Definition json_value.cpp:527
Json::Value::minLargestInt
static constexpr LargestInt minLargestInt
Minimum signed integer value that can be stored in a Json::Value.
Definition value.h:238
Json::Value::isConvertibleTo
bool isConvertibleTo(ValueType other) const
Definition json_value.cpp:875
Json::Value::nullSingleton
static Value const & nullSingleton()
Definition json_value.cpp:70
Json::Value::asFloat
float asFloat() const
Definition json_value.cpp:831
Json::Value::find
Value const * find(char const *begin, char const *end) const
Most general and efficient version of isMember()const, get()const, and operator[]const.
Definition json_value.cpp:1136
Json::Value::asDouble
double asDouble() const
Definition json_value.cpp:809
Json::Value::operator>=
bool operator>=(const Value &other) const
Definition json_value.cpp:587
Json::Value::maxUInt
static constexpr UInt maxUInt
Maximum unsigned int value that can be stored in a Json::Value.
Definition value.h:250
Json::Value::isInt
bool isInt() const
Definition json_value.cpp:1357
Json::ValueIteratorBase
base class for Value iterators.
Definition value.h:869
Json::ValueIteratorBase::isEqual
bool isEqual(const SelfType &other) const
Definition json_valueiterator.inl:55
Json::ValueIteratorBase::operator==
bool operator==(const SelfType &other) const
Definition value.h:876
Json::ValueIteratorBase::memberName
char const * memberName(char const **end) const
Return the member name of the referenced Value, or NULL if it is not an objectValue.
Definition json_valueiterator.inl:93
Json::ValueIteratorBase::size_t
unsigned int size_t
Definition value.h:872
Json::ValueIteratorBase::copy
void copy(const SelfType &other)
Definition json_valueiterator.inl:62
Json::ValueIteratorBase::iterator_category
std::bidirectional_iterator_tag iterator_category
Definition value.h:871
Json::ValueIteratorBase::operator-
difference_type operator-(const SelfType &other) const
Definition value.h:880
Json::ValueIteratorBase::operator!=
bool operator!=(const SelfType &other) const
Definition value.h:878
Json::ValueIteratorBase::deref
const Value & deref() const
Definition json_valueiterator.inl:25
Json::ValueIteratorBase::SelfType
ValueIteratorBase SelfType
Definition value.h:874
Json::ValueIteratorBase::computeDistance
difference_type computeDistance(const SelfType &other) const
Definition json_valueiterator.inl:32
Json::ValueIteratorBase::ValueIteratorBase
ValueIteratorBase()
Definition json_valueiterator.inl:18
Json::ValueIteratorBase::increment
void increment()
Definition json_valueiterator.inl:27
Json::ValueIteratorBase::difference_type
int difference_type
Definition value.h:873
Json::ValueIteratorBase::decrement
void decrement()
Definition json_valueiterator.inl:29
Json::ValueIterator
Iterator for object and array value.
Definition value.h:994
Json::ValueIterator::operator--
SelfType operator--(int)
Definition value.h:1023
Json::ValueIterator::ValueIterator
ValueIterator()
Json::ValueIterator::operator--
SelfType & operator--()
Definition value.h:1029
Json::ValueIterator::pointer
Value * pointer
Definition value.h:1002
Json::ValueIterator::operator*
reference operator*() const
Definition value.h:1044
Json::ValueIterator::operator++
SelfType & operator++()
Definition value.h:1034
Json::ValueIterator::SelfType
ValueIterator SelfType
Definition value.h:1003
Json::ValueIterator::size_t
unsigned int size_t
Definition value.h:999
Json::ValueIterator::reference
Value & reference
Definition value.h:1001
Json::ValueIterator::difference_type
int difference_type
Definition value.h:1000
Json::ValueIterator::ValueIterator
ValueIterator(const ValueIterator &other)
Json::ValueIterator::operator->
pointer operator->() const
Definition value.h:1045
Json::ValueIterator::operator=
SelfType & operator=(const SelfType &other)
Definition json_valueiterator.inl:151
Json::ValueIterator::value_type
Value value_type
Definition value.h:998
Json::ValueIterator::operator++
SelfType operator++(int)
Definition value.h:1017
Json::ValueIterator::Value
friend class Value
Definition value.h:995
Json::ValueMembersIterator
Iterator adapter for range-based for loops.
Definition value.h:1065
Json::ValueMembersIterator::reference
MemberProxy reference
Definition value.h:1071
Json::ValueMembersIterator::operator!=
bool operator!=(ValueMembersIterator const &other) const
Definition value.h:1088
Json::ValueMembersIterator::operator++
ValueMembersIterator & operator++()
Definition value.h:1076
Json::ValueMembersIterator::pointer
MemberProxy * pointer
Definition value.h:1070
Json::ValueMembersIterator::value_type
MemberProxy value_type
Definition value.h:1068
Json::ValueMembersIterator::operator++
ValueMembersIterator operator++(int)
Definition value.h:1080
Json::ValueMembersIterator::difference_type
int difference_type
Definition value.h:1069
Json::ValueMembersIterator::ValueMembersIterator
ValueMembersIterator()=default
Json::ValueMembersIterator::operator==
bool operator==(ValueMembersIterator const &other) const
Definition value.h:1085
Json::ValueMembersIterator::iterator_category
std::forward_iterator_tag iterator_category
Definition value.h:1067
Json::ValueMembersIterator::ValueMembersIterator
ValueMembersIterator(ValueIterator const &iter)
Definition value.h:1074
Json::ValueMembersIterator::operator*
MemberProxy operator*() const
Definition value.h:1091
Json::ValueMembersView
Range-based for loop adapter for object members.
Definition value.h:1136
Json::ValueMembersView::begin
ValueMembersIterator begin() const
Definition value.h:1140
Json::ValueMembersView::ValueMembersView
ValueMembersView(ValueIterator begin, ValueIterator end)
Definition value.h:1138
Json::ValueMembersView::end
ValueMembersIterator end() const
Definition value.h:1141
JSON_API
#define JSON_API
If defined, indicates that the source file is amalgamated to prevent private header inclusion.
Definition config.h:50
JSONCPP_DEPRECATED
#define JSONCPP_DEPRECATED(message)
Definition config.h:89
forwards.h
Json
JSON (JavaScript Object Notation).
Definition allocator.h:16
Json::UInt64
unsigned __int64 UInt64
Definition config.h:119
Json::ArrayIndex
unsigned int ArrayIndex
Definition forwards.h:32
Json::Int64
__int64 Int64
Definition config.h:118
Json::LargestInt
Int64 LargestInt
Definition config.h:124
Json::CommentPlacement
CommentPlacement
Definition value.h:132
Json::commentAfterOnSameLine
@ commentAfterOnSameLine
a comment just after a value on the same line
Definition value.h:134
Json::commentBefore
@ commentBefore
a comment placed on the line before a value
Definition value.h:133
Json::numberOfCommentPlacement
@ numberOfCommentPlacement
root value)
Definition value.h:137
Json::commentAfter
@ commentAfter
a comment on the line after a value (only make sense for
Definition value.h:135
Json::UInt
unsigned int UInt
Definition config.h:110
Json::ValueType
ValueType
Type of the value held by a Value object.
Definition value.h:121
Json::booleanValue
@ booleanValue
bool value
Definition value.h:127
Json::nullValue
@ nullValue
'null' value
Definition value.h:122
Json::stringValue
@ stringValue
UTF-8 string value.
Definition value.h:126
Json::realValue
@ realValue
double value
Definition value.h:125
Json::arrayValue
@ arrayValue
array value (ordered list)
Definition value.h:128
Json::intValue
@ intValue
signed integer value
Definition value.h:123
Json::objectValue
@ objectValue
object value (collection of name/value pairs).
Definition value.h:129
Json::uintValue
@ uintValue
unsigned integer value
Definition value.h:124
Json::Int
int Int
Definition config.h:109
Json::String
std::basic_string< char, std::char_traits< char >, Allocator< char > > String
Definition config.h:135
Json::LargestUInt
UInt64 LargestUInt
Definition config.h:125
Json::PrecisionType
PrecisionType
Type of precision for formatting of real values.
Definition value.h:142
Json::decimalPlaces
@ decimalPlaces
we set max number of digits after "." in string
Definition value.h:144
Json::significantDigits
@ significantDigits
we set max number of significant digits in string
Definition value.h:143
Json::swap
void swap(Value &a, Value &b)
Definition value.h:1173
Json::ConstMemberProxy
Proxy struct to enable range-based for loops over const object members.
Definition value.h:1058
Json::ConstMemberProxy::name
const String name
Definition value.h:1059
Json::ConstMemberProxy::value
const Value & value
Definition value.h:1060
Json::MemberProxy
Proxy struct to enable range-based for loops over object members.
Definition value.h:1050
Json::MemberProxy::name
const String name
Definition value.h:1051
Json::MemberProxy::value
Value & value
Definition value.h:1052
JSONCPP_TEMPLATE_DELETE
#define JSONCPP_TEMPLATE_DELETE
Definition value.h:38
JSONCPP_NORETURN
#define JSONCPP_NORETURN
Definition value.h:18