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