blob: ebca175db6dd0f02921d2d176cb7f63415cd2960 [file] [log] [blame]
Devin Jeanpierre59e4d352017-07-21 03:44:36 -07001// Copyright 2007-2010 Baptiste Lepilleur and The JsonCpp Authors
Baptiste Lepilleur7469f1d2010-04-20 21:35:19 +00002// 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 Dunn6d135cb2007-06-13 15:51:04 +00006#ifndef CPPTL_JSON_H_INCLUDED
Aaron Jacobs9fa4e842014-07-01 08:48:54 +10007#define CPPTL_JSON_H_INCLUDED
Christopher Dunn6d135cb2007-06-13 15:51:04 +00008
Baptiste Lepilleureadc4782011-05-02 21:09:30 +00009#if !defined(JSON_IS_AMALGAMATION)
Aaron Jacobs9fa4e842014-07-01 08:48:54 +100010#include "forwards.h"
Baptiste Lepilleureadc4782011-05-02 21:09:30 +000011#endif // if !defined(JSON_IS_AMALGAMATION)
Aaron Jacobs9fa4e842014-07-01 08:48:54 +100012#include <string>
13#include <vector>
Christopher Dunn75279cc2015-03-08 12:20:06 -050014#include <exception>
Christopher Dunn6d135cb2007-06-13 15:51:04 +000015
Aaron Jacobs9fa4e842014-07-01 08:48:54 +100016#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 Dunn6d135cb2007-06-13 15:51:04 +000024
Gauravd97ea5b2016-03-16 11:15:09 +053025//Conditional NORETURN attribute on the throw functions would:
Dhruv Paranjape8996c372017-07-08 17:27:07 +053026// a) suppress false positives from static code analysis
Gauravd97ea5b2016-03-16 11:15:09 +053027// b) possibly improve optimization opportunities.
28#if !defined(JSONCPP_NORETURN)
29# if defined(_MSC_VER)
30# define JSONCPP_NORETURN __declspec(noreturn)
31# elif defined(__GNUC__)
32# define JSONCPP_NORETURN __attribute__ ((__noreturn__))
33# else
34# define JSONCPP_NORETURN
35# endif
36#endif
37
Aaron Jacobs9fa4e842014-07-01 08:48:54 +100038// Disable warning C4251: <data member>: <type> needs to have dll-interface to
39// be used by...
Baptiste Lepilleureafd7022013-05-08 20:21:11 +000040#if defined(JSONCPP_DISABLE_DLL_INTERFACE_WARNING)
Aaron Jacobs9fa4e842014-07-01 08:48:54 +100041#pragma warning(push)
42#pragma warning(disable : 4251)
Baptiste Lepilleureafd7022013-05-08 20:21:11 +000043#endif // if defined(JSONCPP_DISABLE_DLL_INTERFACE_WARNING)
44
Sergiy80d6e666f2016-12-03 22:29:14 +020045#pragma pack(push, 8)
46
Christopher Dunn6d135cb2007-06-13 15:51:04 +000047/** \brief JSON (JavaScript Object Notation).
48 */
49namespace Json {
50
Christopher Dunn75279cc2015-03-08 12:20:06 -050051/** Base class for all exceptions we throw.
Christopher Dunn4e30c4f2015-03-08 12:56:32 -050052 *
53 * We use nothing but these internally. Of course, STL can throw others.
Christopher Dunn75279cc2015-03-08 12:20:06 -050054 */
Christopher Dunn949babd2015-07-23 00:19:12 -050055class JSON_API Exception : public std::exception {
56public:
Christopher Dunnde5b7922016-03-06 11:19:46 -060057 Exception(JSONCPP_STRING const& msg);
Omkar Wagh91c1d232016-11-07 13:57:00 -050058 ~Exception() JSONCPP_NOEXCEPT JSONCPP_OVERRIDE;
59 char const* what() const JSONCPP_NOEXCEPT JSONCPP_OVERRIDE;
Christopher Dunn949babd2015-07-23 00:19:12 -050060protected:
Christopher Dunnde5b7922016-03-06 11:19:46 -060061 JSONCPP_STRING msg_;
Christopher Dunn949babd2015-07-23 00:19:12 -050062};
63
Christopher Dunn53837942015-03-08 12:31:00 -050064/** Exceptions which the user cannot easily avoid.
65 *
Christopher Dunn4e30c4f2015-03-08 12:56:32 -050066 * E.g. out-of-memory (when we use malloc), stack-overflow, malicious input
Dhruv Paranjape8996c372017-07-08 17:27:07 +053067 *
Christopher Dunn4e30c4f2015-03-08 12:56:32 -050068 * \remark derived from Json::Exception
Christopher Dunn53837942015-03-08 12:31:00 -050069 */
Christopher Dunn949babd2015-07-23 00:19:12 -050070class JSON_API RuntimeError : public Exception {
71public:
Christopher Dunnde5b7922016-03-06 11:19:46 -060072 RuntimeError(JSONCPP_STRING const& msg);
Christopher Dunn949babd2015-07-23 00:19:12 -050073};
74
Christopher Dunn4e30c4f2015-03-08 12:56:32 -050075/** Exceptions thrown by JSON_ASSERT/JSON_FAIL macros.
Christopher Dunn53837942015-03-08 12:31:00 -050076 *
77 * These are precondition-violations (user bugs) and internal errors (our bugs).
Dhruv Paranjape8996c372017-07-08 17:27:07 +053078 *
Christopher Dunn4e30c4f2015-03-08 12:56:32 -050079 * \remark derived from Json::Exception
Christopher Dunn53837942015-03-08 12:31:00 -050080 */
Christopher Dunn949babd2015-07-23 00:19:12 -050081class JSON_API LogicError : public Exception {
82public:
Christopher Dunnde5b7922016-03-06 11:19:46 -060083 LogicError(JSONCPP_STRING const& msg);
Christopher Dunn949babd2015-07-23 00:19:12 -050084};
Christopher Dunn53837942015-03-08 12:31:00 -050085
Christopher Dunn4e30c4f2015-03-08 12:56:32 -050086/// used internally
Gauravd97ea5b2016-03-16 11:15:09 +053087JSONCPP_NORETURN void throwRuntimeError(JSONCPP_STRING const& msg);
Christopher Dunn4e30c4f2015-03-08 12:56:32 -050088/// used internally
Gauravd97ea5b2016-03-16 11:15:09 +053089JSONCPP_NORETURN void throwLogicError(JSONCPP_STRING const& msg);
Christopher Dunn75279cc2015-03-08 12:20:06 -050090
Aaron Jacobs9fa4e842014-07-01 08:48:54 +100091/** \brief Type of the value held by a Value object.
92 */
93enum ValueType {
94 nullValue = 0, ///< 'null' value
95 intValue, ///< signed integer value
96 uintValue, ///< unsigned integer value
97 realValue, ///< double value
98 stringValue, ///< UTF-8 string value
99 booleanValue, ///< bool value
100 arrayValue, ///< array value (ordered list)
101 objectValue ///< object value (collection of name/value pairs).
102};
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000103
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000104enum CommentPlacement {
105 commentBefore = 0, ///< a comment placed on the line before a value
106 commentAfterOnSameLine, ///< a comment just after a value on the same line
107 commentAfter, ///< a comment on the line after a value (only make sense for
Aaron Jacobs3a0c4fc2014-07-01 09:20:48 +1000108 /// root value)
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000109 numberOfCommentPlacement
110};
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000111
112//# ifdef JSON_USE_CPPTL
113// typedef CppTL::AnyEnumerator<const char *> EnumMemberNames;
114// typedef CppTL::AnyEnumerator<const Value &> EnumValues;
115//# endif
116
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000117/** \brief Lightweight wrapper to tag static string.
118 *
119 * Value constructor and objectValue member assignement takes advantage of the
120 * StaticString and avoid the cost of string duplication when storing the
121 * string or the member name.
122 *
123 * Example of usage:
124 * \code
125 * Json::Value aValue( StaticString("some text") );
126 * Json::Value object;
127 * static const StaticString code("code");
128 * object[code] = 1234;
129 * \endcode
130 */
131class JSON_API StaticString {
132public:
Christopher Dunnff617522015-03-06 10:31:46 -0600133 explicit StaticString(const char* czstring) : c_str_(czstring) {}
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000134
Christopher Dunnff617522015-03-06 10:31:46 -0600135 operator const char*() const { return c_str_; }
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000136
Christopher Dunnff617522015-03-06 10:31:46 -0600137 const char* c_str() const { return c_str_; }
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000138
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000139private:
Christopher Dunnff617522015-03-06 10:31:46 -0600140 const char* c_str_;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000141};
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000142
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000143/** \brief Represents a <a HREF="http://www.json.org">JSON</a> value.
144 *
145 * This class is a discriminated union wrapper that can represents a:
146 * - signed integer [range: Value::minInt - Value::maxInt]
147 * - unsigned integer (range: 0 - Value::maxUInt)
148 * - double
149 * - UTF-8 string
150 * - boolean
151 * - 'null'
152 * - an ordered list of Value
153 * - collection of name/value pairs (javascript object)
154 *
155 * The type of the held value is represented by a #ValueType and
156 * can be obtained using type().
157 *
Christopher Dunnc28610f2015-02-21 11:44:16 -0600158 * Values of an #objectValue or #arrayValue can be accessed using operator[]()
159 * methods.
160 * Non-const methods will automatically create the a #nullValue element
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000161 * if it does not exist.
Christopher Dunnc28610f2015-02-21 11:44:16 -0600162 * The sequence of an #arrayValue will be automatically resized and initialized
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000163 * with #nullValue. resize() can be used to enlarge or truncate an #arrayValue.
164 *
Christopher Dunnc28610f2015-02-21 11:44:16 -0600165 * The get() methods can be used to obtain default value in the case the
166 * required element does not exist.
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000167 *
168 * It is possible to iterate over the list of a #objectValue values using
169 * the getMemberNames() method.
Christopher Dunn25342ba2015-03-02 18:05:26 -0600170 *
Christopher Dunnc28610f2015-02-21 11:44:16 -0600171 * \note #Value string-length fit in size_t, but keys must be < 2^30.
172 * (The reason is an implementation detail.) A #CharReader will raise an
Christopher Dunn25342ba2015-03-02 18:05:26 -0600173 * exception if a bound is exceeded to avoid security holes in your app,
174 * but the Value API does *not* check bounds. That is the responsibility
175 * of the caller.
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000176 */
177class JSON_API Value {
178 friend class ValueIteratorBase;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000179public:
Christopher Dunnde5b7922016-03-06 11:19:46 -0600180 typedef std::vector<JSONCPP_STRING> Members;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000181 typedef ValueIterator iterator;
182 typedef ValueConstIterator const_iterator;
183 typedef Json::UInt UInt;
184 typedef Json::Int Int;
185#if defined(JSON_HAS_INT64)
186 typedef Json::UInt64 UInt64;
187 typedef Json::Int64 Int64;
Baptiste Lepilleur842e9ac2010-12-27 17:45:23 +0000188#endif // defined(JSON_HAS_INT64)
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000189 typedef Json::LargestInt LargestInt;
190 typedef Json::LargestUInt LargestUInt;
191 typedef Json::ArrayIndex ArrayIndex;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000192
Christopher Dunn8a702972015-03-03 10:38:27 -0600193 static const Value& null; ///< We regret this reference to a global instance; prefer the simpler Value().
194 static const Value& nullRef; ///< just a kludge for binary-compatibility; same as null
Christopher Dunn0f288ae2016-06-26 18:47:43 -0500195 static Value const& nullSingleton(); ///< Prefer this to null or nullRef.
196
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000197 /// Minimum signed integer value that can be stored in a Json::Value.
198 static const LargestInt minLargestInt;
199 /// Maximum signed integer value that can be stored in a Json::Value.
200 static const LargestInt maxLargestInt;
201 /// Maximum unsigned integer value that can be stored in a Json::Value.
202 static const LargestUInt maxLargestUInt;
Baptiste Lepilleur842e9ac2010-12-27 17:45:23 +0000203
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000204 /// Minimum signed int value that can be stored in a Json::Value.
205 static const Int minInt;
206 /// Maximum signed int value that can be stored in a Json::Value.
207 static const Int maxInt;
208 /// Maximum unsigned int value that can be stored in a Json::Value.
209 static const UInt maxUInt;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000210
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000211#if defined(JSON_HAS_INT64)
212 /// Minimum signed 64 bits int value that can be stored in a Json::Value.
213 static const Int64 minInt64;
214 /// Maximum signed 64 bits int value that can be stored in a Json::Value.
215 static const Int64 maxInt64;
216 /// Maximum unsigned 64 bits int value that can be stored in a Json::Value.
217 static const UInt64 maxUInt64;
Aaron Jacobsf1053e72011-05-24 03:18:02 +0000218#endif // defined(JSON_HAS_INT64)
Baptiste Lepilleur842e9ac2010-12-27 17:45:23 +0000219
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000220private:
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000221#ifndef JSONCPP_DOC_EXCLUDE_IMPLEMENTATION
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000222 class CZString {
223 public:
224 enum DuplicationPolicy {
225 noDuplication = 0,
226 duplicate,
227 duplicateOnCopy
228 };
229 CZString(ArrayIndex index);
Christopher Dunnc28610f2015-02-21 11:44:16 -0600230 CZString(char const* str, unsigned length, DuplicationPolicy allocate);
231 CZString(CZString const& other);
Motti2b008912015-04-20 17:44:47 +0300232#if JSON_HAS_RVALUE_REFERENCES
233 CZString(CZString&& other);
234#endif
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000235 ~CZString();
Dhruv Paranjape0ba8bd72017-07-08 17:47:13 +0530236 CZString& operator=(const CZString& other);
237
238#if JSON_HAS_RVALUE_REFERENCES
239 CZString& operator=(CZString&& other);
240#endif
241
Christopher Dunnc28610f2015-02-21 11:44:16 -0600242 bool operator<(CZString const& other) const;
243 bool operator==(CZString const& other) const;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000244 ArrayIndex index() const;
Christopher Dunnc28610f2015-02-21 11:44:16 -0600245 //const char* c_str() const; ///< \deprecated
246 char const* data() const;
247 unsigned length() const;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000248 bool isStaticString() const;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000249
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000250 private:
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000251 void swap(CZString& other);
Christopher Dunnc28610f2015-02-21 11:44:16 -0600252
Christopher Dunn57ad0512015-03-02 12:10:35 -0600253 struct StringStorage {
Dan Liufcbab022015-04-27 05:20:05 +0100254 unsigned policy_: 2;
Christopher Dunn57ad0512015-03-02 12:10:35 -0600255 unsigned length_: 30; // 1GB max
256 };
257
Christopher Dunnc28610f2015-02-21 11:44:16 -0600258 char const* cstr_; // actually, a prefixed string, unless policy is noDup
Christopher Dunn57ad0512015-03-02 12:10:35 -0600259 union {
260 ArrayIndex index_;
261 StringStorage storage_;
262 };
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000263 };
264
265public:
266#ifndef JSON_USE_CPPTL_SMALLMAP
267 typedef std::map<CZString, Value> ObjectValues;
268#else
269 typedef CppTL::SmallMap<CZString, Value> ObjectValues;
270#endif // ifndef JSON_USE_CPPTL_SMALLMAP
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000271#endif // ifndef JSONCPP_DOC_EXCLUDE_IMPLEMENTATION
272
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000273public:
274 /** \brief Create a default Value of the given type.
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000275
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000276 This is a very useful constructor.
277 To create an empty array, pass arrayValue.
278 To create an empty object, pass objectValue.
279 Another Value can then be set to this one by assignment.
280This is useful since clear() and resize() will not alter types.
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000281
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000282 Examples:
283\code
284Json::Value null_value; // null
285Json::Value arr_value(Json::arrayValue); // []
286Json::Value obj_value(Json::objectValue); // {}
287\endcode
288 */
289 Value(ValueType type = nullValue);
290 Value(Int value);
291 Value(UInt value);
Baptiste Lepilleur842e9ac2010-12-27 17:45:23 +0000292#if defined(JSON_HAS_INT64)
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000293 Value(Int64 value);
294 Value(UInt64 value);
Baptiste Lepilleur842e9ac2010-12-27 17:45:23 +0000295#endif // if defined(JSON_HAS_INT64)
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000296 Value(double value);
Christopher Dunn8a702972015-03-03 10:38:27 -0600297 Value(const char* value); ///< Copy til first 0. (NULL causes to seg-fault.)
Christopher Dunn89704032015-07-11 12:09:59 -0500298 Value(const char* begin, const char* end); ///< Copy all, incl zeroes.
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000299 /** \brief Constructs a value from a static string.
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000300
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000301 * Like other value string constructor but do not duplicate the string for
302 * internal storage. The given string must remain alive after the call to this
303 * constructor.
Christopher Dunnc28610f2015-02-21 11:44:16 -0600304 * \note This works only for null-terminated strings. (We cannot change the
305 * size of this class, so we have nowhere to store the length,
306 * which might be computed later for various operations.)
307 *
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000308 * Example of usage:
309 * \code
Christopher Dunnc28610f2015-02-21 11:44:16 -0600310 * static StaticString foo("some text");
311 * Json::Value aValue(foo);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000312 * \endcode
313 */
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000314 Value(const StaticString& value);
Christopher Dunnde5b7922016-03-06 11:19:46 -0600315 Value(const JSONCPP_STRING& value); ///< Copy data() til size(). Embedded zeroes too.
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000316#ifdef JSON_USE_CPPTL
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000317 Value(const CppTL::ConstString& value);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000318#endif
319 Value(bool value);
Christopher Dunn66eb72f2015-01-20 11:02:22 -0600320 /// Deep copy.
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000321 Value(const Value& other);
Motti2b008912015-04-20 17:44:47 +0300322#if JSON_HAS_RVALUE_REFERENCES
323 /// Move constructor
324 Value(Value&& other);
325#endif
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000326 ~Value();
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000327
Christopher Dunn7f439f42015-03-06 09:22:57 -0600328 /// Deep copy, then swap(other).
329 /// \note Over-write existing comments. To preserve comments, use #swapPayload().
Александр Малинин6a15ca62017-07-31 15:29:02 +0300330 Value& operator=(Value other);
Dhruv Paranjape8996c372017-07-08 17:27:07 +0530331
Christopher Dunn66eb72f2015-01-20 11:02:22 -0600332 /// Swap everything.
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000333 void swap(Value& other);
Christopher Dunn66eb72f2015-01-20 11:02:22 -0600334 /// Swap values but leave comments and source offsets in place.
335 void swapPayload(Value& other);
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000336
Dhruv Paranjape8996c372017-07-08 17:27:07 +0530337 /// copy everything.
338 void copy(const Value& other);
339 /// copy values but leave comments and source offsets in place.
340 void copyPayload(const Value& other);
341
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000342 ValueType type() const;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000343
Christopher Dunn66eb72f2015-01-20 11:02:22 -0600344 /// Compare payload only, not comments etc.
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000345 bool operator<(const Value& other) const;
346 bool operator<=(const Value& other) const;
347 bool operator>=(const Value& other) const;
348 bool operator>(const Value& other) const;
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000349 bool operator==(const Value& other) const;
350 bool operator!=(const Value& other) const;
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000351 int compare(const Value& other) const;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000352
Christopher Dunn8a702972015-03-03 10:38:27 -0600353 const char* asCString() const; ///< Embedded zeroes could cause you trouble!
dawescae564652016-03-14 19:11:02 -0500354#if JSONCPP_USING_SECURE_MEMORY
355 unsigned getCStringLength() const; //Allows you to understand the length of the CString
356#endif
Christopher Dunnde5b7922016-03-06 11:19:46 -0600357 JSONCPP_STRING asString() const; ///< Embedded zeroes are possible.
Christopher Dunnc28610f2015-02-21 11:44:16 -0600358 /** Get raw char* of string-value.
359 * \return false if !string. (Seg-fault if str or end are NULL.)
360 */
361 bool getString(
Christopher Dunn89704032015-07-11 12:09:59 -0500362 char const** begin, char const** end) const;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000363#ifdef JSON_USE_CPPTL
364 CppTL::ConstString asConstString() const;
365#endif
366 Int asInt() const;
367 UInt asUInt() const;
Aaron Jacobsf1053e72011-05-24 03:18:02 +0000368#if defined(JSON_HAS_INT64)
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000369 Int64 asInt64() const;
370 UInt64 asUInt64() const;
Aaron Jacobsf1053e72011-05-24 03:18:02 +0000371#endif // if defined(JSON_HAS_INT64)
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000372 LargestInt asLargestInt() const;
373 LargestUInt asLargestUInt() const;
374 float asFloat() const;
375 double asDouble() const;
376 bool asBool() const;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000377
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000378 bool isNull() const;
379 bool isBool() const;
380 bool isInt() const;
381 bool isInt64() const;
382 bool isUInt() const;
383 bool isUInt64() const;
384 bool isIntegral() const;
385 bool isDouble() const;
386 bool isNumeric() const;
387 bool isString() const;
388 bool isArray() const;
389 bool isObject() const;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000390
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000391 bool isConvertibleTo(ValueType other) const;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000392
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000393 /// Number of values in array or object
394 ArrayIndex size() const;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000395
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000396 /// \brief Return true if empty array, empty object, or null;
397 /// otherwise, false.
398 bool empty() const;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000399
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000400 /// Return isNull()
401 bool operator!() const;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000402
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000403 /// Remove all object members and array elements.
404 /// \pre type() is arrayValue, objectValue, or nullValue
405 /// \post type() is unchanged
406 void clear();
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000407
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000408 /// Resize the array to size elements.
409 /// New elements are initialized to null.
410 /// May only be called on nullValue or arrayValue.
411 /// \pre type() is arrayValue or nullValue
412 /// \post type() is arrayValue
413 void resize(ArrayIndex size);
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000414
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000415 /// Access an array element (zero based index ).
416 /// If the array contains less than index element, then null value are
417 /// inserted
418 /// in the array so that its size is index+1.
419 /// (You may need to say 'value[0u]' to get your compiler to distinguish
420 /// this from the operator[] which takes a string.)
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000421 Value& operator[](ArrayIndex index);
Baptiste Lepilleurfa130ef2010-12-24 12:47:14 +0000422
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000423 /// Access an array element (zero based index ).
424 /// If the array contains less than index element, then null value are
425 /// inserted
426 /// in the array so that its size is index+1.
427 /// (You may need to say 'value[0u]' to get your compiler to distinguish
428 /// this from the operator[] which takes a string.)
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000429 Value& operator[](int index);
Baptiste Lepilleurfa130ef2010-12-24 12:47:14 +0000430
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000431 /// Access an array element (zero based index )
432 /// (You may need to say 'value[0u]' to get your compiler to distinguish
433 /// this from the operator[] which takes a string.)
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000434 const Value& operator[](ArrayIndex index) const;
Baptiste Lepilleurfa130ef2010-12-24 12:47:14 +0000435
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000436 /// Access an array element (zero based index )
437 /// (You may need to say 'value[0u]' to get your compiler to distinguish
438 /// this from the operator[] which takes a string.)
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000439 const Value& operator[](int index) const;
Baptiste Lepilleurfa130ef2010-12-24 12:47:14 +0000440
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000441 /// If the array contains at least index+1 elements, returns the element
442 /// value,
443 /// otherwise returns defaultValue.
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000444 Value get(ArrayIndex index, const Value& defaultValue) const;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000445 /// Return true if index < size().
446 bool isValidIndex(ArrayIndex index) const;
447 /// \brief Append value to array at the end.
448 ///
449 /// Equivalent to jsonvalue[jsonvalue.size()] = value;
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000450 Value& append(const Value& value);
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000451
Dhruv Paranjape0ba8bd72017-07-08 17:47:13 +0530452#if JSON_HAS_RVALUE_REFERENCES
Dhruv Paranjape23c44d92017-07-08 17:30:47 +0530453 Value& append(Value&& value);
454#endif
455
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000456 /// Access an object value by name, create a null member if it does not exist.
Christopher Dunn25342ba2015-03-02 18:05:26 -0600457 /// \note Because of our implementation, keys are limited to 2^30 -1 chars.
458 /// Exceeding that will cause an exception.
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000459 Value& operator[](const char* key);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000460 /// Access an object value by name, returns null if there is no member with
461 /// that name.
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000462 const Value& operator[](const char* key) const;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000463 /// Access an object value by name, create a null member if it does not exist.
Christopher Dunn25342ba2015-03-02 18:05:26 -0600464 /// \param key may contain embedded nulls.
Christopher Dunnde5b7922016-03-06 11:19:46 -0600465 Value& operator[](const JSONCPP_STRING& key);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000466 /// Access an object value by name, returns null if there is no member with
467 /// that name.
Christopher Dunn25342ba2015-03-02 18:05:26 -0600468 /// \param key may contain embedded nulls.
Christopher Dunnde5b7922016-03-06 11:19:46 -0600469 const Value& operator[](const JSONCPP_STRING& key) const;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000470 /** \brief Access an object value by name, create a null member if it does not
471 exist.
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000472
Christopher Dunn25342ba2015-03-02 18:05:26 -0600473 * If the object has no entry for that name, then the member name used to store
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000474 * the new entry is not duplicated.
475 * Example of use:
476 * \code
477 * Json::Value object;
478 * static const StaticString code("code");
479 * object[code] = 1234;
480 * \endcode
481 */
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000482 Value& operator[](const StaticString& key);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000483#ifdef JSON_USE_CPPTL
484 /// Access an object value by name, create a null member if it does not exist.
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000485 Value& operator[](const CppTL::ConstString& key);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000486 /// Access an object value by name, returns null if there is no member with
487 /// that name.
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000488 const Value& operator[](const CppTL::ConstString& key) const;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000489#endif
490 /// Return the member named key if it exist, defaultValue otherwise.
Christopher Dunn0fd28752015-03-05 16:38:43 -0600491 /// \note deep copy
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000492 Value get(const char* key, const Value& defaultValue) const;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000493 /// Return the member named key if it exist, defaultValue otherwise.
Christopher Dunn0fd28752015-03-05 16:38:43 -0600494 /// \note deep copy
Christopher Dunn89704032015-07-11 12:09:59 -0500495 /// \note key may contain embedded nulls.
496 Value get(const char* begin, const char* end, const Value& defaultValue) const;
Christopher Dunn25342ba2015-03-02 18:05:26 -0600497 /// Return the member named key if it exist, defaultValue otherwise.
Christopher Dunn0fd28752015-03-05 16:38:43 -0600498 /// \note deep copy
Christopher Dunn25342ba2015-03-02 18:05:26 -0600499 /// \param key may contain embedded nulls.
Christopher Dunnde5b7922016-03-06 11:19:46 -0600500 Value get(const JSONCPP_STRING& key, const Value& defaultValue) const;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000501#ifdef JSON_USE_CPPTL
502 /// Return the member named key if it exist, defaultValue otherwise.
Christopher Dunn0fd28752015-03-05 16:38:43 -0600503 /// \note deep copy
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000504 Value get(const CppTL::ConstString& key, const Value& defaultValue) const;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000505#endif
Christopher Dunn25342ba2015-03-02 18:05:26 -0600506 /// Most general and efficient version of isMember()const, get()const,
507 /// and operator[]const
Christopher Dunn89704032015-07-11 12:09:59 -0500508 /// \note As stated elsewhere, behavior is undefined if (end-begin) >= 2^30
509 Value const* find(char const* begin, char const* end) const;
Christopher Dunnc28610f2015-02-21 11:44:16 -0600510 /// Most general and efficient version of object-mutators.
Christopher Dunn89704032015-07-11 12:09:59 -0500511 /// \note As stated elsewhere, behavior is undefined if (end-begin) >= 2^30
Christopher Dunnc28610f2015-02-21 11:44:16 -0600512 /// \return non-zero, but JSON_ASSERT if this is neither object nor nullValue.
Christopher Dunn89704032015-07-11 12:09:59 -0500513 Value const* demand(char const* begin, char const* end);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000514 /// \brief Remove and return the named member.
515 ///
516 /// Do nothing if it did not exist.
517 /// \return the removed Value, or null.
518 /// \pre type() is objectValue or nullValue
519 /// \post type() is unchanged
Christopher Dunn76746b02015-01-21 16:01:30 -0600520 /// \deprecated
damiramef16a352017-08-02 22:44:42 -0700521 JSONCPP_DEPRECATED("")
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000522 Value removeMember(const char* key);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000523 /// Same as removeMember(const char*)
Christopher Dunn25342ba2015-03-02 18:05:26 -0600524 /// \param key may contain embedded nulls.
Christopher Dunn76746b02015-01-21 16:01:30 -0600525 /// \deprecated
damiramef16a352017-08-02 22:44:42 -0700526 JSONCPP_DEPRECATED("")
Christopher Dunnde5b7922016-03-06 11:19:46 -0600527 Value removeMember(const JSONCPP_STRING& key);
Christopher Dunn89704032015-07-11 12:09:59 -0500528 /// Same as removeMember(const char* begin, const char* end, Value* removed),
Christopher Dunn25342ba2015-03-02 18:05:26 -0600529 /// but 'key' is null-terminated.
530 bool removeMember(const char* key, Value* removed);
Christopher Dunn76746b02015-01-21 16:01:30 -0600531 /** \brief Remove the named map member.
532
533 Update 'removed' iff removed.
Christopher Dunn25342ba2015-03-02 18:05:26 -0600534 \param key may contain embedded nulls.
Christopher Dunn76746b02015-01-21 16:01:30 -0600535 \return true iff removed (no exceptions)
536 */
Christopher Dunnde5b7922016-03-06 11:19:46 -0600537 bool removeMember(JSONCPP_STRING const& key, Value* removed);
538 /// Same as removeMember(JSONCPP_STRING const& key, Value* removed)
Christopher Dunn89704032015-07-11 12:09:59 -0500539 bool removeMember(const char* begin, const char* end, Value* removed);
Christopher Dunn9de2c2d2015-01-20 16:15:40 -0600540 /** \brief Remove the indexed array element.
541
542 O(n) expensive operations.
543 Update 'removed' iff removed.
Christopher Dunne87e41c2015-01-20 16:24:11 -0600544 \return true iff removed (no exceptions)
Christopher Dunn9de2c2d2015-01-20 16:15:40 -0600545 */
546 bool removeIndex(ArrayIndex i, Value* removed);
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000547
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000548 /// Return true if the object has a member named key.
Christopher Dunnc28610f2015-02-21 11:44:16 -0600549 /// \note 'key' must be null-terminated.
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000550 bool isMember(const char* key) const;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000551 /// Return true if the object has a member named key.
Christopher Dunn25342ba2015-03-02 18:05:26 -0600552 /// \param key may contain embedded nulls.
Christopher Dunnde5b7922016-03-06 11:19:46 -0600553 bool isMember(const JSONCPP_STRING& key) const;
554 /// Same as isMember(JSONCPP_STRING const& key)const
Christopher Dunn89704032015-07-11 12:09:59 -0500555 bool isMember(const char* begin, const char* end) const;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000556#ifdef JSON_USE_CPPTL
557 /// Return true if the object has a member named key.
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000558 bool isMember(const CppTL::ConstString& key) const;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000559#endif
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000560
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000561 /// \brief Return a list of the member names.
562 ///
563 /// If null, return an empty list.
564 /// \pre type() is objectValue or nullValue
565 /// \post if type() was nullValue, it remains nullValue
566 Members getMemberNames() const;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000567
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000568 //# ifdef JSON_USE_CPPTL
569 // EnumMemberNames enumMemberNames() const;
570 // EnumValues enumValues() const;
571 //# endif
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000572
Christopher Dunn1e3149a2015-01-25 14:16:13 -0600573 /// \deprecated Always pass len.
Christopher Dunnde5b7922016-03-06 11:19:46 -0600574 JSONCPP_DEPRECATED("Use setComment(JSONCPP_STRING const&) instead.")
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000575 void setComment(const char* comment, CommentPlacement placement);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000576 /// Comments must be //... or /* ... */
Christopher Dunn1e3149a2015-01-25 14:16:13 -0600577 void setComment(const char* comment, size_t len, CommentPlacement placement);
578 /// Comments must be //... or /* ... */
Christopher Dunnde5b7922016-03-06 11:19:46 -0600579 void setComment(const JSONCPP_STRING& comment, CommentPlacement placement);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000580 bool hasComment(CommentPlacement placement) const;
581 /// Include delimiters and embedded newlines.
Christopher Dunnde5b7922016-03-06 11:19:46 -0600582 JSONCPP_STRING getComment(CommentPlacement placement) const;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000583
Christopher Dunnde5b7922016-03-06 11:19:46 -0600584 JSONCPP_STRING toStyledString() const;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000585
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000586 const_iterator begin() const;
587 const_iterator end() const;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000588
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000589 iterator begin();
590 iterator end();
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000591
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000592 // Accessors for the [start, limit) range of bytes within the JSON text from
593 // which this value was parsed, if any.
Christopher Dunnd4513fc2016-02-06 09:25:20 -0600594 void setOffsetStart(ptrdiff_t start);
595 void setOffsetLimit(ptrdiff_t limit);
596 ptrdiff_t getOffsetStart() const;
597 ptrdiff_t getOffsetLimit() const;
Aaron Jacobs68db6552014-04-23 23:41:12 +0000598
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000599private:
Billy Donahue8eb5d892014-11-10 01:35:42 -0500600 void initBasic(ValueType type, bool allocated = false);
601
Christopher Dunnc28610f2015-02-21 11:44:16 -0600602 Value& resolveReference(const char* key);
603 Value& resolveReference(const char* key, const char* end);
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000604
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000605 struct CommentInfo {
606 CommentInfo();
607 ~CommentInfo();
608
Christopher Dunn1e3149a2015-01-25 14:16:13 -0600609 void setComment(const char* text, size_t len);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000610
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000611 char* comment_;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000612 };
613
614 // struct MemberNamesTransform
615 //{
616 // typedef const char *result_type;
617 // const char *operator()( const CZString &name ) const
618 // {
619 // return name.c_str();
620 // }
621 //};
622
623 union ValueHolder {
624 LargestInt int_;
625 LargestUInt uint_;
626 double real_;
627 bool bool_;
Christopher Dunnc28610f2015-02-21 11:44:16 -0600628 char* string_; // actually ptr to unsigned, followed by str, unless !allocated_
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000629 ObjectValues* map_;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000630 } value_;
631 ValueType type_ : 8;
Christopher Dunn2bc61372015-01-24 13:42:37 -0600632 unsigned int allocated_ : 1; // Notes: if declared as bool, bitfield is useless.
Christopher Dunnc28610f2015-02-21 11:44:16 -0600633 // If not allocated_, string_ must be null-terminated.
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000634 CommentInfo* comments_;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000635
636 // [start, limit) byte offsets in the source JSON text from which this Value
637 // was extracted.
Christopher Dunnd4513fc2016-02-06 09:25:20 -0600638 ptrdiff_t start_;
639 ptrdiff_t limit_;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000640};
641
642/** \brief Experimental and untested: represents an element of the "path" to
643 * access a node.
644 */
645class JSON_API PathArgument {
646public:
647 friend class Path;
648
649 PathArgument();
650 PathArgument(ArrayIndex index);
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000651 PathArgument(const char* key);
Christopher Dunnde5b7922016-03-06 11:19:46 -0600652 PathArgument(const JSONCPP_STRING& key);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000653
654private:
655 enum Kind {
656 kindNone = 0,
657 kindIndex,
658 kindKey
659 };
Christopher Dunnde5b7922016-03-06 11:19:46 -0600660 JSONCPP_STRING key_;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000661 ArrayIndex index_;
662 Kind kind_;
663};
664
665/** \brief Experimental and untested: represents a "path" to access a node.
666 *
667 * Syntax:
668 * - "." => root node
669 * - ".[n]" => elements at index 'n' of root node (an array value)
670 * - ".name" => member named 'name' of root node (an object value)
671 * - ".name1.name2.name3"
672 * - ".[0][1][2].name1[3]"
673 * - ".%" => member name is provided as parameter
674 * - ".[%]" => index is provied as parameter
675 */
676class JSON_API Path {
677public:
Christopher Dunnde5b7922016-03-06 11:19:46 -0600678 Path(const JSONCPP_STRING& path,
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000679 const PathArgument& a1 = PathArgument(),
680 const PathArgument& a2 = PathArgument(),
681 const PathArgument& a3 = PathArgument(),
682 const PathArgument& a4 = PathArgument(),
683 const PathArgument& a5 = PathArgument());
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000684
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000685 const Value& resolve(const Value& root) const;
686 Value resolve(const Value& root, const Value& defaultValue) const;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000687 /// Creates the "path" to access the specified node and returns a reference on
688 /// the node.
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000689 Value& make(Value& root) const;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000690
691private:
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000692 typedef std::vector<const PathArgument*> InArgs;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000693 typedef std::vector<PathArgument> Args;
694
Christopher Dunnde5b7922016-03-06 11:19:46 -0600695 void makePath(const JSONCPP_STRING& path, const InArgs& in);
696 void addPathInArg(const JSONCPP_STRING& path,
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000697 const InArgs& in,
698 InArgs::const_iterator& itInArg,
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000699 PathArgument::Kind kind);
Christopher Dunnde5b7922016-03-06 11:19:46 -0600700 void invalidPath(const JSONCPP_STRING& path, int location);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000701
702 Args args_;
703};
704
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000705/** \brief base class for Value iterators.
706 *
707 */
708class JSON_API ValueIteratorBase {
709public:
710 typedef std::bidirectional_iterator_tag iterator_category;
711 typedef unsigned int size_t;
712 typedef int difference_type;
713 typedef ValueIteratorBase SelfType;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000714
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000715 bool operator==(const SelfType& other) const { return isEqual(other); }
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000716
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000717 bool operator!=(const SelfType& other) const { return !isEqual(other); }
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000718
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000719 difference_type operator-(const SelfType& other) const {
Kevin Grant4c5832a2015-02-14 20:53:35 -0800720 return other.computeDistance(*this);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000721 }
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000722
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000723 /// Return either the index or the member name of the referenced value as a
724 /// Value.
725 Value key() const;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000726
Christopher Dunned495ed2015-03-08 14:01:28 -0500727 /// Return the index of the referenced Value, or -1 if it is not an arrayValue.
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000728 UInt index() const;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000729
Christopher Dunned495ed2015-03-08 14:01:28 -0500730 /// Return the member name of the referenced Value, or "" if it is not an
731 /// objectValue.
732 /// \note Avoid `c_str()` on result, as embedded zeroes are possible.
Christopher Dunnde5b7922016-03-06 11:19:46 -0600733 JSONCPP_STRING name() const;
Christopher Dunned495ed2015-03-08 14:01:28 -0500734
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000735 /// Return the member name of the referenced Value. "" if it is not an
736 /// objectValue.
Christopher Dunnc28610f2015-02-21 11:44:16 -0600737 /// \deprecated This cannot be used for UTF-8 strings, since there can be embedded nulls.
Christopher Dunned495ed2015-03-08 14:01:28 -0500738 JSONCPP_DEPRECATED("Use `key = name();` instead.")
Christopher Dunnc28610f2015-02-21 11:44:16 -0600739 char const* memberName() const;
740 /// Return the member name of the referenced Value, or NULL if it is not an
741 /// objectValue.
Christopher Dunned495ed2015-03-08 14:01:28 -0500742 /// \note Better version than memberName(). Allows embedded nulls.
Christopher Dunnc28610f2015-02-21 11:44:16 -0600743 char const* memberName(char const** end) const;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000744
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000745protected:
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000746 Value& deref() const;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000747
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000748 void increment();
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000749
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000750 void decrement();
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000751
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000752 difference_type computeDistance(const SelfType& other) const;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000753
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000754 bool isEqual(const SelfType& other) const;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000755
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000756 void copy(const SelfType& other);
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000757
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000758private:
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000759 Value::ObjectValues::iterator current_;
760 // Indicates that iterator is for a null value.
761 bool isNull_;
Christopher Dunn2a10f4a2015-04-28 04:55:12 +0100762
763public:
764 // For some reason, BORLAND needs these at the end, rather
765 // than earlier. No idea why.
766 ValueIteratorBase();
767 explicit ValueIteratorBase(const Value::ObjectValues::iterator& current);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000768};
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000769
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000770/** \brief const iterator for object and array value.
771 *
772 */
773class JSON_API ValueConstIterator : public ValueIteratorBase {
774 friend class Value;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000775
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000776public:
777 typedef const Value value_type;
Christopher Dunnc28610f2015-02-21 11:44:16 -0600778 //typedef unsigned int size_t;
779 //typedef int difference_type;
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000780 typedef const Value& reference;
781 typedef const Value* pointer;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000782 typedef ValueConstIterator SelfType;
783
784 ValueConstIterator();
ycqiuc8a8cfc2015-10-06 16:46:19 +0800785 ValueConstIterator(ValueIterator const& other);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000786
787private:
788/*! \internal Use by Value to create an iterator.
789 */
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000790 explicit ValueConstIterator(const Value::ObjectValues::iterator& current);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000791public:
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000792 SelfType& operator=(const ValueIteratorBase& other);
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000793
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000794 SelfType operator++(int) {
795 SelfType temp(*this);
796 ++*this;
797 return temp;
798 }
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000799
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000800 SelfType operator--(int) {
801 SelfType temp(*this);
802 --*this;
803 return temp;
804 }
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000805
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000806 SelfType& operator--() {
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000807 decrement();
808 return *this;
809 }
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000810
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000811 SelfType& operator++() {
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000812 increment();
813 return *this;
814 }
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000815
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000816 reference operator*() const { return deref(); }
Braden McDorman540db3b2014-09-14 02:31:23 -0500817
818 pointer operator->() const { return &deref(); }
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000819};
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000820
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000821/** \brief Iterator for object and array value.
822 */
823class JSON_API ValueIterator : public ValueIteratorBase {
824 friend class Value;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000825
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000826public:
827 typedef Value value_type;
828 typedef unsigned int size_t;
829 typedef int difference_type;
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000830 typedef Value& reference;
831 typedef Value* pointer;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000832 typedef ValueIterator SelfType;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000833
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000834 ValueIterator();
ycqiuc8a8cfc2015-10-06 16:46:19 +0800835 explicit ValueIterator(const ValueConstIterator& other);
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000836 ValueIterator(const ValueIterator& other);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000837
838private:
839/*! \internal Use by Value to create an iterator.
840 */
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000841 explicit ValueIterator(const Value::ObjectValues::iterator& current);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000842public:
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000843 SelfType& operator=(const SelfType& other);
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000844
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000845 SelfType operator++(int) {
846 SelfType temp(*this);
847 ++*this;
848 return temp;
849 }
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000850
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000851 SelfType operator--(int) {
852 SelfType temp(*this);
853 --*this;
854 return temp;
855 }
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000856
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000857 SelfType& operator--() {
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000858 decrement();
859 return *this;
860 }
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000861
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000862 SelfType& operator++() {
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000863 increment();
864 return *this;
865 }
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000866
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000867 reference operator*() const { return deref(); }
Braden McDorman540db3b2014-09-14 02:31:23 -0500868
869 pointer operator->() const { return &deref(); }
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000870};
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000871
872} // namespace Json
873
datadiode9454e682015-01-20 15:25:04 -0600874
875namespace std {
876/// Specialize std::swap() for Json::Value.
877template<>
878inline void swap(Json::Value& a, Json::Value& b) { a.swap(b); }
879}
880
Sergiy80d6e666f2016-12-03 22:29:14 +0200881#pragma pack(pop)
datadiode9454e682015-01-20 15:25:04 -0600882
Baptiste Lepilleureafd7022013-05-08 20:21:11 +0000883#if defined(JSONCPP_DISABLE_DLL_INTERFACE_WARNING)
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000884#pragma warning(pop)
Baptiste Lepilleureafd7022013-05-08 20:21:11 +0000885#endif // if defined(JSONCPP_DISABLE_DLL_INTERFACE_WARNING)
886
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000887#endif // CPPTL_JSON_H_INCLUDED