Devin Jeanpierre | 59e4d35 | 2017-07-21 03:44:36 -0700 | [diff] [blame] | 1 | // Copyright 2007-2010 Baptiste Lepilleur and The JsonCpp Authors |
Baptiste Lepilleur | 7469f1d | 2010-04-20 21:35:19 +0000 | [diff] [blame] | 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 | |
Christopher Dunn | 6d135cb | 2007-06-13 15:51:04 +0000 | [diff] [blame] | 6 | #ifndef CPPTL_JSON_H_INCLUDED |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 7 | #define CPPTL_JSON_H_INCLUDED |
Christopher Dunn | 6d135cb | 2007-06-13 15:51:04 +0000 | [diff] [blame] | 8 | |
Baptiste Lepilleur | eadc478 | 2011-05-02 21:09:30 +0000 | [diff] [blame] | 9 | #if !defined(JSON_IS_AMALGAMATION) |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 10 | #include "forwards.h" |
Baptiste Lepilleur | eadc478 | 2011-05-02 21:09:30 +0000 | [diff] [blame] | 11 | #endif // if !defined(JSON_IS_AMALGAMATION) |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 12 | #include <string> |
| 13 | #include <vector> |
Christopher Dunn | 75279cc | 2015-03-08 12:20:06 -0500 | [diff] [blame] | 14 | #include <exception> |
Christopher Dunn | 6d135cb | 2007-06-13 15:51:04 +0000 | [diff] [blame] | 15 | |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 16 | #ifndef JSON_USE_CPPTL_SMALLMAP |
| 17 | #include <map> |
| 18 | #else |
| 19 | #include <cpptl/smallmap.h> |
| 20 | #endif |
| 21 | #ifdef JSON_USE_CPPTL |
| 22 | #include <cpptl/forwards.h> |
| 23 | #endif |
Christopher Dunn | 6d135cb | 2007-06-13 15:51:04 +0000 | [diff] [blame] | 24 | |
Gaurav | d97ea5b | 2016-03-16 11:15:09 +0530 | [diff] [blame] | 25 | //Conditional NORETURN attribute on the throw functions would: |
Dhruv Paranjape | 8996c37 | 2017-07-08 17:27:07 +0530 | [diff] [blame] | 26 | // a) suppress false positives from static code analysis |
Gaurav | d97ea5b | 2016-03-16 11:15:09 +0530 | [diff] [blame] | 27 | // b) possibly improve optimization opportunities. |
| 28 | #if !defined(JSONCPP_NORETURN) |
| 29 | # if defined(_MSC_VER) |
| 30 | # define JSONCPP_NORETURN __declspec(noreturn) |
| 31 | # elif defined(__GNUC__) |
| 32 | # define JSONCPP_NORETURN __attribute__ ((__noreturn__)) |
| 33 | # else |
| 34 | # define JSONCPP_NORETURN |
| 35 | # endif |
| 36 | #endif |
| 37 | |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 38 | // Disable warning C4251: <data member>: <type> needs to have dll-interface to |
| 39 | // be used by... |
Baptiste Lepilleur | eafd702 | 2013-05-08 20:21:11 +0000 | [diff] [blame] | 40 | #if defined(JSONCPP_DISABLE_DLL_INTERFACE_WARNING) |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 41 | #pragma warning(push) |
| 42 | #pragma warning(disable : 4251) |
Baptiste Lepilleur | eafd702 | 2013-05-08 20:21:11 +0000 | [diff] [blame] | 43 | #endif // if defined(JSONCPP_DISABLE_DLL_INTERFACE_WARNING) |
| 44 | |
Sergiy80 | d6e666f | 2016-12-03 22:29:14 +0200 | [diff] [blame] | 45 | #pragma pack(push, 8) |
| 46 | |
Christopher Dunn | 6d135cb | 2007-06-13 15:51:04 +0000 | [diff] [blame] | 47 | /** \brief JSON (JavaScript Object Notation). |
| 48 | */ |
| 49 | namespace Json { |
| 50 | |
Christopher Dunn | 75279cc | 2015-03-08 12:20:06 -0500 | [diff] [blame] | 51 | /** Base class for all exceptions we throw. |
Christopher Dunn | 4e30c4f | 2015-03-08 12:56:32 -0500 | [diff] [blame] | 52 | * |
| 53 | * We use nothing but these internally. Of course, STL can throw others. |
Christopher Dunn | 75279cc | 2015-03-08 12:20:06 -0500 | [diff] [blame] | 54 | */ |
Christopher Dunn | 949babd | 2015-07-23 00:19:12 -0500 | [diff] [blame] | 55 | class JSON_API Exception : public std::exception { |
| 56 | public: |
Christopher Dunn | de5b792 | 2016-03-06 11:19:46 -0600 | [diff] [blame] | 57 | Exception(JSONCPP_STRING const& msg); |
Omkar Wagh | 91c1d23 | 2016-11-07 13:57:00 -0500 | [diff] [blame] | 58 | ~Exception() JSONCPP_NOEXCEPT JSONCPP_OVERRIDE; |
| 59 | char const* what() const JSONCPP_NOEXCEPT JSONCPP_OVERRIDE; |
Christopher Dunn | 949babd | 2015-07-23 00:19:12 -0500 | [diff] [blame] | 60 | protected: |
Christopher Dunn | de5b792 | 2016-03-06 11:19:46 -0600 | [diff] [blame] | 61 | JSONCPP_STRING msg_; |
Christopher Dunn | 949babd | 2015-07-23 00:19:12 -0500 | [diff] [blame] | 62 | }; |
| 63 | |
Christopher Dunn | 5383794 | 2015-03-08 12:31:00 -0500 | [diff] [blame] | 64 | /** Exceptions which the user cannot easily avoid. |
| 65 | * |
Christopher Dunn | 4e30c4f | 2015-03-08 12:56:32 -0500 | [diff] [blame] | 66 | * E.g. out-of-memory (when we use malloc), stack-overflow, malicious input |
Dhruv Paranjape | 8996c37 | 2017-07-08 17:27:07 +0530 | [diff] [blame] | 67 | * |
Christopher Dunn | 4e30c4f | 2015-03-08 12:56:32 -0500 | [diff] [blame] | 68 | * \remark derived from Json::Exception |
Christopher Dunn | 5383794 | 2015-03-08 12:31:00 -0500 | [diff] [blame] | 69 | */ |
Christopher Dunn | 949babd | 2015-07-23 00:19:12 -0500 | [diff] [blame] | 70 | class JSON_API RuntimeError : public Exception { |
| 71 | public: |
Christopher Dunn | de5b792 | 2016-03-06 11:19:46 -0600 | [diff] [blame] | 72 | RuntimeError(JSONCPP_STRING const& msg); |
Christopher Dunn | 949babd | 2015-07-23 00:19:12 -0500 | [diff] [blame] | 73 | }; |
| 74 | |
Christopher Dunn | 4e30c4f | 2015-03-08 12:56:32 -0500 | [diff] [blame] | 75 | /** Exceptions thrown by JSON_ASSERT/JSON_FAIL macros. |
Christopher Dunn | 5383794 | 2015-03-08 12:31:00 -0500 | [diff] [blame] | 76 | * |
| 77 | * These are precondition-violations (user bugs) and internal errors (our bugs). |
Dhruv Paranjape | 8996c37 | 2017-07-08 17:27:07 +0530 | [diff] [blame] | 78 | * |
Christopher Dunn | 4e30c4f | 2015-03-08 12:56:32 -0500 | [diff] [blame] | 79 | * \remark derived from Json::Exception |
Christopher Dunn | 5383794 | 2015-03-08 12:31:00 -0500 | [diff] [blame] | 80 | */ |
Christopher Dunn | 949babd | 2015-07-23 00:19:12 -0500 | [diff] [blame] | 81 | class JSON_API LogicError : public Exception { |
| 82 | public: |
Christopher Dunn | de5b792 | 2016-03-06 11:19:46 -0600 | [diff] [blame] | 83 | LogicError(JSONCPP_STRING const& msg); |
Christopher Dunn | 949babd | 2015-07-23 00:19:12 -0500 | [diff] [blame] | 84 | }; |
Christopher Dunn | 5383794 | 2015-03-08 12:31:00 -0500 | [diff] [blame] | 85 | |
Christopher Dunn | 4e30c4f | 2015-03-08 12:56:32 -0500 | [diff] [blame] | 86 | /// used internally |
Gaurav | d97ea5b | 2016-03-16 11:15:09 +0530 | [diff] [blame] | 87 | JSONCPP_NORETURN void throwRuntimeError(JSONCPP_STRING const& msg); |
Christopher Dunn | 4e30c4f | 2015-03-08 12:56:32 -0500 | [diff] [blame] | 88 | /// used internally |
Gaurav | d97ea5b | 2016-03-16 11:15:09 +0530 | [diff] [blame] | 89 | JSONCPP_NORETURN void throwLogicError(JSONCPP_STRING const& msg); |
Christopher Dunn | 75279cc | 2015-03-08 12:20:06 -0500 | [diff] [blame] | 90 | |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 91 | /** \brief Type of the value held by a Value object. |
| 92 | */ |
| 93 | enum ValueType { |
| 94 | nullValue = 0, ///< 'null' value |
| 95 | intValue, ///< signed integer value |
| 96 | uintValue, ///< unsigned integer value |
| 97 | realValue, ///< double value |
| 98 | stringValue, ///< UTF-8 string value |
| 99 | booleanValue, ///< bool value |
| 100 | arrayValue, ///< array value (ordered list) |
| 101 | objectValue ///< object value (collection of name/value pairs). |
| 102 | }; |
Christopher Dunn | 6d135cb | 2007-06-13 15:51:04 +0000 | [diff] [blame] | 103 | |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 104 | enum CommentPlacement { |
| 105 | commentBefore = 0, ///< a comment placed on the line before a value |
| 106 | commentAfterOnSameLine, ///< a comment just after a value on the same line |
| 107 | commentAfter, ///< a comment on the line after a value (only make sense for |
Aaron Jacobs | 3a0c4fc | 2014-07-01 09:20:48 +1000 | [diff] [blame] | 108 | /// root value) |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 109 | numberOfCommentPlacement |
| 110 | }; |
Christopher Dunn | 6d135cb | 2007-06-13 15:51:04 +0000 | [diff] [blame] | 111 | |
| 112 | //# ifdef JSON_USE_CPPTL |
| 113 | // typedef CppTL::AnyEnumerator<const char *> EnumMemberNames; |
| 114 | // typedef CppTL::AnyEnumerator<const Value &> EnumValues; |
| 115 | //# endif |
| 116 | |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 117 | /** \brief Lightweight wrapper to tag static string. |
| 118 | * |
Josh Soref | e6a588a | 2017-12-03 11:54:29 -0500 | [diff] [blame] | 119 | * Value constructor and objectValue member assignment takes advantage of the |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 120 | * StaticString and avoid the cost of string duplication when storing the |
| 121 | * string or the member name. |
| 122 | * |
| 123 | * Example of usage: |
| 124 | * \code |
| 125 | * Json::Value aValue( StaticString("some text") ); |
| 126 | * Json::Value object; |
| 127 | * static const StaticString code("code"); |
| 128 | * object[code] = 1234; |
| 129 | * \endcode |
| 130 | */ |
| 131 | class JSON_API StaticString { |
| 132 | public: |
Christopher Dunn | ff61752 | 2015-03-06 10:31:46 -0600 | [diff] [blame] | 133 | explicit StaticString(const char* czstring) : c_str_(czstring) {} |
Christopher Dunn | 6d135cb | 2007-06-13 15:51:04 +0000 | [diff] [blame] | 134 | |
Christopher Dunn | ff61752 | 2015-03-06 10:31:46 -0600 | [diff] [blame] | 135 | operator const char*() const { return c_str_; } |
Christopher Dunn | 6d135cb | 2007-06-13 15:51:04 +0000 | [diff] [blame] | 136 | |
Christopher Dunn | ff61752 | 2015-03-06 10:31:46 -0600 | [diff] [blame] | 137 | const char* c_str() const { return c_str_; } |
Christopher Dunn | 6d135cb | 2007-06-13 15:51:04 +0000 | [diff] [blame] | 138 | |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 139 | private: |
Christopher Dunn | ff61752 | 2015-03-06 10:31:46 -0600 | [diff] [blame] | 140 | const char* c_str_; |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 141 | }; |
Christopher Dunn | 6d135cb | 2007-06-13 15:51:04 +0000 | [diff] [blame] | 142 | |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 143 | /** \brief Represents a <a HREF="http://www.json.org">JSON</a> value. |
| 144 | * |
| 145 | * This class is a discriminated union wrapper that can represents a: |
| 146 | * - signed integer [range: Value::minInt - Value::maxInt] |
| 147 | * - unsigned integer (range: 0 - Value::maxUInt) |
| 148 | * - double |
| 149 | * - UTF-8 string |
| 150 | * - boolean |
| 151 | * - 'null' |
| 152 | * - an ordered list of Value |
| 153 | * - collection of name/value pairs (javascript object) |
| 154 | * |
| 155 | * The type of the held value is represented by a #ValueType and |
| 156 | * can be obtained using type(). |
| 157 | * |
Christopher Dunn | c28610f | 2015-02-21 11:44:16 -0600 | [diff] [blame] | 158 | * Values of an #objectValue or #arrayValue can be accessed using operator[]() |
| 159 | * methods. |
| 160 | * Non-const methods will automatically create the a #nullValue element |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 161 | * if it does not exist. |
Christopher Dunn | c28610f | 2015-02-21 11:44:16 -0600 | [diff] [blame] | 162 | * The sequence of an #arrayValue will be automatically resized and initialized |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 163 | * with #nullValue. resize() can be used to enlarge or truncate an #arrayValue. |
| 164 | * |
Christopher Dunn | c28610f | 2015-02-21 11:44:16 -0600 | [diff] [blame] | 165 | * The get() methods can be used to obtain default value in the case the |
| 166 | * required element does not exist. |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 167 | * |
| 168 | * It is possible to iterate over the list of a #objectValue values using |
| 169 | * the getMemberNames() method. |
Christopher Dunn | 25342ba | 2015-03-02 18:05:26 -0600 | [diff] [blame] | 170 | * |
Christopher Dunn | c28610f | 2015-02-21 11:44:16 -0600 | [diff] [blame] | 171 | * \note #Value string-length fit in size_t, but keys must be < 2^30. |
| 172 | * (The reason is an implementation detail.) A #CharReader will raise an |
Christopher Dunn | 25342ba | 2015-03-02 18:05:26 -0600 | [diff] [blame] | 173 | * exception if a bound is exceeded to avoid security holes in your app, |
| 174 | * but the Value API does *not* check bounds. That is the responsibility |
| 175 | * of the caller. |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 176 | */ |
| 177 | class JSON_API Value { |
| 178 | friend class ValueIteratorBase; |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 179 | public: |
Christopher Dunn | de5b792 | 2016-03-06 11:19:46 -0600 | [diff] [blame] | 180 | typedef std::vector<JSONCPP_STRING> Members; |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 181 | typedef ValueIterator iterator; |
| 182 | typedef ValueConstIterator const_iterator; |
| 183 | typedef Json::UInt UInt; |
| 184 | typedef Json::Int Int; |
| 185 | #if defined(JSON_HAS_INT64) |
| 186 | typedef Json::UInt64 UInt64; |
| 187 | typedef Json::Int64 Int64; |
Baptiste Lepilleur | 842e9ac | 2010-12-27 17:45:23 +0000 | [diff] [blame] | 188 | #endif // defined(JSON_HAS_INT64) |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 189 | typedef Json::LargestInt LargestInt; |
| 190 | typedef Json::LargestUInt LargestUInt; |
| 191 | typedef Json::ArrayIndex ArrayIndex; |
Christopher Dunn | 6d135cb | 2007-06-13 15:51:04 +0000 | [diff] [blame] | 192 | |
Wolfram Rösler | ff6b449 | 2017-09-14 09:31:36 +0200 | [diff] [blame] | 193 | // Required for boost integration, e. g. BOOST_TEST |
| 194 | typedef std::string value_type; |
| 195 | |
Christopher Dunn | 8a70297 | 2015-03-03 10:38:27 -0600 | [diff] [blame] | 196 | static const Value& null; ///< We regret this reference to a global instance; prefer the simpler Value(). |
| 197 | static const Value& nullRef; ///< just a kludge for binary-compatibility; same as null |
Christopher Dunn | 0f288ae | 2016-06-26 18:47:43 -0500 | [diff] [blame] | 198 | static Value const& nullSingleton(); ///< Prefer this to null or nullRef. |
| 199 | |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 200 | /// Minimum signed integer value that can be stored in a Json::Value. |
| 201 | static const LargestInt minLargestInt; |
| 202 | /// Maximum signed integer value that can be stored in a Json::Value. |
| 203 | static const LargestInt maxLargestInt; |
| 204 | /// Maximum unsigned integer value that can be stored in a Json::Value. |
| 205 | static const LargestUInt maxLargestUInt; |
Baptiste Lepilleur | 842e9ac | 2010-12-27 17:45:23 +0000 | [diff] [blame] | 206 | |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 207 | /// Minimum signed int value that can be stored in a Json::Value. |
| 208 | static const Int minInt; |
| 209 | /// Maximum signed int value that can be stored in a Json::Value. |
| 210 | static const Int maxInt; |
| 211 | /// Maximum unsigned int value that can be stored in a Json::Value. |
| 212 | static const UInt maxUInt; |
Christopher Dunn | 6d135cb | 2007-06-13 15:51:04 +0000 | [diff] [blame] | 213 | |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 214 | #if defined(JSON_HAS_INT64) |
| 215 | /// Minimum signed 64 bits int value that can be stored in a Json::Value. |
| 216 | static const Int64 minInt64; |
| 217 | /// Maximum signed 64 bits int value that can be stored in a Json::Value. |
| 218 | static const Int64 maxInt64; |
| 219 | /// Maximum unsigned 64 bits int value that can be stored in a Json::Value. |
| 220 | static const UInt64 maxUInt64; |
Aaron Jacobs | f1053e7 | 2011-05-24 03:18:02 +0000 | [diff] [blame] | 221 | #endif // defined(JSON_HAS_INT64) |
Baptiste Lepilleur | 842e9ac | 2010-12-27 17:45:23 +0000 | [diff] [blame] | 222 | |
Darcy Beurle | 798f6ba | 2017-12-22 22:48:20 +0100 | [diff] [blame^] | 223 | // Workaround for bug in the NVIDIAs CUDA 9.1 nvcc compiler |
| 224 | // when using gcc and clang backend compilers. CZString |
| 225 | // cannot be defined as private. See issue #486 |
| 226 | #ifdef __NVCC__ |
| 227 | public: |
| 228 | #else |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 229 | private: |
Darcy Beurle | 798f6ba | 2017-12-22 22:48:20 +0100 | [diff] [blame^] | 230 | #endif |
Christopher Dunn | 6d135cb | 2007-06-13 15:51:04 +0000 | [diff] [blame] | 231 | #ifndef JSONCPP_DOC_EXCLUDE_IMPLEMENTATION |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 232 | class CZString { |
| 233 | public: |
| 234 | enum DuplicationPolicy { |
| 235 | noDuplication = 0, |
| 236 | duplicate, |
| 237 | duplicateOnCopy |
| 238 | }; |
| 239 | CZString(ArrayIndex index); |
Christopher Dunn | c28610f | 2015-02-21 11:44:16 -0600 | [diff] [blame] | 240 | CZString(char const* str, unsigned length, DuplicationPolicy allocate); |
| 241 | CZString(CZString const& other); |
Motti | 2b00891 | 2015-04-20 17:44:47 +0300 | [diff] [blame] | 242 | #if JSON_HAS_RVALUE_REFERENCES |
| 243 | CZString(CZString&& other); |
| 244 | #endif |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 245 | ~CZString(); |
Dhruv Paranjape | 0ba8bd7 | 2017-07-08 17:47:13 +0530 | [diff] [blame] | 246 | CZString& operator=(const CZString& other); |
| 247 | |
| 248 | #if JSON_HAS_RVALUE_REFERENCES |
| 249 | CZString& operator=(CZString&& other); |
| 250 | #endif |
| 251 | |
Christopher Dunn | c28610f | 2015-02-21 11:44:16 -0600 | [diff] [blame] | 252 | bool operator<(CZString const& other) const; |
| 253 | bool operator==(CZString const& other) const; |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 254 | ArrayIndex index() const; |
Christopher Dunn | c28610f | 2015-02-21 11:44:16 -0600 | [diff] [blame] | 255 | //const char* c_str() const; ///< \deprecated |
| 256 | char const* data() const; |
| 257 | unsigned length() const; |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 258 | bool isStaticString() const; |
Christopher Dunn | 6d135cb | 2007-06-13 15:51:04 +0000 | [diff] [blame] | 259 | |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 260 | private: |
Aaron Jacobs | 11086dd | 2014-09-15 10:15:29 +1000 | [diff] [blame] | 261 | void swap(CZString& other); |
Christopher Dunn | c28610f | 2015-02-21 11:44:16 -0600 | [diff] [blame] | 262 | |
Christopher Dunn | 57ad051 | 2015-03-02 12:10:35 -0600 | [diff] [blame] | 263 | struct StringStorage { |
Dan Liu | fcbab02 | 2015-04-27 05:20:05 +0100 | [diff] [blame] | 264 | unsigned policy_: 2; |
Christopher Dunn | 57ad051 | 2015-03-02 12:10:35 -0600 | [diff] [blame] | 265 | unsigned length_: 30; // 1GB max |
| 266 | }; |
| 267 | |
Christopher Dunn | c28610f | 2015-02-21 11:44:16 -0600 | [diff] [blame] | 268 | char const* cstr_; // actually, a prefixed string, unless policy is noDup |
Christopher Dunn | 57ad051 | 2015-03-02 12:10:35 -0600 | [diff] [blame] | 269 | union { |
| 270 | ArrayIndex index_; |
| 271 | StringStorage storage_; |
| 272 | }; |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 273 | }; |
| 274 | |
| 275 | public: |
| 276 | #ifndef JSON_USE_CPPTL_SMALLMAP |
| 277 | typedef std::map<CZString, Value> ObjectValues; |
| 278 | #else |
| 279 | typedef CppTL::SmallMap<CZString, Value> ObjectValues; |
| 280 | #endif // ifndef JSON_USE_CPPTL_SMALLMAP |
Christopher Dunn | 6d135cb | 2007-06-13 15:51:04 +0000 | [diff] [blame] | 281 | #endif // ifndef JSONCPP_DOC_EXCLUDE_IMPLEMENTATION |
| 282 | |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 283 | public: |
| 284 | /** \brief Create a default Value of the given type. |
Christopher Dunn | 6d135cb | 2007-06-13 15:51:04 +0000 | [diff] [blame] | 285 | |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 286 | This is a very useful constructor. |
| 287 | To create an empty array, pass arrayValue. |
| 288 | To create an empty object, pass objectValue. |
| 289 | Another Value can then be set to this one by assignment. |
| 290 | This is useful since clear() and resize() will not alter types. |
Christopher Dunn | 6d135cb | 2007-06-13 15:51:04 +0000 | [diff] [blame] | 291 | |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 292 | Examples: |
| 293 | \code |
| 294 | Json::Value null_value; // null |
| 295 | Json::Value arr_value(Json::arrayValue); // [] |
| 296 | Json::Value obj_value(Json::objectValue); // {} |
| 297 | \endcode |
| 298 | */ |
| 299 | Value(ValueType type = nullValue); |
| 300 | Value(Int value); |
| 301 | Value(UInt value); |
Baptiste Lepilleur | 842e9ac | 2010-12-27 17:45:23 +0000 | [diff] [blame] | 302 | #if defined(JSON_HAS_INT64) |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 303 | Value(Int64 value); |
| 304 | Value(UInt64 value); |
Baptiste Lepilleur | 842e9ac | 2010-12-27 17:45:23 +0000 | [diff] [blame] | 305 | #endif // if defined(JSON_HAS_INT64) |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 306 | Value(double value); |
Christopher Dunn | 8a70297 | 2015-03-03 10:38:27 -0600 | [diff] [blame] | 307 | Value(const char* value); ///< Copy til first 0. (NULL causes to seg-fault.) |
Christopher Dunn | 8970403 | 2015-07-11 12:09:59 -0500 | [diff] [blame] | 308 | Value(const char* begin, const char* end); ///< Copy all, incl zeroes. |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 309 | /** \brief Constructs a value from a static string. |
Christopher Dunn | 6d135cb | 2007-06-13 15:51:04 +0000 | [diff] [blame] | 310 | |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 311 | * Like other value string constructor but do not duplicate the string for |
| 312 | * internal storage. The given string must remain alive after the call to this |
| 313 | * constructor. |
Christopher Dunn | c28610f | 2015-02-21 11:44:16 -0600 | [diff] [blame] | 314 | * \note This works only for null-terminated strings. (We cannot change the |
| 315 | * size of this class, so we have nowhere to store the length, |
| 316 | * which might be computed later for various operations.) |
| 317 | * |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 318 | * Example of usage: |
| 319 | * \code |
Christopher Dunn | c28610f | 2015-02-21 11:44:16 -0600 | [diff] [blame] | 320 | * static StaticString foo("some text"); |
| 321 | * Json::Value aValue(foo); |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 322 | * \endcode |
| 323 | */ |
Aaron Jacobs | 11086dd | 2014-09-15 10:15:29 +1000 | [diff] [blame] | 324 | Value(const StaticString& value); |
Christopher Dunn | de5b792 | 2016-03-06 11:19:46 -0600 | [diff] [blame] | 325 | Value(const JSONCPP_STRING& value); ///< Copy data() til size(). Embedded zeroes too. |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 326 | #ifdef JSON_USE_CPPTL |
Aaron Jacobs | 11086dd | 2014-09-15 10:15:29 +1000 | [diff] [blame] | 327 | Value(const CppTL::ConstString& value); |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 328 | #endif |
| 329 | Value(bool value); |
Christopher Dunn | 66eb72f | 2015-01-20 11:02:22 -0600 | [diff] [blame] | 330 | /// Deep copy. |
Aaron Jacobs | 11086dd | 2014-09-15 10:15:29 +1000 | [diff] [blame] | 331 | Value(const Value& other); |
Motti | 2b00891 | 2015-04-20 17:44:47 +0300 | [diff] [blame] | 332 | #if JSON_HAS_RVALUE_REFERENCES |
| 333 | /// Move constructor |
| 334 | Value(Value&& other); |
| 335 | #endif |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 336 | ~Value(); |
Christopher Dunn | 6d135cb | 2007-06-13 15:51:04 +0000 | [diff] [blame] | 337 | |
Christopher Dunn | 7f439f4 | 2015-03-06 09:22:57 -0600 | [diff] [blame] | 338 | /// Deep copy, then swap(other). |
| 339 | /// \note Over-write existing comments. To preserve comments, use #swapPayload(). |
Александр Малинин | 6a15ca6 | 2017-07-31 15:29:02 +0300 | [diff] [blame] | 340 | Value& operator=(Value other); |
Dhruv Paranjape | 8996c37 | 2017-07-08 17:27:07 +0530 | [diff] [blame] | 341 | |
Christopher Dunn | 66eb72f | 2015-01-20 11:02:22 -0600 | [diff] [blame] | 342 | /// Swap everything. |
Aaron Jacobs | 11086dd | 2014-09-15 10:15:29 +1000 | [diff] [blame] | 343 | void swap(Value& other); |
Christopher Dunn | 66eb72f | 2015-01-20 11:02:22 -0600 | [diff] [blame] | 344 | /// Swap values but leave comments and source offsets in place. |
| 345 | void swapPayload(Value& other); |
Christopher Dunn | 6d135cb | 2007-06-13 15:51:04 +0000 | [diff] [blame] | 346 | |
Dhruv Paranjape | 8996c37 | 2017-07-08 17:27:07 +0530 | [diff] [blame] | 347 | /// copy everything. |
| 348 | void copy(const Value& other); |
| 349 | /// copy values but leave comments and source offsets in place. |
| 350 | void copyPayload(const Value& other); |
| 351 | |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 352 | ValueType type() const; |
Christopher Dunn | 6d135cb | 2007-06-13 15:51:04 +0000 | [diff] [blame] | 353 | |
Christopher Dunn | 66eb72f | 2015-01-20 11:02:22 -0600 | [diff] [blame] | 354 | /// Compare payload only, not comments etc. |
Aaron Jacobs | 11086dd | 2014-09-15 10:15:29 +1000 | [diff] [blame] | 355 | bool operator<(const Value& other) const; |
| 356 | bool operator<=(const Value& other) const; |
| 357 | bool operator>=(const Value& other) const; |
| 358 | bool operator>(const Value& other) const; |
Aaron Jacobs | 11086dd | 2014-09-15 10:15:29 +1000 | [diff] [blame] | 359 | bool operator==(const Value& other) const; |
| 360 | bool operator!=(const Value& other) const; |
Aaron Jacobs | 11086dd | 2014-09-15 10:15:29 +1000 | [diff] [blame] | 361 | int compare(const Value& other) const; |
Christopher Dunn | 6d135cb | 2007-06-13 15:51:04 +0000 | [diff] [blame] | 362 | |
Christopher Dunn | 8a70297 | 2015-03-03 10:38:27 -0600 | [diff] [blame] | 363 | const char* asCString() const; ///< Embedded zeroes could cause you trouble! |
dawesc | ae56465 | 2016-03-14 19:11:02 -0500 | [diff] [blame] | 364 | #if JSONCPP_USING_SECURE_MEMORY |
| 365 | unsigned getCStringLength() const; //Allows you to understand the length of the CString |
| 366 | #endif |
Christopher Dunn | de5b792 | 2016-03-06 11:19:46 -0600 | [diff] [blame] | 367 | JSONCPP_STRING asString() const; ///< Embedded zeroes are possible. |
Christopher Dunn | c28610f | 2015-02-21 11:44:16 -0600 | [diff] [blame] | 368 | /** Get raw char* of string-value. |
| 369 | * \return false if !string. (Seg-fault if str or end are NULL.) |
| 370 | */ |
| 371 | bool getString( |
Christopher Dunn | 8970403 | 2015-07-11 12:09:59 -0500 | [diff] [blame] | 372 | char const** begin, char const** end) const; |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 373 | #ifdef JSON_USE_CPPTL |
| 374 | CppTL::ConstString asConstString() const; |
| 375 | #endif |
| 376 | Int asInt() const; |
| 377 | UInt asUInt() const; |
Aaron Jacobs | f1053e7 | 2011-05-24 03:18:02 +0000 | [diff] [blame] | 378 | #if defined(JSON_HAS_INT64) |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 379 | Int64 asInt64() const; |
| 380 | UInt64 asUInt64() const; |
Aaron Jacobs | f1053e7 | 2011-05-24 03:18:02 +0000 | [diff] [blame] | 381 | #endif // if defined(JSON_HAS_INT64) |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 382 | LargestInt asLargestInt() const; |
| 383 | LargestUInt asLargestUInt() const; |
| 384 | float asFloat() const; |
| 385 | double asDouble() const; |
| 386 | bool asBool() const; |
Christopher Dunn | 6d135cb | 2007-06-13 15:51:04 +0000 | [diff] [blame] | 387 | |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 388 | bool isNull() const; |
| 389 | bool isBool() const; |
| 390 | bool isInt() const; |
| 391 | bool isInt64() const; |
| 392 | bool isUInt() const; |
| 393 | bool isUInt64() const; |
| 394 | bool isIntegral() const; |
| 395 | bool isDouble() const; |
| 396 | bool isNumeric() const; |
| 397 | bool isString() const; |
| 398 | bool isArray() const; |
| 399 | bool isObject() const; |
Christopher Dunn | 6d135cb | 2007-06-13 15:51:04 +0000 | [diff] [blame] | 400 | |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 401 | bool isConvertibleTo(ValueType other) const; |
Christopher Dunn | 6d135cb | 2007-06-13 15:51:04 +0000 | [diff] [blame] | 402 | |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 403 | /// Number of values in array or object |
| 404 | ArrayIndex size() const; |
Christopher Dunn | 6d135cb | 2007-06-13 15:51:04 +0000 | [diff] [blame] | 405 | |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 406 | /// \brief Return true if empty array, empty object, or null; |
| 407 | /// otherwise, false. |
| 408 | bool empty() const; |
Christopher Dunn | 6d135cb | 2007-06-13 15:51:04 +0000 | [diff] [blame] | 409 | |
Wolfram Rösler | 9079422 | 2017-12-05 18:18:55 +0100 | [diff] [blame] | 410 | /// Return !isNull() |
| 411 | explicit operator bool() const; |
Christopher Dunn | 6d135cb | 2007-06-13 15:51:04 +0000 | [diff] [blame] | 412 | |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 413 | /// Remove all object members and array elements. |
| 414 | /// \pre type() is arrayValue, objectValue, or nullValue |
| 415 | /// \post type() is unchanged |
| 416 | void clear(); |
Christopher Dunn | 6d135cb | 2007-06-13 15:51:04 +0000 | [diff] [blame] | 417 | |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 418 | /// Resize the array to size elements. |
| 419 | /// New elements are initialized to null. |
| 420 | /// May only be called on nullValue or arrayValue. |
| 421 | /// \pre type() is arrayValue or nullValue |
| 422 | /// \post type() is arrayValue |
| 423 | void resize(ArrayIndex size); |
Christopher Dunn | 6d135cb | 2007-06-13 15:51:04 +0000 | [diff] [blame] | 424 | |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 425 | /// Access an array element (zero based index ). |
| 426 | /// If the array contains less than index element, then null value are |
| 427 | /// inserted |
| 428 | /// in the array so that its size is index+1. |
| 429 | /// (You may need to say 'value[0u]' to get your compiler to distinguish |
| 430 | /// this from the operator[] which takes a string.) |
Aaron Jacobs | 11086dd | 2014-09-15 10:15:29 +1000 | [diff] [blame] | 431 | Value& operator[](ArrayIndex index); |
Baptiste Lepilleur | fa130ef | 2010-12-24 12:47:14 +0000 | [diff] [blame] | 432 | |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 433 | /// Access an array element (zero based index ). |
| 434 | /// If the array contains less than index element, then null value are |
| 435 | /// inserted |
| 436 | /// in the array so that its size is index+1. |
| 437 | /// (You may need to say 'value[0u]' to get your compiler to distinguish |
| 438 | /// this from the operator[] which takes a string.) |
Aaron Jacobs | 11086dd | 2014-09-15 10:15:29 +1000 | [diff] [blame] | 439 | Value& operator[](int index); |
Baptiste Lepilleur | fa130ef | 2010-12-24 12:47:14 +0000 | [diff] [blame] | 440 | |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 441 | /// Access an array element (zero based index ) |
| 442 | /// (You may need to say 'value[0u]' to get your compiler to distinguish |
| 443 | /// this from the operator[] which takes a string.) |
Aaron Jacobs | 11086dd | 2014-09-15 10:15:29 +1000 | [diff] [blame] | 444 | const Value& operator[](ArrayIndex index) const; |
Baptiste Lepilleur | fa130ef | 2010-12-24 12:47:14 +0000 | [diff] [blame] | 445 | |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 446 | /// Access an array element (zero based index ) |
| 447 | /// (You may need to say 'value[0u]' to get your compiler to distinguish |
| 448 | /// this from the operator[] which takes a string.) |
Aaron Jacobs | 11086dd | 2014-09-15 10:15:29 +1000 | [diff] [blame] | 449 | const Value& operator[](int index) const; |
Baptiste Lepilleur | fa130ef | 2010-12-24 12:47:14 +0000 | [diff] [blame] | 450 | |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 451 | /// If the array contains at least index+1 elements, returns the element |
| 452 | /// value, |
| 453 | /// otherwise returns defaultValue. |
Aaron Jacobs | 11086dd | 2014-09-15 10:15:29 +1000 | [diff] [blame] | 454 | Value get(ArrayIndex index, const Value& defaultValue) const; |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 455 | /// Return true if index < size(). |
| 456 | bool isValidIndex(ArrayIndex index) const; |
| 457 | /// \brief Append value to array at the end. |
| 458 | /// |
| 459 | /// Equivalent to jsonvalue[jsonvalue.size()] = value; |
Aaron Jacobs | 11086dd | 2014-09-15 10:15:29 +1000 | [diff] [blame] | 460 | Value& append(const Value& value); |
Christopher Dunn | 6d135cb | 2007-06-13 15:51:04 +0000 | [diff] [blame] | 461 | |
Dhruv Paranjape | 0ba8bd7 | 2017-07-08 17:47:13 +0530 | [diff] [blame] | 462 | #if JSON_HAS_RVALUE_REFERENCES |
Dhruv Paranjape | 23c44d9 | 2017-07-08 17:30:47 +0530 | [diff] [blame] | 463 | Value& append(Value&& value); |
| 464 | #endif |
| 465 | |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 466 | /// Access an object value by name, create a null member if it does not exist. |
Christopher Dunn | 25342ba | 2015-03-02 18:05:26 -0600 | [diff] [blame] | 467 | /// \note Because of our implementation, keys are limited to 2^30 -1 chars. |
| 468 | /// Exceeding that will cause an exception. |
Aaron Jacobs | 11086dd | 2014-09-15 10:15:29 +1000 | [diff] [blame] | 469 | Value& operator[](const char* key); |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 470 | /// Access an object value by name, returns null if there is no member with |
| 471 | /// that name. |
Aaron Jacobs | 11086dd | 2014-09-15 10:15:29 +1000 | [diff] [blame] | 472 | const Value& operator[](const char* key) const; |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 473 | /// Access an object value by name, create a null member if it does not exist. |
Christopher Dunn | 25342ba | 2015-03-02 18:05:26 -0600 | [diff] [blame] | 474 | /// \param key may contain embedded nulls. |
Christopher Dunn | de5b792 | 2016-03-06 11:19:46 -0600 | [diff] [blame] | 475 | Value& operator[](const JSONCPP_STRING& key); |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 476 | /// Access an object value by name, returns null if there is no member with |
| 477 | /// that name. |
Christopher Dunn | 25342ba | 2015-03-02 18:05:26 -0600 | [diff] [blame] | 478 | /// \param key may contain embedded nulls. |
Christopher Dunn | de5b792 | 2016-03-06 11:19:46 -0600 | [diff] [blame] | 479 | const Value& operator[](const JSONCPP_STRING& key) const; |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 480 | /** \brief Access an object value by name, create a null member if it does not |
| 481 | exist. |
Christopher Dunn | 6d135cb | 2007-06-13 15:51:04 +0000 | [diff] [blame] | 482 | |
Christopher Dunn | 25342ba | 2015-03-02 18:05:26 -0600 | [diff] [blame] | 483 | * If the object has no entry for that name, then the member name used to store |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 484 | * the new entry is not duplicated. |
| 485 | * Example of use: |
| 486 | * \code |
| 487 | * Json::Value object; |
| 488 | * static const StaticString code("code"); |
| 489 | * object[code] = 1234; |
| 490 | * \endcode |
| 491 | */ |
Aaron Jacobs | 11086dd | 2014-09-15 10:15:29 +1000 | [diff] [blame] | 492 | Value& operator[](const StaticString& key); |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 493 | #ifdef JSON_USE_CPPTL |
| 494 | /// Access an object value by name, create a null member if it does not exist. |
Aaron Jacobs | 11086dd | 2014-09-15 10:15:29 +1000 | [diff] [blame] | 495 | Value& operator[](const CppTL::ConstString& key); |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 496 | /// Access an object value by name, returns null if there is no member with |
| 497 | /// that name. |
Aaron Jacobs | 11086dd | 2014-09-15 10:15:29 +1000 | [diff] [blame] | 498 | const Value& operator[](const CppTL::ConstString& key) const; |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 499 | #endif |
| 500 | /// Return the member named key if it exist, defaultValue otherwise. |
Christopher Dunn | 0fd2875 | 2015-03-05 16:38:43 -0600 | [diff] [blame] | 501 | /// \note deep copy |
Aaron Jacobs | 11086dd | 2014-09-15 10:15:29 +1000 | [diff] [blame] | 502 | Value get(const char* key, const Value& defaultValue) const; |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 503 | /// Return the member named key if it exist, defaultValue otherwise. |
Christopher Dunn | 0fd2875 | 2015-03-05 16:38:43 -0600 | [diff] [blame] | 504 | /// \note deep copy |
Christopher Dunn | 8970403 | 2015-07-11 12:09:59 -0500 | [diff] [blame] | 505 | /// \note key may contain embedded nulls. |
| 506 | Value get(const char* begin, const char* end, const Value& defaultValue) const; |
Christopher Dunn | 25342ba | 2015-03-02 18:05:26 -0600 | [diff] [blame] | 507 | /// Return the member named key if it exist, defaultValue otherwise. |
Christopher Dunn | 0fd2875 | 2015-03-05 16:38:43 -0600 | [diff] [blame] | 508 | /// \note deep copy |
Christopher Dunn | 25342ba | 2015-03-02 18:05:26 -0600 | [diff] [blame] | 509 | /// \param key may contain embedded nulls. |
Christopher Dunn | de5b792 | 2016-03-06 11:19:46 -0600 | [diff] [blame] | 510 | Value get(const JSONCPP_STRING& key, const Value& defaultValue) const; |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 511 | #ifdef JSON_USE_CPPTL |
| 512 | /// Return the member named key if it exist, defaultValue otherwise. |
Christopher Dunn | 0fd2875 | 2015-03-05 16:38:43 -0600 | [diff] [blame] | 513 | /// \note deep copy |
Aaron Jacobs | 11086dd | 2014-09-15 10:15:29 +1000 | [diff] [blame] | 514 | Value get(const CppTL::ConstString& key, const Value& defaultValue) const; |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 515 | #endif |
Christopher Dunn | 25342ba | 2015-03-02 18:05:26 -0600 | [diff] [blame] | 516 | /// Most general and efficient version of isMember()const, get()const, |
| 517 | /// and operator[]const |
Christopher Dunn | 8970403 | 2015-07-11 12:09:59 -0500 | [diff] [blame] | 518 | /// \note As stated elsewhere, behavior is undefined if (end-begin) >= 2^30 |
| 519 | Value const* find(char const* begin, char const* end) const; |
Christopher Dunn | c28610f | 2015-02-21 11:44:16 -0600 | [diff] [blame] | 520 | /// Most general and efficient version of object-mutators. |
Christopher Dunn | 8970403 | 2015-07-11 12:09:59 -0500 | [diff] [blame] | 521 | /// \note As stated elsewhere, behavior is undefined if (end-begin) >= 2^30 |
Christopher Dunn | c28610f | 2015-02-21 11:44:16 -0600 | [diff] [blame] | 522 | /// \return non-zero, but JSON_ASSERT if this is neither object nor nullValue. |
Christopher Dunn | 8970403 | 2015-07-11 12:09:59 -0500 | [diff] [blame] | 523 | Value const* demand(char const* begin, char const* end); |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 524 | /// \brief Remove and return the named member. |
| 525 | /// |
| 526 | /// Do nothing if it did not exist. |
| 527 | /// \return the removed Value, or null. |
| 528 | /// \pre type() is objectValue or nullValue |
| 529 | /// \post type() is unchanged |
Christopher Dunn | 76746b0 | 2015-01-21 16:01:30 -0600 | [diff] [blame] | 530 | /// \deprecated |
Wolfram Rösler | a06b390 | 2017-10-18 07:19:27 +0200 | [diff] [blame] | 531 | void removeMember(const char* key); |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 532 | /// Same as removeMember(const char*) |
Christopher Dunn | 25342ba | 2015-03-02 18:05:26 -0600 | [diff] [blame] | 533 | /// \param key may contain embedded nulls. |
Christopher Dunn | 76746b0 | 2015-01-21 16:01:30 -0600 | [diff] [blame] | 534 | /// \deprecated |
Wolfram Rösler | a06b390 | 2017-10-18 07:19:27 +0200 | [diff] [blame] | 535 | void removeMember(const JSONCPP_STRING& key); |
Christopher Dunn | 8970403 | 2015-07-11 12:09:59 -0500 | [diff] [blame] | 536 | /// Same as removeMember(const char* begin, const char* end, Value* removed), |
Christopher Dunn | 25342ba | 2015-03-02 18:05:26 -0600 | [diff] [blame] | 537 | /// but 'key' is null-terminated. |
| 538 | bool removeMember(const char* key, Value* removed); |
Christopher Dunn | 76746b0 | 2015-01-21 16:01:30 -0600 | [diff] [blame] | 539 | /** \brief Remove the named map member. |
| 540 | |
| 541 | Update 'removed' iff removed. |
Christopher Dunn | 25342ba | 2015-03-02 18:05:26 -0600 | [diff] [blame] | 542 | \param key may contain embedded nulls. |
Christopher Dunn | 76746b0 | 2015-01-21 16:01:30 -0600 | [diff] [blame] | 543 | \return true iff removed (no exceptions) |
| 544 | */ |
Christopher Dunn | de5b792 | 2016-03-06 11:19:46 -0600 | [diff] [blame] | 545 | bool removeMember(JSONCPP_STRING const& key, Value* removed); |
| 546 | /// Same as removeMember(JSONCPP_STRING const& key, Value* removed) |
Christopher Dunn | 8970403 | 2015-07-11 12:09:59 -0500 | [diff] [blame] | 547 | bool removeMember(const char* begin, const char* end, Value* removed); |
Christopher Dunn | 9de2c2d | 2015-01-20 16:15:40 -0600 | [diff] [blame] | 548 | /** \brief Remove the indexed array element. |
| 549 | |
| 550 | O(n) expensive operations. |
| 551 | Update 'removed' iff removed. |
Christopher Dunn | e87e41c | 2015-01-20 16:24:11 -0600 | [diff] [blame] | 552 | \return true iff removed (no exceptions) |
Christopher Dunn | 9de2c2d | 2015-01-20 16:15:40 -0600 | [diff] [blame] | 553 | */ |
| 554 | bool removeIndex(ArrayIndex i, Value* removed); |
Christopher Dunn | 6d135cb | 2007-06-13 15:51:04 +0000 | [diff] [blame] | 555 | |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 556 | /// Return true if the object has a member named key. |
Christopher Dunn | c28610f | 2015-02-21 11:44:16 -0600 | [diff] [blame] | 557 | /// \note 'key' must be null-terminated. |
Aaron Jacobs | 11086dd | 2014-09-15 10:15:29 +1000 | [diff] [blame] | 558 | bool isMember(const char* key) const; |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 559 | /// Return true if the object has a member named key. |
Christopher Dunn | 25342ba | 2015-03-02 18:05:26 -0600 | [diff] [blame] | 560 | /// \param key may contain embedded nulls. |
Christopher Dunn | de5b792 | 2016-03-06 11:19:46 -0600 | [diff] [blame] | 561 | bool isMember(const JSONCPP_STRING& key) const; |
| 562 | /// Same as isMember(JSONCPP_STRING const& key)const |
Christopher Dunn | 8970403 | 2015-07-11 12:09:59 -0500 | [diff] [blame] | 563 | bool isMember(const char* begin, const char* end) const; |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 564 | #ifdef JSON_USE_CPPTL |
| 565 | /// Return true if the object has a member named key. |
Aaron Jacobs | 11086dd | 2014-09-15 10:15:29 +1000 | [diff] [blame] | 566 | bool isMember(const CppTL::ConstString& key) const; |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 567 | #endif |
Christopher Dunn | 6d135cb | 2007-06-13 15:51:04 +0000 | [diff] [blame] | 568 | |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 569 | /// \brief Return a list of the member names. |
| 570 | /// |
| 571 | /// If null, return an empty list. |
| 572 | /// \pre type() is objectValue or nullValue |
| 573 | /// \post if type() was nullValue, it remains nullValue |
| 574 | Members getMemberNames() const; |
Christopher Dunn | 6d135cb | 2007-06-13 15:51:04 +0000 | [diff] [blame] | 575 | |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 576 | //# ifdef JSON_USE_CPPTL |
| 577 | // EnumMemberNames enumMemberNames() const; |
| 578 | // EnumValues enumValues() const; |
| 579 | //# endif |
Christopher Dunn | 6d135cb | 2007-06-13 15:51:04 +0000 | [diff] [blame] | 580 | |
Christopher Dunn | 1e3149a | 2015-01-25 14:16:13 -0600 | [diff] [blame] | 581 | /// \deprecated Always pass len. |
Christopher Dunn | de5b792 | 2016-03-06 11:19:46 -0600 | [diff] [blame] | 582 | JSONCPP_DEPRECATED("Use setComment(JSONCPP_STRING const&) instead.") |
Aaron Jacobs | 11086dd | 2014-09-15 10:15:29 +1000 | [diff] [blame] | 583 | void setComment(const char* comment, CommentPlacement placement); |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 584 | /// Comments must be //... or /* ... */ |
Christopher Dunn | 1e3149a | 2015-01-25 14:16:13 -0600 | [diff] [blame] | 585 | void setComment(const char* comment, size_t len, CommentPlacement placement); |
| 586 | /// Comments must be //... or /* ... */ |
Christopher Dunn | de5b792 | 2016-03-06 11:19:46 -0600 | [diff] [blame] | 587 | void setComment(const JSONCPP_STRING& comment, CommentPlacement placement); |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 588 | bool hasComment(CommentPlacement placement) const; |
| 589 | /// Include delimiters and embedded newlines. |
Christopher Dunn | de5b792 | 2016-03-06 11:19:46 -0600 | [diff] [blame] | 590 | JSONCPP_STRING getComment(CommentPlacement placement) const; |
Christopher Dunn | 6d135cb | 2007-06-13 15:51:04 +0000 | [diff] [blame] | 591 | |
Christopher Dunn | de5b792 | 2016-03-06 11:19:46 -0600 | [diff] [blame] | 592 | JSONCPP_STRING toStyledString() const; |
Christopher Dunn | 6d135cb | 2007-06-13 15:51:04 +0000 | [diff] [blame] | 593 | |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 594 | const_iterator begin() const; |
| 595 | const_iterator end() const; |
Christopher Dunn | 6d135cb | 2007-06-13 15:51:04 +0000 | [diff] [blame] | 596 | |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 597 | iterator begin(); |
| 598 | iterator end(); |
Christopher Dunn | 6d135cb | 2007-06-13 15:51:04 +0000 | [diff] [blame] | 599 | |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 600 | // Accessors for the [start, limit) range of bytes within the JSON text from |
| 601 | // which this value was parsed, if any. |
Christopher Dunn | d4513fc | 2016-02-06 09:25:20 -0600 | [diff] [blame] | 602 | void setOffsetStart(ptrdiff_t start); |
| 603 | void setOffsetLimit(ptrdiff_t limit); |
| 604 | ptrdiff_t getOffsetStart() const; |
| 605 | ptrdiff_t getOffsetLimit() const; |
Aaron Jacobs | 68db655 | 2014-04-23 23:41:12 +0000 | [diff] [blame] | 606 | |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 607 | private: |
Billy Donahue | 8eb5d89 | 2014-11-10 01:35:42 -0500 | [diff] [blame] | 608 | void initBasic(ValueType type, bool allocated = false); |
| 609 | |
Christopher Dunn | c28610f | 2015-02-21 11:44:16 -0600 | [diff] [blame] | 610 | Value& resolveReference(const char* key); |
| 611 | Value& resolveReference(const char* key, const char* end); |
Christopher Dunn | 6d135cb | 2007-06-13 15:51:04 +0000 | [diff] [blame] | 612 | |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 613 | struct CommentInfo { |
| 614 | CommentInfo(); |
| 615 | ~CommentInfo(); |
| 616 | |
Christopher Dunn | 1e3149a | 2015-01-25 14:16:13 -0600 | [diff] [blame] | 617 | void setComment(const char* text, size_t len); |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 618 | |
Aaron Jacobs | 11086dd | 2014-09-15 10:15:29 +1000 | [diff] [blame] | 619 | char* comment_; |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 620 | }; |
| 621 | |
| 622 | // struct MemberNamesTransform |
| 623 | //{ |
| 624 | // typedef const char *result_type; |
| 625 | // const char *operator()( const CZString &name ) const |
| 626 | // { |
| 627 | // return name.c_str(); |
| 628 | // } |
| 629 | //}; |
| 630 | |
| 631 | union ValueHolder { |
| 632 | LargestInt int_; |
| 633 | LargestUInt uint_; |
| 634 | double real_; |
| 635 | bool bool_; |
Christopher Dunn | c28610f | 2015-02-21 11:44:16 -0600 | [diff] [blame] | 636 | char* string_; // actually ptr to unsigned, followed by str, unless !allocated_ |
Aaron Jacobs | 11086dd | 2014-09-15 10:15:29 +1000 | [diff] [blame] | 637 | ObjectValues* map_; |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 638 | } value_; |
| 639 | ValueType type_ : 8; |
Christopher Dunn | 2bc6137 | 2015-01-24 13:42:37 -0600 | [diff] [blame] | 640 | unsigned int allocated_ : 1; // Notes: if declared as bool, bitfield is useless. |
Christopher Dunn | c28610f | 2015-02-21 11:44:16 -0600 | [diff] [blame] | 641 | // If not allocated_, string_ must be null-terminated. |
Aaron Jacobs | 11086dd | 2014-09-15 10:15:29 +1000 | [diff] [blame] | 642 | CommentInfo* comments_; |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 643 | |
| 644 | // [start, limit) byte offsets in the source JSON text from which this Value |
| 645 | // was extracted. |
Christopher Dunn | d4513fc | 2016-02-06 09:25:20 -0600 | [diff] [blame] | 646 | ptrdiff_t start_; |
| 647 | ptrdiff_t limit_; |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 648 | }; |
| 649 | |
| 650 | /** \brief Experimental and untested: represents an element of the "path" to |
| 651 | * access a node. |
| 652 | */ |
| 653 | class JSON_API PathArgument { |
| 654 | public: |
| 655 | friend class Path; |
| 656 | |
| 657 | PathArgument(); |
| 658 | PathArgument(ArrayIndex index); |
Aaron Jacobs | 11086dd | 2014-09-15 10:15:29 +1000 | [diff] [blame] | 659 | PathArgument(const char* key); |
Christopher Dunn | de5b792 | 2016-03-06 11:19:46 -0600 | [diff] [blame] | 660 | PathArgument(const JSONCPP_STRING& key); |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 661 | |
| 662 | private: |
| 663 | enum Kind { |
| 664 | kindNone = 0, |
| 665 | kindIndex, |
| 666 | kindKey |
| 667 | }; |
Christopher Dunn | de5b792 | 2016-03-06 11:19:46 -0600 | [diff] [blame] | 668 | JSONCPP_STRING key_; |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 669 | ArrayIndex index_; |
| 670 | Kind kind_; |
| 671 | }; |
| 672 | |
| 673 | /** \brief Experimental and untested: represents a "path" to access a node. |
| 674 | * |
| 675 | * Syntax: |
| 676 | * - "." => root node |
| 677 | * - ".[n]" => elements at index 'n' of root node (an array value) |
| 678 | * - ".name" => member named 'name' of root node (an object value) |
| 679 | * - ".name1.name2.name3" |
| 680 | * - ".[0][1][2].name1[3]" |
| 681 | * - ".%" => member name is provided as parameter |
| 682 | * - ".[%]" => index is provied as parameter |
| 683 | */ |
| 684 | class JSON_API Path { |
| 685 | public: |
Christopher Dunn | de5b792 | 2016-03-06 11:19:46 -0600 | [diff] [blame] | 686 | Path(const JSONCPP_STRING& path, |
Aaron Jacobs | 11086dd | 2014-09-15 10:15:29 +1000 | [diff] [blame] | 687 | const PathArgument& a1 = PathArgument(), |
| 688 | const PathArgument& a2 = PathArgument(), |
| 689 | const PathArgument& a3 = PathArgument(), |
| 690 | const PathArgument& a4 = PathArgument(), |
| 691 | const PathArgument& a5 = PathArgument()); |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 692 | |
Aaron Jacobs | 11086dd | 2014-09-15 10:15:29 +1000 | [diff] [blame] | 693 | const Value& resolve(const Value& root) const; |
| 694 | Value resolve(const Value& root, const Value& defaultValue) const; |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 695 | /// Creates the "path" to access the specified node and returns a reference on |
| 696 | /// the node. |
Aaron Jacobs | 11086dd | 2014-09-15 10:15:29 +1000 | [diff] [blame] | 697 | Value& make(Value& root) const; |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 698 | |
| 699 | private: |
Aaron Jacobs | 11086dd | 2014-09-15 10:15:29 +1000 | [diff] [blame] | 700 | typedef std::vector<const PathArgument*> InArgs; |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 701 | typedef std::vector<PathArgument> Args; |
| 702 | |
Christopher Dunn | de5b792 | 2016-03-06 11:19:46 -0600 | [diff] [blame] | 703 | void makePath(const JSONCPP_STRING& path, const InArgs& in); |
| 704 | void addPathInArg(const JSONCPP_STRING& path, |
Aaron Jacobs | 11086dd | 2014-09-15 10:15:29 +1000 | [diff] [blame] | 705 | const InArgs& in, |
| 706 | InArgs::const_iterator& itInArg, |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 707 | PathArgument::Kind kind); |
Christopher Dunn | de5b792 | 2016-03-06 11:19:46 -0600 | [diff] [blame] | 708 | void invalidPath(const JSONCPP_STRING& path, int location); |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 709 | |
| 710 | Args args_; |
| 711 | }; |
| 712 | |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 713 | /** \brief base class for Value iterators. |
| 714 | * |
| 715 | */ |
| 716 | class JSON_API ValueIteratorBase { |
| 717 | public: |
| 718 | typedef std::bidirectional_iterator_tag iterator_category; |
| 719 | typedef unsigned int size_t; |
| 720 | typedef int difference_type; |
| 721 | typedef ValueIteratorBase SelfType; |
Christopher Dunn | 6d135cb | 2007-06-13 15:51:04 +0000 | [diff] [blame] | 722 | |
Aaron Jacobs | 11086dd | 2014-09-15 10:15:29 +1000 | [diff] [blame] | 723 | bool operator==(const SelfType& other) const { return isEqual(other); } |
Christopher Dunn | 6d135cb | 2007-06-13 15:51:04 +0000 | [diff] [blame] | 724 | |
Aaron Jacobs | 11086dd | 2014-09-15 10:15:29 +1000 | [diff] [blame] | 725 | bool operator!=(const SelfType& other) const { return !isEqual(other); } |
Christopher Dunn | 6d135cb | 2007-06-13 15:51:04 +0000 | [diff] [blame] | 726 | |
Aaron Jacobs | 11086dd | 2014-09-15 10:15:29 +1000 | [diff] [blame] | 727 | difference_type operator-(const SelfType& other) const { |
Kevin Grant | 4c5832a | 2015-02-14 20:53:35 -0800 | [diff] [blame] | 728 | return other.computeDistance(*this); |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 729 | } |
Christopher Dunn | 6d135cb | 2007-06-13 15:51:04 +0000 | [diff] [blame] | 730 | |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 731 | /// Return either the index or the member name of the referenced value as a |
| 732 | /// Value. |
| 733 | Value key() const; |
Christopher Dunn | 6d135cb | 2007-06-13 15:51:04 +0000 | [diff] [blame] | 734 | |
Christopher Dunn | ed495ed | 2015-03-08 14:01:28 -0500 | [diff] [blame] | 735 | /// Return the index of the referenced Value, or -1 if it is not an arrayValue. |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 736 | UInt index() const; |
Christopher Dunn | 6d135cb | 2007-06-13 15:51:04 +0000 | [diff] [blame] | 737 | |
Christopher Dunn | ed495ed | 2015-03-08 14:01:28 -0500 | [diff] [blame] | 738 | /// Return the member name of the referenced Value, or "" if it is not an |
| 739 | /// objectValue. |
| 740 | /// \note Avoid `c_str()` on result, as embedded zeroes are possible. |
Christopher Dunn | de5b792 | 2016-03-06 11:19:46 -0600 | [diff] [blame] | 741 | JSONCPP_STRING name() const; |
Christopher Dunn | ed495ed | 2015-03-08 14:01:28 -0500 | [diff] [blame] | 742 | |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 743 | /// Return the member name of the referenced Value. "" if it is not an |
| 744 | /// objectValue. |
Christopher Dunn | c28610f | 2015-02-21 11:44:16 -0600 | [diff] [blame] | 745 | /// \deprecated This cannot be used for UTF-8 strings, since there can be embedded nulls. |
Christopher Dunn | ed495ed | 2015-03-08 14:01:28 -0500 | [diff] [blame] | 746 | JSONCPP_DEPRECATED("Use `key = name();` instead.") |
Christopher Dunn | c28610f | 2015-02-21 11:44:16 -0600 | [diff] [blame] | 747 | char const* memberName() const; |
| 748 | /// Return the member name of the referenced Value, or NULL if it is not an |
| 749 | /// objectValue. |
Christopher Dunn | ed495ed | 2015-03-08 14:01:28 -0500 | [diff] [blame] | 750 | /// \note Better version than memberName(). Allows embedded nulls. |
Christopher Dunn | c28610f | 2015-02-21 11:44:16 -0600 | [diff] [blame] | 751 | char const* memberName(char const** end) const; |
Christopher Dunn | 6d135cb | 2007-06-13 15:51:04 +0000 | [diff] [blame] | 752 | |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 753 | protected: |
Aaron Jacobs | 11086dd | 2014-09-15 10:15:29 +1000 | [diff] [blame] | 754 | Value& deref() const; |
Christopher Dunn | 6d135cb | 2007-06-13 15:51:04 +0000 | [diff] [blame] | 755 | |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 756 | void increment(); |
Christopher Dunn | 6d135cb | 2007-06-13 15:51:04 +0000 | [diff] [blame] | 757 | |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 758 | void decrement(); |
Christopher Dunn | 6d135cb | 2007-06-13 15:51:04 +0000 | [diff] [blame] | 759 | |
Aaron Jacobs | 11086dd | 2014-09-15 10:15:29 +1000 | [diff] [blame] | 760 | difference_type computeDistance(const SelfType& other) const; |
Christopher Dunn | 6d135cb | 2007-06-13 15:51:04 +0000 | [diff] [blame] | 761 | |
Aaron Jacobs | 11086dd | 2014-09-15 10:15:29 +1000 | [diff] [blame] | 762 | bool isEqual(const SelfType& other) const; |
Christopher Dunn | 6d135cb | 2007-06-13 15:51:04 +0000 | [diff] [blame] | 763 | |
Aaron Jacobs | 11086dd | 2014-09-15 10:15:29 +1000 | [diff] [blame] | 764 | void copy(const SelfType& other); |
Christopher Dunn | 6d135cb | 2007-06-13 15:51:04 +0000 | [diff] [blame] | 765 | |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 766 | private: |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 767 | Value::ObjectValues::iterator current_; |
| 768 | // Indicates that iterator is for a null value. |
| 769 | bool isNull_; |
Christopher Dunn | 2a10f4a | 2015-04-28 04:55:12 +0100 | [diff] [blame] | 770 | |
| 771 | public: |
| 772 | // For some reason, BORLAND needs these at the end, rather |
| 773 | // than earlier. No idea why. |
| 774 | ValueIteratorBase(); |
| 775 | explicit ValueIteratorBase(const Value::ObjectValues::iterator& current); |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 776 | }; |
Christopher Dunn | 6d135cb | 2007-06-13 15:51:04 +0000 | [diff] [blame] | 777 | |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 778 | /** \brief const iterator for object and array value. |
| 779 | * |
| 780 | */ |
| 781 | class JSON_API ValueConstIterator : public ValueIteratorBase { |
| 782 | friend class Value; |
Christopher Dunn | 6d135cb | 2007-06-13 15:51:04 +0000 | [diff] [blame] | 783 | |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 784 | public: |
| 785 | typedef const Value value_type; |
Christopher Dunn | c28610f | 2015-02-21 11:44:16 -0600 | [diff] [blame] | 786 | //typedef unsigned int size_t; |
| 787 | //typedef int difference_type; |
Aaron Jacobs | 11086dd | 2014-09-15 10:15:29 +1000 | [diff] [blame] | 788 | typedef const Value& reference; |
| 789 | typedef const Value* pointer; |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 790 | typedef ValueConstIterator SelfType; |
| 791 | |
| 792 | ValueConstIterator(); |
ycqiu | c8a8cfc | 2015-10-06 16:46:19 +0800 | [diff] [blame] | 793 | ValueConstIterator(ValueIterator const& other); |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 794 | |
| 795 | private: |
| 796 | /*! \internal Use by Value to create an iterator. |
| 797 | */ |
Aaron Jacobs | 11086dd | 2014-09-15 10:15:29 +1000 | [diff] [blame] | 798 | explicit ValueConstIterator(const Value::ObjectValues::iterator& current); |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 799 | public: |
Aaron Jacobs | 11086dd | 2014-09-15 10:15:29 +1000 | [diff] [blame] | 800 | SelfType& operator=(const ValueIteratorBase& other); |
Christopher Dunn | 6d135cb | 2007-06-13 15:51:04 +0000 | [diff] [blame] | 801 | |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 802 | SelfType operator++(int) { |
| 803 | SelfType temp(*this); |
| 804 | ++*this; |
| 805 | return temp; |
| 806 | } |
Christopher Dunn | 6d135cb | 2007-06-13 15:51:04 +0000 | [diff] [blame] | 807 | |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 808 | SelfType operator--(int) { |
| 809 | SelfType temp(*this); |
| 810 | --*this; |
| 811 | return temp; |
| 812 | } |
Christopher Dunn | 6d135cb | 2007-06-13 15:51:04 +0000 | [diff] [blame] | 813 | |
Aaron Jacobs | 11086dd | 2014-09-15 10:15:29 +1000 | [diff] [blame] | 814 | SelfType& operator--() { |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 815 | decrement(); |
| 816 | return *this; |
| 817 | } |
Christopher Dunn | 6d135cb | 2007-06-13 15:51:04 +0000 | [diff] [blame] | 818 | |
Aaron Jacobs | 11086dd | 2014-09-15 10:15:29 +1000 | [diff] [blame] | 819 | SelfType& operator++() { |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 820 | increment(); |
| 821 | return *this; |
| 822 | } |
Christopher Dunn | 6d135cb | 2007-06-13 15:51:04 +0000 | [diff] [blame] | 823 | |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 824 | reference operator*() const { return deref(); } |
Braden McDorman | 540db3b | 2014-09-14 02:31:23 -0500 | [diff] [blame] | 825 | |
| 826 | pointer operator->() const { return &deref(); } |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 827 | }; |
Christopher Dunn | 6d135cb | 2007-06-13 15:51:04 +0000 | [diff] [blame] | 828 | |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 829 | /** \brief Iterator for object and array value. |
| 830 | */ |
| 831 | class JSON_API ValueIterator : public ValueIteratorBase { |
| 832 | friend class Value; |
Christopher Dunn | 6d135cb | 2007-06-13 15:51:04 +0000 | [diff] [blame] | 833 | |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 834 | public: |
| 835 | typedef Value value_type; |
| 836 | typedef unsigned int size_t; |
| 837 | typedef int difference_type; |
Aaron Jacobs | 11086dd | 2014-09-15 10:15:29 +1000 | [diff] [blame] | 838 | typedef Value& reference; |
| 839 | typedef Value* pointer; |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 840 | typedef ValueIterator SelfType; |
Christopher Dunn | 6d135cb | 2007-06-13 15:51:04 +0000 | [diff] [blame] | 841 | |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 842 | ValueIterator(); |
ycqiu | c8a8cfc | 2015-10-06 16:46:19 +0800 | [diff] [blame] | 843 | explicit ValueIterator(const ValueConstIterator& other); |
Aaron Jacobs | 11086dd | 2014-09-15 10:15:29 +1000 | [diff] [blame] | 844 | ValueIterator(const ValueIterator& other); |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 845 | |
| 846 | private: |
| 847 | /*! \internal Use by Value to create an iterator. |
| 848 | */ |
Aaron Jacobs | 11086dd | 2014-09-15 10:15:29 +1000 | [diff] [blame] | 849 | explicit ValueIterator(const Value::ObjectValues::iterator& current); |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 850 | public: |
Aaron Jacobs | 11086dd | 2014-09-15 10:15:29 +1000 | [diff] [blame] | 851 | SelfType& operator=(const SelfType& other); |
Christopher Dunn | 6d135cb | 2007-06-13 15:51:04 +0000 | [diff] [blame] | 852 | |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 853 | SelfType operator++(int) { |
| 854 | SelfType temp(*this); |
| 855 | ++*this; |
| 856 | return temp; |
| 857 | } |
Christopher Dunn | 6d135cb | 2007-06-13 15:51:04 +0000 | [diff] [blame] | 858 | |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 859 | SelfType operator--(int) { |
| 860 | SelfType temp(*this); |
| 861 | --*this; |
| 862 | return temp; |
| 863 | } |
Christopher Dunn | 6d135cb | 2007-06-13 15:51:04 +0000 | [diff] [blame] | 864 | |
Aaron Jacobs | 11086dd | 2014-09-15 10:15:29 +1000 | [diff] [blame] | 865 | SelfType& operator--() { |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 866 | decrement(); |
| 867 | return *this; |
| 868 | } |
Christopher Dunn | 6d135cb | 2007-06-13 15:51:04 +0000 | [diff] [blame] | 869 | |
Aaron Jacobs | 11086dd | 2014-09-15 10:15:29 +1000 | [diff] [blame] | 870 | SelfType& operator++() { |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 871 | increment(); |
| 872 | return *this; |
| 873 | } |
Christopher Dunn | 6d135cb | 2007-06-13 15:51:04 +0000 | [diff] [blame] | 874 | |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 875 | reference operator*() const { return deref(); } |
Braden McDorman | 540db3b | 2014-09-14 02:31:23 -0500 | [diff] [blame] | 876 | |
| 877 | pointer operator->() const { return &deref(); } |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 878 | }; |
Christopher Dunn | 6d135cb | 2007-06-13 15:51:04 +0000 | [diff] [blame] | 879 | |
| 880 | } // namespace Json |
| 881 | |
datadiode | 9454e68 | 2015-01-20 15:25:04 -0600 | [diff] [blame] | 882 | |
| 883 | namespace std { |
| 884 | /// Specialize std::swap() for Json::Value. |
| 885 | template<> |
| 886 | inline void swap(Json::Value& a, Json::Value& b) { a.swap(b); } |
| 887 | } |
| 888 | |
Sergiy80 | d6e666f | 2016-12-03 22:29:14 +0200 | [diff] [blame] | 889 | #pragma pack(pop) |
datadiode | 9454e68 | 2015-01-20 15:25:04 -0600 | [diff] [blame] | 890 | |
Baptiste Lepilleur | eafd702 | 2013-05-08 20:21:11 +0000 | [diff] [blame] | 891 | #if defined(JSONCPP_DISABLE_DLL_INTERFACE_WARNING) |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 892 | #pragma warning(pop) |
Baptiste Lepilleur | eafd702 | 2013-05-08 20:21:11 +0000 | [diff] [blame] | 893 | #endif // if defined(JSONCPP_DISABLE_DLL_INTERFACE_WARNING) |
| 894 | |
Christopher Dunn | 6d135cb | 2007-06-13 15:51:04 +0000 | [diff] [blame] | 895 | #endif // CPPTL_JSON_H_INCLUDED |