blob: 26bb38b69c9947c6b3712d3200c4a45b25134f5e [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.
Christopher Dunn50069d72015-03-08 13:35:57 -0500515 JSONCPP_DEPRECATED("Use setComment(std::string const&) instead.")
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000516 void setComment(const char* comment, CommentPlacement placement);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000517 /// Comments must be //... or /* ... */
Christopher Dunn1e3149a2015-01-25 14:16:13 -0600518 void setComment(const char* comment, size_t len, CommentPlacement placement);
519 /// Comments must be //... or /* ... */
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000520 void setComment(const std::string& comment, CommentPlacement placement);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000521 bool hasComment(CommentPlacement placement) const;
522 /// Include delimiters and embedded newlines.
523 std::string getComment(CommentPlacement placement) const;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000524
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000525 std::string toStyledString() const;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000526
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000527 const_iterator begin() const;
528 const_iterator end() const;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000529
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000530 iterator begin();
531 iterator end();
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000532
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000533 // Accessors for the [start, limit) range of bytes within the JSON text from
534 // which this value was parsed, if any.
535 void setOffsetStart(size_t start);
536 void setOffsetLimit(size_t limit);
537 size_t getOffsetStart() const;
538 size_t getOffsetLimit() const;
Aaron Jacobs68db6552014-04-23 23:41:12 +0000539
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000540private:
Billy Donahue8eb5d892014-11-10 01:35:42 -0500541 void initBasic(ValueType type, bool allocated = false);
542
Christopher Dunnc28610f2015-02-21 11:44:16 -0600543 Value& resolveReference(const char* key);
544 Value& resolveReference(const char* key, const char* end);
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000545
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000546 struct CommentInfo {
547 CommentInfo();
548 ~CommentInfo();
549
Christopher Dunn1e3149a2015-01-25 14:16:13 -0600550 void setComment(const char* text, size_t len);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000551
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000552 char* comment_;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000553 };
554
555 // struct MemberNamesTransform
556 //{
557 // typedef const char *result_type;
558 // const char *operator()( const CZString &name ) const
559 // {
560 // return name.c_str();
561 // }
562 //};
563
564 union ValueHolder {
565 LargestInt int_;
566 LargestUInt uint_;
567 double real_;
568 bool bool_;
Christopher Dunnc28610f2015-02-21 11:44:16 -0600569 char* string_; // actually ptr to unsigned, followed by str, unless !allocated_
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000570 ObjectValues* map_;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000571 } value_;
572 ValueType type_ : 8;
Christopher Dunn2bc61372015-01-24 13:42:37 -0600573 unsigned int allocated_ : 1; // Notes: if declared as bool, bitfield is useless.
Christopher Dunnc28610f2015-02-21 11:44:16 -0600574 // If not allocated_, string_ must be null-terminated.
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000575 CommentInfo* comments_;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000576
577 // [start, limit) byte offsets in the source JSON text from which this Value
578 // was extracted.
579 size_t start_;
580 size_t limit_;
581};
582
583/** \brief Experimental and untested: represents an element of the "path" to
584 * access a node.
585 */
586class JSON_API PathArgument {
587public:
588 friend class Path;
589
590 PathArgument();
591 PathArgument(ArrayIndex index);
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000592 PathArgument(const char* key);
593 PathArgument(const std::string& key);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000594
595private:
596 enum Kind {
597 kindNone = 0,
598 kindIndex,
599 kindKey
600 };
601 std::string key_;
602 ArrayIndex index_;
603 Kind kind_;
604};
605
606/** \brief Experimental and untested: represents a "path" to access a node.
607 *
608 * Syntax:
609 * - "." => root node
610 * - ".[n]" => elements at index 'n' of root node (an array value)
611 * - ".name" => member named 'name' of root node (an object value)
612 * - ".name1.name2.name3"
613 * - ".[0][1][2].name1[3]"
614 * - ".%" => member name is provided as parameter
615 * - ".[%]" => index is provied as parameter
616 */
617class JSON_API Path {
618public:
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000619 Path(const std::string& path,
620 const PathArgument& a1 = PathArgument(),
621 const PathArgument& a2 = PathArgument(),
622 const PathArgument& a3 = PathArgument(),
623 const PathArgument& a4 = PathArgument(),
624 const PathArgument& a5 = PathArgument());
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000625
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000626 const Value& resolve(const Value& root) const;
627 Value resolve(const Value& root, const Value& defaultValue) const;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000628 /// Creates the "path" to access the specified node and returns a reference on
629 /// the node.
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000630 Value& make(Value& root) const;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000631
632private:
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000633 typedef std::vector<const PathArgument*> InArgs;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000634 typedef std::vector<PathArgument> Args;
635
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000636 void makePath(const std::string& path, const InArgs& in);
637 void addPathInArg(const std::string& path,
638 const InArgs& in,
639 InArgs::const_iterator& itInArg,
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000640 PathArgument::Kind kind);
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000641 void invalidPath(const std::string& path, int location);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000642
643 Args args_;
644};
645
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000646/** \brief base class for Value iterators.
647 *
648 */
649class JSON_API ValueIteratorBase {
650public:
651 typedef std::bidirectional_iterator_tag iterator_category;
652 typedef unsigned int size_t;
653 typedef int difference_type;
654 typedef ValueIteratorBase SelfType;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000655
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000656 ValueIteratorBase();
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000657 explicit ValueIteratorBase(const Value::ObjectValues::iterator& current);
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000658
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000659 bool operator==(const SelfType& other) const { return isEqual(other); }
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000660
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000661 bool operator!=(const SelfType& other) const { return !isEqual(other); }
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000662
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000663 difference_type operator-(const SelfType& other) const {
Kevin Grant4c5832a2015-02-14 20:53:35 -0800664 return other.computeDistance(*this);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000665 }
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000666
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000667 /// Return either the index or the member name of the referenced value as a
668 /// Value.
669 Value key() const;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000670
Christopher Dunned495ed2015-03-08 14:01:28 -0500671 /// Return the index of the referenced Value, or -1 if it is not an arrayValue.
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000672 UInt index() const;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000673
Christopher Dunned495ed2015-03-08 14:01:28 -0500674 /// Return the member name of the referenced Value, or "" if it is not an
675 /// objectValue.
676 /// \note Avoid `c_str()` on result, as embedded zeroes are possible.
677 std::string name() const;
678
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000679 /// Return the member name of the referenced Value. "" if it is not an
680 /// objectValue.
Christopher Dunnc28610f2015-02-21 11:44:16 -0600681 /// \deprecated This cannot be used for UTF-8 strings, since there can be embedded nulls.
Christopher Dunned495ed2015-03-08 14:01:28 -0500682 JSONCPP_DEPRECATED("Use `key = name();` instead.")
Christopher Dunnc28610f2015-02-21 11:44:16 -0600683 char const* memberName() const;
684 /// Return the member name of the referenced Value, or NULL if it is not an
685 /// objectValue.
Christopher Dunned495ed2015-03-08 14:01:28 -0500686 /// \note Better version than memberName(). Allows embedded nulls.
Christopher Dunnc28610f2015-02-21 11:44:16 -0600687 char const* memberName(char const** end) const;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000688
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000689protected:
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000690 Value& deref() const;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000691
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000692 void increment();
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000693
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000694 void decrement();
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000695
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000696 difference_type computeDistance(const SelfType& other) const;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000697
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000698 bool isEqual(const SelfType& other) const;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000699
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000700 void copy(const SelfType& other);
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000701
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000702private:
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000703 Value::ObjectValues::iterator current_;
704 // Indicates that iterator is for a null value.
705 bool isNull_;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000706};
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000707
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000708/** \brief const iterator for object and array value.
709 *
710 */
711class JSON_API ValueConstIterator : public ValueIteratorBase {
712 friend class Value;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000713
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000714public:
715 typedef const Value value_type;
Christopher Dunnc28610f2015-02-21 11:44:16 -0600716 //typedef unsigned int size_t;
717 //typedef int difference_type;
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000718 typedef const Value& reference;
719 typedef const Value* pointer;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000720 typedef ValueConstIterator SelfType;
721
722 ValueConstIterator();
723
724private:
725/*! \internal Use by Value to create an iterator.
726 */
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000727 explicit ValueConstIterator(const Value::ObjectValues::iterator& current);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000728public:
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000729 SelfType& operator=(const ValueIteratorBase& other);
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000730
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000731 SelfType operator++(int) {
732 SelfType temp(*this);
733 ++*this;
734 return temp;
735 }
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000736
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000737 SelfType operator--(int) {
738 SelfType temp(*this);
739 --*this;
740 return temp;
741 }
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000742
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000743 SelfType& operator--() {
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000744 decrement();
745 return *this;
746 }
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000747
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000748 SelfType& operator++() {
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000749 increment();
750 return *this;
751 }
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000752
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000753 reference operator*() const { return deref(); }
Braden McDorman540db3b2014-09-14 02:31:23 -0500754
755 pointer operator->() const { return &deref(); }
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000756};
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000757
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000758/** \brief Iterator for object and array value.
759 */
760class JSON_API ValueIterator : public ValueIteratorBase {
761 friend class Value;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000762
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000763public:
764 typedef Value value_type;
765 typedef unsigned int size_t;
766 typedef int difference_type;
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000767 typedef Value& reference;
768 typedef Value* pointer;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000769 typedef ValueIterator SelfType;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000770
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000771 ValueIterator();
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000772 ValueIterator(const ValueConstIterator& other);
773 ValueIterator(const ValueIterator& other);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000774
775private:
776/*! \internal Use by Value to create an iterator.
777 */
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000778 explicit ValueIterator(const Value::ObjectValues::iterator& current);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000779public:
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000780 SelfType& operator=(const SelfType& other);
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000781
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000782 SelfType operator++(int) {
783 SelfType temp(*this);
784 ++*this;
785 return temp;
786 }
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000787
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000788 SelfType operator--(int) {
789 SelfType temp(*this);
790 --*this;
791 return temp;
792 }
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000793
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000794 SelfType& operator--() {
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000795 decrement();
796 return *this;
797 }
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000798
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000799 SelfType& operator++() {
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000800 increment();
801 return *this;
802 }
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000803
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000804 reference operator*() const { return deref(); }
Braden McDorman540db3b2014-09-14 02:31:23 -0500805
806 pointer operator->() const { return &deref(); }
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000807};
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000808
809} // namespace Json
810
datadiode9454e682015-01-20 15:25:04 -0600811
812namespace std {
813/// Specialize std::swap() for Json::Value.
814template<>
815inline void swap(Json::Value& a, Json::Value& b) { a.swap(b); }
816}
817
818
Baptiste Lepilleureafd7022013-05-08 20:21:11 +0000819#if defined(JSONCPP_DISABLE_DLL_INTERFACE_WARNING)
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000820#pragma warning(pop)
Baptiste Lepilleureafd7022013-05-08 20:21:11 +0000821#endif // if defined(JSONCPP_DISABLE_DLL_INTERFACE_WARNING)
822
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000823#endif // CPPTL_JSON_H_INCLUDED