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