blob: b3f1ceb854d1cd6bfdd9d12bc90581f6ae2aa805 [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);
Motti2b008912015-04-20 17:44:47 +0300215#if JSON_HAS_RVALUE_REFERENCES
216 CZString(CZString&& other);
217#endif
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000218 ~CZString();
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000219 CZString& operator=(CZString other);
Christopher Dunnc28610f2015-02-21 11:44:16 -0600220 bool operator<(CZString const& other) const;
221 bool operator==(CZString const& other) const;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000222 ArrayIndex index() const;
Christopher Dunnc28610f2015-02-21 11:44:16 -0600223 //const char* c_str() const; ///< \deprecated
224 char const* data() const;
225 unsigned length() const;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000226 bool isStaticString() const;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000227
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000228 private:
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000229 void swap(CZString& other);
Christopher Dunnc28610f2015-02-21 11:44:16 -0600230
Christopher Dunn57ad0512015-03-02 12:10:35 -0600231 struct StringStorage {
Dan Liufcbab022015-04-27 05:20:05 +0100232 unsigned policy_: 2;
Christopher Dunn57ad0512015-03-02 12:10:35 -0600233 unsigned length_: 30; // 1GB max
234 };
235
Christopher Dunnc28610f2015-02-21 11:44:16 -0600236 char const* cstr_; // actually, a prefixed string, unless policy is noDup
Christopher Dunn57ad0512015-03-02 12:10:35 -0600237 union {
238 ArrayIndex index_;
239 StringStorage storage_;
240 };
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000241 };
242
243public:
244#ifndef JSON_USE_CPPTL_SMALLMAP
245 typedef std::map<CZString, Value> ObjectValues;
246#else
247 typedef CppTL::SmallMap<CZString, Value> ObjectValues;
248#endif // ifndef JSON_USE_CPPTL_SMALLMAP
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000249#endif // ifndef JSONCPP_DOC_EXCLUDE_IMPLEMENTATION
250
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000251public:
252 /** \brief Create a default Value of the given type.
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000253
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000254 This is a very useful constructor.
255 To create an empty array, pass arrayValue.
256 To create an empty object, pass objectValue.
257 Another Value can then be set to this one by assignment.
258This is useful since clear() and resize() will not alter types.
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000259
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000260 Examples:
261\code
262Json::Value null_value; // null
263Json::Value arr_value(Json::arrayValue); // []
264Json::Value obj_value(Json::objectValue); // {}
265\endcode
266 */
267 Value(ValueType type = nullValue);
268 Value(Int value);
269 Value(UInt value);
Baptiste Lepilleur842e9ac2010-12-27 17:45:23 +0000270#if defined(JSON_HAS_INT64)
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000271 Value(Int64 value);
272 Value(UInt64 value);
Baptiste Lepilleur842e9ac2010-12-27 17:45:23 +0000273#endif // if defined(JSON_HAS_INT64)
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000274 Value(double value);
Christopher Dunn8a702972015-03-03 10:38:27 -0600275 Value(const char* value); ///< Copy til first 0. (NULL causes to seg-fault.)
Christopher Dunn89704032015-07-11 12:09:59 -0500276 Value(const char* begin, const char* end); ///< Copy all, incl zeroes.
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000277 /** \brief Constructs a value from a static string.
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000278
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000279 * Like other value string constructor but do not duplicate the string for
280 * internal storage. The given string must remain alive after the call to this
281 * constructor.
Christopher Dunnc28610f2015-02-21 11:44:16 -0600282 * \note This works only for null-terminated strings. (We cannot change the
283 * size of this class, so we have nowhere to store the length,
284 * which might be computed later for various operations.)
285 *
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000286 * Example of usage:
287 * \code
Christopher Dunnc28610f2015-02-21 11:44:16 -0600288 * static StaticString foo("some text");
289 * Json::Value aValue(foo);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000290 * \endcode
291 */
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000292 Value(const StaticString& value);
Christopher Dunn8a702972015-03-03 10:38:27 -0600293 Value(const std::string& value); ///< Copy data() til size(). Embedded zeroes too.
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000294#ifdef JSON_USE_CPPTL
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000295 Value(const CppTL::ConstString& value);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000296#endif
297 Value(bool value);
Christopher Dunn66eb72f2015-01-20 11:02:22 -0600298 /// Deep copy.
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000299 Value(const Value& other);
Motti2b008912015-04-20 17:44:47 +0300300#if JSON_HAS_RVALUE_REFERENCES
301 /// Move constructor
302 Value(Value&& other);
303#endif
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000304 ~Value();
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000305
Christopher Dunn7f439f42015-03-06 09:22:57 -0600306 /// Deep copy, then swap(other).
307 /// \note Over-write existing comments. To preserve comments, use #swapPayload().
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000308 Value& operator=(Value other);
Christopher Dunn66eb72f2015-01-20 11:02:22 -0600309 /// Swap everything.
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000310 void swap(Value& other);
Christopher Dunn66eb72f2015-01-20 11:02:22 -0600311 /// Swap values but leave comments and source offsets in place.
312 void swapPayload(Value& other);
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000313
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000314 ValueType type() const;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000315
Christopher Dunn66eb72f2015-01-20 11:02:22 -0600316 /// Compare payload only, not comments etc.
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000317 bool operator<(const Value& other) const;
318 bool operator<=(const Value& other) const;
319 bool operator>=(const Value& other) const;
320 bool operator>(const Value& other) const;
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000321 bool operator==(const Value& other) const;
322 bool operator!=(const Value& other) const;
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000323 int compare(const Value& other) const;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000324
Christopher Dunn8a702972015-03-03 10:38:27 -0600325 const char* asCString() const; ///< Embedded zeroes could cause you trouble!
326 std::string asString() const; ///< Embedded zeroes are possible.
Christopher Dunnc28610f2015-02-21 11:44:16 -0600327 /** Get raw char* of string-value.
328 * \return false if !string. (Seg-fault if str or end are NULL.)
329 */
330 bool getString(
Christopher Dunn89704032015-07-11 12:09:59 -0500331 char const** begin, char const** end) const;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000332#ifdef JSON_USE_CPPTL
333 CppTL::ConstString asConstString() const;
334#endif
335 Int asInt() const;
336 UInt asUInt() const;
Aaron Jacobsf1053e72011-05-24 03:18:02 +0000337#if defined(JSON_HAS_INT64)
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000338 Int64 asInt64() const;
339 UInt64 asUInt64() const;
Aaron Jacobsf1053e72011-05-24 03:18:02 +0000340#endif // if defined(JSON_HAS_INT64)
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000341 LargestInt asLargestInt() const;
342 LargestUInt asLargestUInt() const;
343 float asFloat() const;
344 double asDouble() const;
345 bool asBool() const;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000346
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000347 bool isNull() const;
348 bool isBool() const;
349 bool isInt() const;
350 bool isInt64() const;
351 bool isUInt() const;
352 bool isUInt64() const;
353 bool isIntegral() const;
354 bool isDouble() const;
355 bool isNumeric() const;
356 bool isString() const;
357 bool isArray() const;
358 bool isObject() const;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000359
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000360 bool isConvertibleTo(ValueType other) const;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000361
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000362 /// Number of values in array or object
363 ArrayIndex size() const;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000364
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000365 /// \brief Return true if empty array, empty object, or null;
366 /// otherwise, false.
367 bool empty() const;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000368
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000369 /// Return isNull()
370 bool operator!() const;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000371
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000372 /// Remove all object members and array elements.
373 /// \pre type() is arrayValue, objectValue, or nullValue
374 /// \post type() is unchanged
375 void clear();
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000376
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000377 /// Resize the array to size elements.
378 /// New elements are initialized to null.
379 /// May only be called on nullValue or arrayValue.
380 /// \pre type() is arrayValue or nullValue
381 /// \post type() is arrayValue
382 void resize(ArrayIndex size);
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000383
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000384 /// Access an array element (zero based index ).
385 /// If the array contains less than index element, then null value are
386 /// inserted
387 /// in the array so that its size is index+1.
388 /// (You may need to say 'value[0u]' to get your compiler to distinguish
389 /// this from the operator[] which takes a string.)
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000390 Value& operator[](ArrayIndex index);
Baptiste Lepilleurfa130ef2010-12-24 12:47:14 +0000391
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000392 /// Access an array element (zero based index ).
393 /// If the array contains less than index element, then null value are
394 /// inserted
395 /// in the array so that its size is index+1.
396 /// (You may need to say 'value[0u]' to get your compiler to distinguish
397 /// this from the operator[] which takes a string.)
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000398 Value& operator[](int index);
Baptiste Lepilleurfa130ef2010-12-24 12:47:14 +0000399
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000400 /// Access an array element (zero based index )
401 /// (You may need to say 'value[0u]' to get your compiler to distinguish
402 /// this from the operator[] which takes a string.)
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000403 const Value& operator[](ArrayIndex index) const;
Baptiste Lepilleurfa130ef2010-12-24 12:47:14 +0000404
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000405 /// Access an array element (zero based index )
406 /// (You may need to say 'value[0u]' to get your compiler to distinguish
407 /// this from the operator[] which takes a string.)
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000408 const Value& operator[](int index) const;
Baptiste Lepilleurfa130ef2010-12-24 12:47:14 +0000409
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000410 /// If the array contains at least index+1 elements, returns the element
411 /// value,
412 /// otherwise returns defaultValue.
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000413 Value get(ArrayIndex index, const Value& defaultValue) const;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000414 /// Return true if index < size().
415 bool isValidIndex(ArrayIndex index) const;
416 /// \brief Append value to array at the end.
417 ///
418 /// Equivalent to jsonvalue[jsonvalue.size()] = value;
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000419 Value& append(const Value& value);
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000420
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 /// \note Because of our implementation, keys are limited to 2^30 -1 chars.
423 /// Exceeding that will cause an exception.
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000424 Value& operator[](const char* key);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000425 /// Access an object value by name, returns null if there is no member with
426 /// that name.
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000427 const Value& operator[](const char* key) const;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000428 /// Access an object value by name, create a null member if it does not exist.
Christopher Dunn25342ba2015-03-02 18:05:26 -0600429 /// \param key may contain embedded nulls.
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000430 Value& operator[](const std::string& key);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000431 /// Access an object value by name, returns null if there is no member with
432 /// that name.
Christopher Dunn25342ba2015-03-02 18:05:26 -0600433 /// \param key may contain embedded nulls.
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000434 const Value& operator[](const std::string& key) const;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000435 /** \brief Access an object value by name, create a null member if it does not
436 exist.
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000437
Christopher Dunn25342ba2015-03-02 18:05:26 -0600438 * If the object has no entry for that name, then the member name used to store
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000439 * the new entry is not duplicated.
440 * Example of use:
441 * \code
442 * Json::Value object;
443 * static const StaticString code("code");
444 * object[code] = 1234;
445 * \endcode
446 */
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000447 Value& operator[](const StaticString& key);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000448#ifdef JSON_USE_CPPTL
449 /// Access an object value by name, create a null member if it does not exist.
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000450 Value& operator[](const CppTL::ConstString& key);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000451 /// Access an object value by name, returns null if there is no member with
452 /// that name.
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000453 const Value& operator[](const CppTL::ConstString& key) const;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000454#endif
455 /// Return the member named key if it exist, defaultValue otherwise.
Christopher Dunn0fd28752015-03-05 16:38:43 -0600456 /// \note deep copy
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000457 Value get(const char* key, const Value& defaultValue) const;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000458 /// Return the member named key if it exist, defaultValue otherwise.
Christopher Dunn0fd28752015-03-05 16:38:43 -0600459 /// \note deep copy
Christopher Dunn89704032015-07-11 12:09:59 -0500460 /// \note key may contain embedded nulls.
461 Value get(const char* begin, const char* end, const Value& defaultValue) const;
Christopher Dunn25342ba2015-03-02 18:05:26 -0600462 /// Return the member named key if it exist, defaultValue otherwise.
Christopher Dunn0fd28752015-03-05 16:38:43 -0600463 /// \note deep copy
Christopher Dunn25342ba2015-03-02 18:05:26 -0600464 /// \param key may contain embedded nulls.
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000465 Value get(const std::string& key, const Value& defaultValue) const;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000466#ifdef JSON_USE_CPPTL
467 /// Return the member named key if it exist, defaultValue otherwise.
Christopher Dunn0fd28752015-03-05 16:38:43 -0600468 /// \note deep copy
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000469 Value get(const CppTL::ConstString& key, const Value& defaultValue) const;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000470#endif
Christopher Dunn25342ba2015-03-02 18:05:26 -0600471 /// Most general and efficient version of isMember()const, get()const,
472 /// and operator[]const
Christopher Dunn89704032015-07-11 12:09:59 -0500473 /// \note As stated elsewhere, behavior is undefined if (end-begin) >= 2^30
474 Value const* find(char const* begin, char const* end) const;
Christopher Dunnc28610f2015-02-21 11:44:16 -0600475 /// Most general and efficient version of object-mutators.
Christopher Dunn89704032015-07-11 12:09:59 -0500476 /// \note As stated elsewhere, behavior is undefined if (end-begin) >= 2^30
Christopher Dunnc28610f2015-02-21 11:44:16 -0600477 /// \return non-zero, but JSON_ASSERT if this is neither object nor nullValue.
Christopher Dunn89704032015-07-11 12:09:59 -0500478 Value const* demand(char const* begin, char const* end);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000479 /// \brief Remove and return the named member.
480 ///
481 /// Do nothing if it did not exist.
482 /// \return the removed Value, or null.
483 /// \pre type() is objectValue or nullValue
484 /// \post type() is unchanged
Christopher Dunn76746b02015-01-21 16:01:30 -0600485 /// \deprecated
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000486 Value removeMember(const char* key);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000487 /// Same as removeMember(const char*)
Christopher Dunn25342ba2015-03-02 18:05:26 -0600488 /// \param key may contain embedded nulls.
Christopher Dunn76746b02015-01-21 16:01:30 -0600489 /// \deprecated
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000490 Value removeMember(const std::string& key);
Christopher Dunn89704032015-07-11 12:09:59 -0500491 /// Same as removeMember(const char* begin, const char* end, Value* removed),
Christopher Dunn25342ba2015-03-02 18:05:26 -0600492 /// but 'key' is null-terminated.
493 bool removeMember(const char* key, Value* removed);
Christopher Dunn76746b02015-01-21 16:01:30 -0600494 /** \brief Remove the named map member.
495
496 Update 'removed' iff removed.
Christopher Dunn25342ba2015-03-02 18:05:26 -0600497 \param key may contain embedded nulls.
Christopher Dunn76746b02015-01-21 16:01:30 -0600498 \return true iff removed (no exceptions)
499 */
Christopher Dunn25342ba2015-03-02 18:05:26 -0600500 bool removeMember(std::string const& key, Value* removed);
501 /// Same as removeMember(std::string const& key, Value* removed)
Christopher Dunn89704032015-07-11 12:09:59 -0500502 bool removeMember(const char* begin, const char* end, Value* removed);
Christopher Dunn9de2c2d2015-01-20 16:15:40 -0600503 /** \brief Remove the indexed array element.
504
505 O(n) expensive operations.
506 Update 'removed' iff removed.
Christopher Dunne87e41c2015-01-20 16:24:11 -0600507 \return true iff removed (no exceptions)
Christopher Dunn9de2c2d2015-01-20 16:15:40 -0600508 */
509 bool removeIndex(ArrayIndex i, Value* removed);
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000510
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000511 /// Return true if the object has a member named key.
Christopher Dunnc28610f2015-02-21 11:44:16 -0600512 /// \note 'key' must be null-terminated.
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000513 bool isMember(const char* key) const;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000514 /// Return true if the object has a member named key.
Christopher Dunn25342ba2015-03-02 18:05:26 -0600515 /// \param key may contain embedded nulls.
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000516 bool isMember(const std::string& key) const;
Christopher Dunn25342ba2015-03-02 18:05:26 -0600517 /// Same as isMember(std::string const& key)const
Christopher Dunn89704032015-07-11 12:09:59 -0500518 bool isMember(const char* begin, const char* end) const;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000519#ifdef JSON_USE_CPPTL
520 /// Return true if the object has a member named key.
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000521 bool isMember(const CppTL::ConstString& key) const;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000522#endif
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000523
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000524 /// \brief Return a list of the member names.
525 ///
526 /// If null, return an empty list.
527 /// \pre type() is objectValue or nullValue
528 /// \post if type() was nullValue, it remains nullValue
529 Members getMemberNames() const;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000530
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000531 //# ifdef JSON_USE_CPPTL
532 // EnumMemberNames enumMemberNames() const;
533 // EnumValues enumValues() const;
534 //# endif
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000535
Christopher Dunn1e3149a2015-01-25 14:16:13 -0600536 /// \deprecated Always pass len.
Christopher Dunn50069d72015-03-08 13:35:57 -0500537 JSONCPP_DEPRECATED("Use setComment(std::string const&) instead.")
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000538 void setComment(const char* comment, CommentPlacement placement);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000539 /// Comments must be //... or /* ... */
Christopher Dunn1e3149a2015-01-25 14:16:13 -0600540 void setComment(const char* comment, size_t len, CommentPlacement placement);
541 /// Comments must be //... or /* ... */
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000542 void setComment(const std::string& comment, CommentPlacement placement);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000543 bool hasComment(CommentPlacement placement) const;
544 /// Include delimiters and embedded newlines.
545 std::string getComment(CommentPlacement placement) const;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000546
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000547 std::string toStyledString() const;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000548
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000549 const_iterator begin() const;
550 const_iterator end() const;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000551
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000552 iterator begin();
553 iterator end();
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000554
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000555 // Accessors for the [start, limit) range of bytes within the JSON text from
556 // which this value was parsed, if any.
Christopher Dunnd4513fc2016-02-06 09:25:20 -0600557 void setOffsetStart(ptrdiff_t start);
558 void setOffsetLimit(ptrdiff_t limit);
559 ptrdiff_t getOffsetStart() const;
560 ptrdiff_t getOffsetLimit() const;
Aaron Jacobs68db6552014-04-23 23:41:12 +0000561
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000562private:
Billy Donahue8eb5d892014-11-10 01:35:42 -0500563 void initBasic(ValueType type, bool allocated = false);
564
Christopher Dunnc28610f2015-02-21 11:44:16 -0600565 Value& resolveReference(const char* key);
566 Value& resolveReference(const char* key, const char* end);
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000567
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000568 struct CommentInfo {
569 CommentInfo();
570 ~CommentInfo();
571
Christopher Dunn1e3149a2015-01-25 14:16:13 -0600572 void setComment(const char* text, size_t len);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000573
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000574 char* comment_;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000575 };
576
577 // struct MemberNamesTransform
578 //{
579 // typedef const char *result_type;
580 // const char *operator()( const CZString &name ) const
581 // {
582 // return name.c_str();
583 // }
584 //};
585
586 union ValueHolder {
587 LargestInt int_;
588 LargestUInt uint_;
589 double real_;
590 bool bool_;
Christopher Dunnc28610f2015-02-21 11:44:16 -0600591 char* string_; // actually ptr to unsigned, followed by str, unless !allocated_
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000592 ObjectValues* map_;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000593 } value_;
594 ValueType type_ : 8;
Christopher Dunn2bc61372015-01-24 13:42:37 -0600595 unsigned int allocated_ : 1; // Notes: if declared as bool, bitfield is useless.
Christopher Dunnc28610f2015-02-21 11:44:16 -0600596 // If not allocated_, string_ must be null-terminated.
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000597 CommentInfo* comments_;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000598
599 // [start, limit) byte offsets in the source JSON text from which this Value
600 // was extracted.
Christopher Dunnd4513fc2016-02-06 09:25:20 -0600601 ptrdiff_t start_;
602 ptrdiff_t limit_;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000603};
604
605/** \brief Experimental and untested: represents an element of the "path" to
606 * access a node.
607 */
608class JSON_API PathArgument {
609public:
610 friend class Path;
611
612 PathArgument();
613 PathArgument(ArrayIndex index);
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000614 PathArgument(const char* key);
615 PathArgument(const std::string& key);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000616
617private:
618 enum Kind {
619 kindNone = 0,
620 kindIndex,
621 kindKey
622 };
623 std::string key_;
624 ArrayIndex index_;
625 Kind kind_;
626};
627
628/** \brief Experimental and untested: represents a "path" to access a node.
629 *
630 * Syntax:
631 * - "." => root node
632 * - ".[n]" => elements at index 'n' of root node (an array value)
633 * - ".name" => member named 'name' of root node (an object value)
634 * - ".name1.name2.name3"
635 * - ".[0][1][2].name1[3]"
636 * - ".%" => member name is provided as parameter
637 * - ".[%]" => index is provied as parameter
638 */
639class JSON_API Path {
640public:
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000641 Path(const std::string& path,
642 const PathArgument& a1 = PathArgument(),
643 const PathArgument& a2 = PathArgument(),
644 const PathArgument& a3 = PathArgument(),
645 const PathArgument& a4 = PathArgument(),
646 const PathArgument& a5 = PathArgument());
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000647
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000648 const Value& resolve(const Value& root) const;
649 Value resolve(const Value& root, const Value& defaultValue) const;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000650 /// Creates the "path" to access the specified node and returns a reference on
651 /// the node.
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000652 Value& make(Value& root) const;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000653
654private:
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000655 typedef std::vector<const PathArgument*> InArgs;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000656 typedef std::vector<PathArgument> Args;
657
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000658 void makePath(const std::string& path, const InArgs& in);
659 void addPathInArg(const std::string& path,
660 const InArgs& in,
661 InArgs::const_iterator& itInArg,
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000662 PathArgument::Kind kind);
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000663 void invalidPath(const std::string& path, int location);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000664
665 Args args_;
666};
667
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000668/** \brief base class for Value iterators.
669 *
670 */
671class JSON_API ValueIteratorBase {
672public:
673 typedef std::bidirectional_iterator_tag iterator_category;
674 typedef unsigned int size_t;
675 typedef int difference_type;
676 typedef ValueIteratorBase SelfType;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000677
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000678 bool operator==(const SelfType& other) const { return isEqual(other); }
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000679
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000680 bool operator!=(const SelfType& other) const { return !isEqual(other); }
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000681
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000682 difference_type operator-(const SelfType& other) const {
Kevin Grant4c5832a2015-02-14 20:53:35 -0800683 return other.computeDistance(*this);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000684 }
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000685
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000686 /// Return either the index or the member name of the referenced value as a
687 /// Value.
688 Value key() const;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000689
Christopher Dunned495ed2015-03-08 14:01:28 -0500690 /// Return the index of the referenced Value, or -1 if it is not an arrayValue.
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000691 UInt index() const;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000692
Christopher Dunned495ed2015-03-08 14:01:28 -0500693 /// Return the member name of the referenced Value, or "" if it is not an
694 /// objectValue.
695 /// \note Avoid `c_str()` on result, as embedded zeroes are possible.
696 std::string name() const;
697
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000698 /// Return the member name of the referenced Value. "" if it is not an
699 /// objectValue.
Christopher Dunnc28610f2015-02-21 11:44:16 -0600700 /// \deprecated This cannot be used for UTF-8 strings, since there can be embedded nulls.
Christopher Dunned495ed2015-03-08 14:01:28 -0500701 JSONCPP_DEPRECATED("Use `key = name();` instead.")
Christopher Dunnc28610f2015-02-21 11:44:16 -0600702 char const* memberName() const;
703 /// Return the member name of the referenced Value, or NULL if it is not an
704 /// objectValue.
Christopher Dunned495ed2015-03-08 14:01:28 -0500705 /// \note Better version than memberName(). Allows embedded nulls.
Christopher Dunnc28610f2015-02-21 11:44:16 -0600706 char const* memberName(char const** end) const;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000707
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000708protected:
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000709 Value& deref() const;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000710
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000711 void increment();
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000712
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000713 void decrement();
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000714
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000715 difference_type computeDistance(const SelfType& other) const;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000716
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000717 bool isEqual(const SelfType& other) const;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000718
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000719 void copy(const SelfType& other);
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000720
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000721private:
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000722 Value::ObjectValues::iterator current_;
723 // Indicates that iterator is for a null value.
724 bool isNull_;
Christopher Dunn2a10f4a2015-04-28 04:55:12 +0100725
726public:
727 // For some reason, BORLAND needs these at the end, rather
728 // than earlier. No idea why.
729 ValueIteratorBase();
730 explicit ValueIteratorBase(const Value::ObjectValues::iterator& current);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000731};
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000732
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000733/** \brief const iterator for object and array value.
734 *
735 */
736class JSON_API ValueConstIterator : public ValueIteratorBase {
737 friend class Value;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000738
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000739public:
740 typedef const Value value_type;
Christopher Dunnc28610f2015-02-21 11:44:16 -0600741 //typedef unsigned int size_t;
742 //typedef int difference_type;
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000743 typedef const Value& reference;
744 typedef const Value* pointer;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000745 typedef ValueConstIterator SelfType;
746
747 ValueConstIterator();
ycqiuc8a8cfc2015-10-06 16:46:19 +0800748 ValueConstIterator(ValueIterator const& other);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000749
750private:
751/*! \internal Use by Value to create an iterator.
752 */
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000753 explicit ValueConstIterator(const Value::ObjectValues::iterator& current);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000754public:
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000755 SelfType& operator=(const ValueIteratorBase& other);
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000756
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000757 SelfType operator++(int) {
758 SelfType temp(*this);
759 ++*this;
760 return temp;
761 }
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000762
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000763 SelfType operator--(int) {
764 SelfType temp(*this);
765 --*this;
766 return temp;
767 }
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000768
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000769 SelfType& operator--() {
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000770 decrement();
771 return *this;
772 }
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000773
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000774 SelfType& operator++() {
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000775 increment();
776 return *this;
777 }
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000778
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000779 reference operator*() const { return deref(); }
Braden McDorman540db3b2014-09-14 02:31:23 -0500780
781 pointer operator->() const { return &deref(); }
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000782};
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000783
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000784/** \brief Iterator for object and array value.
785 */
786class JSON_API ValueIterator : public ValueIteratorBase {
787 friend class Value;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000788
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000789public:
790 typedef Value value_type;
791 typedef unsigned int size_t;
792 typedef int difference_type;
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000793 typedef Value& reference;
794 typedef Value* pointer;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000795 typedef ValueIterator SelfType;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000796
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000797 ValueIterator();
ycqiuc8a8cfc2015-10-06 16:46:19 +0800798 explicit ValueIterator(const ValueConstIterator& other);
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000799 ValueIterator(const ValueIterator& other);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000800
801private:
802/*! \internal Use by Value to create an iterator.
803 */
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000804 explicit ValueIterator(const Value::ObjectValues::iterator& current);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000805public:
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000806 SelfType& operator=(const SelfType& other);
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000807
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000808 SelfType operator++(int) {
809 SelfType temp(*this);
810 ++*this;
811 return temp;
812 }
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000813
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000814 SelfType operator--(int) {
815 SelfType temp(*this);
816 --*this;
817 return temp;
818 }
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000819
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000820 SelfType& operator--() {
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000821 decrement();
822 return *this;
823 }
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000824
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000825 SelfType& operator++() {
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000826 increment();
827 return *this;
828 }
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000829
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000830 reference operator*() const { return deref(); }
Braden McDorman540db3b2014-09-14 02:31:23 -0500831
832 pointer operator->() const { return &deref(); }
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000833};
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000834
835} // namespace Json
836
datadiode9454e682015-01-20 15:25:04 -0600837
838namespace std {
839/// Specialize std::swap() for Json::Value.
840template<>
841inline void swap(Json::Value& a, Json::Value& b) { a.swap(b); }
842}
843
844
Baptiste Lepilleureafd7022013-05-08 20:21:11 +0000845#if defined(JSONCPP_DISABLE_DLL_INTERFACE_WARNING)
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000846#pragma warning(pop)
Baptiste Lepilleureafd7022013-05-08 20:21:11 +0000847#endif // if defined(JSONCPP_DISABLE_DLL_INTERFACE_WARNING)
848
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000849#endif // CPPTL_JSON_H_INCLUDED