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