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