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 | 6d135cb | 2007-06-13 15:51:04 +0000 | [diff] [blame] | 14 | |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 15 | #ifndef JSON_USE_CPPTL_SMALLMAP |
| 16 | #include <map> |
| 17 | #else |
| 18 | #include <cpptl/smallmap.h> |
| 19 | #endif |
| 20 | #ifdef JSON_USE_CPPTL |
| 21 | #include <cpptl/forwards.h> |
| 22 | #endif |
Christopher Dunn | 6d135cb | 2007-06-13 15:51:04 +0000 | [diff] [blame] | 23 | |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 24 | // Disable warning C4251: <data member>: <type> needs to have dll-interface to |
| 25 | // be used by... |
Baptiste Lepilleur | eafd702 | 2013-05-08 20:21:11 +0000 | [diff] [blame] | 26 | #if defined(JSONCPP_DISABLE_DLL_INTERFACE_WARNING) |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 27 | #pragma warning(push) |
| 28 | #pragma warning(disable : 4251) |
Baptiste Lepilleur | eafd702 | 2013-05-08 20:21:11 +0000 | [diff] [blame] | 29 | #endif // if defined(JSONCPP_DISABLE_DLL_INTERFACE_WARNING) |
| 30 | |
Christopher Dunn | 6d135cb | 2007-06-13 15:51:04 +0000 | [diff] [blame] | 31 | /** \brief JSON (JavaScript Object Notation). |
| 32 | */ |
| 33 | namespace Json { |
| 34 | |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 35 | /** \brief Type of the value held by a Value object. |
| 36 | */ |
| 37 | enum ValueType { |
| 38 | nullValue = 0, ///< 'null' value |
| 39 | intValue, ///< signed integer value |
| 40 | uintValue, ///< unsigned integer value |
| 41 | realValue, ///< double value |
| 42 | stringValue, ///< UTF-8 string value |
| 43 | booleanValue, ///< bool value |
| 44 | arrayValue, ///< array value (ordered list) |
| 45 | objectValue ///< object value (collection of name/value pairs). |
| 46 | }; |
Christopher Dunn | 6d135cb | 2007-06-13 15:51:04 +0000 | [diff] [blame] | 47 | |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 48 | enum CommentPlacement { |
| 49 | commentBefore = 0, ///< a comment placed on the line before a value |
| 50 | commentAfterOnSameLine, ///< a comment just after a value on the same line |
| 51 | 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] | 52 | /// root value) |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 53 | numberOfCommentPlacement |
| 54 | }; |
Christopher Dunn | 6d135cb | 2007-06-13 15:51:04 +0000 | [diff] [blame] | 55 | |
| 56 | //# ifdef JSON_USE_CPPTL |
| 57 | // typedef CppTL::AnyEnumerator<const char *> EnumMemberNames; |
| 58 | // typedef CppTL::AnyEnumerator<const Value &> EnumValues; |
| 59 | //# endif |
| 60 | |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 61 | /** \brief Lightweight wrapper to tag static string. |
| 62 | * |
| 63 | * Value constructor and objectValue member assignement takes advantage of the |
| 64 | * StaticString and avoid the cost of string duplication when storing the |
| 65 | * string or the member name. |
| 66 | * |
| 67 | * Example of usage: |
| 68 | * \code |
| 69 | * Json::Value aValue( StaticString("some text") ); |
| 70 | * Json::Value object; |
| 71 | * static const StaticString code("code"); |
| 72 | * object[code] = 1234; |
| 73 | * \endcode |
| 74 | */ |
| 75 | class JSON_API StaticString { |
| 76 | public: |
Aaron Jacobs | 11086dd | 2014-09-15 10:15:29 +1000 | [diff] [blame] | 77 | explicit StaticString(const char* czstring) : str_(czstring) {} |
Christopher Dunn | 6d135cb | 2007-06-13 15:51:04 +0000 | [diff] [blame] | 78 | |
Aaron Jacobs | 11086dd | 2014-09-15 10:15:29 +1000 | [diff] [blame] | 79 | operator const char*() const { return str_; } |
Christopher Dunn | 6d135cb | 2007-06-13 15:51:04 +0000 | [diff] [blame] | 80 | |
Aaron Jacobs | 11086dd | 2014-09-15 10:15:29 +1000 | [diff] [blame] | 81 | const char* c_str() const { return str_; } |
Christopher Dunn | 6d135cb | 2007-06-13 15:51:04 +0000 | [diff] [blame] | 82 | |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 83 | private: |
Aaron Jacobs | 11086dd | 2014-09-15 10:15:29 +1000 | [diff] [blame] | 84 | const char* str_; |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 85 | }; |
Christopher Dunn | 6d135cb | 2007-06-13 15:51:04 +0000 | [diff] [blame] | 86 | |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 87 | /** \brief Represents a <a HREF="http://www.json.org">JSON</a> value. |
| 88 | * |
| 89 | * This class is a discriminated union wrapper that can represents a: |
| 90 | * - signed integer [range: Value::minInt - Value::maxInt] |
| 91 | * - unsigned integer (range: 0 - Value::maxUInt) |
| 92 | * - double |
| 93 | * - UTF-8 string |
| 94 | * - boolean |
| 95 | * - 'null' |
| 96 | * - an ordered list of Value |
| 97 | * - collection of name/value pairs (javascript object) |
| 98 | * |
| 99 | * The type of the held value is represented by a #ValueType and |
| 100 | * can be obtained using type(). |
| 101 | * |
| 102 | * values of an #objectValue or #arrayValue can be accessed using operator[]() |
| 103 | *methods. |
| 104 | * Non const methods will automatically create the a #nullValue element |
| 105 | * if it does not exist. |
| 106 | * The sequence of an #arrayValue will be automatically resize and initialized |
| 107 | * with #nullValue. resize() can be used to enlarge or truncate an #arrayValue. |
| 108 | * |
| 109 | * The get() methods can be used to obtanis default value in the case the |
| 110 | *required element |
| 111 | * does not exist. |
| 112 | * |
| 113 | * It is possible to iterate over the list of a #objectValue values using |
| 114 | * the getMemberNames() method. |
| 115 | */ |
| 116 | class JSON_API Value { |
| 117 | friend class ValueIteratorBase; |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 118 | public: |
| 119 | typedef std::vector<std::string> Members; |
| 120 | typedef ValueIterator iterator; |
| 121 | typedef ValueConstIterator const_iterator; |
| 122 | typedef Json::UInt UInt; |
| 123 | typedef Json::Int Int; |
| 124 | #if defined(JSON_HAS_INT64) |
| 125 | typedef Json::UInt64 UInt64; |
| 126 | typedef Json::Int64 Int64; |
Baptiste Lepilleur | 842e9ac | 2010-12-27 17:45:23 +0000 | [diff] [blame] | 127 | #endif // defined(JSON_HAS_INT64) |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 128 | typedef Json::LargestInt LargestInt; |
| 129 | typedef Json::LargestUInt LargestUInt; |
| 130 | typedef Json::ArrayIndex ArrayIndex; |
Christopher Dunn | 6d135cb | 2007-06-13 15:51:04 +0000 | [diff] [blame] | 131 | |
Christopher Dunn | 07f0e93 | 2015-02-10 20:45:42 -0600 | [diff] [blame] | 132 | static const Value& null; ///! We regret this reference to a global instance; prefer the simpler Value(). |
| 133 | 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] | 134 | /// Minimum signed integer value that can be stored in a Json::Value. |
| 135 | static const LargestInt minLargestInt; |
| 136 | /// Maximum signed integer value that can be stored in a Json::Value. |
| 137 | static const LargestInt maxLargestInt; |
| 138 | /// Maximum unsigned integer value that can be stored in a Json::Value. |
| 139 | static const LargestUInt maxLargestUInt; |
Baptiste Lepilleur | 842e9ac | 2010-12-27 17:45:23 +0000 | [diff] [blame] | 140 | |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 141 | /// Minimum signed int value that can be stored in a Json::Value. |
| 142 | static const Int minInt; |
| 143 | /// Maximum signed int value that can be stored in a Json::Value. |
| 144 | static const Int maxInt; |
| 145 | /// Maximum unsigned int value that can be stored in a Json::Value. |
| 146 | static const UInt maxUInt; |
Christopher Dunn | 6d135cb | 2007-06-13 15:51:04 +0000 | [diff] [blame] | 147 | |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 148 | #if defined(JSON_HAS_INT64) |
| 149 | /// Minimum signed 64 bits int value that can be stored in a Json::Value. |
| 150 | static const Int64 minInt64; |
| 151 | /// Maximum signed 64 bits int value that can be stored in a Json::Value. |
| 152 | static const Int64 maxInt64; |
| 153 | /// Maximum unsigned 64 bits int value that can be stored in a Json::Value. |
| 154 | static const UInt64 maxUInt64; |
Aaron Jacobs | f1053e7 | 2011-05-24 03:18:02 +0000 | [diff] [blame] | 155 | #endif // defined(JSON_HAS_INT64) |
Baptiste Lepilleur | 842e9ac | 2010-12-27 17:45:23 +0000 | [diff] [blame] | 156 | |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 157 | private: |
Christopher Dunn | 6d135cb | 2007-06-13 15:51:04 +0000 | [diff] [blame] | 158 | #ifndef JSONCPP_DOC_EXCLUDE_IMPLEMENTATION |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 159 | class CZString { |
| 160 | public: |
| 161 | enum DuplicationPolicy { |
| 162 | noDuplication = 0, |
| 163 | duplicate, |
| 164 | duplicateOnCopy |
| 165 | }; |
| 166 | CZString(ArrayIndex index); |
Aaron Jacobs | 11086dd | 2014-09-15 10:15:29 +1000 | [diff] [blame] | 167 | CZString(const char* cstr, DuplicationPolicy allocate); |
| 168 | CZString(const CZString& other); |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 169 | ~CZString(); |
Aaron Jacobs | 11086dd | 2014-09-15 10:15:29 +1000 | [diff] [blame] | 170 | CZString& operator=(CZString other); |
| 171 | bool operator<(const CZString& other) const; |
| 172 | bool operator==(const CZString& other) const; |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 173 | ArrayIndex index() const; |
Aaron Jacobs | 11086dd | 2014-09-15 10:15:29 +1000 | [diff] [blame] | 174 | const char* c_str() const; |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 175 | bool isStaticString() const; |
Christopher Dunn | 6d135cb | 2007-06-13 15:51:04 +0000 | [diff] [blame] | 176 | |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 177 | private: |
Aaron Jacobs | 11086dd | 2014-09-15 10:15:29 +1000 | [diff] [blame] | 178 | void swap(CZString& other); |
| 179 | const char* cstr_; |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 180 | ArrayIndex index_; |
| 181 | }; |
| 182 | |
| 183 | public: |
| 184 | #ifndef JSON_USE_CPPTL_SMALLMAP |
| 185 | typedef std::map<CZString, Value> ObjectValues; |
| 186 | #else |
| 187 | typedef CppTL::SmallMap<CZString, Value> ObjectValues; |
| 188 | #endif // ifndef JSON_USE_CPPTL_SMALLMAP |
Christopher Dunn | 6d135cb | 2007-06-13 15:51:04 +0000 | [diff] [blame] | 189 | #endif // ifndef JSONCPP_DOC_EXCLUDE_IMPLEMENTATION |
| 190 | |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 191 | public: |
| 192 | /** \brief Create a default Value of the given type. |
Christopher Dunn | 6d135cb | 2007-06-13 15:51:04 +0000 | [diff] [blame] | 193 | |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 194 | This is a very useful constructor. |
| 195 | To create an empty array, pass arrayValue. |
| 196 | To create an empty object, pass objectValue. |
| 197 | Another Value can then be set to this one by assignment. |
| 198 | This is useful since clear() and resize() will not alter types. |
Christopher Dunn | 6d135cb | 2007-06-13 15:51:04 +0000 | [diff] [blame] | 199 | |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 200 | Examples: |
| 201 | \code |
| 202 | Json::Value null_value; // null |
| 203 | Json::Value arr_value(Json::arrayValue); // [] |
| 204 | Json::Value obj_value(Json::objectValue); // {} |
| 205 | \endcode |
| 206 | */ |
| 207 | Value(ValueType type = nullValue); |
| 208 | Value(Int value); |
| 209 | Value(UInt value); |
Baptiste Lepilleur | 842e9ac | 2010-12-27 17:45:23 +0000 | [diff] [blame] | 210 | #if defined(JSON_HAS_INT64) |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 211 | Value(Int64 value); |
| 212 | Value(UInt64 value); |
Baptiste Lepilleur | 842e9ac | 2010-12-27 17:45:23 +0000 | [diff] [blame] | 213 | #endif // if defined(JSON_HAS_INT64) |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 214 | Value(double value); |
Aaron Jacobs | 11086dd | 2014-09-15 10:15:29 +1000 | [diff] [blame] | 215 | Value(const char* value); |
| 216 | Value(const char* beginValue, const char* endValue); |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 217 | /** \brief Constructs a value from a static string. |
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 | * Like other value string constructor but do not duplicate the string for |
| 220 | * internal storage. The given string must remain alive after the call to this |
| 221 | * constructor. |
| 222 | * Example of usage: |
| 223 | * \code |
| 224 | * Json::Value aValue( StaticString("some text") ); |
| 225 | * \endcode |
| 226 | */ |
Aaron Jacobs | 11086dd | 2014-09-15 10:15:29 +1000 | [diff] [blame] | 227 | Value(const StaticString& value); |
| 228 | Value(const std::string& value); |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 229 | #ifdef JSON_USE_CPPTL |
Aaron Jacobs | 11086dd | 2014-09-15 10:15:29 +1000 | [diff] [blame] | 230 | Value(const CppTL::ConstString& value); |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 231 | #endif |
| 232 | Value(bool value); |
Christopher Dunn | 66eb72f | 2015-01-20 11:02:22 -0600 | [diff] [blame] | 233 | /// Deep copy. |
Aaron Jacobs | 11086dd | 2014-09-15 10:15:29 +1000 | [diff] [blame] | 234 | Value(const Value& other); |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 235 | ~Value(); |
Christopher Dunn | 6d135cb | 2007-06-13 15:51:04 +0000 | [diff] [blame] | 236 | |
Christopher Dunn | 66eb72f | 2015-01-20 11:02:22 -0600 | [diff] [blame] | 237 | // Deep copy, then swap(other). |
Aaron Jacobs | 11086dd | 2014-09-15 10:15:29 +1000 | [diff] [blame] | 238 | Value& operator=(Value other); |
Christopher Dunn | 66eb72f | 2015-01-20 11:02:22 -0600 | [diff] [blame] | 239 | /// Swap everything. |
Aaron Jacobs | 11086dd | 2014-09-15 10:15:29 +1000 | [diff] [blame] | 240 | void swap(Value& other); |
Christopher Dunn | 66eb72f | 2015-01-20 11:02:22 -0600 | [diff] [blame] | 241 | /// Swap values but leave comments and source offsets in place. |
| 242 | void swapPayload(Value& other); |
Christopher Dunn | 6d135cb | 2007-06-13 15:51:04 +0000 | [diff] [blame] | 243 | |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 244 | ValueType type() const; |
Christopher Dunn | 6d135cb | 2007-06-13 15:51:04 +0000 | [diff] [blame] | 245 | |
Christopher Dunn | 66eb72f | 2015-01-20 11:02:22 -0600 | [diff] [blame] | 246 | /// Compare payload only, not comments etc. |
Aaron Jacobs | 11086dd | 2014-09-15 10:15:29 +1000 | [diff] [blame] | 247 | bool operator<(const Value& other) const; |
| 248 | bool operator<=(const Value& other) const; |
| 249 | bool operator>=(const Value& other) const; |
| 250 | bool operator>(const Value& other) const; |
Aaron Jacobs | 11086dd | 2014-09-15 10:15:29 +1000 | [diff] [blame] | 251 | bool operator==(const Value& other) const; |
| 252 | bool operator!=(const Value& other) const; |
Aaron Jacobs | 11086dd | 2014-09-15 10:15:29 +1000 | [diff] [blame] | 253 | int compare(const Value& other) const; |
Christopher Dunn | 6d135cb | 2007-06-13 15:51:04 +0000 | [diff] [blame] | 254 | |
Aaron Jacobs | 11086dd | 2014-09-15 10:15:29 +1000 | [diff] [blame] | 255 | const char* asCString() const; |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 256 | std::string asString() const; |
| 257 | #ifdef JSON_USE_CPPTL |
| 258 | CppTL::ConstString asConstString() const; |
| 259 | #endif |
| 260 | Int asInt() const; |
| 261 | UInt asUInt() const; |
Aaron Jacobs | f1053e7 | 2011-05-24 03:18:02 +0000 | [diff] [blame] | 262 | #if defined(JSON_HAS_INT64) |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 263 | Int64 asInt64() const; |
| 264 | UInt64 asUInt64() const; |
Aaron Jacobs | f1053e7 | 2011-05-24 03:18:02 +0000 | [diff] [blame] | 265 | #endif // if defined(JSON_HAS_INT64) |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 266 | LargestInt asLargestInt() const; |
| 267 | LargestUInt asLargestUInt() const; |
| 268 | float asFloat() const; |
| 269 | double asDouble() const; |
| 270 | bool asBool() const; |
Christopher Dunn | 6d135cb | 2007-06-13 15:51:04 +0000 | [diff] [blame] | 271 | |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 272 | bool isNull() const; |
| 273 | bool isBool() const; |
| 274 | bool isInt() const; |
| 275 | bool isInt64() const; |
| 276 | bool isUInt() const; |
| 277 | bool isUInt64() const; |
| 278 | bool isIntegral() const; |
| 279 | bool isDouble() const; |
| 280 | bool isNumeric() const; |
| 281 | bool isString() const; |
| 282 | bool isArray() const; |
| 283 | bool isObject() const; |
Christopher Dunn | 6d135cb | 2007-06-13 15:51:04 +0000 | [diff] [blame] | 284 | |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 285 | bool isConvertibleTo(ValueType other) const; |
Christopher Dunn | 6d135cb | 2007-06-13 15:51:04 +0000 | [diff] [blame] | 286 | |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 287 | /// Number of values in array or object |
| 288 | ArrayIndex size() const; |
Christopher Dunn | 6d135cb | 2007-06-13 15:51:04 +0000 | [diff] [blame] | 289 | |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 290 | /// \brief Return true if empty array, empty object, or null; |
| 291 | /// otherwise, false. |
| 292 | bool empty() const; |
Christopher Dunn | 6d135cb | 2007-06-13 15:51:04 +0000 | [diff] [blame] | 293 | |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 294 | /// Return isNull() |
| 295 | bool operator!() const; |
Christopher Dunn | 6d135cb | 2007-06-13 15:51:04 +0000 | [diff] [blame] | 296 | |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 297 | /// Remove all object members and array elements. |
| 298 | /// \pre type() is arrayValue, objectValue, or nullValue |
| 299 | /// \post type() is unchanged |
| 300 | void clear(); |
Christopher Dunn | 6d135cb | 2007-06-13 15:51:04 +0000 | [diff] [blame] | 301 | |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 302 | /// Resize the array to size elements. |
| 303 | /// New elements are initialized to null. |
| 304 | /// May only be called on nullValue or arrayValue. |
| 305 | /// \pre type() is arrayValue or nullValue |
| 306 | /// \post type() is arrayValue |
| 307 | void resize(ArrayIndex size); |
Christopher Dunn | 6d135cb | 2007-06-13 15:51:04 +0000 | [diff] [blame] | 308 | |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 309 | /// Access an array element (zero based index ). |
| 310 | /// If the array contains less than index element, then null value are |
| 311 | /// inserted |
| 312 | /// in the array so that its size is index+1. |
| 313 | /// (You may need to say 'value[0u]' to get your compiler to distinguish |
| 314 | /// this from the operator[] which takes a string.) |
Aaron Jacobs | 11086dd | 2014-09-15 10:15:29 +1000 | [diff] [blame] | 315 | Value& operator[](ArrayIndex index); |
Baptiste Lepilleur | fa130ef | 2010-12-24 12:47:14 +0000 | [diff] [blame] | 316 | |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 317 | /// Access an array element (zero based index ). |
| 318 | /// If the array contains less than index element, then null value are |
| 319 | /// inserted |
| 320 | /// in the array so that its size is index+1. |
| 321 | /// (You may need to say 'value[0u]' to get your compiler to distinguish |
| 322 | /// this from the operator[] which takes a string.) |
Aaron Jacobs | 11086dd | 2014-09-15 10:15:29 +1000 | [diff] [blame] | 323 | Value& operator[](int index); |
Baptiste Lepilleur | fa130ef | 2010-12-24 12:47:14 +0000 | [diff] [blame] | 324 | |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 325 | /// Access an array element (zero based index ) |
| 326 | /// (You may need to say 'value[0u]' to get your compiler to distinguish |
| 327 | /// this from the operator[] which takes a string.) |
Aaron Jacobs | 11086dd | 2014-09-15 10:15:29 +1000 | [diff] [blame] | 328 | const Value& operator[](ArrayIndex index) const; |
Baptiste Lepilleur | fa130ef | 2010-12-24 12:47:14 +0000 | [diff] [blame] | 329 | |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 330 | /// Access an array element (zero based index ) |
| 331 | /// (You may need to say 'value[0u]' to get your compiler to distinguish |
| 332 | /// this from the operator[] which takes a string.) |
Aaron Jacobs | 11086dd | 2014-09-15 10:15:29 +1000 | [diff] [blame] | 333 | const Value& operator[](int index) const; |
Baptiste Lepilleur | fa130ef | 2010-12-24 12:47:14 +0000 | [diff] [blame] | 334 | |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 335 | /// If the array contains at least index+1 elements, returns the element |
| 336 | /// value, |
| 337 | /// otherwise returns defaultValue. |
Aaron Jacobs | 11086dd | 2014-09-15 10:15:29 +1000 | [diff] [blame] | 338 | Value get(ArrayIndex index, const Value& defaultValue) const; |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 339 | /// Return true if index < size(). |
| 340 | bool isValidIndex(ArrayIndex index) const; |
| 341 | /// \brief Append value to array at the end. |
| 342 | /// |
| 343 | /// Equivalent to jsonvalue[jsonvalue.size()] = value; |
Aaron Jacobs | 11086dd | 2014-09-15 10:15:29 +1000 | [diff] [blame] | 344 | Value& append(const Value& value); |
Christopher Dunn | 6d135cb | 2007-06-13 15:51:04 +0000 | [diff] [blame] | 345 | |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 346 | /// 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] | 347 | Value& operator[](const char* key); |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 348 | /// Access an object value by name, returns null if there is no member with |
| 349 | /// that name. |
Aaron Jacobs | 11086dd | 2014-09-15 10:15:29 +1000 | [diff] [blame] | 350 | const Value& operator[](const char* key) const; |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 351 | /// 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] | 352 | Value& operator[](const std::string& key); |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 353 | /// Access an object value by name, returns null if there is no member with |
| 354 | /// that name. |
Aaron Jacobs | 11086dd | 2014-09-15 10:15:29 +1000 | [diff] [blame] | 355 | const Value& operator[](const std::string& key) const; |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 356 | /** \brief Access an object value by name, create a null member if it does not |
| 357 | exist. |
Christopher Dunn | 6d135cb | 2007-06-13 15:51:04 +0000 | [diff] [blame] | 358 | |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 359 | * If the object as no entry for that name, then the member name used to store |
| 360 | * the new entry is not duplicated. |
| 361 | * Example of use: |
| 362 | * \code |
| 363 | * Json::Value object; |
| 364 | * static const StaticString code("code"); |
| 365 | * object[code] = 1234; |
| 366 | * \endcode |
| 367 | */ |
Aaron Jacobs | 11086dd | 2014-09-15 10:15:29 +1000 | [diff] [blame] | 368 | Value& operator[](const StaticString& key); |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 369 | #ifdef JSON_USE_CPPTL |
| 370 | /// 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] | 371 | Value& operator[](const CppTL::ConstString& key); |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 372 | /// Access an object value by name, returns null if there is no member with |
| 373 | /// that name. |
Aaron Jacobs | 11086dd | 2014-09-15 10:15:29 +1000 | [diff] [blame] | 374 | const Value& operator[](const CppTL::ConstString& key) const; |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 375 | #endif |
| 376 | /// Return the member named key if it exist, defaultValue otherwise. |
Aaron Jacobs | 11086dd | 2014-09-15 10:15:29 +1000 | [diff] [blame] | 377 | Value get(const char* key, const Value& defaultValue) const; |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 378 | /// Return the member named key if it exist, defaultValue otherwise. |
Aaron Jacobs | 11086dd | 2014-09-15 10:15:29 +1000 | [diff] [blame] | 379 | Value get(const std::string& key, const Value& defaultValue) const; |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 380 | #ifdef JSON_USE_CPPTL |
| 381 | /// Return the member named key if it exist, defaultValue otherwise. |
Aaron Jacobs | 11086dd | 2014-09-15 10:15:29 +1000 | [diff] [blame] | 382 | Value get(const CppTL::ConstString& key, const Value& defaultValue) const; |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 383 | #endif |
| 384 | /// \brief Remove and return the named member. |
| 385 | /// |
| 386 | /// Do nothing if it did not exist. |
| 387 | /// \return the removed Value, or null. |
| 388 | /// \pre type() is objectValue or nullValue |
| 389 | /// \post type() is unchanged |
Christopher Dunn | 76746b0 | 2015-01-21 16:01:30 -0600 | [diff] [blame] | 390 | /// \deprecated |
Aaron Jacobs | 11086dd | 2014-09-15 10:15:29 +1000 | [diff] [blame] | 391 | Value removeMember(const char* key); |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 392 | /// Same as removeMember(const char*) |
Christopher Dunn | 76746b0 | 2015-01-21 16:01:30 -0600 | [diff] [blame] | 393 | /// \deprecated |
Aaron Jacobs | 11086dd | 2014-09-15 10:15:29 +1000 | [diff] [blame] | 394 | Value removeMember(const std::string& key); |
Christopher Dunn | 76746b0 | 2015-01-21 16:01:30 -0600 | [diff] [blame] | 395 | /** \brief Remove the named map member. |
| 396 | |
| 397 | Update 'removed' iff removed. |
| 398 | \return true iff removed (no exceptions) |
| 399 | */ |
| 400 | bool removeMember(const char* key, Value* removed); |
Christopher Dunn | 9de2c2d | 2015-01-20 16:15:40 -0600 | [diff] [blame] | 401 | /** \brief Remove the indexed array element. |
| 402 | |
| 403 | O(n) expensive operations. |
| 404 | Update 'removed' iff removed. |
Christopher Dunn | e87e41c | 2015-01-20 16:24:11 -0600 | [diff] [blame] | 405 | \return true iff removed (no exceptions) |
Christopher Dunn | 9de2c2d | 2015-01-20 16:15:40 -0600 | [diff] [blame] | 406 | */ |
| 407 | bool removeIndex(ArrayIndex i, Value* removed); |
Christopher Dunn | 6d135cb | 2007-06-13 15:51:04 +0000 | [diff] [blame] | 408 | |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 409 | /// Return true if the object has a member named key. |
Aaron Jacobs | 11086dd | 2014-09-15 10:15:29 +1000 | [diff] [blame] | 410 | bool isMember(const char* key) const; |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 411 | /// Return true if the object has a member named key. |
Aaron Jacobs | 11086dd | 2014-09-15 10:15:29 +1000 | [diff] [blame] | 412 | bool isMember(const std::string& key) const; |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 413 | #ifdef JSON_USE_CPPTL |
| 414 | /// Return true if the object has a member named key. |
Aaron Jacobs | 11086dd | 2014-09-15 10:15:29 +1000 | [diff] [blame] | 415 | bool isMember(const CppTL::ConstString& key) const; |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 416 | #endif |
Christopher Dunn | 6d135cb | 2007-06-13 15:51:04 +0000 | [diff] [blame] | 417 | |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 418 | /// \brief Return a list of the member names. |
| 419 | /// |
| 420 | /// If null, return an empty list. |
| 421 | /// \pre type() is objectValue or nullValue |
| 422 | /// \post if type() was nullValue, it remains nullValue |
| 423 | Members getMemberNames() const; |
Christopher Dunn | 6d135cb | 2007-06-13 15:51:04 +0000 | [diff] [blame] | 424 | |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 425 | //# ifdef JSON_USE_CPPTL |
| 426 | // EnumMemberNames enumMemberNames() const; |
| 427 | // EnumValues enumValues() const; |
| 428 | //# endif |
Christopher Dunn | 6d135cb | 2007-06-13 15:51:04 +0000 | [diff] [blame] | 429 | |
Christopher Dunn | 1e3149a | 2015-01-25 14:16:13 -0600 | [diff] [blame] | 430 | /// \deprecated Always pass len. |
Aaron Jacobs | 11086dd | 2014-09-15 10:15:29 +1000 | [diff] [blame] | 431 | void setComment(const char* comment, CommentPlacement placement); |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 432 | /// Comments must be //... or /* ... */ |
Christopher Dunn | 1e3149a | 2015-01-25 14:16:13 -0600 | [diff] [blame] | 433 | void setComment(const char* comment, size_t len, CommentPlacement placement); |
| 434 | /// Comments must be //... or /* ... */ |
Aaron Jacobs | 11086dd | 2014-09-15 10:15:29 +1000 | [diff] [blame] | 435 | void setComment(const std::string& comment, CommentPlacement placement); |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 436 | bool hasComment(CommentPlacement placement) const; |
| 437 | /// Include delimiters and embedded newlines. |
| 438 | std::string getComment(CommentPlacement placement) const; |
Christopher Dunn | 6d135cb | 2007-06-13 15:51:04 +0000 | [diff] [blame] | 439 | |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 440 | std::string toStyledString() const; |
Christopher Dunn | 6d135cb | 2007-06-13 15:51:04 +0000 | [diff] [blame] | 441 | |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 442 | const_iterator begin() const; |
| 443 | const_iterator end() const; |
Christopher Dunn | 6d135cb | 2007-06-13 15:51:04 +0000 | [diff] [blame] | 444 | |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 445 | iterator begin(); |
| 446 | iterator end(); |
Christopher Dunn | 6d135cb | 2007-06-13 15:51:04 +0000 | [diff] [blame] | 447 | |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 448 | // Accessors for the [start, limit) range of bytes within the JSON text from |
| 449 | // which this value was parsed, if any. |
| 450 | void setOffsetStart(size_t start); |
| 451 | void setOffsetLimit(size_t limit); |
| 452 | size_t getOffsetStart() const; |
| 453 | size_t getOffsetLimit() const; |
Aaron Jacobs | 68db655 | 2014-04-23 23:41:12 +0000 | [diff] [blame] | 454 | |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 455 | private: |
Billy Donahue | 8eb5d89 | 2014-11-10 01:35:42 -0500 | [diff] [blame] | 456 | void initBasic(ValueType type, bool allocated = false); |
| 457 | |
Aaron Jacobs | 11086dd | 2014-09-15 10:15:29 +1000 | [diff] [blame] | 458 | Value& resolveReference(const char* key, bool isStatic); |
Christopher Dunn | 6d135cb | 2007-06-13 15:51:04 +0000 | [diff] [blame] | 459 | |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 460 | struct CommentInfo { |
| 461 | CommentInfo(); |
| 462 | ~CommentInfo(); |
| 463 | |
Christopher Dunn | 1e3149a | 2015-01-25 14:16:13 -0600 | [diff] [blame] | 464 | void setComment(const char* text, size_t len); |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 465 | |
Aaron Jacobs | 11086dd | 2014-09-15 10:15:29 +1000 | [diff] [blame] | 466 | char* comment_; |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 467 | }; |
| 468 | |
| 469 | // struct MemberNamesTransform |
| 470 | //{ |
| 471 | // typedef const char *result_type; |
| 472 | // const char *operator()( const CZString &name ) const |
| 473 | // { |
| 474 | // return name.c_str(); |
| 475 | // } |
| 476 | //}; |
| 477 | |
| 478 | union ValueHolder { |
| 479 | LargestInt int_; |
| 480 | LargestUInt uint_; |
| 481 | double real_; |
| 482 | bool bool_; |
Aaron Jacobs | 11086dd | 2014-09-15 10:15:29 +1000 | [diff] [blame] | 483 | char* string_; |
Aaron Jacobs | 11086dd | 2014-09-15 10:15:29 +1000 | [diff] [blame] | 484 | ObjectValues* map_; |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 485 | } value_; |
| 486 | ValueType type_ : 8; |
Christopher Dunn | 2bc6137 | 2015-01-24 13:42:37 -0600 | [diff] [blame] | 487 | unsigned int allocated_ : 1; // Notes: if declared as bool, bitfield is useless. |
Aaron Jacobs | 11086dd | 2014-09-15 10:15:29 +1000 | [diff] [blame] | 488 | CommentInfo* comments_; |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 489 | |
| 490 | // [start, limit) byte offsets in the source JSON text from which this Value |
| 491 | // was extracted. |
| 492 | size_t start_; |
| 493 | size_t limit_; |
| 494 | }; |
| 495 | |
| 496 | /** \brief Experimental and untested: represents an element of the "path" to |
| 497 | * access a node. |
| 498 | */ |
| 499 | class JSON_API PathArgument { |
| 500 | public: |
| 501 | friend class Path; |
| 502 | |
| 503 | PathArgument(); |
| 504 | PathArgument(ArrayIndex index); |
Aaron Jacobs | 11086dd | 2014-09-15 10:15:29 +1000 | [diff] [blame] | 505 | PathArgument(const char* key); |
| 506 | PathArgument(const std::string& key); |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 507 | |
| 508 | private: |
| 509 | enum Kind { |
| 510 | kindNone = 0, |
| 511 | kindIndex, |
| 512 | kindKey |
| 513 | }; |
| 514 | std::string key_; |
| 515 | ArrayIndex index_; |
| 516 | Kind kind_; |
| 517 | }; |
| 518 | |
| 519 | /** \brief Experimental and untested: represents a "path" to access a node. |
| 520 | * |
| 521 | * Syntax: |
| 522 | * - "." => root node |
| 523 | * - ".[n]" => elements at index 'n' of root node (an array value) |
| 524 | * - ".name" => member named 'name' of root node (an object value) |
| 525 | * - ".name1.name2.name3" |
| 526 | * - ".[0][1][2].name1[3]" |
| 527 | * - ".%" => member name is provided as parameter |
| 528 | * - ".[%]" => index is provied as parameter |
| 529 | */ |
| 530 | class JSON_API Path { |
| 531 | public: |
Aaron Jacobs | 11086dd | 2014-09-15 10:15:29 +1000 | [diff] [blame] | 532 | Path(const std::string& path, |
| 533 | const PathArgument& a1 = PathArgument(), |
| 534 | const PathArgument& a2 = PathArgument(), |
| 535 | const PathArgument& a3 = PathArgument(), |
| 536 | const PathArgument& a4 = PathArgument(), |
| 537 | const PathArgument& a5 = PathArgument()); |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 538 | |
Aaron Jacobs | 11086dd | 2014-09-15 10:15:29 +1000 | [diff] [blame] | 539 | const Value& resolve(const Value& root) const; |
| 540 | Value resolve(const Value& root, const Value& defaultValue) const; |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 541 | /// Creates the "path" to access the specified node and returns a reference on |
| 542 | /// the node. |
Aaron Jacobs | 11086dd | 2014-09-15 10:15:29 +1000 | [diff] [blame] | 543 | Value& make(Value& root) const; |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 544 | |
| 545 | private: |
Aaron Jacobs | 11086dd | 2014-09-15 10:15:29 +1000 | [diff] [blame] | 546 | typedef std::vector<const PathArgument*> InArgs; |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 547 | typedef std::vector<PathArgument> Args; |
| 548 | |
Aaron Jacobs | 11086dd | 2014-09-15 10:15:29 +1000 | [diff] [blame] | 549 | void makePath(const std::string& path, const InArgs& in); |
| 550 | void addPathInArg(const std::string& path, |
| 551 | const InArgs& in, |
| 552 | InArgs::const_iterator& itInArg, |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 553 | PathArgument::Kind kind); |
Aaron Jacobs | 11086dd | 2014-09-15 10:15:29 +1000 | [diff] [blame] | 554 | void invalidPath(const std::string& path, int location); |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 555 | |
| 556 | Args args_; |
| 557 | }; |
| 558 | |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 559 | /** \brief base class for Value iterators. |
| 560 | * |
| 561 | */ |
| 562 | class JSON_API ValueIteratorBase { |
| 563 | public: |
| 564 | typedef std::bidirectional_iterator_tag iterator_category; |
| 565 | typedef unsigned int size_t; |
| 566 | typedef int difference_type; |
| 567 | typedef ValueIteratorBase SelfType; |
Christopher Dunn | 6d135cb | 2007-06-13 15:51:04 +0000 | [diff] [blame] | 568 | |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 569 | ValueIteratorBase(); |
Aaron Jacobs | 11086dd | 2014-09-15 10:15:29 +1000 | [diff] [blame] | 570 | explicit ValueIteratorBase(const Value::ObjectValues::iterator& current); |
Christopher Dunn | 6d135cb | 2007-06-13 15:51:04 +0000 | [diff] [blame] | 571 | |
Aaron Jacobs | 11086dd | 2014-09-15 10:15:29 +1000 | [diff] [blame] | 572 | bool operator==(const SelfType& other) const { return isEqual(other); } |
Christopher Dunn | 6d135cb | 2007-06-13 15:51:04 +0000 | [diff] [blame] | 573 | |
Aaron Jacobs | 11086dd | 2014-09-15 10:15:29 +1000 | [diff] [blame] | 574 | bool operator!=(const SelfType& other) const { return !isEqual(other); } |
Christopher Dunn | 6d135cb | 2007-06-13 15:51:04 +0000 | [diff] [blame] | 575 | |
Aaron Jacobs | 11086dd | 2014-09-15 10:15:29 +1000 | [diff] [blame] | 576 | difference_type operator-(const SelfType& other) const { |
Kevin Grant | 4c5832a | 2015-02-14 20:53:35 -0800 | [diff] [blame] | 577 | return other.computeDistance(*this); |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 578 | } |
Christopher Dunn | 6d135cb | 2007-06-13 15:51:04 +0000 | [diff] [blame] | 579 | |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 580 | /// Return either the index or the member name of the referenced value as a |
| 581 | /// Value. |
| 582 | Value key() const; |
Christopher Dunn | 6d135cb | 2007-06-13 15:51:04 +0000 | [diff] [blame] | 583 | |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 584 | /// Return the index of the referenced Value. -1 if it is not an arrayValue. |
| 585 | UInt index() const; |
Christopher Dunn | 6d135cb | 2007-06-13 15:51:04 +0000 | [diff] [blame] | 586 | |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 587 | /// Return the member name of the referenced Value. "" if it is not an |
| 588 | /// objectValue. |
Aaron Jacobs | 11086dd | 2014-09-15 10:15:29 +1000 | [diff] [blame] | 589 | const char* memberName() const; |
Christopher Dunn | 6d135cb | 2007-06-13 15:51:04 +0000 | [diff] [blame] | 590 | |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 591 | protected: |
Aaron Jacobs | 11086dd | 2014-09-15 10:15:29 +1000 | [diff] [blame] | 592 | Value& deref() const; |
Christopher Dunn | 6d135cb | 2007-06-13 15:51:04 +0000 | [diff] [blame] | 593 | |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 594 | void increment(); |
Christopher Dunn | 6d135cb | 2007-06-13 15:51:04 +0000 | [diff] [blame] | 595 | |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 596 | void decrement(); |
Christopher Dunn | 6d135cb | 2007-06-13 15:51:04 +0000 | [diff] [blame] | 597 | |
Aaron Jacobs | 11086dd | 2014-09-15 10:15:29 +1000 | [diff] [blame] | 598 | difference_type computeDistance(const SelfType& other) const; |
Christopher Dunn | 6d135cb | 2007-06-13 15:51:04 +0000 | [diff] [blame] | 599 | |
Aaron Jacobs | 11086dd | 2014-09-15 10:15:29 +1000 | [diff] [blame] | 600 | bool isEqual(const SelfType& other) const; |
Christopher Dunn | 6d135cb | 2007-06-13 15:51:04 +0000 | [diff] [blame] | 601 | |
Aaron Jacobs | 11086dd | 2014-09-15 10:15:29 +1000 | [diff] [blame] | 602 | void copy(const SelfType& other); |
Christopher Dunn | 6d135cb | 2007-06-13 15:51:04 +0000 | [diff] [blame] | 603 | |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 604 | private: |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 605 | Value::ObjectValues::iterator current_; |
| 606 | // Indicates that iterator is for a null value. |
| 607 | bool isNull_; |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 608 | }; |
Christopher Dunn | 6d135cb | 2007-06-13 15:51:04 +0000 | [diff] [blame] | 609 | |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 610 | /** \brief const iterator for object and array value. |
| 611 | * |
| 612 | */ |
| 613 | class JSON_API ValueConstIterator : public ValueIteratorBase { |
| 614 | friend class Value; |
Christopher Dunn | 6d135cb | 2007-06-13 15:51:04 +0000 | [diff] [blame] | 615 | |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 616 | public: |
| 617 | typedef const Value value_type; |
| 618 | typedef unsigned int size_t; |
| 619 | typedef int difference_type; |
Aaron Jacobs | 11086dd | 2014-09-15 10:15:29 +1000 | [diff] [blame] | 620 | typedef const Value& reference; |
| 621 | typedef const Value* pointer; |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 622 | typedef ValueConstIterator SelfType; |
| 623 | |
| 624 | ValueConstIterator(); |
| 625 | |
| 626 | private: |
| 627 | /*! \internal Use by Value to create an iterator. |
| 628 | */ |
Aaron Jacobs | 11086dd | 2014-09-15 10:15:29 +1000 | [diff] [blame] | 629 | explicit ValueConstIterator(const Value::ObjectValues::iterator& current); |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 630 | public: |
Aaron Jacobs | 11086dd | 2014-09-15 10:15:29 +1000 | [diff] [blame] | 631 | SelfType& operator=(const ValueIteratorBase& other); |
Christopher Dunn | 6d135cb | 2007-06-13 15:51:04 +0000 | [diff] [blame] | 632 | |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 633 | SelfType operator++(int) { |
| 634 | SelfType temp(*this); |
| 635 | ++*this; |
| 636 | return temp; |
| 637 | } |
Christopher Dunn | 6d135cb | 2007-06-13 15:51:04 +0000 | [diff] [blame] | 638 | |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 639 | SelfType operator--(int) { |
| 640 | SelfType temp(*this); |
| 641 | --*this; |
| 642 | return temp; |
| 643 | } |
Christopher Dunn | 6d135cb | 2007-06-13 15:51:04 +0000 | [diff] [blame] | 644 | |
Aaron Jacobs | 11086dd | 2014-09-15 10:15:29 +1000 | [diff] [blame] | 645 | SelfType& operator--() { |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 646 | decrement(); |
| 647 | return *this; |
| 648 | } |
Christopher Dunn | 6d135cb | 2007-06-13 15:51:04 +0000 | [diff] [blame] | 649 | |
Aaron Jacobs | 11086dd | 2014-09-15 10:15:29 +1000 | [diff] [blame] | 650 | SelfType& operator++() { |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 651 | increment(); |
| 652 | return *this; |
| 653 | } |
Christopher Dunn | 6d135cb | 2007-06-13 15:51:04 +0000 | [diff] [blame] | 654 | |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 655 | reference operator*() const { return deref(); } |
Braden McDorman | 540db3b | 2014-09-14 02:31:23 -0500 | [diff] [blame] | 656 | |
| 657 | pointer operator->() const { return &deref(); } |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 658 | }; |
Christopher Dunn | 6d135cb | 2007-06-13 15:51:04 +0000 | [diff] [blame] | 659 | |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 660 | /** \brief Iterator for object and array value. |
| 661 | */ |
| 662 | class JSON_API ValueIterator : public ValueIteratorBase { |
| 663 | friend class Value; |
Christopher Dunn | 6d135cb | 2007-06-13 15:51:04 +0000 | [diff] [blame] | 664 | |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 665 | public: |
| 666 | typedef Value value_type; |
| 667 | typedef unsigned int size_t; |
| 668 | typedef int difference_type; |
Aaron Jacobs | 11086dd | 2014-09-15 10:15:29 +1000 | [diff] [blame] | 669 | typedef Value& reference; |
| 670 | typedef Value* pointer; |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 671 | typedef ValueIterator SelfType; |
Christopher Dunn | 6d135cb | 2007-06-13 15:51:04 +0000 | [diff] [blame] | 672 | |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 673 | ValueIterator(); |
Aaron Jacobs | 11086dd | 2014-09-15 10:15:29 +1000 | [diff] [blame] | 674 | ValueIterator(const ValueConstIterator& other); |
| 675 | ValueIterator(const ValueIterator& other); |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 676 | |
| 677 | private: |
| 678 | /*! \internal Use by Value to create an iterator. |
| 679 | */ |
Aaron Jacobs | 11086dd | 2014-09-15 10:15:29 +1000 | [diff] [blame] | 680 | explicit ValueIterator(const Value::ObjectValues::iterator& current); |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 681 | public: |
Aaron Jacobs | 11086dd | 2014-09-15 10:15:29 +1000 | [diff] [blame] | 682 | SelfType& operator=(const SelfType& other); |
Christopher Dunn | 6d135cb | 2007-06-13 15:51:04 +0000 | [diff] [blame] | 683 | |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 684 | SelfType operator++(int) { |
| 685 | SelfType temp(*this); |
| 686 | ++*this; |
| 687 | return temp; |
| 688 | } |
Christopher Dunn | 6d135cb | 2007-06-13 15:51:04 +0000 | [diff] [blame] | 689 | |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 690 | SelfType operator--(int) { |
| 691 | SelfType temp(*this); |
| 692 | --*this; |
| 693 | return temp; |
| 694 | } |
Christopher Dunn | 6d135cb | 2007-06-13 15:51:04 +0000 | [diff] [blame] | 695 | |
Aaron Jacobs | 11086dd | 2014-09-15 10:15:29 +1000 | [diff] [blame] | 696 | SelfType& operator--() { |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 697 | decrement(); |
| 698 | return *this; |
| 699 | } |
Christopher Dunn | 6d135cb | 2007-06-13 15:51:04 +0000 | [diff] [blame] | 700 | |
Aaron Jacobs | 11086dd | 2014-09-15 10:15:29 +1000 | [diff] [blame] | 701 | SelfType& operator++() { |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 702 | increment(); |
| 703 | return *this; |
| 704 | } |
Christopher Dunn | 6d135cb | 2007-06-13 15:51:04 +0000 | [diff] [blame] | 705 | |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 706 | reference operator*() const { return deref(); } |
Braden McDorman | 540db3b | 2014-09-14 02:31:23 -0500 | [diff] [blame] | 707 | |
| 708 | pointer operator->() const { return &deref(); } |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 709 | }; |
Christopher Dunn | 6d135cb | 2007-06-13 15:51:04 +0000 | [diff] [blame] | 710 | |
| 711 | } // namespace Json |
| 712 | |
datadiode | 9454e68 | 2015-01-20 15:25:04 -0600 | [diff] [blame] | 713 | |
| 714 | namespace std { |
| 715 | /// Specialize std::swap() for Json::Value. |
| 716 | template<> |
| 717 | inline void swap(Json::Value& a, Json::Value& b) { a.swap(b); } |
| 718 | } |
| 719 | |
| 720 | |
Baptiste Lepilleur | eafd702 | 2013-05-08 20:21:11 +0000 | [diff] [blame] | 721 | #if defined(JSONCPP_DISABLE_DLL_INTERFACE_WARNING) |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 722 | #pragma warning(pop) |
Baptiste Lepilleur | eafd702 | 2013-05-08 20:21:11 +0000 | [diff] [blame] | 723 | #endif // if defined(JSONCPP_DISABLE_DLL_INTERFACE_WARNING) |
| 724 | |
Christopher Dunn | 6d135cb | 2007-06-13 15:51:04 +0000 | [diff] [blame] | 725 | #endif // CPPTL_JSON_H_INCLUDED |