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