blob: 7f3ad9b6c79a2e80d36e519e6bc2ce6dd4ad5ba4 [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 Dunn949babd2015-07-23 00:19:12 -050040class JSON_API Exception : public std::exception {
41public:
42 Exception(std::string const& msg);
Gauravaadd0b12015-09-04 14:04:43 +053043 ~Exception() throw() override;
44 char const* what() const throw() override;
Christopher Dunn949babd2015-07-23 00:19:12 -050045protected:
Christopher Dunndc5aa4a2015-09-28 17:04:23 -050046 std::string msg_;
Christopher Dunn949babd2015-07-23 00:19:12 -050047};
48
Christopher Dunn53837942015-03-08 12:31:00 -050049/** Exceptions which the user cannot easily avoid.
50 *
Christopher Dunn4e30c4f2015-03-08 12:56:32 -050051 * E.g. out-of-memory (when we use malloc), stack-overflow, malicious input
52 *
53 * \remark derived from Json::Exception
Christopher Dunn53837942015-03-08 12:31:00 -050054 */
Christopher Dunn949babd2015-07-23 00:19:12 -050055class JSON_API RuntimeError : public Exception {
56public:
57 RuntimeError(std::string const& msg);
58};
59
Christopher Dunn4e30c4f2015-03-08 12:56:32 -050060/** Exceptions thrown by JSON_ASSERT/JSON_FAIL macros.
Christopher Dunn53837942015-03-08 12:31:00 -050061 *
62 * These are precondition-violations (user bugs) and internal errors (our bugs).
Christopher Dunn4e30c4f2015-03-08 12:56:32 -050063 *
64 * \remark derived from Json::Exception
Christopher Dunn53837942015-03-08 12:31:00 -050065 */
Christopher Dunn949babd2015-07-23 00:19:12 -050066class JSON_API LogicError : public Exception {
67public:
68 LogicError(std::string const& msg);
69};
Christopher Dunn53837942015-03-08 12:31:00 -050070
Christopher Dunn4e30c4f2015-03-08 12:56:32 -050071/// used internally
72void throwRuntimeError(std::string const& msg);
73/// used internally
74void throwLogicError(std::string const& msg);
Christopher Dunn75279cc2015-03-08 12:20:06 -050075
Aaron Jacobs9fa4e842014-07-01 08:48:54 +100076/** \brief Type of the value held by a Value object.
77 */
78enum ValueType {
79 nullValue = 0, ///< 'null' value
80 intValue, ///< signed integer value
81 uintValue, ///< unsigned integer value
82 realValue, ///< double value
83 stringValue, ///< UTF-8 string value
84 booleanValue, ///< bool value
85 arrayValue, ///< array value (ordered list)
86 objectValue ///< object value (collection of name/value pairs).
87};
Christopher Dunn6d135cb2007-06-13 15:51:04 +000088
Aaron Jacobs9fa4e842014-07-01 08:48:54 +100089enum CommentPlacement {
90 commentBefore = 0, ///< a comment placed on the line before a value
91 commentAfterOnSameLine, ///< a comment just after a value on the same line
92 commentAfter, ///< a comment on the line after a value (only make sense for
Aaron Jacobs3a0c4fc2014-07-01 09:20:48 +100093 /// root value)
Aaron Jacobs9fa4e842014-07-01 08:48:54 +100094 numberOfCommentPlacement
95};
Christopher Dunn6d135cb2007-06-13 15:51:04 +000096
97//# ifdef JSON_USE_CPPTL
98// typedef CppTL::AnyEnumerator<const char *> EnumMemberNames;
99// typedef CppTL::AnyEnumerator<const Value &> EnumValues;
100//# endif
101
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000102/** \brief Lightweight wrapper to tag static string.
103 *
104 * Value constructor and objectValue member assignement takes advantage of the
105 * StaticString and avoid the cost of string duplication when storing the
106 * string or the member name.
107 *
108 * Example of usage:
109 * \code
110 * Json::Value aValue( StaticString("some text") );
111 * Json::Value object;
112 * static const StaticString code("code");
113 * object[code] = 1234;
114 * \endcode
115 */
116class JSON_API StaticString {
117public:
Christopher Dunnff617522015-03-06 10:31:46 -0600118 explicit StaticString(const char* czstring) : c_str_(czstring) {}
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000119
Christopher Dunnff617522015-03-06 10:31:46 -0600120 operator const char*() const { return c_str_; }
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000121
Christopher Dunnff617522015-03-06 10:31:46 -0600122 const char* c_str() const { return c_str_; }
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000123
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000124private:
Christopher Dunnff617522015-03-06 10:31:46 -0600125 const char* c_str_;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000126};
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000127
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000128/** \brief Represents a <a HREF="http://www.json.org">JSON</a> value.
129 *
130 * This class is a discriminated union wrapper that can represents a:
131 * - signed integer [range: Value::minInt - Value::maxInt]
132 * - unsigned integer (range: 0 - Value::maxUInt)
133 * - double
134 * - UTF-8 string
135 * - boolean
136 * - 'null'
137 * - an ordered list of Value
138 * - collection of name/value pairs (javascript object)
139 *
140 * The type of the held value is represented by a #ValueType and
141 * can be obtained using type().
142 *
Christopher Dunnc28610f2015-02-21 11:44:16 -0600143 * Values of an #objectValue or #arrayValue can be accessed using operator[]()
144 * methods.
145 * Non-const methods will automatically create the a #nullValue element
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000146 * if it does not exist.
Christopher Dunnc28610f2015-02-21 11:44:16 -0600147 * The sequence of an #arrayValue will be automatically resized and initialized
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000148 * with #nullValue. resize() can be used to enlarge or truncate an #arrayValue.
149 *
Christopher Dunnc28610f2015-02-21 11:44:16 -0600150 * The get() methods can be used to obtain default value in the case the
151 * required element does not exist.
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000152 *
153 * It is possible to iterate over the list of a #objectValue values using
154 * the getMemberNames() method.
Christopher Dunn25342ba2015-03-02 18:05:26 -0600155 *
Christopher Dunnc28610f2015-02-21 11:44:16 -0600156 * \note #Value string-length fit in size_t, but keys must be < 2^30.
157 * (The reason is an implementation detail.) A #CharReader will raise an
Christopher Dunn25342ba2015-03-02 18:05:26 -0600158 * exception if a bound is exceeded to avoid security holes in your app,
159 * but the Value API does *not* check bounds. That is the responsibility
160 * of the caller.
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000161 */
162class JSON_API Value {
163 friend class ValueIteratorBase;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000164public:
165 typedef std::vector<std::string> Members;
166 typedef ValueIterator iterator;
167 typedef ValueConstIterator const_iterator;
168 typedef Json::UInt UInt;
169 typedef Json::Int Int;
170#if defined(JSON_HAS_INT64)
171 typedef Json::UInt64 UInt64;
172 typedef Json::Int64 Int64;
Baptiste Lepilleur842e9ac2010-12-27 17:45:23 +0000173#endif // defined(JSON_HAS_INT64)
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000174 typedef Json::LargestInt LargestInt;
175 typedef Json::LargestUInt LargestUInt;
176 typedef Json::ArrayIndex ArrayIndex;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000177
Christopher Dunn8a702972015-03-03 10:38:27 -0600178 static const Value& null; ///< We regret this reference to a global instance; prefer the simpler Value().
179 static const Value& nullRef; ///< just a kludge for binary-compatibility; same as null
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000180 /// Minimum signed integer value that can be stored in a Json::Value.
181 static const LargestInt minLargestInt;
182 /// Maximum signed integer value that can be stored in a Json::Value.
183 static const LargestInt maxLargestInt;
184 /// Maximum unsigned integer value that can be stored in a Json::Value.
185 static const LargestUInt maxLargestUInt;
Baptiste Lepilleur842e9ac2010-12-27 17:45:23 +0000186
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000187 /// Minimum signed int value that can be stored in a Json::Value.
188 static const Int minInt;
189 /// Maximum signed int value that can be stored in a Json::Value.
190 static const Int maxInt;
191 /// Maximum unsigned int value that can be stored in a Json::Value.
192 static const UInt maxUInt;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000193
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000194#if defined(JSON_HAS_INT64)
195 /// Minimum signed 64 bits int value that can be stored in a Json::Value.
196 static const Int64 minInt64;
197 /// Maximum signed 64 bits int value that can be stored in a Json::Value.
198 static const Int64 maxInt64;
199 /// Maximum unsigned 64 bits int value that can be stored in a Json::Value.
200 static const UInt64 maxUInt64;
Aaron Jacobsf1053e72011-05-24 03:18:02 +0000201#endif // defined(JSON_HAS_INT64)
Baptiste Lepilleur842e9ac2010-12-27 17:45:23 +0000202
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000203private:
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000204#ifndef JSONCPP_DOC_EXCLUDE_IMPLEMENTATION
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000205 class CZString {
206 public:
207 enum DuplicationPolicy {
208 noDuplication = 0,
209 duplicate,
210 duplicateOnCopy
211 };
212 CZString(ArrayIndex index);
Christopher Dunnc28610f2015-02-21 11:44:16 -0600213 CZString(char const* str, unsigned length, DuplicationPolicy allocate);
214 CZString(CZString const& other);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000215 ~CZString();
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000216 CZString& operator=(CZString other);
Christopher Dunnc28610f2015-02-21 11:44:16 -0600217 bool operator<(CZString const& other) const;
218 bool operator==(CZString const& other) const;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000219 ArrayIndex index() const;
Christopher Dunnc28610f2015-02-21 11:44:16 -0600220 //const char* c_str() const; ///< \deprecated
221 char const* data() const;
222 unsigned length() const;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000223 bool isStaticString() const;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000224
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000225 private:
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000226 void swap(CZString& other);
Christopher Dunnc28610f2015-02-21 11:44:16 -0600227
Christopher Dunn57ad0512015-03-02 12:10:35 -0600228 struct StringStorage {
Dan Liufcbab022015-04-27 05:20:05 +0100229 unsigned policy_: 2;
Christopher Dunn57ad0512015-03-02 12:10:35 -0600230 unsigned length_: 30; // 1GB max
231 };
232
Christopher Dunnc28610f2015-02-21 11:44:16 -0600233 char const* cstr_; // actually, a prefixed string, unless policy is noDup
Christopher Dunn57ad0512015-03-02 12:10:35 -0600234 union {
235 ArrayIndex index_;
236 StringStorage storage_;
237 };
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000238 };
239
240public:
241#ifndef JSON_USE_CPPTL_SMALLMAP
242 typedef std::map<CZString, Value> ObjectValues;
243#else
244 typedef CppTL::SmallMap<CZString, Value> ObjectValues;
245#endif // ifndef JSON_USE_CPPTL_SMALLMAP
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000246#endif // ifndef JSONCPP_DOC_EXCLUDE_IMPLEMENTATION
247
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000248public:
249 /** \brief Create a default Value of the given type.
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000250
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000251 This is a very useful constructor.
252 To create an empty array, pass arrayValue.
253 To create an empty object, pass objectValue.
254 Another Value can then be set to this one by assignment.
255This is useful since clear() and resize() will not alter types.
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000256
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000257 Examples:
258\code
259Json::Value null_value; // null
260Json::Value arr_value(Json::arrayValue); // []
261Json::Value obj_value(Json::objectValue); // {}
262\endcode
263 */
264 Value(ValueType type = nullValue);
265 Value(Int value);
266 Value(UInt value);
Baptiste Lepilleur842e9ac2010-12-27 17:45:23 +0000267#if defined(JSON_HAS_INT64)
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000268 Value(Int64 value);
269 Value(UInt64 value);
Baptiste Lepilleur842e9ac2010-12-27 17:45:23 +0000270#endif // if defined(JSON_HAS_INT64)
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000271 Value(double value);
Christopher Dunn8a702972015-03-03 10:38:27 -0600272 Value(const char* value); ///< Copy til first 0. (NULL causes to seg-fault.)
Christopher Dunn89704032015-07-11 12:09:59 -0500273 Value(const char* begin, const char* end); ///< Copy all, incl zeroes.
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000274 /** \brief Constructs a value from a static string.
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000275
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000276 * Like other value string constructor but do not duplicate the string for
277 * internal storage. The given string must remain alive after the call to this
278 * constructor.
Christopher Dunnc28610f2015-02-21 11:44:16 -0600279 * \note This works only for null-terminated strings. (We cannot change the
280 * size of this class, so we have nowhere to store the length,
281 * which might be computed later for various operations.)
282 *
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000283 * Example of usage:
284 * \code
Christopher Dunnc28610f2015-02-21 11:44:16 -0600285 * static StaticString foo("some text");
286 * Json::Value aValue(foo);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000287 * \endcode
288 */
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000289 Value(const StaticString& value);
Christopher Dunn8a702972015-03-03 10:38:27 -0600290 Value(const std::string& value); ///< Copy data() til size(). Embedded zeroes too.
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000291#ifdef JSON_USE_CPPTL
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000292 Value(const CppTL::ConstString& value);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000293#endif
294 Value(bool value);
Christopher Dunn66eb72f2015-01-20 11:02:22 -0600295 /// Deep copy.
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000296 Value(const Value& other);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000297 ~Value();
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000298
Christopher Dunn7f439f42015-03-06 09:22:57 -0600299 /// Deep copy, then swap(other).
300 /// \note Over-write existing comments. To preserve comments, use #swapPayload().
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000301 Value& operator=(Value other);
Christopher Dunn66eb72f2015-01-20 11:02:22 -0600302 /// Swap everything.
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000303 void swap(Value& other);
Christopher Dunn66eb72f2015-01-20 11:02:22 -0600304 /// Swap values but leave comments and source offsets in place.
305 void swapPayload(Value& other);
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000306
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000307 ValueType type() const;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000308
Christopher Dunn66eb72f2015-01-20 11:02:22 -0600309 /// Compare payload only, not comments etc.
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000310 bool operator<(const Value& other) const;
311 bool operator<=(const Value& other) const;
312 bool operator>=(const Value& other) const;
313 bool operator>(const Value& other) const;
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000314 bool operator==(const Value& other) const;
315 bool operator!=(const Value& other) const;
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000316 int compare(const Value& other) const;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000317
Christopher Dunn8a702972015-03-03 10:38:27 -0600318 const char* asCString() const; ///< Embedded zeroes could cause you trouble!
319 std::string asString() const; ///< Embedded zeroes are possible.
Christopher Dunnc28610f2015-02-21 11:44:16 -0600320 /** Get raw char* of string-value.
321 * \return false if !string. (Seg-fault if str or end are NULL.)
322 */
323 bool getString(
Christopher Dunn89704032015-07-11 12:09:59 -0500324 char const** begin, char const** end) const;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000325#ifdef JSON_USE_CPPTL
326 CppTL::ConstString asConstString() const;
327#endif
328 Int asInt() const;
329 UInt asUInt() const;
Aaron Jacobsf1053e72011-05-24 03:18:02 +0000330#if defined(JSON_HAS_INT64)
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000331 Int64 asInt64() const;
332 UInt64 asUInt64() const;
Aaron Jacobsf1053e72011-05-24 03:18:02 +0000333#endif // if defined(JSON_HAS_INT64)
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000334 LargestInt asLargestInt() const;
335 LargestUInt asLargestUInt() const;
336 float asFloat() const;
337 double asDouble() const;
338 bool asBool() const;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000339
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000340 bool isNull() const;
341 bool isBool() const;
342 bool isInt() const;
343 bool isInt64() const;
344 bool isUInt() const;
345 bool isUInt64() const;
346 bool isIntegral() const;
347 bool isDouble() const;
348 bool isNumeric() const;
349 bool isString() const;
350 bool isArray() const;
351 bool isObject() const;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000352
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000353 bool isConvertibleTo(ValueType other) const;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000354
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000355 /// Number of values in array or object
356 ArrayIndex size() const;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000357
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000358 /// \brief Return true if empty array, empty object, or null;
359 /// otherwise, false.
360 bool empty() const;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000361
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000362 /// Return isNull()
363 bool operator!() const;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000364
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000365 /// Remove all object members and array elements.
366 /// \pre type() is arrayValue, objectValue, or nullValue
367 /// \post type() is unchanged
368 void clear();
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000369
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000370 /// Resize the array to size elements.
371 /// New elements are initialized to null.
372 /// May only be called on nullValue or arrayValue.
373 /// \pre type() is arrayValue or nullValue
374 /// \post type() is arrayValue
375 void resize(ArrayIndex size);
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000376
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000377 /// Access an array element (zero based index ).
378 /// If the array contains less than index element, then null value are
379 /// inserted
380 /// in the array so that its size is index+1.
381 /// (You may need to say 'value[0u]' to get your compiler to distinguish
382 /// this from the operator[] which takes a string.)
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000383 Value& operator[](ArrayIndex index);
Baptiste Lepilleurfa130ef2010-12-24 12:47:14 +0000384
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000385 /// Access an array element (zero based index ).
386 /// If the array contains less than index element, then null value are
387 /// inserted
388 /// in the array so that its size is index+1.
389 /// (You may need to say 'value[0u]' to get your compiler to distinguish
390 /// this from the operator[] which takes a string.)
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000391 Value& operator[](int index);
Baptiste Lepilleurfa130ef2010-12-24 12:47:14 +0000392
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000393 /// Access an array element (zero based index )
394 /// (You may need to say 'value[0u]' to get your compiler to distinguish
395 /// this from the operator[] which takes a string.)
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000396 const Value& operator[](ArrayIndex index) const;
Baptiste Lepilleurfa130ef2010-12-24 12:47:14 +0000397
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000398 /// Access an array element (zero based index )
399 /// (You may need to say 'value[0u]' to get your compiler to distinguish
400 /// this from the operator[] which takes a string.)
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000401 const Value& operator[](int index) const;
Baptiste Lepilleurfa130ef2010-12-24 12:47:14 +0000402
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000403 /// If the array contains at least index+1 elements, returns the element
404 /// value,
405 /// otherwise returns defaultValue.
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000406 Value get(ArrayIndex index, const Value& defaultValue) const;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000407 /// Return true if index < size().
408 bool isValidIndex(ArrayIndex index) const;
409 /// \brief Append value to array at the end.
410 ///
411 /// Equivalent to jsonvalue[jsonvalue.size()] = value;
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000412 Value& append(const Value& value);
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000413
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000414 /// Access an object value by name, create a null member if it does not exist.
Christopher Dunn25342ba2015-03-02 18:05:26 -0600415 /// \note Because of our implementation, keys are limited to 2^30 -1 chars.
416 /// Exceeding that will cause an exception.
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000417 Value& operator[](const char* key);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000418 /// Access an object value by name, returns null if there is no member with
419 /// that name.
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000420 const Value& operator[](const char* key) const;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000421 /// Access an object value by name, create a null member if it does not exist.
Christopher Dunn25342ba2015-03-02 18:05:26 -0600422 /// \param key may contain embedded nulls.
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000423 Value& operator[](const std::string& key);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000424 /// Access an object value by name, returns null if there is no member with
425 /// that name.
Christopher Dunn25342ba2015-03-02 18:05:26 -0600426 /// \param key may contain embedded nulls.
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000427 const Value& operator[](const std::string& key) const;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000428 /** \brief Access an object value by name, create a null member if it does not
429 exist.
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000430
Christopher Dunn25342ba2015-03-02 18:05:26 -0600431 * If the object has no entry for that name, then the member name used to store
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000432 * the new entry is not duplicated.
433 * Example of use:
434 * \code
435 * Json::Value object;
436 * static const StaticString code("code");
437 * object[code] = 1234;
438 * \endcode
439 */
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000440 Value& operator[](const StaticString& key);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000441#ifdef JSON_USE_CPPTL
442 /// Access an object value by name, create a null member if it does not exist.
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000443 Value& operator[](const CppTL::ConstString& key);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000444 /// Access an object value by name, returns null if there is no member with
445 /// that name.
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000446 const Value& operator[](const CppTL::ConstString& key) const;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000447#endif
448 /// Return the member named key if it exist, defaultValue otherwise.
Christopher Dunn0fd28752015-03-05 16:38:43 -0600449 /// \note deep copy
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000450 Value get(const char* key, const Value& defaultValue) const;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000451 /// Return the member named key if it exist, defaultValue otherwise.
Christopher Dunn0fd28752015-03-05 16:38:43 -0600452 /// \note deep copy
Christopher Dunn89704032015-07-11 12:09:59 -0500453 /// \note key may contain embedded nulls.
454 Value get(const char* begin, const char* end, const Value& defaultValue) const;
Christopher Dunn25342ba2015-03-02 18:05:26 -0600455 /// Return the member named key if it exist, defaultValue otherwise.
Christopher Dunn0fd28752015-03-05 16:38:43 -0600456 /// \note deep copy
Christopher Dunn25342ba2015-03-02 18:05:26 -0600457 /// \param key may contain embedded nulls.
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000458 Value get(const std::string& key, const Value& defaultValue) const;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000459#ifdef JSON_USE_CPPTL
460 /// Return the member named key if it exist, defaultValue otherwise.
Christopher Dunn0fd28752015-03-05 16:38:43 -0600461 /// \note deep copy
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000462 Value get(const CppTL::ConstString& key, const Value& defaultValue) const;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000463#endif
Christopher Dunn25342ba2015-03-02 18:05:26 -0600464 /// Most general and efficient version of isMember()const, get()const,
465 /// and operator[]const
Christopher Dunn89704032015-07-11 12:09:59 -0500466 /// \note As stated elsewhere, behavior is undefined if (end-begin) >= 2^30
467 Value const* find(char const* begin, char const* end) const;
Christopher Dunnc28610f2015-02-21 11:44:16 -0600468 /// Most general and efficient version of object-mutators.
Christopher Dunn89704032015-07-11 12:09:59 -0500469 /// \note As stated elsewhere, behavior is undefined if (end-begin) >= 2^30
Christopher Dunnc28610f2015-02-21 11:44:16 -0600470 /// \return non-zero, but JSON_ASSERT if this is neither object nor nullValue.
Christopher Dunn89704032015-07-11 12:09:59 -0500471 Value const* demand(char const* begin, char const* end);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000472 /// \brief Remove and return the named member.
473 ///
474 /// Do nothing if it did not exist.
475 /// \return the removed Value, or null.
476 /// \pre type() is objectValue or nullValue
477 /// \post type() is unchanged
Christopher Dunn76746b02015-01-21 16:01:30 -0600478 /// \deprecated
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000479 Value removeMember(const char* key);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000480 /// Same as removeMember(const char*)
Christopher Dunn25342ba2015-03-02 18:05:26 -0600481 /// \param key may contain embedded nulls.
Christopher Dunn76746b02015-01-21 16:01:30 -0600482 /// \deprecated
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000483 Value removeMember(const std::string& key);
Christopher Dunn89704032015-07-11 12:09:59 -0500484 /// Same as removeMember(const char* begin, const char* end, Value* removed),
Christopher Dunn25342ba2015-03-02 18:05:26 -0600485 /// but 'key' is null-terminated.
486 bool removeMember(const char* key, Value* removed);
Christopher Dunn76746b02015-01-21 16:01:30 -0600487 /** \brief Remove the named map member.
488
489 Update 'removed' iff removed.
Christopher Dunn25342ba2015-03-02 18:05:26 -0600490 \param key may contain embedded nulls.
Christopher Dunn76746b02015-01-21 16:01:30 -0600491 \return true iff removed (no exceptions)
492 */
Christopher Dunn25342ba2015-03-02 18:05:26 -0600493 bool removeMember(std::string const& key, Value* removed);
494 /// Same as removeMember(std::string const& key, Value* removed)
Christopher Dunn89704032015-07-11 12:09:59 -0500495 bool removeMember(const char* begin, const char* end, Value* removed);
Christopher Dunn9de2c2d2015-01-20 16:15:40 -0600496 /** \brief Remove the indexed array element.
497
498 O(n) expensive operations.
499 Update 'removed' iff removed.
Christopher Dunne87e41c2015-01-20 16:24:11 -0600500 \return true iff removed (no exceptions)
Christopher Dunn9de2c2d2015-01-20 16:15:40 -0600501 */
502 bool removeIndex(ArrayIndex i, Value* removed);
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000503
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000504 /// Return true if the object has a member named key.
Christopher Dunnc28610f2015-02-21 11:44:16 -0600505 /// \note 'key' must be null-terminated.
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000506 bool isMember(const char* key) const;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000507 /// Return true if the object has a member named key.
Christopher Dunn25342ba2015-03-02 18:05:26 -0600508 /// \param key may contain embedded nulls.
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000509 bool isMember(const std::string& key) const;
Christopher Dunn25342ba2015-03-02 18:05:26 -0600510 /// Same as isMember(std::string const& key)const
Christopher Dunn89704032015-07-11 12:09:59 -0500511 bool isMember(const char* begin, const char* end) const;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000512#ifdef JSON_USE_CPPTL
513 /// Return true if the object has a member named key.
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000514 bool isMember(const CppTL::ConstString& key) const;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000515#endif
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000516
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000517 /// \brief Return a list of the member names.
518 ///
519 /// If null, return an empty list.
520 /// \pre type() is objectValue or nullValue
521 /// \post if type() was nullValue, it remains nullValue
522 Members getMemberNames() const;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000523
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000524 //# ifdef JSON_USE_CPPTL
525 // EnumMemberNames enumMemberNames() const;
526 // EnumValues enumValues() const;
527 //# endif
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000528
Christopher Dunn1e3149a2015-01-25 14:16:13 -0600529 /// \deprecated Always pass len.
Christopher Dunn50069d72015-03-08 13:35:57 -0500530 JSONCPP_DEPRECATED("Use setComment(std::string const&) instead.")
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000531 void setComment(const char* comment, CommentPlacement placement);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000532 /// Comments must be //... or /* ... */
Christopher Dunn1e3149a2015-01-25 14:16:13 -0600533 void setComment(const char* comment, size_t len, CommentPlacement placement);
534 /// Comments must be //... or /* ... */
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000535 void setComment(const std::string& comment, CommentPlacement placement);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000536 bool hasComment(CommentPlacement placement) const;
537 /// Include delimiters and embedded newlines.
538 std::string getComment(CommentPlacement placement) const;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000539
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000540 std::string toStyledString() const;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000541
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000542 const_iterator begin() const;
543 const_iterator end() const;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000544
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000545 iterator begin();
546 iterator end();
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000547
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000548 // Accessors for the [start, limit) range of bytes within the JSON text from
549 // which this value was parsed, if any.
550 void setOffsetStart(size_t start);
551 void setOffsetLimit(size_t limit);
552 size_t getOffsetStart() const;
553 size_t getOffsetLimit() const;
Aaron Jacobs68db6552014-04-23 23:41:12 +0000554
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000555private:
Billy Donahue8eb5d892014-11-10 01:35:42 -0500556 void initBasic(ValueType type, bool allocated = false);
557
Christopher Dunnc28610f2015-02-21 11:44:16 -0600558 Value& resolveReference(const char* key);
559 Value& resolveReference(const char* key, const char* end);
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000560
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000561 struct CommentInfo {
562 CommentInfo();
563 ~CommentInfo();
564
Christopher Dunn1e3149a2015-01-25 14:16:13 -0600565 void setComment(const char* text, size_t len);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000566
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000567 char* comment_;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000568 };
569
570 // struct MemberNamesTransform
571 //{
572 // typedef const char *result_type;
573 // const char *operator()( const CZString &name ) const
574 // {
575 // return name.c_str();
576 // }
577 //};
578
579 union ValueHolder {
580 LargestInt int_;
581 LargestUInt uint_;
582 double real_;
583 bool bool_;
Christopher Dunnc28610f2015-02-21 11:44:16 -0600584 char* string_; // actually ptr to unsigned, followed by str, unless !allocated_
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000585 ObjectValues* map_;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000586 } value_;
587 ValueType type_ : 8;
Christopher Dunn2bc61372015-01-24 13:42:37 -0600588 unsigned int allocated_ : 1; // Notes: if declared as bool, bitfield is useless.
Christopher Dunnc28610f2015-02-21 11:44:16 -0600589 // If not allocated_, string_ must be null-terminated.
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000590 CommentInfo* comments_;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000591
592 // [start, limit) byte offsets in the source JSON text from which this Value
593 // was extracted.
594 size_t start_;
595 size_t limit_;
596};
597
598/** \brief Experimental and untested: represents an element of the "path" to
599 * access a node.
600 */
601class JSON_API PathArgument {
602public:
603 friend class Path;
604
605 PathArgument();
606 PathArgument(ArrayIndex index);
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000607 PathArgument(const char* key);
608 PathArgument(const std::string& key);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000609
610private:
611 enum Kind {
612 kindNone = 0,
613 kindIndex,
614 kindKey
615 };
616 std::string key_;
617 ArrayIndex index_;
618 Kind kind_;
619};
620
621/** \brief Experimental and untested: represents a "path" to access a node.
622 *
623 * Syntax:
624 * - "." => root node
625 * - ".[n]" => elements at index 'n' of root node (an array value)
626 * - ".name" => member named 'name' of root node (an object value)
627 * - ".name1.name2.name3"
628 * - ".[0][1][2].name1[3]"
629 * - ".%" => member name is provided as parameter
630 * - ".[%]" => index is provied as parameter
631 */
632class JSON_API Path {
633public:
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000634 Path(const std::string& path,
635 const PathArgument& a1 = PathArgument(),
636 const PathArgument& a2 = PathArgument(),
637 const PathArgument& a3 = PathArgument(),
638 const PathArgument& a4 = PathArgument(),
639 const PathArgument& a5 = PathArgument());
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000640
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000641 const Value& resolve(const Value& root) const;
642 Value resolve(const Value& root, const Value& defaultValue) const;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000643 /// Creates the "path" to access the specified node and returns a reference on
644 /// the node.
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000645 Value& make(Value& root) const;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000646
647private:
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000648 typedef std::vector<const PathArgument*> InArgs;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000649 typedef std::vector<PathArgument> Args;
650
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000651 void makePath(const std::string& path, const InArgs& in);
652 void addPathInArg(const std::string& path,
653 const InArgs& in,
654 InArgs::const_iterator& itInArg,
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000655 PathArgument::Kind kind);
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000656 void invalidPath(const std::string& path, int location);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000657
658 Args args_;
659};
660
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000661/** \brief base class for Value iterators.
662 *
663 */
664class JSON_API ValueIteratorBase {
665public:
666 typedef std::bidirectional_iterator_tag iterator_category;
667 typedef unsigned int size_t;
668 typedef int difference_type;
669 typedef ValueIteratorBase SelfType;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000670
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000671 bool operator==(const SelfType& other) const { return isEqual(other); }
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000672
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000673 bool operator!=(const SelfType& other) const { return !isEqual(other); }
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000674
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000675 difference_type operator-(const SelfType& other) const {
Kevin Grant4c5832a2015-02-14 20:53:35 -0800676 return other.computeDistance(*this);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000677 }
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000678
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000679 /// Return either the index or the member name of the referenced value as a
680 /// Value.
681 Value key() const;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000682
Christopher Dunned495ed2015-03-08 14:01:28 -0500683 /// Return the index of the referenced Value, or -1 if it is not an arrayValue.
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000684 UInt index() const;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000685
Christopher Dunned495ed2015-03-08 14:01:28 -0500686 /// Return the member name of the referenced Value, or "" if it is not an
687 /// objectValue.
688 /// \note Avoid `c_str()` on result, as embedded zeroes are possible.
689 std::string name() const;
690
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000691 /// Return the member name of the referenced Value. "" if it is not an
692 /// objectValue.
Christopher Dunnc28610f2015-02-21 11:44:16 -0600693 /// \deprecated This cannot be used for UTF-8 strings, since there can be embedded nulls.
Christopher Dunned495ed2015-03-08 14:01:28 -0500694 JSONCPP_DEPRECATED("Use `key = name();` instead.")
Christopher Dunnc28610f2015-02-21 11:44:16 -0600695 char const* memberName() const;
696 /// Return the member name of the referenced Value, or NULL if it is not an
697 /// objectValue.
Christopher Dunned495ed2015-03-08 14:01:28 -0500698 /// \note Better version than memberName(). Allows embedded nulls.
Christopher Dunnc28610f2015-02-21 11:44:16 -0600699 char const* memberName(char const** end) const;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000700
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000701protected:
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000702 Value& deref() const;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000703
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000704 void increment();
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000705
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000706 void decrement();
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000707
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000708 difference_type computeDistance(const SelfType& other) const;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000709
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000710 bool isEqual(const SelfType& other) const;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000711
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000712 void copy(const SelfType& other);
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000713
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000714private:
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000715 Value::ObjectValues::iterator current_;
716 // Indicates that iterator is for a null value.
717 bool isNull_;
Christopher Dunn2a10f4a2015-04-28 04:55:12 +0100718
719public:
720 // For some reason, BORLAND needs these at the end, rather
721 // than earlier. No idea why.
722 ValueIteratorBase();
723 explicit ValueIteratorBase(const Value::ObjectValues::iterator& current);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000724};
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000725
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000726/** \brief const iterator for object and array value.
727 *
728 */
729class JSON_API ValueConstIterator : public ValueIteratorBase {
730 friend class Value;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000731
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000732public:
733 typedef const Value value_type;
Christopher Dunnc28610f2015-02-21 11:44:16 -0600734 //typedef unsigned int size_t;
735 //typedef int difference_type;
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000736 typedef const Value& reference;
737 typedef const Value* pointer;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000738 typedef ValueConstIterator SelfType;
739
740 ValueConstIterator();
ycqiuc8a8cfc2015-10-06 16:46:19 +0800741 ValueConstIterator(ValueIterator const& other);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000742
743private:
744/*! \internal Use by Value to create an iterator.
745 */
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000746 explicit ValueConstIterator(const Value::ObjectValues::iterator& current);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000747public:
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000748 SelfType& operator=(const ValueIteratorBase& other);
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000749
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000750 SelfType operator++(int) {
751 SelfType temp(*this);
752 ++*this;
753 return temp;
754 }
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000755
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000756 SelfType operator--(int) {
757 SelfType temp(*this);
758 --*this;
759 return temp;
760 }
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000761
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000762 SelfType& operator--() {
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000763 decrement();
764 return *this;
765 }
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000766
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000767 SelfType& operator++() {
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000768 increment();
769 return *this;
770 }
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000771
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000772 reference operator*() const { return deref(); }
Braden McDorman540db3b2014-09-14 02:31:23 -0500773
774 pointer operator->() const { return &deref(); }
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000775};
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000776
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000777/** \brief Iterator for object and array value.
778 */
779class JSON_API ValueIterator : public ValueIteratorBase {
780 friend class Value;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000781
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000782public:
783 typedef Value value_type;
784 typedef unsigned int size_t;
785 typedef int difference_type;
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000786 typedef Value& reference;
787 typedef Value* pointer;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000788 typedef ValueIterator SelfType;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000789
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000790 ValueIterator();
ycqiuc8a8cfc2015-10-06 16:46:19 +0800791 explicit ValueIterator(const ValueConstIterator& other);
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000792 ValueIterator(const ValueIterator& other);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000793
794private:
795/*! \internal Use by Value to create an iterator.
796 */
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000797 explicit ValueIterator(const Value::ObjectValues::iterator& current);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000798public:
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000799 SelfType& operator=(const SelfType& other);
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000800
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000801 SelfType operator++(int) {
802 SelfType temp(*this);
803 ++*this;
804 return temp;
805 }
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000806
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000807 SelfType operator--(int) {
808 SelfType temp(*this);
809 --*this;
810 return temp;
811 }
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000812
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000813 SelfType& operator--() {
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000814 decrement();
815 return *this;
816 }
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000817
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000818 SelfType& operator++() {
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000819 increment();
820 return *this;
821 }
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000822
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000823 reference operator*() const { return deref(); }
Braden McDorman540db3b2014-09-14 02:31:23 -0500824
825 pointer operator->() const { return &deref(); }
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000826};
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000827
828} // namespace Json
829
datadiode9454e682015-01-20 15:25:04 -0600830
831namespace std {
832/// Specialize std::swap() for Json::Value.
833template<>
834inline void swap(Json::Value& a, Json::Value& b) { a.swap(b); }
835}
836
837
Baptiste Lepilleureafd7022013-05-08 20:21:11 +0000838#if defined(JSONCPP_DISABLE_DLL_INTERFACE_WARNING)
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000839#pragma warning(pop)
Baptiste Lepilleureafd7022013-05-08 20:21:11 +0000840#endif // if defined(JSONCPP_DISABLE_DLL_INTERFACE_WARNING)
841
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000842#endif // CPPTL_JSON_H_INCLUDED