blob: 6b4083193d16d8f7b9fe0a1343961e9b1435da33 [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:
Dhruv Paranjape8996c372017-07-08 17:27:07 +053026// a) suppress false positives from static code analysis
Gauravd97ea5b2016-03-16 11:15:09 +053027// 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
Sergiy80d6e666f2016-12-03 22:29:14 +020045#pragma pack(push, 8)
46
Christopher Dunn6d135cb2007-06-13 15:51:04 +000047/** \brief JSON (JavaScript Object Notation).
48 */
49namespace Json {
50
Christopher Dunn75279cc2015-03-08 12:20:06 -050051/** Base class for all exceptions we throw.
Christopher Dunn4e30c4f2015-03-08 12:56:32 -050052 *
53 * We use nothing but these internally. Of course, STL can throw others.
Christopher Dunn75279cc2015-03-08 12:20:06 -050054 */
Christopher Dunn949babd2015-07-23 00:19:12 -050055class JSON_API Exception : public std::exception {
56public:
Christopher Dunnde5b7922016-03-06 11:19:46 -060057 Exception(JSONCPP_STRING const& msg);
Omkar Wagh91c1d232016-11-07 13:57:00 -050058 ~Exception() JSONCPP_NOEXCEPT JSONCPP_OVERRIDE;
59 char const* what() const JSONCPP_NOEXCEPT JSONCPP_OVERRIDE;
Christopher Dunn949babd2015-07-23 00:19:12 -050060protected:
Christopher Dunnde5b7922016-03-06 11:19:46 -060061 JSONCPP_STRING msg_;
Christopher Dunn949babd2015-07-23 00:19:12 -050062};
63
Christopher Dunn53837942015-03-08 12:31:00 -050064/** Exceptions which the user cannot easily avoid.
65 *
Christopher Dunn4e30c4f2015-03-08 12:56:32 -050066 * E.g. out-of-memory (when we use malloc), stack-overflow, malicious input
Dhruv Paranjape8996c372017-07-08 17:27:07 +053067 *
Christopher Dunn4e30c4f2015-03-08 12:56:32 -050068 * \remark derived from Json::Exception
Christopher Dunn53837942015-03-08 12:31:00 -050069 */
Christopher Dunn949babd2015-07-23 00:19:12 -050070class JSON_API RuntimeError : public Exception {
71public:
Christopher Dunnde5b7922016-03-06 11:19:46 -060072 RuntimeError(JSONCPP_STRING const& msg);
Christopher Dunn949babd2015-07-23 00:19:12 -050073};
74
Christopher Dunn4e30c4f2015-03-08 12:56:32 -050075/** Exceptions thrown by JSON_ASSERT/JSON_FAIL macros.
Christopher Dunn53837942015-03-08 12:31:00 -050076 *
77 * These are precondition-violations (user bugs) and internal errors (our bugs).
Dhruv Paranjape8996c372017-07-08 17:27:07 +053078 *
Christopher Dunn4e30c4f2015-03-08 12:56:32 -050079 * \remark derived from Json::Exception
Christopher Dunn53837942015-03-08 12:31:00 -050080 */
Christopher Dunn949babd2015-07-23 00:19:12 -050081class JSON_API LogicError : public Exception {
82public:
Christopher Dunnde5b7922016-03-06 11:19:46 -060083 LogicError(JSONCPP_STRING const& msg);
Christopher Dunn949babd2015-07-23 00:19:12 -050084};
Christopher Dunn53837942015-03-08 12:31:00 -050085
Christopher Dunn4e30c4f2015-03-08 12:56:32 -050086/// used internally
Gauravd97ea5b2016-03-16 11:15:09 +053087JSONCPP_NORETURN void throwRuntimeError(JSONCPP_STRING const& msg);
Christopher Dunn4e30c4f2015-03-08 12:56:32 -050088/// used internally
Gauravd97ea5b2016-03-16 11:15:09 +053089JSONCPP_NORETURN void throwLogicError(JSONCPP_STRING const& msg);
Christopher Dunn75279cc2015-03-08 12:20:06 -050090
Aaron Jacobs9fa4e842014-07-01 08:48:54 +100091/** \brief Type of the value held by a Value object.
92 */
93enum ValueType {
94 nullValue = 0, ///< 'null' value
95 intValue, ///< signed integer value
96 uintValue, ///< unsigned integer value
97 realValue, ///< double value
98 stringValue, ///< UTF-8 string value
99 booleanValue, ///< bool value
100 arrayValue, ///< array value (ordered list)
101 objectValue ///< object value (collection of name/value pairs).
102};
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000103
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000104enum CommentPlacement {
105 commentBefore = 0, ///< a comment placed on the line before a value
106 commentAfterOnSameLine, ///< a comment just after a value on the same line
107 commentAfter, ///< a comment on the line after a value (only make sense for
Aaron Jacobs3a0c4fc2014-07-01 09:20:48 +1000108 /// root value)
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000109 numberOfCommentPlacement
110};
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000111
112//# ifdef JSON_USE_CPPTL
113// typedef CppTL::AnyEnumerator<const char *> EnumMemberNames;
114// typedef CppTL::AnyEnumerator<const Value &> EnumValues;
115//# endif
116
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000117/** \brief Lightweight wrapper to tag static string.
118 *
119 * Value constructor and objectValue member assignement takes advantage of the
120 * StaticString and avoid the cost of string duplication when storing the
121 * string or the member name.
122 *
123 * Example of usage:
124 * \code
125 * Json::Value aValue( StaticString("some text") );
126 * Json::Value object;
127 * static const StaticString code("code");
128 * object[code] = 1234;
129 * \endcode
130 */
131class JSON_API StaticString {
132public:
Christopher Dunnff617522015-03-06 10:31:46 -0600133 explicit StaticString(const char* czstring) : c_str_(czstring) {}
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000134
Christopher Dunnff617522015-03-06 10:31:46 -0600135 operator const char*() const { return c_str_; }
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000136
Christopher Dunnff617522015-03-06 10:31:46 -0600137 const char* c_str() const { return c_str_; }
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000138
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000139private:
Christopher Dunnff617522015-03-06 10:31:46 -0600140 const char* c_str_;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000141};
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000142
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000143/** \brief Represents a <a HREF="http://www.json.org">JSON</a> value.
144 *
145 * This class is a discriminated union wrapper that can represents a:
146 * - signed integer [range: Value::minInt - Value::maxInt]
147 * - unsigned integer (range: 0 - Value::maxUInt)
148 * - double
149 * - UTF-8 string
150 * - boolean
151 * - 'null'
152 * - an ordered list of Value
153 * - collection of name/value pairs (javascript object)
154 *
155 * The type of the held value is represented by a #ValueType and
156 * can be obtained using type().
157 *
Christopher Dunnc28610f2015-02-21 11:44:16 -0600158 * Values of an #objectValue or #arrayValue can be accessed using operator[]()
159 * methods.
160 * Non-const methods will automatically create the a #nullValue element
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000161 * if it does not exist.
Christopher Dunnc28610f2015-02-21 11:44:16 -0600162 * The sequence of an #arrayValue will be automatically resized and initialized
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000163 * with #nullValue. resize() can be used to enlarge or truncate an #arrayValue.
164 *
Christopher Dunnc28610f2015-02-21 11:44:16 -0600165 * The get() methods can be used to obtain default value in the case the
166 * required element does not exist.
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000167 *
168 * It is possible to iterate over the list of a #objectValue values using
169 * the getMemberNames() method.
Christopher Dunn25342ba2015-03-02 18:05:26 -0600170 *
Christopher Dunnc28610f2015-02-21 11:44:16 -0600171 * \note #Value string-length fit in size_t, but keys must be < 2^30.
172 * (The reason is an implementation detail.) A #CharReader will raise an
Christopher Dunn25342ba2015-03-02 18:05:26 -0600173 * exception if a bound is exceeded to avoid security holes in your app,
174 * but the Value API does *not* check bounds. That is the responsibility
175 * of the caller.
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000176 */
177class JSON_API Value {
178 friend class ValueIteratorBase;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000179public:
Christopher Dunnde5b7922016-03-06 11:19:46 -0600180 typedef std::vector<JSONCPP_STRING> Members;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000181 typedef ValueIterator iterator;
182 typedef ValueConstIterator const_iterator;
183 typedef Json::UInt UInt;
184 typedef Json::Int Int;
185#if defined(JSON_HAS_INT64)
186 typedef Json::UInt64 UInt64;
187 typedef Json::Int64 Int64;
Baptiste Lepilleur842e9ac2010-12-27 17:45:23 +0000188#endif // defined(JSON_HAS_INT64)
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000189 typedef Json::LargestInt LargestInt;
190 typedef Json::LargestUInt LargestUInt;
191 typedef Json::ArrayIndex ArrayIndex;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000192
Christopher Dunn8a702972015-03-03 10:38:27 -0600193 static const Value& null; ///< We regret this reference to a global instance; prefer the simpler Value().
194 static const Value& nullRef; ///< just a kludge for binary-compatibility; same as null
Christopher Dunn0f288ae2016-06-26 18:47:43 -0500195 static Value const& nullSingleton(); ///< Prefer this to null or nullRef.
196
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000197 /// Minimum signed integer value that can be stored in a Json::Value.
198 static const LargestInt minLargestInt;
199 /// Maximum signed integer value that can be stored in a Json::Value.
200 static const LargestInt maxLargestInt;
201 /// Maximum unsigned integer value that can be stored in a Json::Value.
202 static const LargestUInt maxLargestUInt;
Baptiste Lepilleur842e9ac2010-12-27 17:45:23 +0000203
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000204 /// Minimum signed int value that can be stored in a Json::Value.
205 static const Int minInt;
206 /// Maximum signed int value that can be stored in a Json::Value.
207 static const Int maxInt;
208 /// Maximum unsigned int value that can be stored in a Json::Value.
209 static const UInt maxUInt;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000210
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000211#if defined(JSON_HAS_INT64)
212 /// Minimum signed 64 bits int value that can be stored in a Json::Value.
213 static const Int64 minInt64;
214 /// Maximum signed 64 bits int value that can be stored in a Json::Value.
215 static const Int64 maxInt64;
216 /// Maximum unsigned 64 bits int value that can be stored in a Json::Value.
217 static const UInt64 maxUInt64;
Aaron Jacobsf1053e72011-05-24 03:18:02 +0000218#endif // defined(JSON_HAS_INT64)
Baptiste Lepilleur842e9ac2010-12-27 17:45:23 +0000219
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000220private:
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000221#ifndef JSONCPP_DOC_EXCLUDE_IMPLEMENTATION
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000222 class CZString {
223 public:
224 enum DuplicationPolicy {
225 noDuplication = 0,
226 duplicate,
227 duplicateOnCopy
228 };
229 CZString(ArrayIndex index);
Christopher Dunnc28610f2015-02-21 11:44:16 -0600230 CZString(char const* str, unsigned length, DuplicationPolicy allocate);
231 CZString(CZString const& other);
Motti2b008912015-04-20 17:44:47 +0300232#if JSON_HAS_RVALUE_REFERENCES
233 CZString(CZString&& other);
234#endif
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000235 ~CZString();
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000236 CZString& operator=(CZString other);
Christopher Dunnc28610f2015-02-21 11:44:16 -0600237 bool operator<(CZString const& other) const;
238 bool operator==(CZString const& other) const;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000239 ArrayIndex index() const;
Christopher Dunnc28610f2015-02-21 11:44:16 -0600240 //const char* c_str() const; ///< \deprecated
241 char const* data() const;
242 unsigned length() const;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000243 bool isStaticString() const;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000244
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000245 private:
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000246 void swap(CZString& other);
Christopher Dunnc28610f2015-02-21 11:44:16 -0600247
Christopher Dunn57ad0512015-03-02 12:10:35 -0600248 struct StringStorage {
Dan Liufcbab022015-04-27 05:20:05 +0100249 unsigned policy_: 2;
Christopher Dunn57ad0512015-03-02 12:10:35 -0600250 unsigned length_: 30; // 1GB max
251 };
252
Christopher Dunnc28610f2015-02-21 11:44:16 -0600253 char const* cstr_; // actually, a prefixed string, unless policy is noDup
Christopher Dunn57ad0512015-03-02 12:10:35 -0600254 union {
255 ArrayIndex index_;
256 StringStorage storage_;
257 };
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000258 };
259
260public:
261#ifndef JSON_USE_CPPTL_SMALLMAP
262 typedef std::map<CZString, Value> ObjectValues;
263#else
264 typedef CppTL::SmallMap<CZString, Value> ObjectValues;
265#endif // ifndef JSON_USE_CPPTL_SMALLMAP
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000266#endif // ifndef JSONCPP_DOC_EXCLUDE_IMPLEMENTATION
267
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000268public:
269 /** \brief Create a default Value of the given type.
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000270
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000271 This is a very useful constructor.
272 To create an empty array, pass arrayValue.
273 To create an empty object, pass objectValue.
274 Another Value can then be set to this one by assignment.
275This is useful since clear() and resize() will not alter types.
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000276
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000277 Examples:
278\code
279Json::Value null_value; // null
280Json::Value arr_value(Json::arrayValue); // []
281Json::Value obj_value(Json::objectValue); // {}
282\endcode
283 */
284 Value(ValueType type = nullValue);
285 Value(Int value);
286 Value(UInt value);
Baptiste Lepilleur842e9ac2010-12-27 17:45:23 +0000287#if defined(JSON_HAS_INT64)
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000288 Value(Int64 value);
289 Value(UInt64 value);
Baptiste Lepilleur842e9ac2010-12-27 17:45:23 +0000290#endif // if defined(JSON_HAS_INT64)
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000291 Value(double value);
Christopher Dunn8a702972015-03-03 10:38:27 -0600292 Value(const char* value); ///< Copy til first 0. (NULL causes to seg-fault.)
Christopher Dunn89704032015-07-11 12:09:59 -0500293 Value(const char* begin, const char* end); ///< Copy all, incl zeroes.
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000294 /** \brief Constructs a value from a static string.
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000295
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000296 * Like other value string constructor but do not duplicate the string for
297 * internal storage. The given string must remain alive after the call to this
298 * constructor.
Christopher Dunnc28610f2015-02-21 11:44:16 -0600299 * \note This works only for null-terminated strings. (We cannot change the
300 * size of this class, so we have nowhere to store the length,
301 * which might be computed later for various operations.)
302 *
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000303 * Example of usage:
304 * \code
Christopher Dunnc28610f2015-02-21 11:44:16 -0600305 * static StaticString foo("some text");
306 * Json::Value aValue(foo);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000307 * \endcode
308 */
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000309 Value(const StaticString& value);
Christopher Dunnde5b7922016-03-06 11:19:46 -0600310 Value(const JSONCPP_STRING& value); ///< Copy data() til size(). Embedded zeroes too.
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000311#ifdef JSON_USE_CPPTL
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000312 Value(const CppTL::ConstString& value);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000313#endif
314 Value(bool value);
Christopher Dunn66eb72f2015-01-20 11:02:22 -0600315 /// Deep copy.
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000316 Value(const Value& other);
Motti2b008912015-04-20 17:44:47 +0300317#if JSON_HAS_RVALUE_REFERENCES
318 /// Move constructor
319 Value(Value&& other);
320#endif
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000321 ~Value();
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000322
Christopher Dunn7f439f42015-03-06 09:22:57 -0600323 /// Deep copy, then swap(other).
324 /// \note Over-write existing comments. To preserve comments, use #swapPayload().
Dhruv Paranjape8996c372017-07-08 17:27:07 +0530325 Value& operator=(const Value& other);
326#if JSON_HAS_RVALUE_REFERENCES
327 Value& operator=(Value&& other);
328#endif
329
Christopher Dunn66eb72f2015-01-20 11:02:22 -0600330 /// Swap everything.
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000331 void swap(Value& other);
Christopher Dunn66eb72f2015-01-20 11:02:22 -0600332 /// Swap values but leave comments and source offsets in place.
333 void swapPayload(Value& other);
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000334
Dhruv Paranjape8996c372017-07-08 17:27:07 +0530335 /// copy everything.
336 void copy(const Value& other);
337 /// copy values but leave comments and source offsets in place.
338 void copyPayload(const Value& other);
339
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000340 ValueType type() const;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000341
Christopher Dunn66eb72f2015-01-20 11:02:22 -0600342 /// Compare payload only, not comments etc.
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000343 bool operator<(const Value& other) const;
344 bool operator<=(const Value& other) const;
345 bool operator>=(const Value& other) const;
346 bool operator>(const Value& other) const;
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000347 bool operator==(const Value& other) const;
348 bool operator!=(const Value& other) const;
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000349 int compare(const Value& other) const;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000350
Christopher Dunn8a702972015-03-03 10:38:27 -0600351 const char* asCString() const; ///< Embedded zeroes could cause you trouble!
dawescae564652016-03-14 19:11:02 -0500352#if JSONCPP_USING_SECURE_MEMORY
353 unsigned getCStringLength() const; //Allows you to understand the length of the CString
354#endif
Christopher Dunnde5b7922016-03-06 11:19:46 -0600355 JSONCPP_STRING asString() const; ///< Embedded zeroes are possible.
Christopher Dunnc28610f2015-02-21 11:44:16 -0600356 /** Get raw char* of string-value.
357 * \return false if !string. (Seg-fault if str or end are NULL.)
358 */
359 bool getString(
Christopher Dunn89704032015-07-11 12:09:59 -0500360 char const** begin, char const** end) const;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000361#ifdef JSON_USE_CPPTL
362 CppTL::ConstString asConstString() const;
363#endif
364 Int asInt() const;
365 UInt asUInt() const;
Aaron Jacobsf1053e72011-05-24 03:18:02 +0000366#if defined(JSON_HAS_INT64)
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000367 Int64 asInt64() const;
368 UInt64 asUInt64() const;
Aaron Jacobsf1053e72011-05-24 03:18:02 +0000369#endif // if defined(JSON_HAS_INT64)
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000370 LargestInt asLargestInt() const;
371 LargestUInt asLargestUInt() const;
372 float asFloat() const;
373 double asDouble() const;
374 bool asBool() const;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000375
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000376 bool isNull() const;
377 bool isBool() const;
378 bool isInt() const;
379 bool isInt64() const;
380 bool isUInt() const;
381 bool isUInt64() const;
382 bool isIntegral() const;
383 bool isDouble() const;
384 bool isNumeric() const;
385 bool isString() const;
386 bool isArray() const;
387 bool isObject() const;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000388
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000389 bool isConvertibleTo(ValueType other) const;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000390
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000391 /// Number of values in array or object
392 ArrayIndex size() const;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000393
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000394 /// \brief Return true if empty array, empty object, or null;
395 /// otherwise, false.
396 bool empty() const;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000397
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000398 /// Return isNull()
399 bool operator!() const;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000400
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000401 /// Remove all object members and array elements.
402 /// \pre type() is arrayValue, objectValue, or nullValue
403 /// \post type() is unchanged
404 void clear();
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000405
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000406 /// Resize the array to size elements.
407 /// New elements are initialized to null.
408 /// May only be called on nullValue or arrayValue.
409 /// \pre type() is arrayValue or nullValue
410 /// \post type() is arrayValue
411 void resize(ArrayIndex size);
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000412
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000413 /// Access an array element (zero based index ).
414 /// If the array contains less than index element, then null value are
415 /// inserted
416 /// in the array so that its size is index+1.
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 Value& operator[](ArrayIndex index);
Baptiste Lepilleurfa130ef2010-12-24 12:47:14 +0000420
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000421 /// Access an array element (zero based index ).
422 /// If the array contains less than index element, then null value are
423 /// inserted
424 /// in the array so that its size is index+1.
425 /// (You may need to say 'value[0u]' to get your compiler to distinguish
426 /// this from the operator[] which takes a string.)
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000427 Value& operator[](int index);
Baptiste Lepilleurfa130ef2010-12-24 12:47:14 +0000428
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000429 /// Access an array element (zero based index )
430 /// (You may need to say 'value[0u]' to get your compiler to distinguish
431 /// this from the operator[] which takes a string.)
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000432 const Value& operator[](ArrayIndex index) const;
Baptiste Lepilleurfa130ef2010-12-24 12:47:14 +0000433
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000434 /// Access an array element (zero based index )
435 /// (You may need to say 'value[0u]' to get your compiler to distinguish
436 /// this from the operator[] which takes a string.)
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000437 const Value& operator[](int index) const;
Baptiste Lepilleurfa130ef2010-12-24 12:47:14 +0000438
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000439 /// If the array contains at least index+1 elements, returns the element
440 /// value,
441 /// otherwise returns defaultValue.
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000442 Value get(ArrayIndex index, const Value& defaultValue) const;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000443 /// Return true if index < size().
444 bool isValidIndex(ArrayIndex index) const;
445 /// \brief Append value to array at the end.
446 ///
447 /// Equivalent to jsonvalue[jsonvalue.size()] = value;
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000448 Value& append(const Value& value);
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000449
Dhruv Paranjape23c44d92017-07-08 17:30:47 +0530450#ifdef JSON_HAS_RVALUE_REFERENCES
451 Value& append(Value&& value);
452#endif
453
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000454 /// Access an object value by name, create a null member if it does not exist.
Christopher Dunn25342ba2015-03-02 18:05:26 -0600455 /// \note Because of our implementation, keys are limited to 2^30 -1 chars.
456 /// Exceeding that will cause an exception.
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000457 Value& operator[](const char* key);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000458 /// Access an object value by name, returns null if there is no member with
459 /// that name.
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000460 const Value& operator[](const char* key) const;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000461 /// Access an object value by name, create a null member if it does not exist.
Christopher Dunn25342ba2015-03-02 18:05:26 -0600462 /// \param key may contain embedded nulls.
Christopher Dunnde5b7922016-03-06 11:19:46 -0600463 Value& operator[](const JSONCPP_STRING& key);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000464 /// Access an object value by name, returns null if there is no member with
465 /// that name.
Christopher Dunn25342ba2015-03-02 18:05:26 -0600466 /// \param key may contain embedded nulls.
Christopher Dunnde5b7922016-03-06 11:19:46 -0600467 const Value& operator[](const JSONCPP_STRING& key) const;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000468 /** \brief Access an object value by name, create a null member if it does not
469 exist.
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000470
Christopher Dunn25342ba2015-03-02 18:05:26 -0600471 * If the object has no entry for that name, then the member name used to store
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000472 * the new entry is not duplicated.
473 * Example of use:
474 * \code
475 * Json::Value object;
476 * static const StaticString code("code");
477 * object[code] = 1234;
478 * \endcode
479 */
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000480 Value& operator[](const StaticString& key);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000481#ifdef JSON_USE_CPPTL
482 /// Access an object value by name, create a null member if it does not exist.
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000483 Value& operator[](const CppTL::ConstString& key);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000484 /// Access an object value by name, returns null if there is no member with
485 /// that name.
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000486 const Value& operator[](const CppTL::ConstString& key) const;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000487#endif
488 /// Return the member named key if it exist, defaultValue otherwise.
Christopher Dunn0fd28752015-03-05 16:38:43 -0600489 /// \note deep copy
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000490 Value get(const char* key, const Value& defaultValue) const;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000491 /// Return the member named key if it exist, defaultValue otherwise.
Christopher Dunn0fd28752015-03-05 16:38:43 -0600492 /// \note deep copy
Christopher Dunn89704032015-07-11 12:09:59 -0500493 /// \note key may contain embedded nulls.
494 Value get(const char* begin, const char* end, const Value& defaultValue) const;
Christopher Dunn25342ba2015-03-02 18:05:26 -0600495 /// Return the member named key if it exist, defaultValue otherwise.
Christopher Dunn0fd28752015-03-05 16:38:43 -0600496 /// \note deep copy
Christopher Dunn25342ba2015-03-02 18:05:26 -0600497 /// \param key may contain embedded nulls.
Christopher Dunnde5b7922016-03-06 11:19:46 -0600498 Value get(const JSONCPP_STRING& key, const Value& defaultValue) const;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000499#ifdef JSON_USE_CPPTL
500 /// Return the member named key if it exist, defaultValue otherwise.
Christopher Dunn0fd28752015-03-05 16:38:43 -0600501 /// \note deep copy
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000502 Value get(const CppTL::ConstString& key, const Value& defaultValue) const;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000503#endif
Christopher Dunn25342ba2015-03-02 18:05:26 -0600504 /// Most general and efficient version of isMember()const, get()const,
505 /// and operator[]const
Christopher Dunn89704032015-07-11 12:09:59 -0500506 /// \note As stated elsewhere, behavior is undefined if (end-begin) >= 2^30
507 Value const* find(char const* begin, char const* end) const;
Christopher Dunnc28610f2015-02-21 11:44:16 -0600508 /// Most general and efficient version of object-mutators.
Christopher Dunn89704032015-07-11 12:09:59 -0500509 /// \note As stated elsewhere, behavior is undefined if (end-begin) >= 2^30
Christopher Dunnc28610f2015-02-21 11:44:16 -0600510 /// \return non-zero, but JSON_ASSERT if this is neither object nor nullValue.
Christopher Dunn89704032015-07-11 12:09:59 -0500511 Value const* demand(char const* begin, char const* end);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000512 /// \brief Remove and return the named member.
513 ///
514 /// Do nothing if it did not exist.
515 /// \return the removed Value, or null.
516 /// \pre type() is objectValue or nullValue
517 /// \post type() is unchanged
Christopher Dunn76746b02015-01-21 16:01:30 -0600518 /// \deprecated
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000519 Value removeMember(const char* key);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000520 /// Same as removeMember(const char*)
Christopher Dunn25342ba2015-03-02 18:05:26 -0600521 /// \param key may contain embedded nulls.
Christopher Dunn76746b02015-01-21 16:01:30 -0600522 /// \deprecated
Christopher Dunnde5b7922016-03-06 11:19:46 -0600523 Value removeMember(const JSONCPP_STRING& key);
Christopher Dunn89704032015-07-11 12:09:59 -0500524 /// Same as removeMember(const char* begin, const char* end, Value* removed),
Christopher Dunn25342ba2015-03-02 18:05:26 -0600525 /// but 'key' is null-terminated.
526 bool removeMember(const char* key, Value* removed);
Christopher Dunn76746b02015-01-21 16:01:30 -0600527 /** \brief Remove the named map member.
528
529 Update 'removed' iff removed.
Christopher Dunn25342ba2015-03-02 18:05:26 -0600530 \param key may contain embedded nulls.
Christopher Dunn76746b02015-01-21 16:01:30 -0600531 \return true iff removed (no exceptions)
532 */
Christopher Dunnde5b7922016-03-06 11:19:46 -0600533 bool removeMember(JSONCPP_STRING const& key, Value* removed);
534 /// Same as removeMember(JSONCPP_STRING const& key, Value* removed)
Christopher Dunn89704032015-07-11 12:09:59 -0500535 bool removeMember(const char* begin, const char* end, Value* removed);
Christopher Dunn9de2c2d2015-01-20 16:15:40 -0600536 /** \brief Remove the indexed array element.
537
538 O(n) expensive operations.
539 Update 'removed' iff removed.
Christopher Dunne87e41c2015-01-20 16:24:11 -0600540 \return true iff removed (no exceptions)
Christopher Dunn9de2c2d2015-01-20 16:15:40 -0600541 */
542 bool removeIndex(ArrayIndex i, Value* removed);
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000543
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000544 /// Return true if the object has a member named key.
Christopher Dunnc28610f2015-02-21 11:44:16 -0600545 /// \note 'key' must be null-terminated.
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000546 bool isMember(const char* key) const;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000547 /// Return true if the object has a member named key.
Christopher Dunn25342ba2015-03-02 18:05:26 -0600548 /// \param key may contain embedded nulls.
Christopher Dunnde5b7922016-03-06 11:19:46 -0600549 bool isMember(const JSONCPP_STRING& key) const;
550 /// Same as isMember(JSONCPP_STRING const& key)const
Christopher Dunn89704032015-07-11 12:09:59 -0500551 bool isMember(const char* begin, const char* end) const;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000552#ifdef JSON_USE_CPPTL
553 /// Return true if the object has a member named key.
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000554 bool isMember(const CppTL::ConstString& key) const;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000555#endif
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000556
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000557 /// \brief Return a list of the member names.
558 ///
559 /// If null, return an empty list.
560 /// \pre type() is objectValue or nullValue
561 /// \post if type() was nullValue, it remains nullValue
562 Members getMemberNames() const;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000563
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000564 //# ifdef JSON_USE_CPPTL
565 // EnumMemberNames enumMemberNames() const;
566 // EnumValues enumValues() const;
567 //# endif
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000568
Christopher Dunn1e3149a2015-01-25 14:16:13 -0600569 /// \deprecated Always pass len.
Christopher Dunnde5b7922016-03-06 11:19:46 -0600570 JSONCPP_DEPRECATED("Use setComment(JSONCPP_STRING const&) instead.")
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000571 void setComment(const char* comment, CommentPlacement placement);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000572 /// Comments must be //... or /* ... */
Christopher Dunn1e3149a2015-01-25 14:16:13 -0600573 void setComment(const char* comment, size_t len, CommentPlacement placement);
574 /// Comments must be //... or /* ... */
Christopher Dunnde5b7922016-03-06 11:19:46 -0600575 void setComment(const JSONCPP_STRING& comment, CommentPlacement placement);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000576 bool hasComment(CommentPlacement placement) const;
577 /// Include delimiters and embedded newlines.
Christopher Dunnde5b7922016-03-06 11:19:46 -0600578 JSONCPP_STRING getComment(CommentPlacement placement) const;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000579
Christopher Dunnde5b7922016-03-06 11:19:46 -0600580 JSONCPP_STRING toStyledString() const;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000581
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000582 const_iterator begin() const;
583 const_iterator end() const;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000584
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000585 iterator begin();
586 iterator end();
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000587
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000588 // Accessors for the [start, limit) range of bytes within the JSON text from
589 // which this value was parsed, if any.
Christopher Dunnd4513fc2016-02-06 09:25:20 -0600590 void setOffsetStart(ptrdiff_t start);
591 void setOffsetLimit(ptrdiff_t limit);
592 ptrdiff_t getOffsetStart() const;
593 ptrdiff_t getOffsetLimit() const;
Aaron Jacobs68db6552014-04-23 23:41:12 +0000594
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000595private:
Billy Donahue8eb5d892014-11-10 01:35:42 -0500596 void initBasic(ValueType type, bool allocated = false);
597
Christopher Dunnc28610f2015-02-21 11:44:16 -0600598 Value& resolveReference(const char* key);
599 Value& resolveReference(const char* key, const char* end);
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000600
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000601 struct CommentInfo {
602 CommentInfo();
603 ~CommentInfo();
604
Christopher Dunn1e3149a2015-01-25 14:16:13 -0600605 void setComment(const char* text, size_t len);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000606
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000607 char* comment_;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000608 };
609
610 // struct MemberNamesTransform
611 //{
612 // typedef const char *result_type;
613 // const char *operator()( const CZString &name ) const
614 // {
615 // return name.c_str();
616 // }
617 //};
618
619 union ValueHolder {
620 LargestInt int_;
621 LargestUInt uint_;
622 double real_;
623 bool bool_;
Christopher Dunnc28610f2015-02-21 11:44:16 -0600624 char* string_; // actually ptr to unsigned, followed by str, unless !allocated_
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000625 ObjectValues* map_;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000626 } value_;
627 ValueType type_ : 8;
Christopher Dunn2bc61372015-01-24 13:42:37 -0600628 unsigned int allocated_ : 1; // Notes: if declared as bool, bitfield is useless.
Christopher Dunnc28610f2015-02-21 11:44:16 -0600629 // If not allocated_, string_ must be null-terminated.
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000630 CommentInfo* comments_;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000631
632 // [start, limit) byte offsets in the source JSON text from which this Value
633 // was extracted.
Christopher Dunnd4513fc2016-02-06 09:25:20 -0600634 ptrdiff_t start_;
635 ptrdiff_t limit_;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000636};
637
638/** \brief Experimental and untested: represents an element of the "path" to
639 * access a node.
640 */
641class JSON_API PathArgument {
642public:
643 friend class Path;
644
645 PathArgument();
646 PathArgument(ArrayIndex index);
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000647 PathArgument(const char* key);
Christopher Dunnde5b7922016-03-06 11:19:46 -0600648 PathArgument(const JSONCPP_STRING& key);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000649
650private:
651 enum Kind {
652 kindNone = 0,
653 kindIndex,
654 kindKey
655 };
Christopher Dunnde5b7922016-03-06 11:19:46 -0600656 JSONCPP_STRING key_;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000657 ArrayIndex index_;
658 Kind kind_;
659};
660
661/** \brief Experimental and untested: represents a "path" to access a node.
662 *
663 * Syntax:
664 * - "." => root node
665 * - ".[n]" => elements at index 'n' of root node (an array value)
666 * - ".name" => member named 'name' of root node (an object value)
667 * - ".name1.name2.name3"
668 * - ".[0][1][2].name1[3]"
669 * - ".%" => member name is provided as parameter
670 * - ".[%]" => index is provied as parameter
671 */
672class JSON_API Path {
673public:
Christopher Dunnde5b7922016-03-06 11:19:46 -0600674 Path(const JSONCPP_STRING& path,
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000675 const PathArgument& a1 = PathArgument(),
676 const PathArgument& a2 = PathArgument(),
677 const PathArgument& a3 = PathArgument(),
678 const PathArgument& a4 = PathArgument(),
679 const PathArgument& a5 = PathArgument());
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000680
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000681 const Value& resolve(const Value& root) const;
682 Value resolve(const Value& root, const Value& defaultValue) const;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000683 /// Creates the "path" to access the specified node and returns a reference on
684 /// the node.
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000685 Value& make(Value& root) const;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000686
687private:
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000688 typedef std::vector<const PathArgument*> InArgs;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000689 typedef std::vector<PathArgument> Args;
690
Christopher Dunnde5b7922016-03-06 11:19:46 -0600691 void makePath(const JSONCPP_STRING& path, const InArgs& in);
692 void addPathInArg(const JSONCPP_STRING& path,
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000693 const InArgs& in,
694 InArgs::const_iterator& itInArg,
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000695 PathArgument::Kind kind);
Christopher Dunnde5b7922016-03-06 11:19:46 -0600696 void invalidPath(const JSONCPP_STRING& path, int location);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000697
698 Args args_;
699};
700
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000701/** \brief base class for Value iterators.
702 *
703 */
704class JSON_API ValueIteratorBase {
705public:
706 typedef std::bidirectional_iterator_tag iterator_category;
707 typedef unsigned int size_t;
708 typedef int difference_type;
709 typedef ValueIteratorBase SelfType;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000710
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000711 bool operator==(const SelfType& other) const { return isEqual(other); }
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000712
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000713 bool operator!=(const SelfType& other) const { return !isEqual(other); }
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000714
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000715 difference_type operator-(const SelfType& other) const {
Kevin Grant4c5832a2015-02-14 20:53:35 -0800716 return other.computeDistance(*this);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000717 }
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000718
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000719 /// Return either the index or the member name of the referenced value as a
720 /// Value.
721 Value key() const;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000722
Christopher Dunned495ed2015-03-08 14:01:28 -0500723 /// Return the index of the referenced Value, or -1 if it is not an arrayValue.
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000724 UInt index() const;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000725
Christopher Dunned495ed2015-03-08 14:01:28 -0500726 /// Return the member name of the referenced Value, or "" if it is not an
727 /// objectValue.
728 /// \note Avoid `c_str()` on result, as embedded zeroes are possible.
Christopher Dunnde5b7922016-03-06 11:19:46 -0600729 JSONCPP_STRING name() const;
Christopher Dunned495ed2015-03-08 14:01:28 -0500730
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000731 /// Return the member name of the referenced Value. "" if it is not an
732 /// objectValue.
Christopher Dunnc28610f2015-02-21 11:44:16 -0600733 /// \deprecated This cannot be used for UTF-8 strings, since there can be embedded nulls.
Christopher Dunned495ed2015-03-08 14:01:28 -0500734 JSONCPP_DEPRECATED("Use `key = name();` instead.")
Christopher Dunnc28610f2015-02-21 11:44:16 -0600735 char const* memberName() const;
736 /// Return the member name of the referenced Value, or NULL if it is not an
737 /// objectValue.
Christopher Dunned495ed2015-03-08 14:01:28 -0500738 /// \note Better version than memberName(). Allows embedded nulls.
Christopher Dunnc28610f2015-02-21 11:44:16 -0600739 char const* memberName(char const** end) const;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000740
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000741protected:
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000742 Value& deref() const;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000743
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000744 void increment();
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000745
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000746 void decrement();
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000747
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000748 difference_type computeDistance(const SelfType& other) const;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000749
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000750 bool isEqual(const SelfType& other) const;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000751
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000752 void copy(const SelfType& other);
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000753
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000754private:
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000755 Value::ObjectValues::iterator current_;
756 // Indicates that iterator is for a null value.
757 bool isNull_;
Christopher Dunn2a10f4a2015-04-28 04:55:12 +0100758
759public:
760 // For some reason, BORLAND needs these at the end, rather
761 // than earlier. No idea why.
762 ValueIteratorBase();
763 explicit ValueIteratorBase(const Value::ObjectValues::iterator& current);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000764};
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000765
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000766/** \brief const iterator for object and array value.
767 *
768 */
769class JSON_API ValueConstIterator : public ValueIteratorBase {
770 friend class Value;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000771
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000772public:
773 typedef const Value value_type;
Christopher Dunnc28610f2015-02-21 11:44:16 -0600774 //typedef unsigned int size_t;
775 //typedef int difference_type;
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000776 typedef const Value& reference;
777 typedef const Value* pointer;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000778 typedef ValueConstIterator SelfType;
779
780 ValueConstIterator();
ycqiuc8a8cfc2015-10-06 16:46:19 +0800781 ValueConstIterator(ValueIterator const& other);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000782
783private:
784/*! \internal Use by Value to create an iterator.
785 */
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000786 explicit ValueConstIterator(const Value::ObjectValues::iterator& current);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000787public:
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000788 SelfType& operator=(const ValueIteratorBase& other);
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000789
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000790 SelfType operator++(int) {
791 SelfType temp(*this);
792 ++*this;
793 return temp;
794 }
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000795
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000796 SelfType operator--(int) {
797 SelfType temp(*this);
798 --*this;
799 return temp;
800 }
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000801
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000802 SelfType& operator--() {
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000803 decrement();
804 return *this;
805 }
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000806
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000807 SelfType& operator++() {
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000808 increment();
809 return *this;
810 }
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000811
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000812 reference operator*() const { return deref(); }
Braden McDorman540db3b2014-09-14 02:31:23 -0500813
814 pointer operator->() const { return &deref(); }
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000815};
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000816
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000817/** \brief Iterator for object and array value.
818 */
819class JSON_API ValueIterator : public ValueIteratorBase {
820 friend class Value;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000821
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000822public:
823 typedef Value value_type;
824 typedef unsigned int size_t;
825 typedef int difference_type;
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000826 typedef Value& reference;
827 typedef Value* pointer;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000828 typedef ValueIterator SelfType;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000829
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000830 ValueIterator();
ycqiuc8a8cfc2015-10-06 16:46:19 +0800831 explicit ValueIterator(const ValueConstIterator& other);
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000832 ValueIterator(const ValueIterator& other);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000833
834private:
835/*! \internal Use by Value to create an iterator.
836 */
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000837 explicit ValueIterator(const Value::ObjectValues::iterator& current);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000838public:
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000839 SelfType& operator=(const SelfType& other);
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000840
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000841 SelfType operator++(int) {
842 SelfType temp(*this);
843 ++*this;
844 return temp;
845 }
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000846
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000847 SelfType operator--(int) {
848 SelfType temp(*this);
849 --*this;
850 return temp;
851 }
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000852
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000853 SelfType& operator--() {
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000854 decrement();
855 return *this;
856 }
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000857
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000858 SelfType& operator++() {
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000859 increment();
860 return *this;
861 }
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000862
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000863 reference operator*() const { return deref(); }
Braden McDorman540db3b2014-09-14 02:31:23 -0500864
865 pointer operator->() const { return &deref(); }
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000866};
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000867
868} // namespace Json
869
datadiode9454e682015-01-20 15:25:04 -0600870
871namespace std {
872/// Specialize std::swap() for Json::Value.
873template<>
874inline void swap(Json::Value& a, Json::Value& b) { a.swap(b); }
875}
876
Sergiy80d6e666f2016-12-03 22:29:14 +0200877#pragma pack(pop)
datadiode9454e682015-01-20 15:25:04 -0600878
Baptiste Lepilleureafd7022013-05-08 20:21:11 +0000879#if defined(JSONCPP_DISABLE_DLL_INTERFACE_WARNING)
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000880#pragma warning(pop)
Baptiste Lepilleureafd7022013-05-08 20:21:11 +0000881#endif // if defined(JSONCPP_DISABLE_DLL_INTERFACE_WARNING)
882
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000883#endif // CPPTL_JSON_H_INCLUDED