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