blob: 5948935c1d5026204284511f483fd5fab0e827eb [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
Gauravd97ea5b2016-03-16 11:15:09 +053025//Conditional NORETURN attribute on the throw functions would:
26// a) suppress false positives from static code analysis
27// b) possibly improve optimization opportunities.
28#if !defined(JSONCPP_NORETURN)
29# if defined(_MSC_VER)
30# define JSONCPP_NORETURN __declspec(noreturn)
31# elif defined(__GNUC__)
32# define JSONCPP_NORETURN __attribute__ ((__noreturn__))
33# else
34# define JSONCPP_NORETURN
35# endif
36#endif
37
Aaron Jacobs9fa4e842014-07-01 08:48:54 +100038// Disable warning C4251: <data member>: <type> needs to have dll-interface to
39// be used by...
Baptiste Lepilleureafd7022013-05-08 20:21:11 +000040#if defined(JSONCPP_DISABLE_DLL_INTERFACE_WARNING)
Aaron Jacobs9fa4e842014-07-01 08:48:54 +100041#pragma warning(push)
42#pragma warning(disable : 4251)
Baptiste Lepilleureafd7022013-05-08 20:21:11 +000043#endif // if defined(JSONCPP_DISABLE_DLL_INTERFACE_WARNING)
44
Christopher Dunn6d135cb2007-06-13 15:51:04 +000045/** \brief JSON (JavaScript Object Notation).
46 */
47namespace Json {
48
Christopher Dunn75279cc2015-03-08 12:20:06 -050049/** Base class for all exceptions we throw.
Christopher Dunn4e30c4f2015-03-08 12:56:32 -050050 *
51 * We use nothing but these internally. Of course, STL can throw others.
Christopher Dunn75279cc2015-03-08 12:20:06 -050052 */
Christopher Dunn949babd2015-07-23 00:19:12 -050053class JSON_API Exception : public std::exception {
54public:
Christopher Dunnde5b7922016-03-06 11:19:46 -060055 Exception(JSONCPP_STRING const& msg);
Gauravaadd0b12015-09-04 14:04:43 +053056 ~Exception() throw() override;
57 char const* what() const throw() override;
Christopher Dunn949babd2015-07-23 00:19:12 -050058protected:
Christopher Dunnde5b7922016-03-06 11:19:46 -060059 JSONCPP_STRING msg_;
Christopher Dunn949babd2015-07-23 00:19:12 -050060};
61
Christopher Dunn53837942015-03-08 12:31:00 -050062/** Exceptions which the user cannot easily avoid.
63 *
Christopher Dunn4e30c4f2015-03-08 12:56:32 -050064 * E.g. out-of-memory (when we use malloc), stack-overflow, malicious input
65 *
66 * \remark derived from Json::Exception
Christopher Dunn53837942015-03-08 12:31:00 -050067 */
Christopher Dunn949babd2015-07-23 00:19:12 -050068class JSON_API RuntimeError : public Exception {
69public:
Christopher Dunnde5b7922016-03-06 11:19:46 -060070 RuntimeError(JSONCPP_STRING const& msg);
Christopher Dunn949babd2015-07-23 00:19:12 -050071};
72
Christopher Dunn4e30c4f2015-03-08 12:56:32 -050073/** Exceptions thrown by JSON_ASSERT/JSON_FAIL macros.
Christopher Dunn53837942015-03-08 12:31:00 -050074 *
75 * These are precondition-violations (user bugs) and internal errors (our bugs).
Christopher Dunn4e30c4f2015-03-08 12:56:32 -050076 *
77 * \remark derived from Json::Exception
Christopher Dunn53837942015-03-08 12:31:00 -050078 */
Christopher Dunn949babd2015-07-23 00:19:12 -050079class JSON_API LogicError : public Exception {
80public:
Christopher Dunnde5b7922016-03-06 11:19:46 -060081 LogicError(JSONCPP_STRING const& msg);
Christopher Dunn949babd2015-07-23 00:19:12 -050082};
Christopher Dunn53837942015-03-08 12:31:00 -050083
Christopher Dunn4e30c4f2015-03-08 12:56:32 -050084/// used internally
Gauravd97ea5b2016-03-16 11:15:09 +053085JSONCPP_NORETURN void throwRuntimeError(JSONCPP_STRING const& msg);
Christopher Dunn4e30c4f2015-03-08 12:56:32 -050086/// used internally
Gauravd97ea5b2016-03-16 11:15:09 +053087JSONCPP_NORETURN void throwLogicError(JSONCPP_STRING const& msg);
Christopher Dunn75279cc2015-03-08 12:20:06 -050088
Aaron Jacobs9fa4e842014-07-01 08:48:54 +100089/** \brief Type of the value held by a Value object.
90 */
91enum ValueType {
92 nullValue = 0, ///< 'null' value
93 intValue, ///< signed integer value
94 uintValue, ///< unsigned integer value
95 realValue, ///< double value
96 stringValue, ///< UTF-8 string value
97 booleanValue, ///< bool value
98 arrayValue, ///< array value (ordered list)
99 objectValue ///< object value (collection of name/value pairs).
100};
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000101
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000102enum CommentPlacement {
103 commentBefore = 0, ///< a comment placed on the line before a value
104 commentAfterOnSameLine, ///< a comment just after a value on the same line
105 commentAfter, ///< a comment on the line after a value (only make sense for
Aaron Jacobs3a0c4fc2014-07-01 09:20:48 +1000106 /// root value)
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000107 numberOfCommentPlacement
108};
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000109
110//# ifdef JSON_USE_CPPTL
111// typedef CppTL::AnyEnumerator<const char *> EnumMemberNames;
112// typedef CppTL::AnyEnumerator<const Value &> EnumValues;
113//# endif
114
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000115/** \brief Lightweight wrapper to tag static string.
116 *
117 * Value constructor and objectValue member assignement takes advantage of the
118 * StaticString and avoid the cost of string duplication when storing the
119 * string or the member name.
120 *
121 * Example of usage:
122 * \code
123 * Json::Value aValue( StaticString("some text") );
124 * Json::Value object;
125 * static const StaticString code("code");
126 * object[code] = 1234;
127 * \endcode
128 */
129class JSON_API StaticString {
130public:
Christopher Dunnff617522015-03-06 10:31:46 -0600131 explicit StaticString(const char* czstring) : c_str_(czstring) {}
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000132
Christopher Dunnff617522015-03-06 10:31:46 -0600133 operator const char*() const { return c_str_; }
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000134
Christopher Dunnff617522015-03-06 10:31:46 -0600135 const char* c_str() const { return c_str_; }
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000136
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000137private:
Christopher Dunnff617522015-03-06 10:31:46 -0600138 const char* c_str_;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000139};
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000140
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000141/** \brief Represents a <a HREF="http://www.json.org">JSON</a> value.
142 *
143 * This class is a discriminated union wrapper that can represents a:
144 * - signed integer [range: Value::minInt - Value::maxInt]
145 * - unsigned integer (range: 0 - Value::maxUInt)
146 * - double
147 * - UTF-8 string
148 * - boolean
149 * - 'null'
150 * - an ordered list of Value
151 * - collection of name/value pairs (javascript object)
152 *
153 * The type of the held value is represented by a #ValueType and
154 * can be obtained using type().
155 *
Christopher Dunnc28610f2015-02-21 11:44:16 -0600156 * Values of an #objectValue or #arrayValue can be accessed using operator[]()
157 * methods.
158 * Non-const methods will automatically create the a #nullValue element
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000159 * if it does not exist.
Christopher Dunnc28610f2015-02-21 11:44:16 -0600160 * The sequence of an #arrayValue will be automatically resized and initialized
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000161 * with #nullValue. resize() can be used to enlarge or truncate an #arrayValue.
162 *
Christopher Dunnc28610f2015-02-21 11:44:16 -0600163 * The get() methods can be used to obtain default value in the case the
164 * required element does not exist.
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000165 *
166 * It is possible to iterate over the list of a #objectValue values using
167 * the getMemberNames() method.
Christopher Dunn25342ba2015-03-02 18:05:26 -0600168 *
Christopher Dunnc28610f2015-02-21 11:44:16 -0600169 * \note #Value string-length fit in size_t, but keys must be < 2^30.
170 * (The reason is an implementation detail.) A #CharReader will raise an
Christopher Dunn25342ba2015-03-02 18:05:26 -0600171 * exception if a bound is exceeded to avoid security holes in your app,
172 * but the Value API does *not* check bounds. That is the responsibility
173 * of the caller.
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000174 */
175class JSON_API Value {
176 friend class ValueIteratorBase;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000177public:
Christopher Dunnde5b7922016-03-06 11:19:46 -0600178 typedef std::vector<JSONCPP_STRING> Members;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000179 typedef ValueIterator iterator;
180 typedef ValueConstIterator const_iterator;
181 typedef Json::UInt UInt;
182 typedef Json::Int Int;
183#if defined(JSON_HAS_INT64)
184 typedef Json::UInt64 UInt64;
185 typedef Json::Int64 Int64;
Baptiste Lepilleur842e9ac2010-12-27 17:45:23 +0000186#endif // defined(JSON_HAS_INT64)
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000187 typedef Json::LargestInt LargestInt;
188 typedef Json::LargestUInt LargestUInt;
189 typedef Json::ArrayIndex ArrayIndex;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000190
Christopher Dunn8a702972015-03-03 10:38:27 -0600191 static const Value& null; ///< We regret this reference to a global instance; prefer the simpler Value().
192 static const Value& nullRef; ///< just a kludge for binary-compatibility; same as null
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000193 /// Minimum signed integer value that can be stored in a Json::Value.
194 static const LargestInt minLargestInt;
195 /// Maximum signed integer value that can be stored in a Json::Value.
196 static const LargestInt maxLargestInt;
197 /// Maximum unsigned integer value that can be stored in a Json::Value.
198 static const LargestUInt maxLargestUInt;
Baptiste Lepilleur842e9ac2010-12-27 17:45:23 +0000199
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000200 /// Minimum signed int value that can be stored in a Json::Value.
201 static const Int minInt;
202 /// Maximum signed int value that can be stored in a Json::Value.
203 static const Int maxInt;
204 /// Maximum unsigned int value that can be stored in a Json::Value.
205 static const UInt maxUInt;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000206
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000207#if defined(JSON_HAS_INT64)
208 /// Minimum signed 64 bits int value that can be stored in a Json::Value.
209 static const Int64 minInt64;
210 /// Maximum signed 64 bits int value that can be stored in a Json::Value.
211 static const Int64 maxInt64;
212 /// Maximum unsigned 64 bits int value that can be stored in a Json::Value.
213 static const UInt64 maxUInt64;
Aaron Jacobsf1053e72011-05-24 03:18:02 +0000214#endif // defined(JSON_HAS_INT64)
Baptiste Lepilleur842e9ac2010-12-27 17:45:23 +0000215
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000216private:
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000217#ifndef JSONCPP_DOC_EXCLUDE_IMPLEMENTATION
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000218 class CZString {
219 public:
220 enum DuplicationPolicy {
221 noDuplication = 0,
222 duplicate,
223 duplicateOnCopy
224 };
225 CZString(ArrayIndex index);
Christopher Dunnc28610f2015-02-21 11:44:16 -0600226 CZString(char const* str, unsigned length, DuplicationPolicy allocate);
227 CZString(CZString const& other);
Motti2b008912015-04-20 17:44:47 +0300228#if JSON_HAS_RVALUE_REFERENCES
229 CZString(CZString&& other);
230#endif
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000231 ~CZString();
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000232 CZString& operator=(CZString other);
Christopher Dunnc28610f2015-02-21 11:44:16 -0600233 bool operator<(CZString const& other) const;
234 bool operator==(CZString const& other) const;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000235 ArrayIndex index() const;
Christopher Dunnc28610f2015-02-21 11:44:16 -0600236 //const char* c_str() const; ///< \deprecated
237 char const* data() const;
238 unsigned length() const;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000239 bool isStaticString() const;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000240
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000241 private:
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000242 void swap(CZString& other);
Christopher Dunnc28610f2015-02-21 11:44:16 -0600243
Christopher Dunn57ad0512015-03-02 12:10:35 -0600244 struct StringStorage {
Dan Liufcbab022015-04-27 05:20:05 +0100245 unsigned policy_: 2;
Christopher Dunn57ad0512015-03-02 12:10:35 -0600246 unsigned length_: 30; // 1GB max
247 };
248
Christopher Dunnc28610f2015-02-21 11:44:16 -0600249 char const* cstr_; // actually, a prefixed string, unless policy is noDup
Christopher Dunn57ad0512015-03-02 12:10:35 -0600250 union {
251 ArrayIndex index_;
252 StringStorage storage_;
253 };
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000254 };
255
256public:
257#ifndef JSON_USE_CPPTL_SMALLMAP
258 typedef std::map<CZString, Value> ObjectValues;
259#else
260 typedef CppTL::SmallMap<CZString, Value> ObjectValues;
261#endif // ifndef JSON_USE_CPPTL_SMALLMAP
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000262#endif // ifndef JSONCPP_DOC_EXCLUDE_IMPLEMENTATION
263
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000264public:
265 /** \brief Create a default Value of the given type.
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000266
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000267 This is a very useful constructor.
268 To create an empty array, pass arrayValue.
269 To create an empty object, pass objectValue.
270 Another Value can then be set to this one by assignment.
271This is useful since clear() and resize() will not alter types.
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000272
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000273 Examples:
274\code
275Json::Value null_value; // null
276Json::Value arr_value(Json::arrayValue); // []
277Json::Value obj_value(Json::objectValue); // {}
278\endcode
279 */
280 Value(ValueType type = nullValue);
281 Value(Int value);
282 Value(UInt value);
Baptiste Lepilleur842e9ac2010-12-27 17:45:23 +0000283#if defined(JSON_HAS_INT64)
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000284 Value(Int64 value);
285 Value(UInt64 value);
Baptiste Lepilleur842e9ac2010-12-27 17:45:23 +0000286#endif // if defined(JSON_HAS_INT64)
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000287 Value(double value);
Christopher Dunn8a702972015-03-03 10:38:27 -0600288 Value(const char* value); ///< Copy til first 0. (NULL causes to seg-fault.)
Christopher Dunn89704032015-07-11 12:09:59 -0500289 Value(const char* begin, const char* end); ///< Copy all, incl zeroes.
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000290 /** \brief Constructs a value from a static string.
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000291
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000292 * Like other value string constructor but do not duplicate the string for
293 * internal storage. The given string must remain alive after the call to this
294 * constructor.
Christopher Dunnc28610f2015-02-21 11:44:16 -0600295 * \note This works only for null-terminated strings. (We cannot change the
296 * size of this class, so we have nowhere to store the length,
297 * which might be computed later for various operations.)
298 *
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000299 * Example of usage:
300 * \code
Christopher Dunnc28610f2015-02-21 11:44:16 -0600301 * static StaticString foo("some text");
302 * Json::Value aValue(foo);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000303 * \endcode
304 */
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000305 Value(const StaticString& value);
Christopher Dunnde5b7922016-03-06 11:19:46 -0600306 Value(const JSONCPP_STRING& value); ///< Copy data() til size(). Embedded zeroes too.
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000307#ifdef JSON_USE_CPPTL
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000308 Value(const CppTL::ConstString& value);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000309#endif
310 Value(bool value);
Christopher Dunn66eb72f2015-01-20 11:02:22 -0600311 /// Deep copy.
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000312 Value(const Value& other);
Motti2b008912015-04-20 17:44:47 +0300313#if JSON_HAS_RVALUE_REFERENCES
314 /// Move constructor
315 Value(Value&& other);
316#endif
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000317 ~Value();
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000318
Christopher Dunn7f439f42015-03-06 09:22:57 -0600319 /// Deep copy, then swap(other).
320 /// \note Over-write existing comments. To preserve comments, use #swapPayload().
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000321 Value& operator=(Value other);
Christopher Dunn66eb72f2015-01-20 11:02:22 -0600322 /// Swap everything.
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000323 void swap(Value& other);
Christopher Dunn66eb72f2015-01-20 11:02:22 -0600324 /// Swap values but leave comments and source offsets in place.
325 void swapPayload(Value& other);
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000326
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000327 ValueType type() const;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000328
Christopher Dunn66eb72f2015-01-20 11:02:22 -0600329 /// Compare payload only, not comments etc.
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000330 bool operator<(const Value& other) const;
331 bool operator<=(const Value& other) const;
332 bool operator>=(const Value& other) const;
333 bool operator>(const Value& other) const;
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000334 bool operator==(const Value& other) const;
335 bool operator!=(const Value& other) const;
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000336 int compare(const Value& other) const;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000337
Christopher Dunn8a702972015-03-03 10:38:27 -0600338 const char* asCString() const; ///< Embedded zeroes could cause you trouble!
Christopher Dunnde5b7922016-03-06 11:19:46 -0600339 JSONCPP_STRING asString() const; ///< Embedded zeroes are possible.
Christopher Dunnc28610f2015-02-21 11:44:16 -0600340 /** Get raw char* of string-value.
341 * \return false if !string. (Seg-fault if str or end are NULL.)
342 */
343 bool getString(
Christopher Dunn89704032015-07-11 12:09:59 -0500344 char const** begin, char const** end) const;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000345#ifdef JSON_USE_CPPTL
346 CppTL::ConstString asConstString() const;
347#endif
348 Int asInt() const;
349 UInt asUInt() const;
Aaron Jacobsf1053e72011-05-24 03:18:02 +0000350#if defined(JSON_HAS_INT64)
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000351 Int64 asInt64() const;
352 UInt64 asUInt64() const;
Aaron Jacobsf1053e72011-05-24 03:18:02 +0000353#endif // if defined(JSON_HAS_INT64)
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000354 LargestInt asLargestInt() const;
355 LargestUInt asLargestUInt() const;
356 float asFloat() const;
357 double asDouble() const;
358 bool asBool() const;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000359
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000360 bool isNull() const;
361 bool isBool() const;
362 bool isInt() const;
363 bool isInt64() const;
364 bool isUInt() const;
365 bool isUInt64() const;
366 bool isIntegral() const;
367 bool isDouble() const;
368 bool isNumeric() const;
369 bool isString() const;
370 bool isArray() const;
371 bool isObject() const;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000372
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000373 bool isConvertibleTo(ValueType other) const;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000374
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000375 /// Number of values in array or object
376 ArrayIndex size() const;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000377
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000378 /// \brief Return true if empty array, empty object, or null;
379 /// otherwise, false.
380 bool empty() const;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000381
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000382 /// Return isNull()
383 bool operator!() const;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000384
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000385 /// Remove all object members and array elements.
386 /// \pre type() is arrayValue, objectValue, or nullValue
387 /// \post type() is unchanged
388 void clear();
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000389
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000390 /// Resize the array to size elements.
391 /// New elements are initialized to null.
392 /// May only be called on nullValue or arrayValue.
393 /// \pre type() is arrayValue or nullValue
394 /// \post type() is arrayValue
395 void resize(ArrayIndex size);
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000396
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000397 /// Access an array element (zero based index ).
398 /// If the array contains less than index element, then null value are
399 /// inserted
400 /// in the array so that its size is index+1.
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 Value& operator[](ArrayIndex index);
Baptiste Lepilleurfa130ef2010-12-24 12:47:14 +0000404
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000405 /// Access an array element (zero based index ).
406 /// If the array contains less than index element, then null value are
407 /// inserted
408 /// in the array so that its size is index+1.
409 /// (You may need to say 'value[0u]' to get your compiler to distinguish
410 /// this from the operator[] which takes a string.)
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000411 Value& operator[](int index);
Baptiste Lepilleurfa130ef2010-12-24 12:47:14 +0000412
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000413 /// Access an array element (zero based index )
414 /// (You may need to say 'value[0u]' to get your compiler to distinguish
415 /// this from the operator[] which takes a string.)
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000416 const Value& operator[](ArrayIndex index) const;
Baptiste Lepilleurfa130ef2010-12-24 12:47:14 +0000417
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000418 /// Access an array element (zero based index )
419 /// (You may need to say 'value[0u]' to get your compiler to distinguish
420 /// this from the operator[] which takes a string.)
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000421 const Value& operator[](int index) const;
Baptiste Lepilleurfa130ef2010-12-24 12:47:14 +0000422
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000423 /// If the array contains at least index+1 elements, returns the element
424 /// value,
425 /// otherwise returns defaultValue.
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000426 Value get(ArrayIndex index, const Value& defaultValue) const;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000427 /// Return true if index < size().
428 bool isValidIndex(ArrayIndex index) const;
429 /// \brief Append value to array at the end.
430 ///
431 /// Equivalent to jsonvalue[jsonvalue.size()] = value;
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000432 Value& append(const Value& value);
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000433
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000434 /// Access an object value by name, create a null member if it does not exist.
Christopher Dunn25342ba2015-03-02 18:05:26 -0600435 /// \note Because of our implementation, keys are limited to 2^30 -1 chars.
436 /// Exceeding that will cause an exception.
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000437 Value& operator[](const char* key);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000438 /// Access an object value by name, returns null if there is no member with
439 /// that name.
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000440 const Value& operator[](const char* key) const;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000441 /// Access an object value by name, create a null member if it does not exist.
Christopher Dunn25342ba2015-03-02 18:05:26 -0600442 /// \param key may contain embedded nulls.
Christopher Dunnde5b7922016-03-06 11:19:46 -0600443 Value& operator[](const JSONCPP_STRING& 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.
Christopher Dunn25342ba2015-03-02 18:05:26 -0600446 /// \param key may contain embedded nulls.
Christopher Dunnde5b7922016-03-06 11:19:46 -0600447 const Value& operator[](const JSONCPP_STRING& key) const;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000448 /** \brief Access an object value by name, create a null member if it does not
449 exist.
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000450
Christopher Dunn25342ba2015-03-02 18:05:26 -0600451 * If the object has no entry for that name, then the member name used to store
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000452 * the new entry is not duplicated.
453 * Example of use:
454 * \code
455 * Json::Value object;
456 * static const StaticString code("code");
457 * object[code] = 1234;
458 * \endcode
459 */
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000460 Value& operator[](const StaticString& key);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000461#ifdef JSON_USE_CPPTL
462 /// Access an object value by name, create a null member if it does not exist.
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000463 Value& operator[](const CppTL::ConstString& key);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000464 /// Access an object value by name, returns null if there is no member with
465 /// that name.
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000466 const Value& operator[](const CppTL::ConstString& key) const;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000467#endif
468 /// Return the member named key if it exist, defaultValue otherwise.
Christopher Dunn0fd28752015-03-05 16:38:43 -0600469 /// \note deep copy
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000470 Value get(const char* key, const Value& defaultValue) const;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000471 /// Return the member named key if it exist, defaultValue otherwise.
Christopher Dunn0fd28752015-03-05 16:38:43 -0600472 /// \note deep copy
Christopher Dunn89704032015-07-11 12:09:59 -0500473 /// \note key may contain embedded nulls.
474 Value get(const char* begin, const char* end, const Value& defaultValue) const;
Christopher Dunn25342ba2015-03-02 18:05:26 -0600475 /// Return the member named key if it exist, defaultValue otherwise.
Christopher Dunn0fd28752015-03-05 16:38:43 -0600476 /// \note deep copy
Christopher Dunn25342ba2015-03-02 18:05:26 -0600477 /// \param key may contain embedded nulls.
Christopher Dunnde5b7922016-03-06 11:19:46 -0600478 Value get(const JSONCPP_STRING& key, const Value& defaultValue) const;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000479#ifdef JSON_USE_CPPTL
480 /// Return the member named key if it exist, defaultValue otherwise.
Christopher Dunn0fd28752015-03-05 16:38:43 -0600481 /// \note deep copy
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000482 Value get(const CppTL::ConstString& key, const Value& defaultValue) const;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000483#endif
Christopher Dunn25342ba2015-03-02 18:05:26 -0600484 /// Most general and efficient version of isMember()const, get()const,
485 /// and operator[]const
Christopher Dunn89704032015-07-11 12:09:59 -0500486 /// \note As stated elsewhere, behavior is undefined if (end-begin) >= 2^30
487 Value const* find(char const* begin, char const* end) const;
Christopher Dunnc28610f2015-02-21 11:44:16 -0600488 /// Most general and efficient version of object-mutators.
Christopher Dunn89704032015-07-11 12:09:59 -0500489 /// \note As stated elsewhere, behavior is undefined if (end-begin) >= 2^30
Christopher Dunnc28610f2015-02-21 11:44:16 -0600490 /// \return non-zero, but JSON_ASSERT if this is neither object nor nullValue.
Christopher Dunn89704032015-07-11 12:09:59 -0500491 Value const* demand(char const* begin, char const* end);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000492 /// \brief Remove and return the named member.
493 ///
494 /// Do nothing if it did not exist.
495 /// \return the removed Value, or null.
496 /// \pre type() is objectValue or nullValue
497 /// \post type() is unchanged
Christopher Dunn76746b02015-01-21 16:01:30 -0600498 /// \deprecated
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000499 Value removeMember(const char* key);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000500 /// Same as removeMember(const char*)
Christopher Dunn25342ba2015-03-02 18:05:26 -0600501 /// \param key may contain embedded nulls.
Christopher Dunn76746b02015-01-21 16:01:30 -0600502 /// \deprecated
Christopher Dunnde5b7922016-03-06 11:19:46 -0600503 Value removeMember(const JSONCPP_STRING& key);
Christopher Dunn89704032015-07-11 12:09:59 -0500504 /// Same as removeMember(const char* begin, const char* end, Value* removed),
Christopher Dunn25342ba2015-03-02 18:05:26 -0600505 /// but 'key' is null-terminated.
506 bool removeMember(const char* key, Value* removed);
Christopher Dunn76746b02015-01-21 16:01:30 -0600507 /** \brief Remove the named map member.
508
509 Update 'removed' iff removed.
Christopher Dunn25342ba2015-03-02 18:05:26 -0600510 \param key may contain embedded nulls.
Christopher Dunn76746b02015-01-21 16:01:30 -0600511 \return true iff removed (no exceptions)
512 */
Christopher Dunnde5b7922016-03-06 11:19:46 -0600513 bool removeMember(JSONCPP_STRING const& key, Value* removed);
514 /// Same as removeMember(JSONCPP_STRING const& key, Value* removed)
Christopher Dunn89704032015-07-11 12:09:59 -0500515 bool removeMember(const char* begin, const char* end, Value* removed);
Christopher Dunn9de2c2d2015-01-20 16:15:40 -0600516 /** \brief Remove the indexed array element.
517
518 O(n) expensive operations.
519 Update 'removed' iff removed.
Christopher Dunne87e41c2015-01-20 16:24:11 -0600520 \return true iff removed (no exceptions)
Christopher Dunn9de2c2d2015-01-20 16:15:40 -0600521 */
522 bool removeIndex(ArrayIndex i, Value* removed);
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000523
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000524 /// Return true if the object has a member named key.
Christopher Dunnc28610f2015-02-21 11:44:16 -0600525 /// \note 'key' must be null-terminated.
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000526 bool isMember(const char* key) const;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000527 /// Return true if the object has a member named key.
Christopher Dunn25342ba2015-03-02 18:05:26 -0600528 /// \param key may contain embedded nulls.
Christopher Dunnde5b7922016-03-06 11:19:46 -0600529 bool isMember(const JSONCPP_STRING& key) const;
530 /// Same as isMember(JSONCPP_STRING const& key)const
Christopher Dunn89704032015-07-11 12:09:59 -0500531 bool isMember(const char* begin, const char* end) const;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000532#ifdef JSON_USE_CPPTL
533 /// Return true if the object has a member named key.
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000534 bool isMember(const CppTL::ConstString& key) const;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000535#endif
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000536
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000537 /// \brief Return a list of the member names.
538 ///
539 /// If null, return an empty list.
540 /// \pre type() is objectValue or nullValue
541 /// \post if type() was nullValue, it remains nullValue
542 Members getMemberNames() const;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000543
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000544 //# ifdef JSON_USE_CPPTL
545 // EnumMemberNames enumMemberNames() const;
546 // EnumValues enumValues() const;
547 //# endif
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000548
Christopher Dunn1e3149a2015-01-25 14:16:13 -0600549 /// \deprecated Always pass len.
Christopher Dunnde5b7922016-03-06 11:19:46 -0600550 JSONCPP_DEPRECATED("Use setComment(JSONCPP_STRING const&) instead.")
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000551 void setComment(const char* comment, CommentPlacement placement);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000552 /// Comments must be //... or /* ... */
Christopher Dunn1e3149a2015-01-25 14:16:13 -0600553 void setComment(const char* comment, size_t len, CommentPlacement placement);
554 /// Comments must be //... or /* ... */
Christopher Dunnde5b7922016-03-06 11:19:46 -0600555 void setComment(const JSONCPP_STRING& comment, CommentPlacement placement);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000556 bool hasComment(CommentPlacement placement) const;
557 /// Include delimiters and embedded newlines.
Christopher Dunnde5b7922016-03-06 11:19:46 -0600558 JSONCPP_STRING getComment(CommentPlacement placement) const;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000559
Christopher Dunnde5b7922016-03-06 11:19:46 -0600560 JSONCPP_STRING toStyledString() const;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000561
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000562 const_iterator begin() const;
563 const_iterator end() const;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000564
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000565 iterator begin();
566 iterator end();
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000567
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000568 // Accessors for the [start, limit) range of bytes within the JSON text from
569 // which this value was parsed, if any.
Christopher Dunnd4513fc2016-02-06 09:25:20 -0600570 void setOffsetStart(ptrdiff_t start);
571 void setOffsetLimit(ptrdiff_t limit);
572 ptrdiff_t getOffsetStart() const;
573 ptrdiff_t getOffsetLimit() const;
Aaron Jacobs68db6552014-04-23 23:41:12 +0000574
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000575private:
Billy Donahue8eb5d892014-11-10 01:35:42 -0500576 void initBasic(ValueType type, bool allocated = false);
577
Christopher Dunnc28610f2015-02-21 11:44:16 -0600578 Value& resolveReference(const char* key);
579 Value& resolveReference(const char* key, const char* end);
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000580
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000581 struct CommentInfo {
582 CommentInfo();
583 ~CommentInfo();
584
Christopher Dunn1e3149a2015-01-25 14:16:13 -0600585 void setComment(const char* text, size_t len);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000586
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000587 char* comment_;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000588 };
589
590 // struct MemberNamesTransform
591 //{
592 // typedef const char *result_type;
593 // const char *operator()( const CZString &name ) const
594 // {
595 // return name.c_str();
596 // }
597 //};
598
599 union ValueHolder {
600 LargestInt int_;
601 LargestUInt uint_;
602 double real_;
603 bool bool_;
Christopher Dunnc28610f2015-02-21 11:44:16 -0600604 char* string_; // actually ptr to unsigned, followed by str, unless !allocated_
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000605 ObjectValues* map_;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000606 } value_;
607 ValueType type_ : 8;
Christopher Dunn2bc61372015-01-24 13:42:37 -0600608 unsigned int allocated_ : 1; // Notes: if declared as bool, bitfield is useless.
Christopher Dunnc28610f2015-02-21 11:44:16 -0600609 // If not allocated_, string_ must be null-terminated.
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000610 CommentInfo* comments_;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000611
612 // [start, limit) byte offsets in the source JSON text from which this Value
613 // was extracted.
Christopher Dunnd4513fc2016-02-06 09:25:20 -0600614 ptrdiff_t start_;
615 ptrdiff_t limit_;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000616};
617
618/** \brief Experimental and untested: represents an element of the "path" to
619 * access a node.
620 */
621class JSON_API PathArgument {
622public:
623 friend class Path;
624
625 PathArgument();
626 PathArgument(ArrayIndex index);
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000627 PathArgument(const char* key);
Christopher Dunnde5b7922016-03-06 11:19:46 -0600628 PathArgument(const JSONCPP_STRING& key);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000629
630private:
631 enum Kind {
632 kindNone = 0,
633 kindIndex,
634 kindKey
635 };
Christopher Dunnde5b7922016-03-06 11:19:46 -0600636 JSONCPP_STRING key_;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000637 ArrayIndex index_;
638 Kind kind_;
639};
640
641/** \brief Experimental and untested: represents a "path" to access a node.
642 *
643 * Syntax:
644 * - "." => root node
645 * - ".[n]" => elements at index 'n' of root node (an array value)
646 * - ".name" => member named 'name' of root node (an object value)
647 * - ".name1.name2.name3"
648 * - ".[0][1][2].name1[3]"
649 * - ".%" => member name is provided as parameter
650 * - ".[%]" => index is provied as parameter
651 */
652class JSON_API Path {
653public:
Christopher Dunnde5b7922016-03-06 11:19:46 -0600654 Path(const JSONCPP_STRING& path,
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000655 const PathArgument& a1 = PathArgument(),
656 const PathArgument& a2 = PathArgument(),
657 const PathArgument& a3 = PathArgument(),
658 const PathArgument& a4 = PathArgument(),
659 const PathArgument& a5 = PathArgument());
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000660
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000661 const Value& resolve(const Value& root) const;
662 Value resolve(const Value& root, const Value& defaultValue) const;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000663 /// Creates the "path" to access the specified node and returns a reference on
664 /// the node.
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000665 Value& make(Value& root) const;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000666
667private:
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000668 typedef std::vector<const PathArgument*> InArgs;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000669 typedef std::vector<PathArgument> Args;
670
Christopher Dunnde5b7922016-03-06 11:19:46 -0600671 void makePath(const JSONCPP_STRING& path, const InArgs& in);
672 void addPathInArg(const JSONCPP_STRING& path,
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000673 const InArgs& in,
674 InArgs::const_iterator& itInArg,
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000675 PathArgument::Kind kind);
Christopher Dunnde5b7922016-03-06 11:19:46 -0600676 void invalidPath(const JSONCPP_STRING& path, int location);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000677
678 Args args_;
679};
680
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000681/** \brief base class for Value iterators.
682 *
683 */
684class JSON_API ValueIteratorBase {
685public:
686 typedef std::bidirectional_iterator_tag iterator_category;
687 typedef unsigned int size_t;
688 typedef int difference_type;
689 typedef ValueIteratorBase SelfType;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000690
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000691 bool operator==(const SelfType& other) const { return isEqual(other); }
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000692
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000693 bool operator!=(const SelfType& other) const { return !isEqual(other); }
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000694
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000695 difference_type operator-(const SelfType& other) const {
Kevin Grant4c5832a2015-02-14 20:53:35 -0800696 return other.computeDistance(*this);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000697 }
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000698
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000699 /// Return either the index or the member name of the referenced value as a
700 /// Value.
701 Value key() const;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000702
Christopher Dunned495ed2015-03-08 14:01:28 -0500703 /// Return the index of the referenced Value, or -1 if it is not an arrayValue.
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000704 UInt index() const;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000705
Christopher Dunned495ed2015-03-08 14:01:28 -0500706 /// Return the member name of the referenced Value, or "" if it is not an
707 /// objectValue.
708 /// \note Avoid `c_str()` on result, as embedded zeroes are possible.
Christopher Dunnde5b7922016-03-06 11:19:46 -0600709 JSONCPP_STRING name() const;
Christopher Dunned495ed2015-03-08 14:01:28 -0500710
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000711 /// Return the member name of the referenced Value. "" if it is not an
712 /// objectValue.
Christopher Dunnc28610f2015-02-21 11:44:16 -0600713 /// \deprecated This cannot be used for UTF-8 strings, since there can be embedded nulls.
Christopher Dunned495ed2015-03-08 14:01:28 -0500714 JSONCPP_DEPRECATED("Use `key = name();` instead.")
Christopher Dunnc28610f2015-02-21 11:44:16 -0600715 char const* memberName() const;
716 /// Return the member name of the referenced Value, or NULL if it is not an
717 /// objectValue.
Christopher Dunned495ed2015-03-08 14:01:28 -0500718 /// \note Better version than memberName(). Allows embedded nulls.
Christopher Dunnc28610f2015-02-21 11:44:16 -0600719 char const* memberName(char const** end) const;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000720
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000721protected:
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000722 Value& deref() const;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000723
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000724 void increment();
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000725
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000726 void decrement();
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000727
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000728 difference_type computeDistance(const SelfType& other) const;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000729
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000730 bool isEqual(const SelfType& other) const;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000731
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000732 void copy(const SelfType& other);
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000733
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000734private:
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000735 Value::ObjectValues::iterator current_;
736 // Indicates that iterator is for a null value.
737 bool isNull_;
Christopher Dunn2a10f4a2015-04-28 04:55:12 +0100738
739public:
740 // For some reason, BORLAND needs these at the end, rather
741 // than earlier. No idea why.
742 ValueIteratorBase();
743 explicit ValueIteratorBase(const Value::ObjectValues::iterator& current);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000744};
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000745
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000746/** \brief const iterator for object and array value.
747 *
748 */
749class JSON_API ValueConstIterator : public ValueIteratorBase {
750 friend class Value;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000751
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000752public:
753 typedef const Value value_type;
Christopher Dunnc28610f2015-02-21 11:44:16 -0600754 //typedef unsigned int size_t;
755 //typedef int difference_type;
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000756 typedef const Value& reference;
757 typedef const Value* pointer;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000758 typedef ValueConstIterator SelfType;
759
760 ValueConstIterator();
ycqiuc8a8cfc2015-10-06 16:46:19 +0800761 ValueConstIterator(ValueIterator const& other);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000762
763private:
764/*! \internal Use by Value to create an iterator.
765 */
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000766 explicit ValueConstIterator(const Value::ObjectValues::iterator& current);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000767public:
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000768 SelfType& operator=(const ValueIteratorBase& other);
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000769
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000770 SelfType operator++(int) {
771 SelfType temp(*this);
772 ++*this;
773 return temp;
774 }
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000775
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000776 SelfType operator--(int) {
777 SelfType temp(*this);
778 --*this;
779 return temp;
780 }
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000781
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000782 SelfType& operator--() {
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000783 decrement();
784 return *this;
785 }
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000786
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000787 SelfType& operator++() {
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000788 increment();
789 return *this;
790 }
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000791
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000792 reference operator*() const { return deref(); }
Braden McDorman540db3b2014-09-14 02:31:23 -0500793
794 pointer operator->() const { return &deref(); }
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000795};
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000796
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000797/** \brief Iterator for object and array value.
798 */
799class JSON_API ValueIterator : public ValueIteratorBase {
800 friend class Value;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000801
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000802public:
803 typedef Value value_type;
804 typedef unsigned int size_t;
805 typedef int difference_type;
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000806 typedef Value& reference;
807 typedef Value* pointer;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000808 typedef ValueIterator SelfType;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000809
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000810 ValueIterator();
ycqiuc8a8cfc2015-10-06 16:46:19 +0800811 explicit ValueIterator(const ValueConstIterator& other);
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000812 ValueIterator(const ValueIterator& other);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000813
814private:
815/*! \internal Use by Value to create an iterator.
816 */
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000817 explicit ValueIterator(const Value::ObjectValues::iterator& current);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000818public:
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000819 SelfType& operator=(const SelfType& other);
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000820
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000821 SelfType operator++(int) {
822 SelfType temp(*this);
823 ++*this;
824 return temp;
825 }
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000826
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000827 SelfType operator--(int) {
828 SelfType temp(*this);
829 --*this;
830 return temp;
831 }
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000832
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000833 SelfType& operator--() {
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000834 decrement();
835 return *this;
836 }
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000837
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000838 SelfType& operator++() {
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000839 increment();
840 return *this;
841 }
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000842
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000843 reference operator*() const { return deref(); }
Braden McDorman540db3b2014-09-14 02:31:23 -0500844
845 pointer operator->() const { return &deref(); }
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000846};
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000847
848} // namespace Json
849
datadiode9454e682015-01-20 15:25:04 -0600850
851namespace std {
852/// Specialize std::swap() for Json::Value.
853template<>
854inline void swap(Json::Value& a, Json::Value& b) { a.swap(b); }
855}
856
857
Baptiste Lepilleureafd7022013-05-08 20:21:11 +0000858#if defined(JSONCPP_DISABLE_DLL_INTERFACE_WARNING)
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000859#pragma warning(pop)
Baptiste Lepilleureafd7022013-05-08 20:21:11 +0000860#endif // if defined(JSONCPP_DISABLE_DLL_INTERFACE_WARNING)
861
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000862#endif // CPPTL_JSON_H_INCLUDED