blob: 4c29382b5a688da5a0af3ede3a812843a1bae094 [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);
Christopher Dunn98e981d2016-03-21 21:00:24 -050056 ~Exception() throw() JSONCPP_OVERRIDE;
57 char const* what() const throw() JSONCPP_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!
dawescae564652016-03-14 19:11:02 -0500339#if JSONCPP_USING_SECURE_MEMORY
340 unsigned getCStringLength() const; //Allows you to understand the length of the CString
341#endif
Christopher Dunnde5b7922016-03-06 11:19:46 -0600342 JSONCPP_STRING asString() const; ///< Embedded zeroes are possible.
Christopher Dunnc28610f2015-02-21 11:44:16 -0600343 /** Get raw char* of string-value.
344 * \return false if !string. (Seg-fault if str or end are NULL.)
345 */
346 bool getString(
Christopher Dunn89704032015-07-11 12:09:59 -0500347 char const** begin, char const** end) const;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000348#ifdef JSON_USE_CPPTL
349 CppTL::ConstString asConstString() const;
350#endif
351 Int asInt() const;
352 UInt asUInt() const;
Aaron Jacobsf1053e72011-05-24 03:18:02 +0000353#if defined(JSON_HAS_INT64)
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000354 Int64 asInt64() const;
355 UInt64 asUInt64() const;
Aaron Jacobsf1053e72011-05-24 03:18:02 +0000356#endif // if defined(JSON_HAS_INT64)
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000357 LargestInt asLargestInt() const;
358 LargestUInt asLargestUInt() const;
359 float asFloat() const;
360 double asDouble() const;
361 bool asBool() const;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000362
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000363 bool isNull() const;
364 bool isBool() const;
365 bool isInt() const;
366 bool isInt64() const;
367 bool isUInt() const;
368 bool isUInt64() const;
369 bool isIntegral() const;
370 bool isDouble() const;
371 bool isNumeric() const;
372 bool isString() const;
373 bool isArray() const;
374 bool isObject() const;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000375
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000376 bool isConvertibleTo(ValueType other) const;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000377
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000378 /// Number of values in array or object
379 ArrayIndex size() const;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000380
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000381 /// \brief Return true if empty array, empty object, or null;
382 /// otherwise, false.
383 bool empty() const;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000384
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000385 /// Return isNull()
386 bool operator!() const;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000387
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000388 /// Remove all object members and array elements.
389 /// \pre type() is arrayValue, objectValue, or nullValue
390 /// \post type() is unchanged
391 void clear();
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000392
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000393 /// Resize the array to size elements.
394 /// New elements are initialized to null.
395 /// May only be called on nullValue or arrayValue.
396 /// \pre type() is arrayValue or nullValue
397 /// \post type() is arrayValue
398 void resize(ArrayIndex size);
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000399
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000400 /// Access an array element (zero based index ).
401 /// If the array contains less than index element, then null value are
402 /// inserted
403 /// in the array so that its size is index+1.
404 /// (You may need to say 'value[0u]' to get your compiler to distinguish
405 /// this from the operator[] which takes a string.)
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000406 Value& operator[](ArrayIndex index);
Baptiste Lepilleurfa130ef2010-12-24 12:47:14 +0000407
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000408 /// Access an array element (zero based index ).
409 /// If the array contains less than index element, then null value are
410 /// inserted
411 /// in the array so that its size is index+1.
412 /// (You may need to say 'value[0u]' to get your compiler to distinguish
413 /// this from the operator[] which takes a string.)
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000414 Value& operator[](int index);
Baptiste Lepilleurfa130ef2010-12-24 12:47:14 +0000415
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000416 /// Access an array element (zero based index )
417 /// (You may need to say 'value[0u]' to get your compiler to distinguish
418 /// this from the operator[] which takes a string.)
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000419 const Value& operator[](ArrayIndex index) const;
Baptiste Lepilleurfa130ef2010-12-24 12:47:14 +0000420
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000421 /// Access an array element (zero based index )
422 /// (You may need to say 'value[0u]' to get your compiler to distinguish
423 /// this from the operator[] which takes a string.)
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000424 const Value& operator[](int index) const;
Baptiste Lepilleurfa130ef2010-12-24 12:47:14 +0000425
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000426 /// If the array contains at least index+1 elements, returns the element
427 /// value,
428 /// otherwise returns defaultValue.
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000429 Value get(ArrayIndex index, const Value& defaultValue) const;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000430 /// Return true if index < size().
431 bool isValidIndex(ArrayIndex index) const;
432 /// \brief Append value to array at the end.
433 ///
434 /// Equivalent to jsonvalue[jsonvalue.size()] = value;
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000435 Value& append(const Value& value);
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000436
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000437 /// Access an object value by name, create a null member if it does not exist.
Christopher Dunn25342ba2015-03-02 18:05:26 -0600438 /// \note Because of our implementation, keys are limited to 2^30 -1 chars.
439 /// Exceeding that will cause an exception.
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000440 Value& operator[](const char* key);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000441 /// Access an object value by name, returns null if there is no member with
442 /// that name.
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000443 const Value& operator[](const char* key) const;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000444 /// Access an object value by name, create a null member if it does not exist.
Christopher Dunn25342ba2015-03-02 18:05:26 -0600445 /// \param key may contain embedded nulls.
Christopher Dunnde5b7922016-03-06 11:19:46 -0600446 Value& operator[](const JSONCPP_STRING& key);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000447 /// Access an object value by name, returns null if there is no member with
448 /// that name.
Christopher Dunn25342ba2015-03-02 18:05:26 -0600449 /// \param key may contain embedded nulls.
Christopher Dunnde5b7922016-03-06 11:19:46 -0600450 const Value& operator[](const JSONCPP_STRING& key) const;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000451 /** \brief Access an object value by name, create a null member if it does not
452 exist.
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000453
Christopher Dunn25342ba2015-03-02 18:05:26 -0600454 * If the object has no entry for that name, then the member name used to store
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000455 * the new entry is not duplicated.
456 * Example of use:
457 * \code
458 * Json::Value object;
459 * static const StaticString code("code");
460 * object[code] = 1234;
461 * \endcode
462 */
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000463 Value& operator[](const StaticString& key);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000464#ifdef JSON_USE_CPPTL
465 /// Access an object value by name, create a null member if it does not exist.
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000466 Value& operator[](const CppTL::ConstString& key);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000467 /// Access an object value by name, returns null if there is no member with
468 /// that name.
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000469 const Value& operator[](const CppTL::ConstString& key) const;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000470#endif
471 /// Return the member named key if it exist, defaultValue otherwise.
Christopher Dunn0fd28752015-03-05 16:38:43 -0600472 /// \note deep copy
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000473 Value get(const char* key, const Value& defaultValue) const;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000474 /// Return the member named key if it exist, defaultValue otherwise.
Christopher Dunn0fd28752015-03-05 16:38:43 -0600475 /// \note deep copy
Christopher Dunn89704032015-07-11 12:09:59 -0500476 /// \note key may contain embedded nulls.
477 Value get(const char* begin, const char* end, const Value& defaultValue) const;
Christopher Dunn25342ba2015-03-02 18:05:26 -0600478 /// Return the member named key if it exist, defaultValue otherwise.
Christopher Dunn0fd28752015-03-05 16:38:43 -0600479 /// \note deep copy
Christopher Dunn25342ba2015-03-02 18:05:26 -0600480 /// \param key may contain embedded nulls.
Christopher Dunnde5b7922016-03-06 11:19:46 -0600481 Value get(const JSONCPP_STRING& key, const Value& defaultValue) const;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000482#ifdef JSON_USE_CPPTL
483 /// Return the member named key if it exist, defaultValue otherwise.
Christopher Dunn0fd28752015-03-05 16:38:43 -0600484 /// \note deep copy
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000485 Value get(const CppTL::ConstString& key, const Value& defaultValue) const;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000486#endif
Christopher Dunn25342ba2015-03-02 18:05:26 -0600487 /// Most general and efficient version of isMember()const, get()const,
488 /// and operator[]const
Christopher Dunn89704032015-07-11 12:09:59 -0500489 /// \note As stated elsewhere, behavior is undefined if (end-begin) >= 2^30
490 Value const* find(char const* begin, char const* end) const;
Christopher Dunnc28610f2015-02-21 11:44:16 -0600491 /// Most general and efficient version of object-mutators.
Christopher Dunn89704032015-07-11 12:09:59 -0500492 /// \note As stated elsewhere, behavior is undefined if (end-begin) >= 2^30
Christopher Dunnc28610f2015-02-21 11:44:16 -0600493 /// \return non-zero, but JSON_ASSERT if this is neither object nor nullValue.
Christopher Dunn89704032015-07-11 12:09:59 -0500494 Value const* demand(char const* begin, char const* end);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000495 /// \brief Remove and return the named member.
496 ///
497 /// Do nothing if it did not exist.
498 /// \return the removed Value, or null.
499 /// \pre type() is objectValue or nullValue
500 /// \post type() is unchanged
Christopher Dunn76746b02015-01-21 16:01:30 -0600501 /// \deprecated
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000502 Value removeMember(const char* key);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000503 /// Same as removeMember(const char*)
Christopher Dunn25342ba2015-03-02 18:05:26 -0600504 /// \param key may contain embedded nulls.
Christopher Dunn76746b02015-01-21 16:01:30 -0600505 /// \deprecated
Christopher Dunnde5b7922016-03-06 11:19:46 -0600506 Value removeMember(const JSONCPP_STRING& key);
Christopher Dunn89704032015-07-11 12:09:59 -0500507 /// Same as removeMember(const char* begin, const char* end, Value* removed),
Christopher Dunn25342ba2015-03-02 18:05:26 -0600508 /// but 'key' is null-terminated.
509 bool removeMember(const char* key, Value* removed);
Christopher Dunn76746b02015-01-21 16:01:30 -0600510 /** \brief Remove the named map member.
511
512 Update 'removed' iff removed.
Christopher Dunn25342ba2015-03-02 18:05:26 -0600513 \param key may contain embedded nulls.
Christopher Dunn76746b02015-01-21 16:01:30 -0600514 \return true iff removed (no exceptions)
515 */
Christopher Dunnde5b7922016-03-06 11:19:46 -0600516 bool removeMember(JSONCPP_STRING const& key, Value* removed);
517 /// Same as removeMember(JSONCPP_STRING const& key, Value* removed)
Christopher Dunn89704032015-07-11 12:09:59 -0500518 bool removeMember(const char* begin, const char* end, Value* removed);
Christopher Dunn9de2c2d2015-01-20 16:15:40 -0600519 /** \brief Remove the indexed array element.
520
521 O(n) expensive operations.
522 Update 'removed' iff removed.
Christopher Dunne87e41c2015-01-20 16:24:11 -0600523 \return true iff removed (no exceptions)
Christopher Dunn9de2c2d2015-01-20 16:15:40 -0600524 */
525 bool removeIndex(ArrayIndex i, Value* removed);
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000526
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000527 /// Return true if the object has a member named key.
Christopher Dunnc28610f2015-02-21 11:44:16 -0600528 /// \note 'key' must be null-terminated.
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000529 bool isMember(const char* key) const;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000530 /// Return true if the object has a member named key.
Christopher Dunn25342ba2015-03-02 18:05:26 -0600531 /// \param key may contain embedded nulls.
Christopher Dunnde5b7922016-03-06 11:19:46 -0600532 bool isMember(const JSONCPP_STRING& key) const;
533 /// Same as isMember(JSONCPP_STRING const& key)const
Christopher Dunn89704032015-07-11 12:09:59 -0500534 bool isMember(const char* begin, const char* end) const;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000535#ifdef JSON_USE_CPPTL
536 /// Return true if the object has a member named key.
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000537 bool isMember(const CppTL::ConstString& key) const;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000538#endif
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000539
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000540 /// \brief Return a list of the member names.
541 ///
542 /// If null, return an empty list.
543 /// \pre type() is objectValue or nullValue
544 /// \post if type() was nullValue, it remains nullValue
545 Members getMemberNames() const;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000546
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000547 //# ifdef JSON_USE_CPPTL
548 // EnumMemberNames enumMemberNames() const;
549 // EnumValues enumValues() const;
550 //# endif
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000551
Christopher Dunn1e3149a2015-01-25 14:16:13 -0600552 /// \deprecated Always pass len.
Christopher Dunnde5b7922016-03-06 11:19:46 -0600553 JSONCPP_DEPRECATED("Use setComment(JSONCPP_STRING const&) instead.")
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000554 void setComment(const char* comment, CommentPlacement placement);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000555 /// Comments must be //... or /* ... */
Christopher Dunn1e3149a2015-01-25 14:16:13 -0600556 void setComment(const char* comment, size_t len, CommentPlacement placement);
557 /// Comments must be //... or /* ... */
Christopher Dunnde5b7922016-03-06 11:19:46 -0600558 void setComment(const JSONCPP_STRING& comment, CommentPlacement placement);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000559 bool hasComment(CommentPlacement placement) const;
560 /// Include delimiters and embedded newlines.
Christopher Dunnde5b7922016-03-06 11:19:46 -0600561 JSONCPP_STRING getComment(CommentPlacement placement) const;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000562
Christopher Dunnde5b7922016-03-06 11:19:46 -0600563 JSONCPP_STRING toStyledString() const;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000564
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000565 const_iterator begin() const;
566 const_iterator end() const;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000567
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000568 iterator begin();
569 iterator end();
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000570
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000571 // Accessors for the [start, limit) range of bytes within the JSON text from
572 // which this value was parsed, if any.
Christopher Dunnd4513fc2016-02-06 09:25:20 -0600573 void setOffsetStart(ptrdiff_t start);
574 void setOffsetLimit(ptrdiff_t limit);
575 ptrdiff_t getOffsetStart() const;
576 ptrdiff_t getOffsetLimit() const;
Aaron Jacobs68db6552014-04-23 23:41:12 +0000577
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000578private:
Billy Donahue8eb5d892014-11-10 01:35:42 -0500579 void initBasic(ValueType type, bool allocated = false);
580
Christopher Dunnc28610f2015-02-21 11:44:16 -0600581 Value& resolveReference(const char* key);
582 Value& resolveReference(const char* key, const char* end);
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000583
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000584 struct CommentInfo {
585 CommentInfo();
586 ~CommentInfo();
587
Christopher Dunn1e3149a2015-01-25 14:16:13 -0600588 void setComment(const char* text, size_t len);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000589
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000590 char* comment_;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000591 };
592
593 // struct MemberNamesTransform
594 //{
595 // typedef const char *result_type;
596 // const char *operator()( const CZString &name ) const
597 // {
598 // return name.c_str();
599 // }
600 //};
601
602 union ValueHolder {
603 LargestInt int_;
604 LargestUInt uint_;
605 double real_;
606 bool bool_;
Christopher Dunnc28610f2015-02-21 11:44:16 -0600607 char* string_; // actually ptr to unsigned, followed by str, unless !allocated_
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000608 ObjectValues* map_;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000609 } value_;
610 ValueType type_ : 8;
Christopher Dunn2bc61372015-01-24 13:42:37 -0600611 unsigned int allocated_ : 1; // Notes: if declared as bool, bitfield is useless.
Christopher Dunnc28610f2015-02-21 11:44:16 -0600612 // If not allocated_, string_ must be null-terminated.
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000613 CommentInfo* comments_;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000614
615 // [start, limit) byte offsets in the source JSON text from which this Value
616 // was extracted.
Christopher Dunnd4513fc2016-02-06 09:25:20 -0600617 ptrdiff_t start_;
618 ptrdiff_t limit_;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000619};
620
621/** \brief Experimental and untested: represents an element of the "path" to
622 * access a node.
623 */
624class JSON_API PathArgument {
625public:
626 friend class Path;
627
628 PathArgument();
629 PathArgument(ArrayIndex index);
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000630 PathArgument(const char* key);
Christopher Dunnde5b7922016-03-06 11:19:46 -0600631 PathArgument(const JSONCPP_STRING& key);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000632
633private:
634 enum Kind {
635 kindNone = 0,
636 kindIndex,
637 kindKey
638 };
Christopher Dunnde5b7922016-03-06 11:19:46 -0600639 JSONCPP_STRING key_;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000640 ArrayIndex index_;
641 Kind kind_;
642};
643
644/** \brief Experimental and untested: represents a "path" to access a node.
645 *
646 * Syntax:
647 * - "." => root node
648 * - ".[n]" => elements at index 'n' of root node (an array value)
649 * - ".name" => member named 'name' of root node (an object value)
650 * - ".name1.name2.name3"
651 * - ".[0][1][2].name1[3]"
652 * - ".%" => member name is provided as parameter
653 * - ".[%]" => index is provied as parameter
654 */
655class JSON_API Path {
656public:
Christopher Dunnde5b7922016-03-06 11:19:46 -0600657 Path(const JSONCPP_STRING& path,
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000658 const PathArgument& a1 = PathArgument(),
659 const PathArgument& a2 = PathArgument(),
660 const PathArgument& a3 = PathArgument(),
661 const PathArgument& a4 = PathArgument(),
662 const PathArgument& a5 = PathArgument());
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000663
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000664 const Value& resolve(const Value& root) const;
665 Value resolve(const Value& root, const Value& defaultValue) const;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000666 /// Creates the "path" to access the specified node and returns a reference on
667 /// the node.
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000668 Value& make(Value& root) const;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000669
670private:
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000671 typedef std::vector<const PathArgument*> InArgs;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000672 typedef std::vector<PathArgument> Args;
673
Christopher Dunnde5b7922016-03-06 11:19:46 -0600674 void makePath(const JSONCPP_STRING& path, const InArgs& in);
675 void addPathInArg(const JSONCPP_STRING& path,
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000676 const InArgs& in,
677 InArgs::const_iterator& itInArg,
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000678 PathArgument::Kind kind);
Christopher Dunnde5b7922016-03-06 11:19:46 -0600679 void invalidPath(const JSONCPP_STRING& path, int location);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000680
681 Args args_;
682};
683
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000684/** \brief base class for Value iterators.
685 *
686 */
687class JSON_API ValueIteratorBase {
688public:
689 typedef std::bidirectional_iterator_tag iterator_category;
690 typedef unsigned int size_t;
691 typedef int difference_type;
692 typedef ValueIteratorBase SelfType;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000693
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000694 bool operator==(const SelfType& other) const { return isEqual(other); }
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000695
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000696 bool operator!=(const SelfType& other) const { return !isEqual(other); }
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000697
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000698 difference_type operator-(const SelfType& other) const {
Kevin Grant4c5832a2015-02-14 20:53:35 -0800699 return other.computeDistance(*this);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000700 }
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000701
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000702 /// Return either the index or the member name of the referenced value as a
703 /// Value.
704 Value key() const;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000705
Christopher Dunned495ed2015-03-08 14:01:28 -0500706 /// Return the index of the referenced Value, or -1 if it is not an arrayValue.
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000707 UInt index() const;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000708
Christopher Dunned495ed2015-03-08 14:01:28 -0500709 /// Return the member name of the referenced Value, or "" if it is not an
710 /// objectValue.
711 /// \note Avoid `c_str()` on result, as embedded zeroes are possible.
Christopher Dunnde5b7922016-03-06 11:19:46 -0600712 JSONCPP_STRING name() const;
Christopher Dunned495ed2015-03-08 14:01:28 -0500713
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000714 /// Return the member name of the referenced Value. "" if it is not an
715 /// objectValue.
Christopher Dunnc28610f2015-02-21 11:44:16 -0600716 /// \deprecated This cannot be used for UTF-8 strings, since there can be embedded nulls.
Christopher Dunned495ed2015-03-08 14:01:28 -0500717 JSONCPP_DEPRECATED("Use `key = name();` instead.")
Christopher Dunnc28610f2015-02-21 11:44:16 -0600718 char const* memberName() const;
719 /// Return the member name of the referenced Value, or NULL if it is not an
720 /// objectValue.
Christopher Dunned495ed2015-03-08 14:01:28 -0500721 /// \note Better version than memberName(). Allows embedded nulls.
Christopher Dunnc28610f2015-02-21 11:44:16 -0600722 char const* memberName(char const** end) const;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000723
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000724protected:
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000725 Value& deref() const;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000726
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000727 void increment();
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000728
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000729 void decrement();
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000730
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000731 difference_type computeDistance(const SelfType& other) const;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000732
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000733 bool isEqual(const SelfType& other) const;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000734
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000735 void copy(const SelfType& other);
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000736
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000737private:
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000738 Value::ObjectValues::iterator current_;
739 // Indicates that iterator is for a null value.
740 bool isNull_;
Christopher Dunn2a10f4a2015-04-28 04:55:12 +0100741
742public:
743 // For some reason, BORLAND needs these at the end, rather
744 // than earlier. No idea why.
745 ValueIteratorBase();
746 explicit ValueIteratorBase(const Value::ObjectValues::iterator& current);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000747};
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000748
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000749/** \brief const iterator for object and array value.
750 *
751 */
752class JSON_API ValueConstIterator : public ValueIteratorBase {
753 friend class Value;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000754
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000755public:
756 typedef const Value value_type;
Christopher Dunnc28610f2015-02-21 11:44:16 -0600757 //typedef unsigned int size_t;
758 //typedef int difference_type;
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000759 typedef const Value& reference;
760 typedef const Value* pointer;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000761 typedef ValueConstIterator SelfType;
762
763 ValueConstIterator();
ycqiuc8a8cfc2015-10-06 16:46:19 +0800764 ValueConstIterator(ValueIterator const& other);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000765
766private:
767/*! \internal Use by Value to create an iterator.
768 */
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000769 explicit ValueConstIterator(const Value::ObjectValues::iterator& current);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000770public:
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000771 SelfType& operator=(const ValueIteratorBase& other);
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000772
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000773 SelfType operator++(int) {
774 SelfType temp(*this);
775 ++*this;
776 return temp;
777 }
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000778
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000779 SelfType operator--(int) {
780 SelfType temp(*this);
781 --*this;
782 return temp;
783 }
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000784
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000785 SelfType& operator--() {
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000786 decrement();
787 return *this;
788 }
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000789
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000790 SelfType& operator++() {
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000791 increment();
792 return *this;
793 }
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000794
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000795 reference operator*() const { return deref(); }
Braden McDorman540db3b2014-09-14 02:31:23 -0500796
797 pointer operator->() const { return &deref(); }
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000798};
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000799
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000800/** \brief Iterator for object and array value.
801 */
802class JSON_API ValueIterator : public ValueIteratorBase {
803 friend class Value;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000804
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000805public:
806 typedef Value value_type;
807 typedef unsigned int size_t;
808 typedef int difference_type;
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000809 typedef Value& reference;
810 typedef Value* pointer;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000811 typedef ValueIterator SelfType;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000812
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000813 ValueIterator();
ycqiuc8a8cfc2015-10-06 16:46:19 +0800814 explicit ValueIterator(const ValueConstIterator& other);
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000815 ValueIterator(const ValueIterator& other);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000816
817private:
818/*! \internal Use by Value to create an iterator.
819 */
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000820 explicit ValueIterator(const Value::ObjectValues::iterator& current);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000821public:
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000822 SelfType& operator=(const SelfType& other);
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000823
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000824 SelfType operator++(int) {
825 SelfType temp(*this);
826 ++*this;
827 return temp;
828 }
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000829
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000830 SelfType operator--(int) {
831 SelfType temp(*this);
832 --*this;
833 return temp;
834 }
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000835
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000836 SelfType& operator--() {
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000837 decrement();
838 return *this;
839 }
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000840
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000841 SelfType& operator++() {
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000842 increment();
843 return *this;
844 }
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000845
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000846 reference operator*() const { return deref(); }
Braden McDorman540db3b2014-09-14 02:31:23 -0500847
848 pointer operator->() const { return &deref(); }
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000849};
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000850
851} // namespace Json
852
datadiode9454e682015-01-20 15:25:04 -0600853
854namespace std {
855/// Specialize std::swap() for Json::Value.
856template<>
857inline void swap(Json::Value& a, Json::Value& b) { a.swap(b); }
858}
859
860
Baptiste Lepilleureafd7022013-05-08 20:21:11 +0000861#if defined(JSONCPP_DISABLE_DLL_INTERFACE_WARNING)
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000862#pragma warning(pop)
Baptiste Lepilleureafd7022013-05-08 20:21:11 +0000863#endif // if defined(JSONCPP_DISABLE_DLL_INTERFACE_WARNING)
864
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000865#endif // CPPTL_JSON_H_INCLUDED