blob: d91ccc421c589fee366d4cfc669fc62bf227a51c [file] [log] [blame]
Baptiste Lepilleur7469f1d2010-04-20 21:35:19 +00001// 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 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
Aaron Jacobs9fa4e842014-07-01 08:48:54 +100025// Disable warning C4251: <data member>: <type> needs to have dll-interface to
26// be used by...
Baptiste Lepilleureafd7022013-05-08 20:21:11 +000027#if defined(JSONCPP_DISABLE_DLL_INTERFACE_WARNING)
Aaron Jacobs9fa4e842014-07-01 08:48:54 +100028#pragma warning(push)
29#pragma warning(disable : 4251)
Baptiste Lepilleureafd7022013-05-08 20:21:11 +000030#endif // if defined(JSONCPP_DISABLE_DLL_INTERFACE_WARNING)
31
Christopher Dunn6d135cb2007-06-13 15:51:04 +000032/** \brief JSON (JavaScript Object Notation).
33 */
34namespace Json {
35
Christopher Dunn75279cc2015-03-08 12:20:06 -050036/** Base class for all exceptions we throw.
Christopher Dunn4e30c4f2015-03-08 12:56:32 -050037 *
38 * We use nothing but these internally. Of course, STL can throw others.
Christopher Dunn75279cc2015-03-08 12:20:06 -050039 */
Christopher Dunn53837942015-03-08 12:31:00 -050040class JSON_API Exception;
41/** Exceptions which the user cannot easily avoid.
42 *
Christopher Dunn4e30c4f2015-03-08 12:56:32 -050043 * E.g. out-of-memory (when we use malloc), stack-overflow, malicious input
44 *
45 * \remark derived from Json::Exception
Christopher Dunn53837942015-03-08 12:31:00 -050046 */
47class JSON_API RuntimeError;
Christopher Dunn4e30c4f2015-03-08 12:56:32 -050048/** Exceptions thrown by JSON_ASSERT/JSON_FAIL macros.
Christopher Dunn53837942015-03-08 12:31:00 -050049 *
50 * These are precondition-violations (user bugs) and internal errors (our bugs).
Christopher Dunn4e30c4f2015-03-08 12:56:32 -050051 *
52 * \remark derived from Json::Exception
Christopher Dunn53837942015-03-08 12:31:00 -050053 */
54class JSON_API LogicError;
55
Christopher Dunn4e30c4f2015-03-08 12:56:32 -050056/// used internally
57void throwRuntimeError(std::string const& msg);
58/// used internally
59void throwLogicError(std::string const& msg);
Christopher Dunn75279cc2015-03-08 12:20:06 -050060
Aaron Jacobs9fa4e842014-07-01 08:48:54 +100061/** \brief Type of the value held by a Value object.
62 */
63enum ValueType {
64 nullValue = 0, ///< 'null' value
65 intValue, ///< signed integer value
66 uintValue, ///< unsigned integer value
67 realValue, ///< double value
68 stringValue, ///< UTF-8 string value
69 booleanValue, ///< bool value
70 arrayValue, ///< array value (ordered list)
71 objectValue ///< object value (collection of name/value pairs).
72};
Christopher Dunn6d135cb2007-06-13 15:51:04 +000073
Aaron Jacobs9fa4e842014-07-01 08:48:54 +100074enum CommentPlacement {
75 commentBefore = 0, ///< a comment placed on the line before a value
76 commentAfterOnSameLine, ///< a comment just after a value on the same line
77 commentAfter, ///< a comment on the line after a value (only make sense for
Aaron Jacobs3a0c4fc2014-07-01 09:20:48 +100078 /// root value)
Aaron Jacobs9fa4e842014-07-01 08:48:54 +100079 numberOfCommentPlacement
80};
Christopher Dunn6d135cb2007-06-13 15:51:04 +000081
82//# ifdef JSON_USE_CPPTL
83// typedef CppTL::AnyEnumerator<const char *> EnumMemberNames;
84// typedef CppTL::AnyEnumerator<const Value &> EnumValues;
85//# endif
86
Aaron Jacobs9fa4e842014-07-01 08:48:54 +100087/** \brief Lightweight wrapper to tag static string.
88 *
89 * Value constructor and objectValue member assignement takes advantage of the
90 * StaticString and avoid the cost of string duplication when storing the
91 * string or the member name.
92 *
93 * Example of usage:
94 * \code
95 * Json::Value aValue( StaticString("some text") );
96 * Json::Value object;
97 * static const StaticString code("code");
98 * object[code] = 1234;
99 * \endcode
100 */
101class JSON_API StaticString {
102public:
Christopher Dunnff617522015-03-06 10:31:46 -0600103 explicit StaticString(const char* czstring) : c_str_(czstring) {}
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000104
Christopher Dunnff617522015-03-06 10:31:46 -0600105 operator const char*() const { return c_str_; }
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000106
Christopher Dunnff617522015-03-06 10:31:46 -0600107 const char* c_str() const { return c_str_; }
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000108
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000109private:
Christopher Dunnff617522015-03-06 10:31:46 -0600110 const char* c_str_;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000111};
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000112
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000113/** \brief Represents a <a HREF="http://www.json.org">JSON</a> value.
114 *
115 * This class is a discriminated union wrapper that can represents a:
116 * - signed integer [range: Value::minInt - Value::maxInt]
117 * - unsigned integer (range: 0 - Value::maxUInt)
118 * - double
119 * - UTF-8 string
120 * - boolean
121 * - 'null'
122 * - an ordered list of Value
123 * - collection of name/value pairs (javascript object)
124 *
125 * The type of the held value is represented by a #ValueType and
126 * can be obtained using type().
127 *
Christopher Dunnc28610f2015-02-21 11:44:16 -0600128 * Values of an #objectValue or #arrayValue can be accessed using operator[]()
129 * methods.
130 * Non-const methods will automatically create the a #nullValue element
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000131 * if it does not exist.
Christopher Dunnc28610f2015-02-21 11:44:16 -0600132 * The sequence of an #arrayValue will be automatically resized and initialized
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000133 * with #nullValue. resize() can be used to enlarge or truncate an #arrayValue.
134 *
Christopher Dunnc28610f2015-02-21 11:44:16 -0600135 * The get() methods can be used to obtain default value in the case the
136 * required element does not exist.
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000137 *
138 * It is possible to iterate over the list of a #objectValue values using
139 * the getMemberNames() method.
Christopher Dunn25342ba2015-03-02 18:05:26 -0600140 *
Christopher Dunnc28610f2015-02-21 11:44:16 -0600141 * \note #Value string-length fit in size_t, but keys must be < 2^30.
142 * (The reason is an implementation detail.) A #CharReader will raise an
Christopher Dunn25342ba2015-03-02 18:05:26 -0600143 * exception if a bound is exceeded to avoid security holes in your app,
144 * but the Value API does *not* check bounds. That is the responsibility
145 * of the caller.
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000146 */
147class JSON_API Value {
148 friend class ValueIteratorBase;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000149public:
150 typedef std::vector<std::string> Members;
151 typedef ValueIterator iterator;
152 typedef ValueConstIterator const_iterator;
153 typedef Json::UInt UInt;
154 typedef Json::Int Int;
155#if defined(JSON_HAS_INT64)
156 typedef Json::UInt64 UInt64;
157 typedef Json::Int64 Int64;
Baptiste Lepilleur842e9ac2010-12-27 17:45:23 +0000158#endif // defined(JSON_HAS_INT64)
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000159 typedef Json::LargestInt LargestInt;
160 typedef Json::LargestUInt LargestUInt;
161 typedef Json::ArrayIndex ArrayIndex;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000162
Christopher Dunn8a702972015-03-03 10:38:27 -0600163 static const Value& null; ///< We regret this reference to a global instance; prefer the simpler Value().
164 static const Value& nullRef; ///< just a kludge for binary-compatibility; same as null
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000165 /// Minimum signed integer value that can be stored in a Json::Value.
166 static const LargestInt minLargestInt;
167 /// Maximum signed integer value that can be stored in a Json::Value.
168 static const LargestInt maxLargestInt;
169 /// Maximum unsigned integer value that can be stored in a Json::Value.
170 static const LargestUInt maxLargestUInt;
Baptiste Lepilleur842e9ac2010-12-27 17:45:23 +0000171
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000172 /// Minimum signed int value that can be stored in a Json::Value.
173 static const Int minInt;
174 /// Maximum signed int value that can be stored in a Json::Value.
175 static const Int maxInt;
176 /// Maximum unsigned int value that can be stored in a Json::Value.
177 static const UInt maxUInt;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000178
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000179#if defined(JSON_HAS_INT64)
180 /// Minimum signed 64 bits int value that can be stored in a Json::Value.
181 static const Int64 minInt64;
182 /// Maximum signed 64 bits int value that can be stored in a Json::Value.
183 static const Int64 maxInt64;
184 /// Maximum unsigned 64 bits int value that can be stored in a Json::Value.
185 static const UInt64 maxUInt64;
Aaron Jacobsf1053e72011-05-24 03:18:02 +0000186#endif // defined(JSON_HAS_INT64)
Baptiste Lepilleur842e9ac2010-12-27 17:45:23 +0000187
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000188private:
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000189#ifndef JSONCPP_DOC_EXCLUDE_IMPLEMENTATION
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000190 class CZString {
191 public:
192 enum DuplicationPolicy {
193 noDuplication = 0,
194 duplicate,
195 duplicateOnCopy
196 };
197 CZString(ArrayIndex index);
Christopher Dunnc28610f2015-02-21 11:44:16 -0600198 CZString(char const* str, unsigned length, DuplicationPolicy allocate);
199 CZString(CZString const& other);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000200 ~CZString();
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000201 CZString& operator=(CZString other);
Christopher Dunnc28610f2015-02-21 11:44:16 -0600202 bool operator<(CZString const& other) const;
203 bool operator==(CZString const& other) const;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000204 ArrayIndex index() const;
Christopher Dunnc28610f2015-02-21 11:44:16 -0600205 //const char* c_str() const; ///< \deprecated
206 char const* data() const;
207 unsigned length() const;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000208 bool isStaticString() const;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000209
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000210 private:
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000211 void swap(CZString& other);
Christopher Dunnc28610f2015-02-21 11:44:16 -0600212
Christopher Dunn57ad0512015-03-02 12:10:35 -0600213 struct StringStorage {
214 DuplicationPolicy policy_: 2;
215 unsigned length_: 30; // 1GB max
216 };
217
Christopher Dunnc28610f2015-02-21 11:44:16 -0600218 char const* cstr_; // actually, a prefixed string, unless policy is noDup
Christopher Dunn57ad0512015-03-02 12:10:35 -0600219 union {
220 ArrayIndex index_;
221 StringStorage storage_;
222 };
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000223 };
224
225public:
226#ifndef JSON_USE_CPPTL_SMALLMAP
227 typedef std::map<CZString, Value> ObjectValues;
228#else
229 typedef CppTL::SmallMap<CZString, Value> ObjectValues;
230#endif // ifndef JSON_USE_CPPTL_SMALLMAP
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000231#endif // ifndef JSONCPP_DOC_EXCLUDE_IMPLEMENTATION
232
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000233public:
234 /** \brief Create a default Value of the given type.
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000235
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000236 This is a very useful constructor.
237 To create an empty array, pass arrayValue.
238 To create an empty object, pass objectValue.
239 Another Value can then be set to this one by assignment.
240This is useful since clear() and resize() will not alter types.
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000241
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000242 Examples:
243\code
244Json::Value null_value; // null
245Json::Value arr_value(Json::arrayValue); // []
246Json::Value obj_value(Json::objectValue); // {}
247\endcode
248 */
249 Value(ValueType type = nullValue);
250 Value(Int value);
251 Value(UInt value);
Baptiste Lepilleur842e9ac2010-12-27 17:45:23 +0000252#if defined(JSON_HAS_INT64)
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000253 Value(Int64 value);
254 Value(UInt64 value);
Baptiste Lepilleur842e9ac2010-12-27 17:45:23 +0000255#endif // if defined(JSON_HAS_INT64)
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000256 Value(double value);
Christopher Dunn8a702972015-03-03 10:38:27 -0600257 Value(const char* value); ///< Copy til first 0. (NULL causes to seg-fault.)
258 Value(const char* beginValue, const char* endValue); ///< Copy all, incl zeroes.
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000259 /** \brief Constructs a value from a static string.
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000260
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000261 * Like other value string constructor but do not duplicate the string for
262 * internal storage. The given string must remain alive after the call to this
263 * constructor.
Christopher Dunnc28610f2015-02-21 11:44:16 -0600264 * \note This works only for null-terminated strings. (We cannot change the
265 * size of this class, so we have nowhere to store the length,
266 * which might be computed later for various operations.)
267 *
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000268 * Example of usage:
269 * \code
Christopher Dunnc28610f2015-02-21 11:44:16 -0600270 * static StaticString foo("some text");
271 * Json::Value aValue(foo);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000272 * \endcode
273 */
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000274 Value(const StaticString& value);
Christopher Dunn8a702972015-03-03 10:38:27 -0600275 Value(const std::string& value); ///< Copy data() til size(). Embedded zeroes too.
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000276#ifdef JSON_USE_CPPTL
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000277 Value(const CppTL::ConstString& value);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000278#endif
279 Value(bool value);
Christopher Dunn66eb72f2015-01-20 11:02:22 -0600280 /// Deep copy.
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000281 Value(const Value& other);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000282 ~Value();
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000283
Christopher Dunn7f439f42015-03-06 09:22:57 -0600284 /// Deep copy, then swap(other).
285 /// \note Over-write existing comments. To preserve comments, use #swapPayload().
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000286 Value& operator=(Value other);
Christopher Dunn66eb72f2015-01-20 11:02:22 -0600287 /// Swap everything.
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000288 void swap(Value& other);
Christopher Dunn66eb72f2015-01-20 11:02:22 -0600289 /// Swap values but leave comments and source offsets in place.
290 void swapPayload(Value& other);
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000291
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000292 ValueType type() const;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000293
Christopher Dunn66eb72f2015-01-20 11:02:22 -0600294 /// Compare payload only, not comments etc.
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000295 bool operator<(const Value& other) const;
296 bool operator<=(const Value& other) const;
297 bool operator>=(const Value& other) const;
298 bool operator>(const Value& other) const;
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000299 bool operator==(const Value& other) const;
300 bool operator!=(const Value& other) const;
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000301 int compare(const Value& other) const;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000302
Christopher Dunn8a702972015-03-03 10:38:27 -0600303 const char* asCString() const; ///< Embedded zeroes could cause you trouble!
304 std::string asString() const; ///< Embedded zeroes are possible.
Christopher Dunnc28610f2015-02-21 11:44:16 -0600305 /** Get raw char* of string-value.
306 * \return false if !string. (Seg-fault if str or end are NULL.)
307 */
308 bool getString(
309 char const** str, char const** end) const;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000310#ifdef JSON_USE_CPPTL
311 CppTL::ConstString asConstString() const;
312#endif
313 Int asInt() const;
314 UInt asUInt() const;
Aaron Jacobsf1053e72011-05-24 03:18:02 +0000315#if defined(JSON_HAS_INT64)
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000316 Int64 asInt64() const;
317 UInt64 asUInt64() const;
Aaron Jacobsf1053e72011-05-24 03:18:02 +0000318#endif // if defined(JSON_HAS_INT64)
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000319 LargestInt asLargestInt() const;
320 LargestUInt asLargestUInt() const;
321 float asFloat() const;
322 double asDouble() const;
323 bool asBool() const;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000324
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000325 bool isNull() const;
326 bool isBool() const;
327 bool isInt() const;
328 bool isInt64() const;
329 bool isUInt() const;
330 bool isUInt64() const;
331 bool isIntegral() const;
332 bool isDouble() const;
333 bool isNumeric() const;
334 bool isString() const;
335 bool isArray() const;
336 bool isObject() const;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000337
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000338 bool isConvertibleTo(ValueType other) const;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000339
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000340 /// Number of values in array or object
341 ArrayIndex size() const;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000342
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000343 /// \brief Return true if empty array, empty object, or null;
344 /// otherwise, false.
345 bool empty() const;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000346
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000347 /// Return isNull()
348 bool operator!() const;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000349
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000350 /// Remove all object members and array elements.
351 /// \pre type() is arrayValue, objectValue, or nullValue
352 /// \post type() is unchanged
353 void clear();
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000354
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000355 /// Resize the array to size elements.
356 /// New elements are initialized to null.
357 /// May only be called on nullValue or arrayValue.
358 /// \pre type() is arrayValue or nullValue
359 /// \post type() is arrayValue
360 void resize(ArrayIndex size);
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000361
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000362 /// Access an array element (zero based index ).
363 /// If the array contains less than index element, then null value are
364 /// inserted
365 /// in the array so that its size is index+1.
366 /// (You may need to say 'value[0u]' to get your compiler to distinguish
367 /// this from the operator[] which takes a string.)
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000368 Value& operator[](ArrayIndex index);
Baptiste Lepilleurfa130ef2010-12-24 12:47:14 +0000369
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000370 /// Access an array element (zero based index ).
371 /// If the array contains less than index element, then null value are
372 /// inserted
373 /// in the array so that its size is index+1.
374 /// (You may need to say 'value[0u]' to get your compiler to distinguish
375 /// this from the operator[] which takes a string.)
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000376 Value& operator[](int index);
Baptiste Lepilleurfa130ef2010-12-24 12:47:14 +0000377
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000378 /// Access an array element (zero based index )
379 /// (You may need to say 'value[0u]' to get your compiler to distinguish
380 /// this from the operator[] which takes a string.)
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000381 const Value& operator[](ArrayIndex index) const;
Baptiste Lepilleurfa130ef2010-12-24 12:47:14 +0000382
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000383 /// Access an array element (zero based index )
384 /// (You may need to say 'value[0u]' to get your compiler to distinguish
385 /// this from the operator[] which takes a string.)
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000386 const Value& operator[](int index) const;
Baptiste Lepilleurfa130ef2010-12-24 12:47:14 +0000387
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000388 /// If the array contains at least index+1 elements, returns the element
389 /// value,
390 /// otherwise returns defaultValue.
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000391 Value get(ArrayIndex index, const Value& defaultValue) const;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000392 /// Return true if index < size().
393 bool isValidIndex(ArrayIndex index) const;
394 /// \brief Append value to array at the end.
395 ///
396 /// Equivalent to jsonvalue[jsonvalue.size()] = value;
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000397 Value& append(const Value& value);
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000398
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000399 /// Access an object value by name, create a null member if it does not exist.
Christopher Dunn25342ba2015-03-02 18:05:26 -0600400 /// \note Because of our implementation, keys are limited to 2^30 -1 chars.
401 /// Exceeding that will cause an exception.
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000402 Value& operator[](const char* key);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000403 /// Access an object value by name, returns null if there is no member with
404 /// that name.
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000405 const Value& operator[](const char* key) const;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000406 /// Access an object value by name, create a null member if it does not exist.
Christopher Dunn25342ba2015-03-02 18:05:26 -0600407 /// \param key may contain embedded nulls.
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000408 Value& operator[](const std::string& key);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000409 /// Access an object value by name, returns null if there is no member with
410 /// that name.
Christopher Dunn25342ba2015-03-02 18:05:26 -0600411 /// \param key may contain embedded nulls.
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000412 const Value& operator[](const std::string& key) const;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000413 /** \brief Access an object value by name, create a null member if it does not
414 exist.
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000415
Christopher Dunn25342ba2015-03-02 18:05:26 -0600416 * If the object has no entry for that name, then the member name used to store
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000417 * the new entry is not duplicated.
418 * Example of use:
419 * \code
420 * Json::Value object;
421 * static const StaticString code("code");
422 * object[code] = 1234;
423 * \endcode
424 */
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000425 Value& operator[](const StaticString& key);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000426#ifdef JSON_USE_CPPTL
427 /// Access an object value by name, create a null member if it does not exist.
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000428 Value& operator[](const CppTL::ConstString& key);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000429 /// Access an object value by name, returns null if there is no member with
430 /// that name.
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000431 const Value& operator[](const CppTL::ConstString& key) const;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000432#endif
433 /// Return the member named key if it exist, defaultValue otherwise.
Christopher Dunn0fd28752015-03-05 16:38:43 -0600434 /// \note deep copy
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000435 Value get(const char* key, const Value& defaultValue) const;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000436 /// Return the member named key if it exist, defaultValue otherwise.
Christopher Dunn0fd28752015-03-05 16:38:43 -0600437 /// \note deep copy
Christopher Dunn25342ba2015-03-02 18:05:26 -0600438 /// \param key may contain embedded nulls.
439 Value get(const char* key, const char* end, const Value& defaultValue) const;
440 /// Return the member named key if it exist, defaultValue otherwise.
Christopher Dunn0fd28752015-03-05 16:38:43 -0600441 /// \note deep copy
Christopher Dunn25342ba2015-03-02 18:05:26 -0600442 /// \param key may contain embedded nulls.
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000443 Value get(const std::string& key, const Value& defaultValue) const;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000444#ifdef JSON_USE_CPPTL
445 /// Return the member named key if it exist, defaultValue otherwise.
Christopher Dunn0fd28752015-03-05 16:38:43 -0600446 /// \note deep copy
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000447 Value get(const CppTL::ConstString& key, const Value& defaultValue) const;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000448#endif
Christopher Dunn25342ba2015-03-02 18:05:26 -0600449 /// Most general and efficient version of isMember()const, get()const,
450 /// and operator[]const
451 /// \note As stated elsewhere, behavior is undefined if (end-key) >= 2^30
452 Value const* find(char const* key, char const* end) const;
Christopher Dunnc28610f2015-02-21 11:44:16 -0600453 /// Most general and efficient version of object-mutators.
454 /// \note As stated elsewhere, behavior is undefined if (end-key) >= 2^30
455 /// \return non-zero, but JSON_ASSERT if this is neither object nor nullValue.
456 Value const* demand(char const* key, char const* end);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000457 /// \brief Remove and return the named member.
458 ///
459 /// Do nothing if it did not exist.
460 /// \return the removed Value, or null.
461 /// \pre type() is objectValue or nullValue
462 /// \post type() is unchanged
Christopher Dunn76746b02015-01-21 16:01:30 -0600463 /// \deprecated
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000464 Value removeMember(const char* key);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000465 /// Same as removeMember(const char*)
Christopher Dunn25342ba2015-03-02 18:05:26 -0600466 /// \param key may contain embedded nulls.
Christopher Dunn76746b02015-01-21 16:01:30 -0600467 /// \deprecated
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000468 Value removeMember(const std::string& key);
Christopher Dunn25342ba2015-03-02 18:05:26 -0600469 /// Same as removeMember(const char* key, const char* end, Value* removed),
470 /// but 'key' is null-terminated.
471 bool removeMember(const char* key, Value* removed);
Christopher Dunn76746b02015-01-21 16:01:30 -0600472 /** \brief Remove the named map member.
473
474 Update 'removed' iff removed.
Christopher Dunn25342ba2015-03-02 18:05:26 -0600475 \param key may contain embedded nulls.
Christopher Dunn76746b02015-01-21 16:01:30 -0600476 \return true iff removed (no exceptions)
477 */
Christopher Dunn25342ba2015-03-02 18:05:26 -0600478 bool removeMember(std::string const& key, Value* removed);
479 /// Same as removeMember(std::string const& key, Value* removed)
480 bool removeMember(const char* key, const char* end, Value* removed);
Christopher Dunn9de2c2d2015-01-20 16:15:40 -0600481 /** \brief Remove the indexed array element.
482
483 O(n) expensive operations.
484 Update 'removed' iff removed.
Christopher Dunne87e41c2015-01-20 16:24:11 -0600485 \return true iff removed (no exceptions)
Christopher Dunn9de2c2d2015-01-20 16:15:40 -0600486 */
487 bool removeIndex(ArrayIndex i, Value* removed);
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000488
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000489 /// Return true if the object has a member named key.
Christopher Dunnc28610f2015-02-21 11:44:16 -0600490 /// \note 'key' must be null-terminated.
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000491 bool isMember(const char* key) const;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000492 /// Return true if the object has a member named key.
Christopher Dunn25342ba2015-03-02 18:05:26 -0600493 /// \param key may contain embedded nulls.
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000494 bool isMember(const std::string& key) const;
Christopher Dunn25342ba2015-03-02 18:05:26 -0600495 /// Same as isMember(std::string const& key)const
496 bool isMember(const char* key, const char* end) const;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000497#ifdef JSON_USE_CPPTL
498 /// Return true if the object has a member named key.
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000499 bool isMember(const CppTL::ConstString& key) const;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000500#endif
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000501
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000502 /// \brief Return a list of the member names.
503 ///
504 /// If null, return an empty list.
505 /// \pre type() is objectValue or nullValue
506 /// \post if type() was nullValue, it remains nullValue
507 Members getMemberNames() const;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000508
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000509 //# ifdef JSON_USE_CPPTL
510 // EnumMemberNames enumMemberNames() const;
511 // EnumValues enumValues() const;
512 //# endif
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000513
Christopher Dunn1e3149a2015-01-25 14:16:13 -0600514 /// \deprecated Always pass len.
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000515 void setComment(const char* comment, CommentPlacement placement);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000516 /// Comments must be //... or /* ... */
Christopher Dunn1e3149a2015-01-25 14:16:13 -0600517 void setComment(const char* comment, size_t len, CommentPlacement placement);
518 /// Comments must be //... or /* ... */
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000519 void setComment(const std::string& comment, CommentPlacement placement);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000520 bool hasComment(CommentPlacement placement) const;
521 /// Include delimiters and embedded newlines.
522 std::string getComment(CommentPlacement placement) const;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000523
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000524 std::string toStyledString() const;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000525
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000526 const_iterator begin() const;
527 const_iterator end() const;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000528
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000529 iterator begin();
530 iterator end();
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000531
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000532 // Accessors for the [start, limit) range of bytes within the JSON text from
533 // which this value was parsed, if any.
534 void setOffsetStart(size_t start);
535 void setOffsetLimit(size_t limit);
536 size_t getOffsetStart() const;
537 size_t getOffsetLimit() const;
Aaron Jacobs68db6552014-04-23 23:41:12 +0000538
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000539private:
Billy Donahue8eb5d892014-11-10 01:35:42 -0500540 void initBasic(ValueType type, bool allocated = false);
541
Christopher Dunnc28610f2015-02-21 11:44:16 -0600542 Value& resolveReference(const char* key);
543 Value& resolveReference(const char* key, const char* end);
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000544
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000545 struct CommentInfo {
546 CommentInfo();
547 ~CommentInfo();
548
Christopher Dunn1e3149a2015-01-25 14:16:13 -0600549 void setComment(const char* text, size_t len);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000550
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000551 char* comment_;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000552 };
553
554 // struct MemberNamesTransform
555 //{
556 // typedef const char *result_type;
557 // const char *operator()( const CZString &name ) const
558 // {
559 // return name.c_str();
560 // }
561 //};
562
563 union ValueHolder {
564 LargestInt int_;
565 LargestUInt uint_;
566 double real_;
567 bool bool_;
Christopher Dunnc28610f2015-02-21 11:44:16 -0600568 char* string_; // actually ptr to unsigned, followed by str, unless !allocated_
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000569 ObjectValues* map_;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000570 } value_;
571 ValueType type_ : 8;
Christopher Dunn2bc61372015-01-24 13:42:37 -0600572 unsigned int allocated_ : 1; // Notes: if declared as bool, bitfield is useless.
Christopher Dunnc28610f2015-02-21 11:44:16 -0600573 // If not allocated_, string_ must be null-terminated.
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000574 CommentInfo* comments_;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000575
576 // [start, limit) byte offsets in the source JSON text from which this Value
577 // was extracted.
578 size_t start_;
579 size_t limit_;
580};
581
582/** \brief Experimental and untested: represents an element of the "path" to
583 * access a node.
584 */
585class JSON_API PathArgument {
586public:
587 friend class Path;
588
589 PathArgument();
590 PathArgument(ArrayIndex index);
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000591 PathArgument(const char* key);
592 PathArgument(const std::string& key);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000593
594private:
595 enum Kind {
596 kindNone = 0,
597 kindIndex,
598 kindKey
599 };
600 std::string key_;
601 ArrayIndex index_;
602 Kind kind_;
603};
604
605/** \brief Experimental and untested: represents a "path" to access a node.
606 *
607 * Syntax:
608 * - "." => root node
609 * - ".[n]" => elements at index 'n' of root node (an array value)
610 * - ".name" => member named 'name' of root node (an object value)
611 * - ".name1.name2.name3"
612 * - ".[0][1][2].name1[3]"
613 * - ".%" => member name is provided as parameter
614 * - ".[%]" => index is provied as parameter
615 */
616class JSON_API Path {
617public:
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000618 Path(const std::string& path,
619 const PathArgument& a1 = PathArgument(),
620 const PathArgument& a2 = PathArgument(),
621 const PathArgument& a3 = PathArgument(),
622 const PathArgument& a4 = PathArgument(),
623 const PathArgument& a5 = PathArgument());
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000624
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000625 const Value& resolve(const Value& root) const;
626 Value resolve(const Value& root, const Value& defaultValue) const;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000627 /// Creates the "path" to access the specified node and returns a reference on
628 /// the node.
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000629 Value& make(Value& root) const;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000630
631private:
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000632 typedef std::vector<const PathArgument*> InArgs;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000633 typedef std::vector<PathArgument> Args;
634
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000635 void makePath(const std::string& path, const InArgs& in);
636 void addPathInArg(const std::string& path,
637 const InArgs& in,
638 InArgs::const_iterator& itInArg,
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000639 PathArgument::Kind kind);
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000640 void invalidPath(const std::string& path, int location);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000641
642 Args args_;
643};
644
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000645/** \brief base class for Value iterators.
646 *
647 */
648class JSON_API ValueIteratorBase {
649public:
650 typedef std::bidirectional_iterator_tag iterator_category;
651 typedef unsigned int size_t;
652 typedef int difference_type;
653 typedef ValueIteratorBase SelfType;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000654
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000655 ValueIteratorBase();
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000656 explicit ValueIteratorBase(const Value::ObjectValues::iterator& current);
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000657
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000658 bool operator==(const SelfType& other) const { return isEqual(other); }
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000659
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000660 bool operator!=(const SelfType& other) const { return !isEqual(other); }
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000661
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000662 difference_type operator-(const SelfType& other) const {
Kevin Grant4c5832a2015-02-14 20:53:35 -0800663 return other.computeDistance(*this);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000664 }
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000665
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000666 /// Return either the index or the member name of the referenced value as a
667 /// Value.
668 Value key() const;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000669
Christopher Dunned495ed2015-03-08 14:01:28 -0500670 /// Return the index of the referenced Value, or -1 if it is not an arrayValue.
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000671 UInt index() const;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000672
Christopher Dunned495ed2015-03-08 14:01:28 -0500673 /// Return the member name of the referenced Value, or "" if it is not an
674 /// objectValue.
675 /// \note Avoid `c_str()` on result, as embedded zeroes are possible.
676 std::string name() const;
677
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000678 /// Return the member name of the referenced Value. "" if it is not an
679 /// objectValue.
Christopher Dunnc28610f2015-02-21 11:44:16 -0600680 /// \deprecated This cannot be used for UTF-8 strings, since there can be embedded nulls.
Christopher Dunned495ed2015-03-08 14:01:28 -0500681 JSONCPP_DEPRECATED("Use `key = name();` instead.")
Christopher Dunnc28610f2015-02-21 11:44:16 -0600682 char const* memberName() const;
683 /// Return the member name of the referenced Value, or NULL if it is not an
684 /// objectValue.
Christopher Dunned495ed2015-03-08 14:01:28 -0500685 /// \note Better version than memberName(). Allows embedded nulls.
Christopher Dunnc28610f2015-02-21 11:44:16 -0600686 char const* memberName(char const** end) const;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000687
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000688protected:
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000689 Value& deref() const;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000690
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000691 void increment();
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000692
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000693 void decrement();
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000694
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000695 difference_type computeDistance(const SelfType& other) const;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000696
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000697 bool isEqual(const SelfType& other) const;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000698
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000699 void copy(const SelfType& other);
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000700
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000701private:
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000702 Value::ObjectValues::iterator current_;
703 // Indicates that iterator is for a null value.
704 bool isNull_;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000705};
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000706
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000707/** \brief const iterator for object and array value.
708 *
709 */
710class JSON_API ValueConstIterator : public ValueIteratorBase {
711 friend class Value;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000712
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000713public:
714 typedef const Value value_type;
Christopher Dunnc28610f2015-02-21 11:44:16 -0600715 //typedef unsigned int size_t;
716 //typedef int difference_type;
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000717 typedef const Value& reference;
718 typedef const Value* pointer;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000719 typedef ValueConstIterator SelfType;
720
721 ValueConstIterator();
722
723private:
724/*! \internal Use by Value to create an iterator.
725 */
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000726 explicit ValueConstIterator(const Value::ObjectValues::iterator& current);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000727public:
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000728 SelfType& operator=(const ValueIteratorBase& other);
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000729
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000730 SelfType operator++(int) {
731 SelfType temp(*this);
732 ++*this;
733 return temp;
734 }
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000735
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000736 SelfType operator--(int) {
737 SelfType temp(*this);
738 --*this;
739 return temp;
740 }
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000741
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000742 SelfType& operator--() {
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000743 decrement();
744 return *this;
745 }
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000746
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000747 SelfType& operator++() {
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000748 increment();
749 return *this;
750 }
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000751
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000752 reference operator*() const { return deref(); }
Braden McDorman540db3b2014-09-14 02:31:23 -0500753
754 pointer operator->() const { return &deref(); }
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000755};
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000756
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000757/** \brief Iterator for object and array value.
758 */
759class JSON_API ValueIterator : public ValueIteratorBase {
760 friend class Value;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000761
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000762public:
763 typedef Value value_type;
764 typedef unsigned int size_t;
765 typedef int difference_type;
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000766 typedef Value& reference;
767 typedef Value* pointer;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000768 typedef ValueIterator SelfType;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000769
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000770 ValueIterator();
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000771 ValueIterator(const ValueConstIterator& other);
772 ValueIterator(const ValueIterator& other);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000773
774private:
775/*! \internal Use by Value to create an iterator.
776 */
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000777 explicit ValueIterator(const Value::ObjectValues::iterator& current);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000778public:
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000779 SelfType& operator=(const SelfType& other);
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000780
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000781 SelfType operator++(int) {
782 SelfType temp(*this);
783 ++*this;
784 return temp;
785 }
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000786
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000787 SelfType operator--(int) {
788 SelfType temp(*this);
789 --*this;
790 return temp;
791 }
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000792
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000793 SelfType& operator--() {
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000794 decrement();
795 return *this;
796 }
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000797
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000798 SelfType& operator++() {
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000799 increment();
800 return *this;
801 }
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000802
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000803 reference operator*() const { return deref(); }
Braden McDorman540db3b2014-09-14 02:31:23 -0500804
805 pointer operator->() const { return &deref(); }
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000806};
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000807
808} // namespace Json
809
datadiode9454e682015-01-20 15:25:04 -0600810
811namespace std {
812/// Specialize std::swap() for Json::Value.
813template<>
814inline void swap(Json::Value& a, Json::Value& b) { a.swap(b); }
815}
816
817
Baptiste Lepilleureafd7022013-05-08 20:21:11 +0000818#if defined(JSONCPP_DISABLE_DLL_INTERFACE_WARNING)
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000819#pragma warning(pop)
Baptiste Lepilleureafd7022013-05-08 20:21:11 +0000820#endif // if defined(JSONCPP_DISABLE_DLL_INTERFACE_WARNING)
821
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000822#endif // CPPTL_JSON_H_INCLUDED