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); |
Christopher Dunn | 57ad051 | 2015-03-02 12:10:35 -0600 | [diff] [blame^] | 179 | struct StringStorage { |
| 180 | DuplicationPolicy policy_: 2; |
| 181 | unsigned length_: 30; // 1GB max |
| 182 | }; |
| 183 | |
Aaron Jacobs | 11086dd | 2014-09-15 10:15:29 +1000 | [diff] [blame] | 184 | const char* cstr_; |
Christopher Dunn | 57ad051 | 2015-03-02 12:10:35 -0600 | [diff] [blame^] | 185 | union { |
| 186 | ArrayIndex index_; |
| 187 | StringStorage storage_; |
| 188 | }; |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 189 | }; |
| 190 | |
| 191 | public: |
| 192 | #ifndef JSON_USE_CPPTL_SMALLMAP |
| 193 | typedef std::map<CZString, Value> ObjectValues; |
| 194 | #else |
| 195 | typedef CppTL::SmallMap<CZString, Value> ObjectValues; |
| 196 | #endif // ifndef JSON_USE_CPPTL_SMALLMAP |
Christopher Dunn | 6d135cb | 2007-06-13 15:51:04 +0000 | [diff] [blame] | 197 | #endif // ifndef JSONCPP_DOC_EXCLUDE_IMPLEMENTATION |
| 198 | |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 199 | public: |
| 200 | /** \brief Create a default Value of the given type. |
Christopher Dunn | 6d135cb | 2007-06-13 15:51:04 +0000 | [diff] [blame] | 201 | |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 202 | This is a very useful constructor. |
| 203 | To create an empty array, pass arrayValue. |
| 204 | To create an empty object, pass objectValue. |
| 205 | Another Value can then be set to this one by assignment. |
| 206 | This is useful since clear() and resize() will not alter types. |
Christopher Dunn | 6d135cb | 2007-06-13 15:51:04 +0000 | [diff] [blame] | 207 | |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 208 | Examples: |
| 209 | \code |
| 210 | Json::Value null_value; // null |
| 211 | Json::Value arr_value(Json::arrayValue); // [] |
| 212 | Json::Value obj_value(Json::objectValue); // {} |
| 213 | \endcode |
| 214 | */ |
| 215 | Value(ValueType type = nullValue); |
| 216 | Value(Int value); |
| 217 | Value(UInt value); |
Baptiste Lepilleur | 842e9ac | 2010-12-27 17:45:23 +0000 | [diff] [blame] | 218 | #if defined(JSON_HAS_INT64) |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 219 | Value(Int64 value); |
| 220 | Value(UInt64 value); |
Baptiste Lepilleur | 842e9ac | 2010-12-27 17:45:23 +0000 | [diff] [blame] | 221 | #endif // if defined(JSON_HAS_INT64) |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 222 | Value(double value); |
Aaron Jacobs | 11086dd | 2014-09-15 10:15:29 +1000 | [diff] [blame] | 223 | Value(const char* value); |
| 224 | Value(const char* beginValue, const char* endValue); |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 225 | /** \brief Constructs a value from a static string. |
Christopher Dunn | 6d135cb | 2007-06-13 15:51:04 +0000 | [diff] [blame] | 226 | |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 227 | * Like other value string constructor but do not duplicate the string for |
| 228 | * internal storage. The given string must remain alive after the call to this |
| 229 | * constructor. |
| 230 | * Example of usage: |
| 231 | * \code |
| 232 | * Json::Value aValue( StaticString("some text") ); |
| 233 | * \endcode |
| 234 | */ |
Aaron Jacobs | 11086dd | 2014-09-15 10:15:29 +1000 | [diff] [blame] | 235 | Value(const StaticString& value); |
| 236 | Value(const std::string& value); |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 237 | #ifdef JSON_USE_CPPTL |
Aaron Jacobs | 11086dd | 2014-09-15 10:15:29 +1000 | [diff] [blame] | 238 | Value(const CppTL::ConstString& value); |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 239 | #endif |
| 240 | Value(bool value); |
Christopher Dunn | 66eb72f | 2015-01-20 11:02:22 -0600 | [diff] [blame] | 241 | /// Deep copy. |
Aaron Jacobs | 11086dd | 2014-09-15 10:15:29 +1000 | [diff] [blame] | 242 | Value(const Value& other); |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 243 | ~Value(); |
Christopher Dunn | 6d135cb | 2007-06-13 15:51:04 +0000 | [diff] [blame] | 244 | |
Christopher Dunn | 66eb72f | 2015-01-20 11:02:22 -0600 | [diff] [blame] | 245 | // Deep copy, then swap(other). |
Aaron Jacobs | 11086dd | 2014-09-15 10:15:29 +1000 | [diff] [blame] | 246 | Value& operator=(Value other); |
Christopher Dunn | 66eb72f | 2015-01-20 11:02:22 -0600 | [diff] [blame] | 247 | /// Swap everything. |
Aaron Jacobs | 11086dd | 2014-09-15 10:15:29 +1000 | [diff] [blame] | 248 | void swap(Value& other); |
Christopher Dunn | 66eb72f | 2015-01-20 11:02:22 -0600 | [diff] [blame] | 249 | /// Swap values but leave comments and source offsets in place. |
| 250 | void swapPayload(Value& other); |
Christopher Dunn | 6d135cb | 2007-06-13 15:51:04 +0000 | [diff] [blame] | 251 | |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 252 | ValueType type() const; |
Christopher Dunn | 6d135cb | 2007-06-13 15:51:04 +0000 | [diff] [blame] | 253 | |
Christopher Dunn | 66eb72f | 2015-01-20 11:02:22 -0600 | [diff] [blame] | 254 | /// Compare payload only, not comments etc. |
Aaron Jacobs | 11086dd | 2014-09-15 10:15:29 +1000 | [diff] [blame] | 255 | bool operator<(const Value& other) const; |
| 256 | bool operator<=(const Value& other) const; |
| 257 | bool operator>=(const Value& other) const; |
| 258 | bool operator>(const Value& other) const; |
Aaron Jacobs | 11086dd | 2014-09-15 10:15:29 +1000 | [diff] [blame] | 259 | bool operator==(const Value& other) const; |
| 260 | bool operator!=(const Value& other) const; |
Aaron Jacobs | 11086dd | 2014-09-15 10:15:29 +1000 | [diff] [blame] | 261 | int compare(const Value& other) const; |
Christopher Dunn | 6d135cb | 2007-06-13 15:51:04 +0000 | [diff] [blame] | 262 | |
Aaron Jacobs | 11086dd | 2014-09-15 10:15:29 +1000 | [diff] [blame] | 263 | const char* asCString() const; |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 264 | std::string asString() const; |
| 265 | #ifdef JSON_USE_CPPTL |
| 266 | CppTL::ConstString asConstString() const; |
| 267 | #endif |
| 268 | Int asInt() const; |
| 269 | UInt asUInt() const; |
Aaron Jacobs | f1053e7 | 2011-05-24 03:18:02 +0000 | [diff] [blame] | 270 | #if defined(JSON_HAS_INT64) |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 271 | Int64 asInt64() const; |
| 272 | UInt64 asUInt64() const; |
Aaron Jacobs | f1053e7 | 2011-05-24 03:18:02 +0000 | [diff] [blame] | 273 | #endif // if defined(JSON_HAS_INT64) |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 274 | LargestInt asLargestInt() const; |
| 275 | LargestUInt asLargestUInt() const; |
| 276 | float asFloat() const; |
| 277 | double asDouble() const; |
| 278 | bool asBool() const; |
Christopher Dunn | 6d135cb | 2007-06-13 15:51:04 +0000 | [diff] [blame] | 279 | |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 280 | bool isNull() const; |
| 281 | bool isBool() const; |
| 282 | bool isInt() const; |
| 283 | bool isInt64() const; |
| 284 | bool isUInt() const; |
| 285 | bool isUInt64() const; |
| 286 | bool isIntegral() const; |
| 287 | bool isDouble() const; |
| 288 | bool isNumeric() const; |
| 289 | bool isString() const; |
| 290 | bool isArray() const; |
| 291 | bool isObject() const; |
Christopher Dunn | 6d135cb | 2007-06-13 15:51:04 +0000 | [diff] [blame] | 292 | |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 293 | bool isConvertibleTo(ValueType other) const; |
Christopher Dunn | 6d135cb | 2007-06-13 15:51:04 +0000 | [diff] [blame] | 294 | |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 295 | /// Number of values in array or object |
| 296 | ArrayIndex size() const; |
Christopher Dunn | 6d135cb | 2007-06-13 15:51:04 +0000 | [diff] [blame] | 297 | |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 298 | /// \brief Return true if empty array, empty object, or null; |
| 299 | /// otherwise, false. |
| 300 | bool empty() const; |
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 | /// Return isNull() |
| 303 | bool operator!() const; |
Christopher Dunn | 6d135cb | 2007-06-13 15:51:04 +0000 | [diff] [blame] | 304 | |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 305 | /// Remove all object members and array elements. |
| 306 | /// \pre type() is arrayValue, objectValue, or nullValue |
| 307 | /// \post type() is unchanged |
| 308 | void clear(); |
Christopher Dunn | 6d135cb | 2007-06-13 15:51:04 +0000 | [diff] [blame] | 309 | |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 310 | /// Resize the array to size elements. |
| 311 | /// New elements are initialized to null. |
| 312 | /// May only be called on nullValue or arrayValue. |
| 313 | /// \pre type() is arrayValue or nullValue |
| 314 | /// \post type() is arrayValue |
| 315 | void resize(ArrayIndex size); |
Christopher Dunn | 6d135cb | 2007-06-13 15:51:04 +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[](ArrayIndex 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 | /// If the array contains less than index element, then null value are |
| 327 | /// inserted |
| 328 | /// in the array so that its size is index+1. |
| 329 | /// (You may need to say 'value[0u]' to get your compiler to distinguish |
| 330 | /// this from the operator[] which takes a string.) |
Aaron Jacobs | 11086dd | 2014-09-15 10:15:29 +1000 | [diff] [blame] | 331 | Value& operator[](int index); |
Baptiste Lepilleur | fa130ef | 2010-12-24 12:47:14 +0000 | [diff] [blame] | 332 | |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 333 | /// Access an array element (zero based index ) |
| 334 | /// (You may need to say 'value[0u]' to get your compiler to distinguish |
| 335 | /// this from the operator[] which takes a string.) |
Aaron Jacobs | 11086dd | 2014-09-15 10:15:29 +1000 | [diff] [blame] | 336 | const Value& operator[](ArrayIndex index) const; |
Baptiste Lepilleur | fa130ef | 2010-12-24 12:47:14 +0000 | [diff] [blame] | 337 | |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 338 | /// Access an array element (zero based index ) |
| 339 | /// (You may need to say 'value[0u]' to get your compiler to distinguish |
| 340 | /// this from the operator[] which takes a string.) |
Aaron Jacobs | 11086dd | 2014-09-15 10:15:29 +1000 | [diff] [blame] | 341 | const Value& operator[](int index) const; |
Baptiste Lepilleur | fa130ef | 2010-12-24 12:47:14 +0000 | [diff] [blame] | 342 | |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 343 | /// If the array contains at least index+1 elements, returns the element |
| 344 | /// value, |
| 345 | /// otherwise returns defaultValue. |
Aaron Jacobs | 11086dd | 2014-09-15 10:15:29 +1000 | [diff] [blame] | 346 | Value get(ArrayIndex index, const Value& defaultValue) const; |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 347 | /// Return true if index < size(). |
| 348 | bool isValidIndex(ArrayIndex index) const; |
| 349 | /// \brief Append value to array at the end. |
| 350 | /// |
| 351 | /// Equivalent to jsonvalue[jsonvalue.size()] = value; |
Aaron Jacobs | 11086dd | 2014-09-15 10:15:29 +1000 | [diff] [blame] | 352 | Value& append(const Value& value); |
Christopher Dunn | 6d135cb | 2007-06-13 15:51:04 +0000 | [diff] [blame] | 353 | |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 354 | /// 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] | 355 | Value& operator[](const char* key); |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 356 | /// Access an object value by name, returns null if there is no member with |
| 357 | /// that name. |
Aaron Jacobs | 11086dd | 2014-09-15 10:15:29 +1000 | [diff] [blame] | 358 | const Value& operator[](const char* key) const; |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 359 | /// 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] | 360 | Value& operator[](const std::string& key); |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 361 | /// Access an object value by name, returns null if there is no member with |
| 362 | /// that name. |
Aaron Jacobs | 11086dd | 2014-09-15 10:15:29 +1000 | [diff] [blame] | 363 | const Value& operator[](const std::string& key) const; |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 364 | /** \brief Access an object value by name, create a null member if it does not |
| 365 | exist. |
Christopher Dunn | 6d135cb | 2007-06-13 15:51:04 +0000 | [diff] [blame] | 366 | |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 367 | * If the object as no entry for that name, then the member name used to store |
| 368 | * the new entry is not duplicated. |
| 369 | * Example of use: |
| 370 | * \code |
| 371 | * Json::Value object; |
| 372 | * static const StaticString code("code"); |
| 373 | * object[code] = 1234; |
| 374 | * \endcode |
| 375 | */ |
Aaron Jacobs | 11086dd | 2014-09-15 10:15:29 +1000 | [diff] [blame] | 376 | Value& operator[](const StaticString& key); |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 377 | #ifdef JSON_USE_CPPTL |
| 378 | /// 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] | 379 | Value& operator[](const CppTL::ConstString& key); |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 380 | /// Access an object value by name, returns null if there is no member with |
| 381 | /// that name. |
Aaron Jacobs | 11086dd | 2014-09-15 10:15:29 +1000 | [diff] [blame] | 382 | const Value& operator[](const CppTL::ConstString& key) const; |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 383 | #endif |
| 384 | /// Return the member named key if it exist, defaultValue otherwise. |
Aaron Jacobs | 11086dd | 2014-09-15 10:15:29 +1000 | [diff] [blame] | 385 | Value get(const char* key, const Value& defaultValue) const; |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 386 | /// Return the member named key if it exist, defaultValue otherwise. |
Aaron Jacobs | 11086dd | 2014-09-15 10:15:29 +1000 | [diff] [blame] | 387 | Value get(const std::string& key, const Value& defaultValue) const; |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 388 | #ifdef JSON_USE_CPPTL |
| 389 | /// Return the member named key if it exist, defaultValue otherwise. |
Aaron Jacobs | 11086dd | 2014-09-15 10:15:29 +1000 | [diff] [blame] | 390 | Value get(const CppTL::ConstString& key, const Value& defaultValue) const; |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 391 | #endif |
| 392 | /// \brief Remove and return the named member. |
| 393 | /// |
| 394 | /// Do nothing if it did not exist. |
| 395 | /// \return the removed Value, or null. |
| 396 | /// \pre type() is objectValue or nullValue |
| 397 | /// \post type() is unchanged |
Christopher Dunn | 76746b0 | 2015-01-21 16:01:30 -0600 | [diff] [blame] | 398 | /// \deprecated |
Aaron Jacobs | 11086dd | 2014-09-15 10:15:29 +1000 | [diff] [blame] | 399 | Value removeMember(const char* key); |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 400 | /// Same as removeMember(const char*) |
Christopher Dunn | 76746b0 | 2015-01-21 16:01:30 -0600 | [diff] [blame] | 401 | /// \deprecated |
Aaron Jacobs | 11086dd | 2014-09-15 10:15:29 +1000 | [diff] [blame] | 402 | Value removeMember(const std::string& key); |
Christopher Dunn | 76746b0 | 2015-01-21 16:01:30 -0600 | [diff] [blame] | 403 | /** \brief Remove the named map member. |
| 404 | |
| 405 | Update 'removed' iff removed. |
| 406 | \return true iff removed (no exceptions) |
| 407 | */ |
| 408 | bool removeMember(const char* key, Value* removed); |
Christopher Dunn | 9de2c2d | 2015-01-20 16:15:40 -0600 | [diff] [blame] | 409 | /** \brief Remove the indexed array element. |
| 410 | |
| 411 | O(n) expensive operations. |
| 412 | Update 'removed' iff removed. |
Christopher Dunn | e87e41c | 2015-01-20 16:24:11 -0600 | [diff] [blame] | 413 | \return true iff removed (no exceptions) |
Christopher Dunn | 9de2c2d | 2015-01-20 16:15:40 -0600 | [diff] [blame] | 414 | */ |
| 415 | bool removeIndex(ArrayIndex i, Value* removed); |
Christopher Dunn | 6d135cb | 2007-06-13 15:51:04 +0000 | [diff] [blame] | 416 | |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 417 | /// Return true if the object has a member named key. |
Aaron Jacobs | 11086dd | 2014-09-15 10:15:29 +1000 | [diff] [blame] | 418 | bool isMember(const char* key) const; |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 419 | /// Return true if the object has a member named key. |
Aaron Jacobs | 11086dd | 2014-09-15 10:15:29 +1000 | [diff] [blame] | 420 | bool isMember(const std::string& key) const; |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 421 | #ifdef JSON_USE_CPPTL |
| 422 | /// Return true if the object has a member named key. |
Aaron Jacobs | 11086dd | 2014-09-15 10:15:29 +1000 | [diff] [blame] | 423 | bool isMember(const CppTL::ConstString& key) const; |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 424 | #endif |
Christopher Dunn | 6d135cb | 2007-06-13 15:51:04 +0000 | [diff] [blame] | 425 | |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 426 | /// \brief Return a list of the member names. |
| 427 | /// |
| 428 | /// If null, return an empty list. |
| 429 | /// \pre type() is objectValue or nullValue |
| 430 | /// \post if type() was nullValue, it remains nullValue |
| 431 | Members getMemberNames() const; |
Christopher Dunn | 6d135cb | 2007-06-13 15:51:04 +0000 | [diff] [blame] | 432 | |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 433 | //# ifdef JSON_USE_CPPTL |
| 434 | // EnumMemberNames enumMemberNames() const; |
| 435 | // EnumValues enumValues() const; |
| 436 | //# endif |
Christopher Dunn | 6d135cb | 2007-06-13 15:51:04 +0000 | [diff] [blame] | 437 | |
Christopher Dunn | 1e3149a | 2015-01-25 14:16:13 -0600 | [diff] [blame] | 438 | /// \deprecated Always pass len. |
Aaron Jacobs | 11086dd | 2014-09-15 10:15:29 +1000 | [diff] [blame] | 439 | void setComment(const char* comment, CommentPlacement placement); |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 440 | /// Comments must be //... or /* ... */ |
Christopher Dunn | 1e3149a | 2015-01-25 14:16:13 -0600 | [diff] [blame] | 441 | void setComment(const char* comment, size_t len, CommentPlacement placement); |
| 442 | /// Comments must be //... or /* ... */ |
Aaron Jacobs | 11086dd | 2014-09-15 10:15:29 +1000 | [diff] [blame] | 443 | void setComment(const std::string& comment, CommentPlacement placement); |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 444 | bool hasComment(CommentPlacement placement) const; |
| 445 | /// Include delimiters and embedded newlines. |
| 446 | std::string getComment(CommentPlacement placement) const; |
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 | std::string toStyledString() const; |
Christopher Dunn | 6d135cb | 2007-06-13 15:51:04 +0000 | [diff] [blame] | 449 | |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 450 | const_iterator begin() const; |
| 451 | const_iterator end() const; |
Christopher Dunn | 6d135cb | 2007-06-13 15:51:04 +0000 | [diff] [blame] | 452 | |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 453 | iterator begin(); |
| 454 | iterator end(); |
Christopher Dunn | 6d135cb | 2007-06-13 15:51:04 +0000 | [diff] [blame] | 455 | |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 456 | // Accessors for the [start, limit) range of bytes within the JSON text from |
| 457 | // which this value was parsed, if any. |
| 458 | void setOffsetStart(size_t start); |
| 459 | void setOffsetLimit(size_t limit); |
| 460 | size_t getOffsetStart() const; |
| 461 | size_t getOffsetLimit() const; |
Aaron Jacobs | 68db655 | 2014-04-23 23:41:12 +0000 | [diff] [blame] | 462 | |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 463 | private: |
Billy Donahue | 8eb5d89 | 2014-11-10 01:35:42 -0500 | [diff] [blame] | 464 | void initBasic(ValueType type, bool allocated = false); |
| 465 | |
Aaron Jacobs | 11086dd | 2014-09-15 10:15:29 +1000 | [diff] [blame] | 466 | Value& resolveReference(const char* key, bool isStatic); |
Christopher Dunn | 6d135cb | 2007-06-13 15:51:04 +0000 | [diff] [blame] | 467 | |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 468 | struct CommentInfo { |
| 469 | CommentInfo(); |
| 470 | ~CommentInfo(); |
| 471 | |
Christopher Dunn | 1e3149a | 2015-01-25 14:16:13 -0600 | [diff] [blame] | 472 | void setComment(const char* text, size_t len); |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 473 | |
Aaron Jacobs | 11086dd | 2014-09-15 10:15:29 +1000 | [diff] [blame] | 474 | char* comment_; |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 475 | }; |
| 476 | |
| 477 | // struct MemberNamesTransform |
| 478 | //{ |
| 479 | // typedef const char *result_type; |
| 480 | // const char *operator()( const CZString &name ) const |
| 481 | // { |
| 482 | // return name.c_str(); |
| 483 | // } |
| 484 | //}; |
| 485 | |
| 486 | union ValueHolder { |
| 487 | LargestInt int_; |
| 488 | LargestUInt uint_; |
| 489 | double real_; |
| 490 | bool bool_; |
Aaron Jacobs | 11086dd | 2014-09-15 10:15:29 +1000 | [diff] [blame] | 491 | char* string_; |
Aaron Jacobs | 11086dd | 2014-09-15 10:15:29 +1000 | [diff] [blame] | 492 | ObjectValues* map_; |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 493 | } value_; |
| 494 | ValueType type_ : 8; |
Christopher Dunn | 2bc6137 | 2015-01-24 13:42:37 -0600 | [diff] [blame] | 495 | unsigned int allocated_ : 1; // Notes: if declared as bool, bitfield is useless. |
Aaron Jacobs | 11086dd | 2014-09-15 10:15:29 +1000 | [diff] [blame] | 496 | CommentInfo* comments_; |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 497 | |
| 498 | // [start, limit) byte offsets in the source JSON text from which this Value |
| 499 | // was extracted. |
| 500 | size_t start_; |
| 501 | size_t limit_; |
| 502 | }; |
| 503 | |
| 504 | /** \brief Experimental and untested: represents an element of the "path" to |
| 505 | * access a node. |
| 506 | */ |
| 507 | class JSON_API PathArgument { |
| 508 | public: |
| 509 | friend class Path; |
| 510 | |
| 511 | PathArgument(); |
| 512 | PathArgument(ArrayIndex index); |
Aaron Jacobs | 11086dd | 2014-09-15 10:15:29 +1000 | [diff] [blame] | 513 | PathArgument(const char* key); |
| 514 | PathArgument(const std::string& key); |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 515 | |
| 516 | private: |
| 517 | enum Kind { |
| 518 | kindNone = 0, |
| 519 | kindIndex, |
| 520 | kindKey |
| 521 | }; |
| 522 | std::string key_; |
| 523 | ArrayIndex index_; |
| 524 | Kind kind_; |
| 525 | }; |
| 526 | |
| 527 | /** \brief Experimental and untested: represents a "path" to access a node. |
| 528 | * |
| 529 | * Syntax: |
| 530 | * - "." => root node |
| 531 | * - ".[n]" => elements at index 'n' of root node (an array value) |
| 532 | * - ".name" => member named 'name' of root node (an object value) |
| 533 | * - ".name1.name2.name3" |
| 534 | * - ".[0][1][2].name1[3]" |
| 535 | * - ".%" => member name is provided as parameter |
| 536 | * - ".[%]" => index is provied as parameter |
| 537 | */ |
| 538 | class JSON_API Path { |
| 539 | public: |
Aaron Jacobs | 11086dd | 2014-09-15 10:15:29 +1000 | [diff] [blame] | 540 | Path(const std::string& path, |
| 541 | const PathArgument& a1 = PathArgument(), |
| 542 | const PathArgument& a2 = PathArgument(), |
| 543 | const PathArgument& a3 = PathArgument(), |
| 544 | const PathArgument& a4 = PathArgument(), |
| 545 | const PathArgument& a5 = PathArgument()); |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 546 | |
Aaron Jacobs | 11086dd | 2014-09-15 10:15:29 +1000 | [diff] [blame] | 547 | const Value& resolve(const Value& root) const; |
| 548 | Value resolve(const Value& root, const Value& defaultValue) const; |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 549 | /// Creates the "path" to access the specified node and returns a reference on |
| 550 | /// the node. |
Aaron Jacobs | 11086dd | 2014-09-15 10:15:29 +1000 | [diff] [blame] | 551 | Value& make(Value& root) const; |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 552 | |
| 553 | private: |
Aaron Jacobs | 11086dd | 2014-09-15 10:15:29 +1000 | [diff] [blame] | 554 | typedef std::vector<const PathArgument*> InArgs; |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 555 | typedef std::vector<PathArgument> Args; |
| 556 | |
Aaron Jacobs | 11086dd | 2014-09-15 10:15:29 +1000 | [diff] [blame] | 557 | void makePath(const std::string& path, const InArgs& in); |
| 558 | void addPathInArg(const std::string& path, |
| 559 | const InArgs& in, |
| 560 | InArgs::const_iterator& itInArg, |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 561 | PathArgument::Kind kind); |
Aaron Jacobs | 11086dd | 2014-09-15 10:15:29 +1000 | [diff] [blame] | 562 | void invalidPath(const std::string& path, int location); |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 563 | |
| 564 | Args args_; |
| 565 | }; |
| 566 | |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 567 | /** \brief base class for Value iterators. |
| 568 | * |
| 569 | */ |
| 570 | class JSON_API ValueIteratorBase { |
| 571 | public: |
| 572 | typedef std::bidirectional_iterator_tag iterator_category; |
| 573 | typedef unsigned int size_t; |
| 574 | typedef int difference_type; |
| 575 | typedef ValueIteratorBase SelfType; |
Christopher Dunn | 6d135cb | 2007-06-13 15:51:04 +0000 | [diff] [blame] | 576 | |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 577 | ValueIteratorBase(); |
Aaron Jacobs | 11086dd | 2014-09-15 10:15:29 +1000 | [diff] [blame] | 578 | explicit ValueIteratorBase(const Value::ObjectValues::iterator& current); |
Christopher Dunn | 6d135cb | 2007-06-13 15:51:04 +0000 | [diff] [blame] | 579 | |
Aaron Jacobs | 11086dd | 2014-09-15 10:15:29 +1000 | [diff] [blame] | 580 | bool operator==(const SelfType& other) const { return isEqual(other); } |
Christopher Dunn | 6d135cb | 2007-06-13 15:51:04 +0000 | [diff] [blame] | 581 | |
Aaron Jacobs | 11086dd | 2014-09-15 10:15:29 +1000 | [diff] [blame] | 582 | bool operator!=(const SelfType& other) const { return !isEqual(other); } |
Christopher Dunn | 6d135cb | 2007-06-13 15:51:04 +0000 | [diff] [blame] | 583 | |
Aaron Jacobs | 11086dd | 2014-09-15 10:15:29 +1000 | [diff] [blame] | 584 | difference_type operator-(const SelfType& other) const { |
Kevin Grant | 4c5832a | 2015-02-14 20:53:35 -0800 | [diff] [blame] | 585 | return other.computeDistance(*this); |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 586 | } |
Christopher Dunn | 6d135cb | 2007-06-13 15:51:04 +0000 | [diff] [blame] | 587 | |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 588 | /// Return either the index or the member name of the referenced value as a |
| 589 | /// Value. |
| 590 | Value key() const; |
Christopher Dunn | 6d135cb | 2007-06-13 15:51:04 +0000 | [diff] [blame] | 591 | |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 592 | /// Return the index of the referenced Value. -1 if it is not an arrayValue. |
| 593 | UInt index() const; |
Christopher Dunn | 6d135cb | 2007-06-13 15:51:04 +0000 | [diff] [blame] | 594 | |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 595 | /// Return the member name of the referenced Value. "" if it is not an |
| 596 | /// objectValue. |
Aaron Jacobs | 11086dd | 2014-09-15 10:15:29 +1000 | [diff] [blame] | 597 | const char* memberName() const; |
Christopher Dunn | 6d135cb | 2007-06-13 15:51:04 +0000 | [diff] [blame] | 598 | |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 599 | protected: |
Aaron Jacobs | 11086dd | 2014-09-15 10:15:29 +1000 | [diff] [blame] | 600 | Value& deref() const; |
Christopher Dunn | 6d135cb | 2007-06-13 15:51:04 +0000 | [diff] [blame] | 601 | |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 602 | void increment(); |
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 | void decrement(); |
Christopher Dunn | 6d135cb | 2007-06-13 15:51:04 +0000 | [diff] [blame] | 605 | |
Aaron Jacobs | 11086dd | 2014-09-15 10:15:29 +1000 | [diff] [blame] | 606 | difference_type computeDistance(const SelfType& other) const; |
Christopher Dunn | 6d135cb | 2007-06-13 15:51:04 +0000 | [diff] [blame] | 607 | |
Aaron Jacobs | 11086dd | 2014-09-15 10:15:29 +1000 | [diff] [blame] | 608 | bool isEqual(const SelfType& other) const; |
Christopher Dunn | 6d135cb | 2007-06-13 15:51:04 +0000 | [diff] [blame] | 609 | |
Aaron Jacobs | 11086dd | 2014-09-15 10:15:29 +1000 | [diff] [blame] | 610 | void copy(const SelfType& other); |
Christopher Dunn | 6d135cb | 2007-06-13 15:51:04 +0000 | [diff] [blame] | 611 | |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 612 | private: |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 613 | Value::ObjectValues::iterator current_; |
| 614 | // Indicates that iterator is for a null value. |
| 615 | bool isNull_; |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 616 | }; |
Christopher Dunn | 6d135cb | 2007-06-13 15:51:04 +0000 | [diff] [blame] | 617 | |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 618 | /** \brief const iterator for object and array value. |
| 619 | * |
| 620 | */ |
| 621 | class JSON_API ValueConstIterator : public ValueIteratorBase { |
| 622 | friend class Value; |
Christopher Dunn | 6d135cb | 2007-06-13 15:51:04 +0000 | [diff] [blame] | 623 | |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 624 | public: |
| 625 | typedef const Value value_type; |
| 626 | typedef unsigned int size_t; |
| 627 | typedef int difference_type; |
Aaron Jacobs | 11086dd | 2014-09-15 10:15:29 +1000 | [diff] [blame] | 628 | typedef const Value& reference; |
| 629 | typedef const Value* pointer; |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 630 | typedef ValueConstIterator SelfType; |
| 631 | |
| 632 | ValueConstIterator(); |
| 633 | |
| 634 | private: |
| 635 | /*! \internal Use by Value to create an iterator. |
| 636 | */ |
Aaron Jacobs | 11086dd | 2014-09-15 10:15:29 +1000 | [diff] [blame] | 637 | explicit ValueConstIterator(const Value::ObjectValues::iterator& current); |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 638 | public: |
Aaron Jacobs | 11086dd | 2014-09-15 10:15:29 +1000 | [diff] [blame] | 639 | SelfType& operator=(const ValueIteratorBase& other); |
Christopher Dunn | 6d135cb | 2007-06-13 15:51:04 +0000 | [diff] [blame] | 640 | |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 641 | SelfType operator++(int) { |
| 642 | SelfType temp(*this); |
| 643 | ++*this; |
| 644 | return temp; |
| 645 | } |
Christopher Dunn | 6d135cb | 2007-06-13 15:51:04 +0000 | [diff] [blame] | 646 | |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 647 | SelfType operator--(int) { |
| 648 | SelfType temp(*this); |
| 649 | --*this; |
| 650 | return temp; |
| 651 | } |
Christopher Dunn | 6d135cb | 2007-06-13 15:51:04 +0000 | [diff] [blame] | 652 | |
Aaron Jacobs | 11086dd | 2014-09-15 10:15:29 +1000 | [diff] [blame] | 653 | SelfType& operator--() { |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 654 | decrement(); |
| 655 | return *this; |
| 656 | } |
Christopher Dunn | 6d135cb | 2007-06-13 15:51:04 +0000 | [diff] [blame] | 657 | |
Aaron Jacobs | 11086dd | 2014-09-15 10:15:29 +1000 | [diff] [blame] | 658 | SelfType& operator++() { |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 659 | increment(); |
| 660 | return *this; |
| 661 | } |
Christopher Dunn | 6d135cb | 2007-06-13 15:51:04 +0000 | [diff] [blame] | 662 | |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 663 | reference operator*() const { return deref(); } |
Braden McDorman | 540db3b | 2014-09-14 02:31:23 -0500 | [diff] [blame] | 664 | |
| 665 | pointer operator->() const { return &deref(); } |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 666 | }; |
Christopher Dunn | 6d135cb | 2007-06-13 15:51:04 +0000 | [diff] [blame] | 667 | |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 668 | /** \brief Iterator for object and array value. |
| 669 | */ |
| 670 | class JSON_API ValueIterator : public ValueIteratorBase { |
| 671 | friend class Value; |
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 | public: |
| 674 | typedef Value value_type; |
| 675 | typedef unsigned int size_t; |
| 676 | typedef int difference_type; |
Aaron Jacobs | 11086dd | 2014-09-15 10:15:29 +1000 | [diff] [blame] | 677 | typedef Value& reference; |
| 678 | typedef Value* pointer; |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 679 | typedef ValueIterator SelfType; |
Christopher Dunn | 6d135cb | 2007-06-13 15:51:04 +0000 | [diff] [blame] | 680 | |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 681 | ValueIterator(); |
Aaron Jacobs | 11086dd | 2014-09-15 10:15:29 +1000 | [diff] [blame] | 682 | ValueIterator(const ValueConstIterator& other); |
| 683 | ValueIterator(const ValueIterator& other); |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 684 | |
| 685 | private: |
| 686 | /*! \internal Use by Value to create an iterator. |
| 687 | */ |
Aaron Jacobs | 11086dd | 2014-09-15 10:15:29 +1000 | [diff] [blame] | 688 | explicit ValueIterator(const Value::ObjectValues::iterator& current); |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 689 | public: |
Aaron Jacobs | 11086dd | 2014-09-15 10:15:29 +1000 | [diff] [blame] | 690 | SelfType& operator=(const SelfType& other); |
Christopher Dunn | 6d135cb | 2007-06-13 15:51:04 +0000 | [diff] [blame] | 691 | |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 692 | SelfType operator++(int) { |
| 693 | SelfType temp(*this); |
| 694 | ++*this; |
| 695 | return temp; |
| 696 | } |
Christopher Dunn | 6d135cb | 2007-06-13 15:51:04 +0000 | [diff] [blame] | 697 | |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 698 | SelfType operator--(int) { |
| 699 | SelfType temp(*this); |
| 700 | --*this; |
| 701 | return temp; |
| 702 | } |
Christopher Dunn | 6d135cb | 2007-06-13 15:51:04 +0000 | [diff] [blame] | 703 | |
Aaron Jacobs | 11086dd | 2014-09-15 10:15:29 +1000 | [diff] [blame] | 704 | SelfType& operator--() { |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 705 | decrement(); |
| 706 | return *this; |
| 707 | } |
Christopher Dunn | 6d135cb | 2007-06-13 15:51:04 +0000 | [diff] [blame] | 708 | |
Aaron Jacobs | 11086dd | 2014-09-15 10:15:29 +1000 | [diff] [blame] | 709 | SelfType& operator++() { |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 710 | increment(); |
| 711 | return *this; |
| 712 | } |
Christopher Dunn | 6d135cb | 2007-06-13 15:51:04 +0000 | [diff] [blame] | 713 | |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 714 | reference operator*() const { return deref(); } |
Braden McDorman | 540db3b | 2014-09-14 02:31:23 -0500 | [diff] [blame] | 715 | |
| 716 | pointer operator->() const { return &deref(); } |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 717 | }; |
Christopher Dunn | 6d135cb | 2007-06-13 15:51:04 +0000 | [diff] [blame] | 718 | |
| 719 | } // namespace Json |
| 720 | |
datadiode | 9454e68 | 2015-01-20 15:25:04 -0600 | [diff] [blame] | 721 | |
| 722 | namespace std { |
| 723 | /// Specialize std::swap() for Json::Value. |
| 724 | template<> |
| 725 | inline void swap(Json::Value& a, Json::Value& b) { a.swap(b); } |
| 726 | } |
| 727 | |
| 728 | |
Baptiste Lepilleur | eafd702 | 2013-05-08 20:21:11 +0000 | [diff] [blame] | 729 | #if defined(JSONCPP_DISABLE_DLL_INTERFACE_WARNING) |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 730 | #pragma warning(pop) |
Baptiste Lepilleur | eafd702 | 2013-05-08 20:21:11 +0000 | [diff] [blame] | 731 | #endif // if defined(JSONCPP_DISABLE_DLL_INTERFACE_WARNING) |
| 732 | |
Christopher Dunn | 6d135cb | 2007-06-13 15:51:04 +0000 | [diff] [blame] | 733 | #endif // CPPTL_JSON_H_INCLUDED |