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