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