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