blob: fb88c18295191a75d11c1feaa80be5f4a8ca81d8 [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
Christopher Dunn0f288ae2016-06-26 18:47:43 -0500193 static Value const& nullSingleton(); ///< Prefer this to null or nullRef.
194
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000195 /// Minimum signed integer value that can be stored in a Json::Value.
196 static const LargestInt minLargestInt;
197 /// Maximum signed integer value that can be stored in a Json::Value.
198 static const LargestInt maxLargestInt;
199 /// Maximum unsigned integer value that can be stored in a Json::Value.
200 static const LargestUInt maxLargestUInt;
Baptiste Lepilleur842e9ac2010-12-27 17:45:23 +0000201
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000202 /// Minimum signed int value that can be stored in a Json::Value.
203 static const Int minInt;
204 /// Maximum signed int value that can be stored in a Json::Value.
205 static const Int maxInt;
206 /// Maximum unsigned int value that can be stored in a Json::Value.
207 static const UInt maxUInt;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000208
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000209#if defined(JSON_HAS_INT64)
210 /// Minimum signed 64 bits int value that can be stored in a Json::Value.
211 static const Int64 minInt64;
212 /// Maximum signed 64 bits int value that can be stored in a Json::Value.
213 static const Int64 maxInt64;
214 /// Maximum unsigned 64 bits int value that can be stored in a Json::Value.
215 static const UInt64 maxUInt64;
Aaron Jacobsf1053e72011-05-24 03:18:02 +0000216#endif // defined(JSON_HAS_INT64)
Baptiste Lepilleur842e9ac2010-12-27 17:45:23 +0000217
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000218private:
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000219#ifndef JSONCPP_DOC_EXCLUDE_IMPLEMENTATION
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000220 class CZString {
221 public:
222 enum DuplicationPolicy {
223 noDuplication = 0,
224 duplicate,
225 duplicateOnCopy
226 };
227 CZString(ArrayIndex index);
Christopher Dunnc28610f2015-02-21 11:44:16 -0600228 CZString(char const* str, unsigned length, DuplicationPolicy allocate);
229 CZString(CZString const& other);
Motti2b008912015-04-20 17:44:47 +0300230#if JSON_HAS_RVALUE_REFERENCES
231 CZString(CZString&& other);
232#endif
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000233 ~CZString();
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000234 CZString& operator=(CZString other);
Christopher Dunnc28610f2015-02-21 11:44:16 -0600235 bool operator<(CZString const& other) const;
236 bool operator==(CZString const& other) const;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000237 ArrayIndex index() const;
Christopher Dunnc28610f2015-02-21 11:44:16 -0600238 //const char* c_str() const; ///< \deprecated
239 char const* data() const;
240 unsigned length() const;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000241 bool isStaticString() const;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000242
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000243 private:
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000244 void swap(CZString& other);
Christopher Dunnc28610f2015-02-21 11:44:16 -0600245
Christopher Dunn57ad0512015-03-02 12:10:35 -0600246 struct StringStorage {
Dan Liufcbab022015-04-27 05:20:05 +0100247 unsigned policy_: 2;
Christopher Dunn57ad0512015-03-02 12:10:35 -0600248 unsigned length_: 30; // 1GB max
249 };
250
Christopher Dunnc28610f2015-02-21 11:44:16 -0600251 char const* cstr_; // actually, a prefixed string, unless policy is noDup
Christopher Dunn57ad0512015-03-02 12:10:35 -0600252 union {
253 ArrayIndex index_;
254 StringStorage storage_;
255 };
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000256 };
257
258public:
259#ifndef JSON_USE_CPPTL_SMALLMAP
260 typedef std::map<CZString, Value> ObjectValues;
261#else
262 typedef CppTL::SmallMap<CZString, Value> ObjectValues;
263#endif // ifndef JSON_USE_CPPTL_SMALLMAP
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000264#endif // ifndef JSONCPP_DOC_EXCLUDE_IMPLEMENTATION
265
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000266public:
267 /** \brief Create a default Value of the given type.
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000268
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000269 This is a very useful constructor.
270 To create an empty array, pass arrayValue.
271 To create an empty object, pass objectValue.
272 Another Value can then be set to this one by assignment.
273This is useful since clear() and resize() will not alter types.
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000274
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000275 Examples:
276\code
277Json::Value null_value; // null
278Json::Value arr_value(Json::arrayValue); // []
279Json::Value obj_value(Json::objectValue); // {}
280\endcode
281 */
282 Value(ValueType type = nullValue);
283 Value(Int value);
284 Value(UInt value);
Baptiste Lepilleur842e9ac2010-12-27 17:45:23 +0000285#if defined(JSON_HAS_INT64)
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000286 Value(Int64 value);
287 Value(UInt64 value);
Baptiste Lepilleur842e9ac2010-12-27 17:45:23 +0000288#endif // if defined(JSON_HAS_INT64)
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000289 Value(double value);
Christopher Dunn8a702972015-03-03 10:38:27 -0600290 Value(const char* value); ///< Copy til first 0. (NULL causes to seg-fault.)
Christopher Dunn89704032015-07-11 12:09:59 -0500291 Value(const char* begin, const char* end); ///< Copy all, incl zeroes.
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000292 /** \brief Constructs a value from a static string.
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000293
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000294 * Like other value string constructor but do not duplicate the string for
295 * internal storage. The given string must remain alive after the call to this
296 * constructor.
Christopher Dunnc28610f2015-02-21 11:44:16 -0600297 * \note This works only for null-terminated strings. (We cannot change the
298 * size of this class, so we have nowhere to store the length,
299 * which might be computed later for various operations.)
300 *
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000301 * Example of usage:
302 * \code
Christopher Dunnc28610f2015-02-21 11:44:16 -0600303 * static StaticString foo("some text");
304 * Json::Value aValue(foo);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000305 * \endcode
306 */
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000307 Value(const StaticString& value);
Christopher Dunnde5b7922016-03-06 11:19:46 -0600308 Value(const JSONCPP_STRING& value); ///< Copy data() til size(). Embedded zeroes too.
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000309#ifdef JSON_USE_CPPTL
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000310 Value(const CppTL::ConstString& value);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000311#endif
312 Value(bool value);
Christopher Dunn66eb72f2015-01-20 11:02:22 -0600313 /// Deep copy.
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000314 Value(const Value& other);
Motti2b008912015-04-20 17:44:47 +0300315#if JSON_HAS_RVALUE_REFERENCES
316 /// Move constructor
317 Value(Value&& other);
318#endif
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000319 ~Value();
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000320
Christopher Dunn7f439f42015-03-06 09:22:57 -0600321 /// Deep copy, then swap(other).
322 /// \note Over-write existing comments. To preserve comments, use #swapPayload().
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000323 Value& operator=(Value other);
Christopher Dunn66eb72f2015-01-20 11:02:22 -0600324 /// Swap everything.
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000325 void swap(Value& other);
Christopher Dunn66eb72f2015-01-20 11:02:22 -0600326 /// Swap values but leave comments and source offsets in place.
327 void swapPayload(Value& other);
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000328
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000329 ValueType type() const;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000330
Christopher Dunn66eb72f2015-01-20 11:02:22 -0600331 /// Compare payload only, not comments etc.
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000332 bool operator<(const Value& other) const;
333 bool operator<=(const Value& other) const;
334 bool operator>=(const Value& other) const;
335 bool operator>(const Value& other) const;
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000336 bool operator==(const Value& other) const;
337 bool operator!=(const Value& other) const;
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000338 int compare(const Value& other) const;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000339
Christopher Dunn8a702972015-03-03 10:38:27 -0600340 const char* asCString() const; ///< Embedded zeroes could cause you trouble!
dawescae564652016-03-14 19:11:02 -0500341#if JSONCPP_USING_SECURE_MEMORY
342 unsigned getCStringLength() const; //Allows you to understand the length of the CString
343#endif
Christopher Dunnde5b7922016-03-06 11:19:46 -0600344 JSONCPP_STRING asString() const; ///< Embedded zeroes are possible.
Christopher Dunnc28610f2015-02-21 11:44:16 -0600345 /** Get raw char* of string-value.
346 * \return false if !string. (Seg-fault if str or end are NULL.)
347 */
348 bool getString(
Christopher Dunn89704032015-07-11 12:09:59 -0500349 char const** begin, char const** end) const;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000350#ifdef JSON_USE_CPPTL
351 CppTL::ConstString asConstString() const;
352#endif
353 Int asInt() const;
354 UInt asUInt() const;
Aaron Jacobsf1053e72011-05-24 03:18:02 +0000355#if defined(JSON_HAS_INT64)
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000356 Int64 asInt64() const;
357 UInt64 asUInt64() const;
Aaron Jacobsf1053e72011-05-24 03:18:02 +0000358#endif // if defined(JSON_HAS_INT64)
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000359 LargestInt asLargestInt() const;
360 LargestUInt asLargestUInt() const;
361 float asFloat() const;
362 double asDouble() const;
363 bool asBool() const;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000364
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000365 bool isNull() const;
366 bool isBool() const;
367 bool isInt() const;
368 bool isInt64() const;
369 bool isUInt() const;
370 bool isUInt64() const;
371 bool isIntegral() const;
372 bool isDouble() const;
373 bool isNumeric() const;
374 bool isString() const;
375 bool isArray() const;
376 bool isObject() const;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000377
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000378 bool isConvertibleTo(ValueType other) const;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000379
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000380 /// Number of values in array or object
381 ArrayIndex size() const;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000382
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000383 /// \brief Return true if empty array, empty object, or null;
384 /// otherwise, false.
385 bool empty() const;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000386
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000387 /// Return isNull()
388 bool operator!() const;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000389
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000390 /// Remove all object members and array elements.
391 /// \pre type() is arrayValue, objectValue, or nullValue
392 /// \post type() is unchanged
393 void clear();
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000394
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000395 /// Resize the array to size elements.
396 /// New elements are initialized to null.
397 /// May only be called on nullValue or arrayValue.
398 /// \pre type() is arrayValue or nullValue
399 /// \post type() is arrayValue
400 void resize(ArrayIndex size);
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000401
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000402 /// Access an array element (zero based index ).
403 /// If the array contains less than index element, then null value are
404 /// inserted
405 /// in the array so that its size is index+1.
406 /// (You may need to say 'value[0u]' to get your compiler to distinguish
407 /// this from the operator[] which takes a string.)
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000408 Value& operator[](ArrayIndex index);
Baptiste Lepilleurfa130ef2010-12-24 12:47:14 +0000409
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000410 /// Access an array element (zero based index ).
411 /// If the array contains less than index element, then null value are
412 /// inserted
413 /// in the array so that its size is index+1.
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 Value& operator[](int index);
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[](ArrayIndex index) const;
Baptiste Lepilleurfa130ef2010-12-24 12:47:14 +0000422
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000423 /// Access an array element (zero based index )
424 /// (You may need to say 'value[0u]' to get your compiler to distinguish
425 /// this from the operator[] which takes a string.)
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000426 const Value& operator[](int index) const;
Baptiste Lepilleurfa130ef2010-12-24 12:47:14 +0000427
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000428 /// If the array contains at least index+1 elements, returns the element
429 /// value,
430 /// otherwise returns defaultValue.
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000431 Value get(ArrayIndex index, const Value& defaultValue) const;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000432 /// Return true if index < size().
433 bool isValidIndex(ArrayIndex index) const;
434 /// \brief Append value to array at the end.
435 ///
436 /// Equivalent to jsonvalue[jsonvalue.size()] = value;
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000437 Value& append(const Value& value);
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000438
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000439 /// Access an object value by name, create a null member if it does not exist.
Christopher Dunn25342ba2015-03-02 18:05:26 -0600440 /// \note Because of our implementation, keys are limited to 2^30 -1 chars.
441 /// Exceeding that will cause an exception.
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000442 Value& operator[](const char* key);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000443 /// Access an object value by name, returns null if there is no member with
444 /// that name.
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000445 const Value& operator[](const char* key) const;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000446 /// Access an object value by name, create a null member if it does not exist.
Christopher Dunn25342ba2015-03-02 18:05:26 -0600447 /// \param key may contain embedded nulls.
Christopher Dunnde5b7922016-03-06 11:19:46 -0600448 Value& operator[](const JSONCPP_STRING& key);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000449 /// Access an object value by name, returns null if there is no member with
450 /// that name.
Christopher Dunn25342ba2015-03-02 18:05:26 -0600451 /// \param key may contain embedded nulls.
Christopher Dunnde5b7922016-03-06 11:19:46 -0600452 const Value& operator[](const JSONCPP_STRING& key) const;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000453 /** \brief Access an object value by name, create a null member if it does not
454 exist.
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000455
Christopher Dunn25342ba2015-03-02 18:05:26 -0600456 * If the object has no entry for that name, then the member name used to store
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000457 * the new entry is not duplicated.
458 * Example of use:
459 * \code
460 * Json::Value object;
461 * static const StaticString code("code");
462 * object[code] = 1234;
463 * \endcode
464 */
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000465 Value& operator[](const StaticString& key);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000466#ifdef JSON_USE_CPPTL
467 /// Access an object value by name, create a null member if it does not exist.
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000468 Value& operator[](const CppTL::ConstString& key);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000469 /// Access an object value by name, returns null if there is no member with
470 /// that name.
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000471 const Value& operator[](const CppTL::ConstString& key) const;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000472#endif
473 /// Return the member named key if it exist, defaultValue otherwise.
Christopher Dunn0fd28752015-03-05 16:38:43 -0600474 /// \note deep copy
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000475 Value get(const char* key, const Value& defaultValue) const;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000476 /// Return the member named key if it exist, defaultValue otherwise.
Christopher Dunn0fd28752015-03-05 16:38:43 -0600477 /// \note deep copy
Christopher Dunn89704032015-07-11 12:09:59 -0500478 /// \note key may contain embedded nulls.
479 Value get(const char* begin, const char* end, const Value& defaultValue) const;
Christopher Dunn25342ba2015-03-02 18:05:26 -0600480 /// Return the member named key if it exist, defaultValue otherwise.
Christopher Dunn0fd28752015-03-05 16:38:43 -0600481 /// \note deep copy
Christopher Dunn25342ba2015-03-02 18:05:26 -0600482 /// \param key may contain embedded nulls.
Christopher Dunnde5b7922016-03-06 11:19:46 -0600483 Value get(const JSONCPP_STRING& key, const Value& defaultValue) const;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000484#ifdef JSON_USE_CPPTL
485 /// Return the member named key if it exist, defaultValue otherwise.
Christopher Dunn0fd28752015-03-05 16:38:43 -0600486 /// \note deep copy
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000487 Value get(const CppTL::ConstString& key, const Value& defaultValue) const;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000488#endif
Christopher Dunn25342ba2015-03-02 18:05:26 -0600489 /// Most general and efficient version of isMember()const, get()const,
490 /// and operator[]const
Christopher Dunn89704032015-07-11 12:09:59 -0500491 /// \note As stated elsewhere, behavior is undefined if (end-begin) >= 2^30
492 Value const* find(char const* begin, char const* end) const;
Christopher Dunnc28610f2015-02-21 11:44:16 -0600493 /// Most general and efficient version of object-mutators.
Christopher Dunn89704032015-07-11 12:09:59 -0500494 /// \note As stated elsewhere, behavior is undefined if (end-begin) >= 2^30
Christopher Dunnc28610f2015-02-21 11:44:16 -0600495 /// \return non-zero, but JSON_ASSERT if this is neither object nor nullValue.
Christopher Dunn89704032015-07-11 12:09:59 -0500496 Value const* demand(char const* begin, char const* end);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000497 /// \brief Remove and return the named member.
498 ///
499 /// Do nothing if it did not exist.
500 /// \return the removed Value, or null.
501 /// \pre type() is objectValue or nullValue
502 /// \post type() is unchanged
Christopher Dunn76746b02015-01-21 16:01:30 -0600503 /// \deprecated
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000504 Value removeMember(const char* key);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000505 /// Same as removeMember(const char*)
Christopher Dunn25342ba2015-03-02 18:05:26 -0600506 /// \param key may contain embedded nulls.
Christopher Dunn76746b02015-01-21 16:01:30 -0600507 /// \deprecated
Christopher Dunnde5b7922016-03-06 11:19:46 -0600508 Value removeMember(const JSONCPP_STRING& key);
Christopher Dunn89704032015-07-11 12:09:59 -0500509 /// Same as removeMember(const char* begin, const char* end, Value* removed),
Christopher Dunn25342ba2015-03-02 18:05:26 -0600510 /// but 'key' is null-terminated.
511 bool removeMember(const char* key, Value* removed);
Christopher Dunn76746b02015-01-21 16:01:30 -0600512 /** \brief Remove the named map member.
513
514 Update 'removed' iff removed.
Christopher Dunn25342ba2015-03-02 18:05:26 -0600515 \param key may contain embedded nulls.
Christopher Dunn76746b02015-01-21 16:01:30 -0600516 \return true iff removed (no exceptions)
517 */
Christopher Dunnde5b7922016-03-06 11:19:46 -0600518 bool removeMember(JSONCPP_STRING const& key, Value* removed);
519 /// Same as removeMember(JSONCPP_STRING const& key, Value* removed)
Christopher Dunn89704032015-07-11 12:09:59 -0500520 bool removeMember(const char* begin, const char* end, Value* removed);
Christopher Dunn9de2c2d2015-01-20 16:15:40 -0600521 /** \brief Remove the indexed array element.
522
523 O(n) expensive operations.
524 Update 'removed' iff removed.
Christopher Dunne87e41c2015-01-20 16:24:11 -0600525 \return true iff removed (no exceptions)
Christopher Dunn9de2c2d2015-01-20 16:15:40 -0600526 */
527 bool removeIndex(ArrayIndex i, Value* removed);
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000528
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000529 /// Return true if the object has a member named key.
Christopher Dunnc28610f2015-02-21 11:44:16 -0600530 /// \note 'key' must be null-terminated.
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000531 bool isMember(const char* key) const;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000532 /// Return true if the object has a member named key.
Christopher Dunn25342ba2015-03-02 18:05:26 -0600533 /// \param key may contain embedded nulls.
Christopher Dunnde5b7922016-03-06 11:19:46 -0600534 bool isMember(const JSONCPP_STRING& key) const;
535 /// Same as isMember(JSONCPP_STRING const& key)const
Christopher Dunn89704032015-07-11 12:09:59 -0500536 bool isMember(const char* begin, const char* end) const;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000537#ifdef JSON_USE_CPPTL
538 /// Return true if the object has a member named key.
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000539 bool isMember(const CppTL::ConstString& key) const;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000540#endif
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000541
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000542 /// \brief Return a list of the member names.
543 ///
544 /// If null, return an empty list.
545 /// \pre type() is objectValue or nullValue
546 /// \post if type() was nullValue, it remains nullValue
547 Members getMemberNames() const;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000548
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000549 //# ifdef JSON_USE_CPPTL
550 // EnumMemberNames enumMemberNames() const;
551 // EnumValues enumValues() const;
552 //# endif
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000553
Christopher Dunn1e3149a2015-01-25 14:16:13 -0600554 /// \deprecated Always pass len.
Christopher Dunnde5b7922016-03-06 11:19:46 -0600555 JSONCPP_DEPRECATED("Use setComment(JSONCPP_STRING const&) instead.")
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000556 void setComment(const char* comment, CommentPlacement placement);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000557 /// Comments must be //... or /* ... */
Christopher Dunn1e3149a2015-01-25 14:16:13 -0600558 void setComment(const char* comment, size_t len, CommentPlacement placement);
559 /// Comments must be //... or /* ... */
Christopher Dunnde5b7922016-03-06 11:19:46 -0600560 void setComment(const JSONCPP_STRING& comment, CommentPlacement placement);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000561 bool hasComment(CommentPlacement placement) const;
562 /// Include delimiters and embedded newlines.
Christopher Dunnde5b7922016-03-06 11:19:46 -0600563 JSONCPP_STRING getComment(CommentPlacement placement) const;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000564
Christopher Dunnde5b7922016-03-06 11:19:46 -0600565 JSONCPP_STRING toStyledString() const;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000566
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000567 const_iterator begin() const;
568 const_iterator end() const;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000569
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000570 iterator begin();
571 iterator end();
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000572
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000573 // Accessors for the [start, limit) range of bytes within the JSON text from
574 // which this value was parsed, if any.
Christopher Dunnd4513fc2016-02-06 09:25:20 -0600575 void setOffsetStart(ptrdiff_t start);
576 void setOffsetLimit(ptrdiff_t limit);
577 ptrdiff_t getOffsetStart() const;
578 ptrdiff_t getOffsetLimit() const;
Aaron Jacobs68db6552014-04-23 23:41:12 +0000579
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000580private:
Billy Donahue8eb5d892014-11-10 01:35:42 -0500581 void initBasic(ValueType type, bool allocated = false);
582
Christopher Dunnc28610f2015-02-21 11:44:16 -0600583 Value& resolveReference(const char* key);
584 Value& resolveReference(const char* key, const char* end);
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000585
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000586 struct CommentInfo {
587 CommentInfo();
588 ~CommentInfo();
589
Christopher Dunn1e3149a2015-01-25 14:16:13 -0600590 void setComment(const char* text, size_t len);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000591
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000592 char* comment_;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000593 };
594
595 // struct MemberNamesTransform
596 //{
597 // typedef const char *result_type;
598 // const char *operator()( const CZString &name ) const
599 // {
600 // return name.c_str();
601 // }
602 //};
603
604 union ValueHolder {
605 LargestInt int_;
606 LargestUInt uint_;
607 double real_;
608 bool bool_;
Christopher Dunnc28610f2015-02-21 11:44:16 -0600609 char* string_; // actually ptr to unsigned, followed by str, unless !allocated_
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000610 ObjectValues* map_;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000611 } value_;
612 ValueType type_ : 8;
Christopher Dunn2bc61372015-01-24 13:42:37 -0600613 unsigned int allocated_ : 1; // Notes: if declared as bool, bitfield is useless.
Christopher Dunnc28610f2015-02-21 11:44:16 -0600614 // If not allocated_, string_ must be null-terminated.
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000615 CommentInfo* comments_;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000616
617 // [start, limit) byte offsets in the source JSON text from which this Value
618 // was extracted.
Christopher Dunnd4513fc2016-02-06 09:25:20 -0600619 ptrdiff_t start_;
620 ptrdiff_t limit_;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000621};
622
623/** \brief Experimental and untested: represents an element of the "path" to
624 * access a node.
625 */
626class JSON_API PathArgument {
627public:
628 friend class Path;
629
630 PathArgument();
631 PathArgument(ArrayIndex index);
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000632 PathArgument(const char* key);
Christopher Dunnde5b7922016-03-06 11:19:46 -0600633 PathArgument(const JSONCPP_STRING& key);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000634
635private:
636 enum Kind {
637 kindNone = 0,
638 kindIndex,
639 kindKey
640 };
Christopher Dunnde5b7922016-03-06 11:19:46 -0600641 JSONCPP_STRING key_;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000642 ArrayIndex index_;
643 Kind kind_;
644};
645
646/** \brief Experimental and untested: represents a "path" to access a node.
647 *
648 * Syntax:
649 * - "." => root node
650 * - ".[n]" => elements at index 'n' of root node (an array value)
651 * - ".name" => member named 'name' of root node (an object value)
652 * - ".name1.name2.name3"
653 * - ".[0][1][2].name1[3]"
654 * - ".%" => member name is provided as parameter
655 * - ".[%]" => index is provied as parameter
656 */
657class JSON_API Path {
658public:
Christopher Dunnde5b7922016-03-06 11:19:46 -0600659 Path(const JSONCPP_STRING& path,
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000660 const PathArgument& a1 = PathArgument(),
661 const PathArgument& a2 = PathArgument(),
662 const PathArgument& a3 = PathArgument(),
663 const PathArgument& a4 = PathArgument(),
664 const PathArgument& a5 = PathArgument());
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000665
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000666 const Value& resolve(const Value& root) const;
667 Value resolve(const Value& root, const Value& defaultValue) const;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000668 /// Creates the "path" to access the specified node and returns a reference on
669 /// the node.
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000670 Value& make(Value& root) const;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000671
672private:
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000673 typedef std::vector<const PathArgument*> InArgs;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000674 typedef std::vector<PathArgument> Args;
675
Christopher Dunnde5b7922016-03-06 11:19:46 -0600676 void makePath(const JSONCPP_STRING& path, const InArgs& in);
677 void addPathInArg(const JSONCPP_STRING& path,
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000678 const InArgs& in,
679 InArgs::const_iterator& itInArg,
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000680 PathArgument::Kind kind);
Christopher Dunnde5b7922016-03-06 11:19:46 -0600681 void invalidPath(const JSONCPP_STRING& path, int location);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000682
683 Args args_;
684};
685
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000686/** \brief base class for Value iterators.
687 *
688 */
689class JSON_API ValueIteratorBase {
690public:
691 typedef std::bidirectional_iterator_tag iterator_category;
692 typedef unsigned int size_t;
693 typedef int difference_type;
694 typedef ValueIteratorBase SelfType;
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 bool operator!=(const SelfType& other) const { return !isEqual(other); }
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000699
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000700 difference_type operator-(const SelfType& other) const {
Kevin Grant4c5832a2015-02-14 20:53:35 -0800701 return other.computeDistance(*this);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000702 }
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000703
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000704 /// Return either the index or the member name of the referenced value as a
705 /// Value.
706 Value key() const;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000707
Christopher Dunned495ed2015-03-08 14:01:28 -0500708 /// Return the index of the referenced Value, or -1 if it is not an arrayValue.
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000709 UInt index() const;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000710
Christopher Dunned495ed2015-03-08 14:01:28 -0500711 /// Return the member name of the referenced Value, or "" if it is not an
712 /// objectValue.
713 /// \note Avoid `c_str()` on result, as embedded zeroes are possible.
Christopher Dunnde5b7922016-03-06 11:19:46 -0600714 JSONCPP_STRING name() const;
Christopher Dunned495ed2015-03-08 14:01:28 -0500715
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000716 /// Return the member name of the referenced Value. "" if it is not an
717 /// objectValue.
Christopher Dunnc28610f2015-02-21 11:44:16 -0600718 /// \deprecated This cannot be used for UTF-8 strings, since there can be embedded nulls.
Christopher Dunned495ed2015-03-08 14:01:28 -0500719 JSONCPP_DEPRECATED("Use `key = name();` instead.")
Christopher Dunnc28610f2015-02-21 11:44:16 -0600720 char const* memberName() const;
721 /// Return the member name of the referenced Value, or NULL if it is not an
722 /// objectValue.
Christopher Dunned495ed2015-03-08 14:01:28 -0500723 /// \note Better version than memberName(). Allows embedded nulls.
Christopher Dunnc28610f2015-02-21 11:44:16 -0600724 char const* memberName(char const** end) const;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000725
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000726protected:
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000727 Value& deref() const;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000728
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000729 void increment();
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000730
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000731 void decrement();
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000732
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000733 difference_type computeDistance(const SelfType& other) const;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000734
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000735 bool isEqual(const SelfType& other) const;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000736
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000737 void copy(const SelfType& other);
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000738
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000739private:
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000740 Value::ObjectValues::iterator current_;
741 // Indicates that iterator is for a null value.
742 bool isNull_;
Christopher Dunn2a10f4a2015-04-28 04:55:12 +0100743
744public:
745 // For some reason, BORLAND needs these at the end, rather
746 // than earlier. No idea why.
747 ValueIteratorBase();
748 explicit ValueIteratorBase(const Value::ObjectValues::iterator& current);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000749};
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000750
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000751/** \brief const iterator for object and array value.
752 *
753 */
754class JSON_API ValueConstIterator : public ValueIteratorBase {
755 friend class Value;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000756
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000757public:
758 typedef const Value value_type;
Christopher Dunnc28610f2015-02-21 11:44:16 -0600759 //typedef unsigned int size_t;
760 //typedef int difference_type;
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000761 typedef const Value& reference;
762 typedef const Value* pointer;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000763 typedef ValueConstIterator SelfType;
764
765 ValueConstIterator();
ycqiuc8a8cfc2015-10-06 16:46:19 +0800766 ValueConstIterator(ValueIterator const& other);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000767
768private:
769/*! \internal Use by Value to create an iterator.
770 */
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000771 explicit ValueConstIterator(const Value::ObjectValues::iterator& current);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000772public:
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000773 SelfType& operator=(const ValueIteratorBase& other);
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000774
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000775 SelfType operator++(int) {
776 SelfType temp(*this);
777 ++*this;
778 return temp;
779 }
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000780
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000781 SelfType operator--(int) {
782 SelfType temp(*this);
783 --*this;
784 return temp;
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 decrement();
789 return *this;
790 }
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000791
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000792 SelfType& operator++() {
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000793 increment();
794 return *this;
795 }
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000796
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000797 reference operator*() const { return deref(); }
Braden McDorman540db3b2014-09-14 02:31:23 -0500798
799 pointer operator->() const { return &deref(); }
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000800};
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000801
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000802/** \brief Iterator for object and array value.
803 */
804class JSON_API ValueIterator : public ValueIteratorBase {
805 friend class Value;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000806
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000807public:
808 typedef Value value_type;
809 typedef unsigned int size_t;
810 typedef int difference_type;
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000811 typedef Value& reference;
812 typedef Value* pointer;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000813 typedef ValueIterator SelfType;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000814
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000815 ValueIterator();
ycqiuc8a8cfc2015-10-06 16:46:19 +0800816 explicit ValueIterator(const ValueConstIterator& other);
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000817 ValueIterator(const ValueIterator& other);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000818
819private:
820/*! \internal Use by Value to create an iterator.
821 */
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000822 explicit ValueIterator(const Value::ObjectValues::iterator& current);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000823public:
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000824 SelfType& operator=(const SelfType& other);
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000825
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000826 SelfType operator++(int) {
827 SelfType temp(*this);
828 ++*this;
829 return temp;
830 }
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000831
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000832 SelfType operator--(int) {
833 SelfType temp(*this);
834 --*this;
835 return temp;
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 decrement();
840 return *this;
841 }
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000842
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000843 SelfType& operator++() {
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000844 increment();
845 return *this;
846 }
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000847
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000848 reference operator*() const { return deref(); }
Braden McDorman540db3b2014-09-14 02:31:23 -0500849
850 pointer operator->() const { return &deref(); }
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000851};
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000852
853} // namespace Json
854
datadiode9454e682015-01-20 15:25:04 -0600855
856namespace std {
857/// Specialize std::swap() for Json::Value.
858template<>
859inline void swap(Json::Value& a, Json::Value& b) { a.swap(b); }
860}
861
862
Baptiste Lepilleureafd7022013-05-08 20:21:11 +0000863#if defined(JSONCPP_DISABLE_DLL_INTERFACE_WARNING)
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000864#pragma warning(pop)
Baptiste Lepilleureafd7022013-05-08 20:21:11 +0000865#endif // if defined(JSONCPP_DISABLE_DLL_INTERFACE_WARNING)
866
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000867#endif // CPPTL_JSON_H_INCLUDED