blob: 3474cd840642ae915b360f5bc4f9358af4e56cc1 [file] [log] [blame]
Devin Jeanpierre59e4d352017-07-21 03:44:36 -07001// Copyright 2007-2010 Baptiste Lepilleur and The JsonCpp Authors
Baptiste Lepilleur7469f1d2010-04-20 21:35:19 +00002// 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
Mike Ra07fc532018-03-13 23:35:31 +0300112/** \brief Type of precision for formatting of real values.
113 */
114enum PrecisionType {
115 significantDigits = 0, ///< we set max number of significant digits in string
116 decimalPlaces ///< we set max number of digits after "." in string
117};
118
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000119//# ifdef JSON_USE_CPPTL
120// typedef CppTL::AnyEnumerator<const char *> EnumMemberNames;
121// typedef CppTL::AnyEnumerator<const Value &> EnumValues;
122//# endif
123
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000124/** \brief Lightweight wrapper to tag static string.
125 *
Josh Sorefe6a588a2017-12-03 11:54:29 -0500126 * Value constructor and objectValue member assignment takes advantage of the
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000127 * StaticString and avoid the cost of string duplication when storing the
128 * string or the member name.
129 *
130 * Example of usage:
131 * \code
132 * Json::Value aValue( StaticString("some text") );
133 * Json::Value object;
134 * static const StaticString code("code");
135 * object[code] = 1234;
136 * \endcode
137 */
138class JSON_API StaticString {
139public:
Christopher Dunnff617522015-03-06 10:31:46 -0600140 explicit StaticString(const char* czstring) : c_str_(czstring) {}
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000141
Christopher Dunnff617522015-03-06 10:31:46 -0600142 operator const char*() const { return c_str_; }
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000143
Christopher Dunnff617522015-03-06 10:31:46 -0600144 const char* c_str() const { return c_str_; }
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000145
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000146private:
Christopher Dunnff617522015-03-06 10:31:46 -0600147 const char* c_str_;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000148};
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000149
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000150/** \brief Represents a <a HREF="http://www.json.org">JSON</a> value.
151 *
152 * This class is a discriminated union wrapper that can represents a:
153 * - signed integer [range: Value::minInt - Value::maxInt]
154 * - unsigned integer (range: 0 - Value::maxUInt)
155 * - double
156 * - UTF-8 string
157 * - boolean
158 * - 'null'
159 * - an ordered list of Value
160 * - collection of name/value pairs (javascript object)
161 *
162 * The type of the held value is represented by a #ValueType and
163 * can be obtained using type().
164 *
Christopher Dunnc28610f2015-02-21 11:44:16 -0600165 * Values of an #objectValue or #arrayValue can be accessed using operator[]()
166 * methods.
167 * Non-const methods will automatically create the a #nullValue element
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000168 * if it does not exist.
Christopher Dunnc28610f2015-02-21 11:44:16 -0600169 * The sequence of an #arrayValue will be automatically resized and initialized
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000170 * with #nullValue. resize() can be used to enlarge or truncate an #arrayValue.
171 *
Christopher Dunnc28610f2015-02-21 11:44:16 -0600172 * The get() methods can be used to obtain default value in the case the
173 * required element does not exist.
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000174 *
175 * It is possible to iterate over the list of a #objectValue values using
176 * the getMemberNames() method.
Christopher Dunn25342ba2015-03-02 18:05:26 -0600177 *
Christopher Dunnc28610f2015-02-21 11:44:16 -0600178 * \note #Value string-length fit in size_t, but keys must be < 2^30.
179 * (The reason is an implementation detail.) A #CharReader will raise an
Christopher Dunn25342ba2015-03-02 18:05:26 -0600180 * exception if a bound is exceeded to avoid security holes in your app,
181 * but the Value API does *not* check bounds. That is the responsibility
182 * of the caller.
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000183 */
184class JSON_API Value {
185 friend class ValueIteratorBase;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000186public:
Christopher Dunnde5b7922016-03-06 11:19:46 -0600187 typedef std::vector<JSONCPP_STRING> Members;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000188 typedef ValueIterator iterator;
189 typedef ValueConstIterator const_iterator;
190 typedef Json::UInt UInt;
191 typedef Json::Int Int;
192#if defined(JSON_HAS_INT64)
193 typedef Json::UInt64 UInt64;
194 typedef Json::Int64 Int64;
Baptiste Lepilleur842e9ac2010-12-27 17:45:23 +0000195#endif // defined(JSON_HAS_INT64)
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000196 typedef Json::LargestInt LargestInt;
197 typedef Json::LargestUInt LargestUInt;
198 typedef Json::ArrayIndex ArrayIndex;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000199
Wolfram Röslerff6b4492017-09-14 09:31:36 +0200200 // Required for boost integration, e. g. BOOST_TEST
201 typedef std::string value_type;
202
Christopher Dunn8a702972015-03-03 10:38:27 -0600203 static const Value& null; ///< We regret this reference to a global instance; prefer the simpler Value().
204 static const Value& nullRef; ///< just a kludge for binary-compatibility; same as null
Christopher Dunn0f288ae2016-06-26 18:47:43 -0500205 static Value const& nullSingleton(); ///< Prefer this to null or nullRef.
206
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000207 /// Minimum signed integer value that can be stored in a Json::Value.
208 static const LargestInt minLargestInt;
209 /// Maximum signed integer value that can be stored in a Json::Value.
210 static const LargestInt maxLargestInt;
211 /// Maximum unsigned integer value that can be stored in a Json::Value.
212 static const LargestUInt maxLargestUInt;
Baptiste Lepilleur842e9ac2010-12-27 17:45:23 +0000213
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000214 /// Minimum signed int value that can be stored in a Json::Value.
215 static const Int minInt;
216 /// Maximum signed int value that can be stored in a Json::Value.
217 static const Int maxInt;
218 /// Maximum unsigned int value that can be stored in a Json::Value.
219 static const UInt maxUInt;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000220
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000221#if defined(JSON_HAS_INT64)
222 /// Minimum signed 64 bits int value that can be stored in a Json::Value.
223 static const Int64 minInt64;
224 /// Maximum signed 64 bits int value that can be stored in a Json::Value.
225 static const Int64 maxInt64;
226 /// Maximum unsigned 64 bits int value that can be stored in a Json::Value.
227 static const UInt64 maxUInt64;
Aaron Jacobsf1053e72011-05-24 03:18:02 +0000228#endif // defined(JSON_HAS_INT64)
Baptiste Lepilleur842e9ac2010-12-27 17:45:23 +0000229
Mike Ra07fc532018-03-13 23:35:31 +0300230 /// Default precision for real value for string representation.
231 static const UInt defaultRealPrecision;
232
Darcy Beurle798f6ba2017-12-22 22:48:20 +0100233// Workaround for bug in the NVIDIAs CUDA 9.1 nvcc compiler
234// when using gcc and clang backend compilers. CZString
235// cannot be defined as private. See issue #486
236#ifdef __NVCC__
237public:
238#else
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000239private:
Darcy Beurle798f6ba2017-12-22 22:48:20 +0100240#endif
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000241#ifndef JSONCPP_DOC_EXCLUDE_IMPLEMENTATION
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000242 class CZString {
243 public:
244 enum DuplicationPolicy {
245 noDuplication = 0,
246 duplicate,
247 duplicateOnCopy
248 };
249 CZString(ArrayIndex index);
Christopher Dunnc28610f2015-02-21 11:44:16 -0600250 CZString(char const* str, unsigned length, DuplicationPolicy allocate);
251 CZString(CZString const& other);
Motti2b008912015-04-20 17:44:47 +0300252#if JSON_HAS_RVALUE_REFERENCES
253 CZString(CZString&& other);
254#endif
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000255 ~CZString();
Dhruv Paranjape0ba8bd72017-07-08 17:47:13 +0530256 CZString& operator=(const CZString& other);
257
258#if JSON_HAS_RVALUE_REFERENCES
259 CZString& operator=(CZString&& other);
260#endif
261
Christopher Dunnc28610f2015-02-21 11:44:16 -0600262 bool operator<(CZString const& other) const;
263 bool operator==(CZString const& other) const;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000264 ArrayIndex index() const;
Christopher Dunnc28610f2015-02-21 11:44:16 -0600265 //const char* c_str() const; ///< \deprecated
266 char const* data() const;
267 unsigned length() const;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000268 bool isStaticString() const;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000269
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000270 private:
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000271 void swap(CZString& other);
Christopher Dunnc28610f2015-02-21 11:44:16 -0600272
Christopher Dunn57ad0512015-03-02 12:10:35 -0600273 struct StringStorage {
Dan Liufcbab022015-04-27 05:20:05 +0100274 unsigned policy_: 2;
Christopher Dunn57ad0512015-03-02 12:10:35 -0600275 unsigned length_: 30; // 1GB max
276 };
277
Christopher Dunnc28610f2015-02-21 11:44:16 -0600278 char const* cstr_; // actually, a prefixed string, unless policy is noDup
Christopher Dunn57ad0512015-03-02 12:10:35 -0600279 union {
280 ArrayIndex index_;
281 StringStorage storage_;
282 };
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000283 };
284
285public:
286#ifndef JSON_USE_CPPTL_SMALLMAP
287 typedef std::map<CZString, Value> ObjectValues;
288#else
289 typedef CppTL::SmallMap<CZString, Value> ObjectValues;
290#endif // ifndef JSON_USE_CPPTL_SMALLMAP
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000291#endif // ifndef JSONCPP_DOC_EXCLUDE_IMPLEMENTATION
292
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000293public:
294 /** \brief Create a default Value of the given type.
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000295
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000296 This is a very useful constructor.
297 To create an empty array, pass arrayValue.
298 To create an empty object, pass objectValue.
299 Another Value can then be set to this one by assignment.
300This is useful since clear() and resize() will not alter types.
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000301
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000302 Examples:
303\code
304Json::Value null_value; // null
305Json::Value arr_value(Json::arrayValue); // []
306Json::Value obj_value(Json::objectValue); // {}
307\endcode
308 */
309 Value(ValueType type = nullValue);
310 Value(Int value);
311 Value(UInt value);
Baptiste Lepilleur842e9ac2010-12-27 17:45:23 +0000312#if defined(JSON_HAS_INT64)
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000313 Value(Int64 value);
314 Value(UInt64 value);
Baptiste Lepilleur842e9ac2010-12-27 17:45:23 +0000315#endif // if defined(JSON_HAS_INT64)
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000316 Value(double value);
Christopher Dunn8a702972015-03-03 10:38:27 -0600317 Value(const char* value); ///< Copy til first 0. (NULL causes to seg-fault.)
Christopher Dunn89704032015-07-11 12:09:59 -0500318 Value(const char* begin, const char* end); ///< Copy all, incl zeroes.
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000319 /** \brief Constructs a value from a static string.
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000320
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000321 * Like other value string constructor but do not duplicate the string for
322 * internal storage. The given string must remain alive after the call to this
323 * constructor.
Christopher Dunnc28610f2015-02-21 11:44:16 -0600324 * \note This works only for null-terminated strings. (We cannot change the
325 * size of this class, so we have nowhere to store the length,
326 * which might be computed later for various operations.)
327 *
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000328 * Example of usage:
329 * \code
Christopher Dunnc28610f2015-02-21 11:44:16 -0600330 * static StaticString foo("some text");
331 * Json::Value aValue(foo);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000332 * \endcode
333 */
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000334 Value(const StaticString& value);
Christopher Dunnde5b7922016-03-06 11:19:46 -0600335 Value(const JSONCPP_STRING& value); ///< Copy data() til size(). Embedded zeroes too.
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000336#ifdef JSON_USE_CPPTL
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000337 Value(const CppTL::ConstString& value);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000338#endif
339 Value(bool value);
Christopher Dunn66eb72f2015-01-20 11:02:22 -0600340 /// Deep copy.
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000341 Value(const Value& other);
Motti2b008912015-04-20 17:44:47 +0300342#if JSON_HAS_RVALUE_REFERENCES
343 /// Move constructor
344 Value(Value&& other);
345#endif
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000346 ~Value();
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000347
Christopher Dunn7f439f42015-03-06 09:22:57 -0600348 /// Deep copy, then swap(other).
349 /// \note Over-write existing comments. To preserve comments, use #swapPayload().
Александр Малинин6a15ca62017-07-31 15:29:02 +0300350 Value& operator=(Value other);
Dhruv Paranjape8996c372017-07-08 17:27:07 +0530351
Christopher Dunn66eb72f2015-01-20 11:02:22 -0600352 /// Swap everything.
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000353 void swap(Value& other);
Christopher Dunn66eb72f2015-01-20 11:02:22 -0600354 /// Swap values but leave comments and source offsets in place.
355 void swapPayload(Value& other);
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000356
Dhruv Paranjape8996c372017-07-08 17:27:07 +0530357 /// copy everything.
358 void copy(const Value& other);
359 /// copy values but leave comments and source offsets in place.
360 void copyPayload(const Value& other);
361
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000362 ValueType type() const;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000363
Christopher Dunn66eb72f2015-01-20 11:02:22 -0600364 /// Compare payload only, not comments etc.
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000365 bool operator<(const Value& other) const;
366 bool operator<=(const Value& other) const;
367 bool operator>=(const Value& other) const;
368 bool operator>(const Value& other) const;
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000369 bool operator==(const Value& other) const;
370 bool operator!=(const Value& other) const;
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000371 int compare(const Value& other) const;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000372
Christopher Dunn8a702972015-03-03 10:38:27 -0600373 const char* asCString() const; ///< Embedded zeroes could cause you trouble!
dawescae564652016-03-14 19:11:02 -0500374#if JSONCPP_USING_SECURE_MEMORY
375 unsigned getCStringLength() const; //Allows you to understand the length of the CString
376#endif
Christopher Dunnde5b7922016-03-06 11:19:46 -0600377 JSONCPP_STRING asString() const; ///< Embedded zeroes are possible.
Christopher Dunnc28610f2015-02-21 11:44:16 -0600378 /** Get raw char* of string-value.
379 * \return false if !string. (Seg-fault if str or end are NULL.)
380 */
381 bool getString(
Christopher Dunn89704032015-07-11 12:09:59 -0500382 char const** begin, char const** end) const;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000383#ifdef JSON_USE_CPPTL
384 CppTL::ConstString asConstString() const;
385#endif
386 Int asInt() const;
387 UInt asUInt() const;
Aaron Jacobsf1053e72011-05-24 03:18:02 +0000388#if defined(JSON_HAS_INT64)
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000389 Int64 asInt64() const;
390 UInt64 asUInt64() const;
Aaron Jacobsf1053e72011-05-24 03:18:02 +0000391#endif // if defined(JSON_HAS_INT64)
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000392 LargestInt asLargestInt() const;
393 LargestUInt asLargestUInt() const;
394 float asFloat() const;
395 double asDouble() const;
396 bool asBool() const;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000397
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000398 bool isNull() const;
399 bool isBool() const;
400 bool isInt() const;
401 bool isInt64() const;
402 bool isUInt() const;
403 bool isUInt64() const;
404 bool isIntegral() const;
405 bool isDouble() const;
406 bool isNumeric() const;
407 bool isString() const;
408 bool isArray() const;
409 bool isObject() const;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000410
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000411 bool isConvertibleTo(ValueType other) const;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000412
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000413 /// Number of values in array or object
414 ArrayIndex size() const;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000415
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000416 /// \brief Return true if empty array, empty object, or null;
417 /// otherwise, false.
418 bool empty() const;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000419
Wolfram Rösler90794222017-12-05 18:18:55 +0100420 /// Return !isNull()
drgler04abe382018-01-13 15:28:19 +0100421 JSONCPP_OP_EXPLICIT operator bool() const;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000422
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000423 /// Remove all object members and array elements.
424 /// \pre type() is arrayValue, objectValue, or nullValue
425 /// \post type() is unchanged
426 void clear();
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000427
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000428 /// Resize the array to size elements.
429 /// New elements are initialized to null.
430 /// May only be called on nullValue or arrayValue.
431 /// \pre type() is arrayValue or nullValue
432 /// \post type() is arrayValue
433 void resize(ArrayIndex size);
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000434
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000435 /// Access an array element (zero based index ).
436 /// If the array contains less than index element, then null value are
437 /// inserted
438 /// in the array so that its size is index+1.
439 /// (You may need to say 'value[0u]' to get your compiler to distinguish
440 /// this from the operator[] which takes a string.)
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000441 Value& operator[](ArrayIndex index);
Baptiste Lepilleurfa130ef2010-12-24 12:47:14 +0000442
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000443 /// Access an array element (zero based index ).
444 /// If the array contains less than index element, then null value are
445 /// inserted
446 /// in the array so that its size is index+1.
447 /// (You may need to say 'value[0u]' to get your compiler to distinguish
448 /// this from the operator[] which takes a string.)
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000449 Value& operator[](int index);
Baptiste Lepilleurfa130ef2010-12-24 12:47:14 +0000450
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000451 /// Access an array element (zero based index )
452 /// (You may need to say 'value[0u]' to get your compiler to distinguish
453 /// this from the operator[] which takes a string.)
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000454 const Value& operator[](ArrayIndex index) const;
Baptiste Lepilleurfa130ef2010-12-24 12:47:14 +0000455
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000456 /// Access an array element (zero based index )
457 /// (You may need to say 'value[0u]' to get your compiler to distinguish
458 /// this from the operator[] which takes a string.)
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000459 const Value& operator[](int index) const;
Baptiste Lepilleurfa130ef2010-12-24 12:47:14 +0000460
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000461 /// If the array contains at least index+1 elements, returns the element
462 /// value,
463 /// otherwise returns defaultValue.
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000464 Value get(ArrayIndex index, const Value& defaultValue) const;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000465 /// Return true if index < size().
466 bool isValidIndex(ArrayIndex index) const;
467 /// \brief Append value to array at the end.
468 ///
469 /// Equivalent to jsonvalue[jsonvalue.size()] = value;
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000470 Value& append(const Value& value);
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000471
Dhruv Paranjape0ba8bd72017-07-08 17:47:13 +0530472#if JSON_HAS_RVALUE_REFERENCES
Dhruv Paranjape23c44d92017-07-08 17:30:47 +0530473 Value& append(Value&& value);
474#endif
475
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000476 /// Access an object value by name, create a null member if it does not exist.
Christopher Dunn25342ba2015-03-02 18:05:26 -0600477 /// \note Because of our implementation, keys are limited to 2^30 -1 chars.
478 /// Exceeding that will cause an exception.
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000479 Value& operator[](const char* key);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000480 /// Access an object value by name, returns null if there is no member with
481 /// that name.
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000482 const Value& operator[](const char* key) const;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000483 /// Access an object value by name, create a null member if it does not exist.
Christopher Dunn25342ba2015-03-02 18:05:26 -0600484 /// \param key may contain embedded nulls.
Christopher Dunnde5b7922016-03-06 11:19:46 -0600485 Value& operator[](const JSONCPP_STRING& key);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000486 /// Access an object value by name, returns null if there is no member with
487 /// that name.
Christopher Dunn25342ba2015-03-02 18:05:26 -0600488 /// \param key may contain embedded nulls.
Christopher Dunnde5b7922016-03-06 11:19:46 -0600489 const Value& operator[](const JSONCPP_STRING& key) const;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000490 /** \brief Access an object value by name, create a null member if it does not
491 exist.
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000492
Christopher Dunn25342ba2015-03-02 18:05:26 -0600493 * If the object has no entry for that name, then the member name used to store
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000494 * the new entry is not duplicated.
495 * Example of use:
496 * \code
497 * Json::Value object;
498 * static const StaticString code("code");
499 * object[code] = 1234;
500 * \endcode
501 */
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000502 Value& operator[](const StaticString& key);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000503#ifdef JSON_USE_CPPTL
504 /// Access an object value by name, create a null member if it does not exist.
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000505 Value& operator[](const CppTL::ConstString& key);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000506 /// Access an object value by name, returns null if there is no member with
507 /// that name.
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000508 const Value& operator[](const CppTL::ConstString& key) const;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000509#endif
510 /// Return the member named key if it exist, defaultValue otherwise.
Christopher Dunn0fd28752015-03-05 16:38:43 -0600511 /// \note deep copy
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000512 Value get(const char* key, const Value& defaultValue) const;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000513 /// Return the member named key if it exist, defaultValue otherwise.
Christopher Dunn0fd28752015-03-05 16:38:43 -0600514 /// \note deep copy
Christopher Dunn89704032015-07-11 12:09:59 -0500515 /// \note key may contain embedded nulls.
516 Value get(const char* begin, const char* end, const Value& defaultValue) const;
Christopher Dunn25342ba2015-03-02 18:05:26 -0600517 /// Return the member named key if it exist, defaultValue otherwise.
Christopher Dunn0fd28752015-03-05 16:38:43 -0600518 /// \note deep copy
Christopher Dunn25342ba2015-03-02 18:05:26 -0600519 /// \param key may contain embedded nulls.
Christopher Dunnde5b7922016-03-06 11:19:46 -0600520 Value get(const JSONCPP_STRING& key, const Value& defaultValue) const;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000521#ifdef JSON_USE_CPPTL
522 /// Return the member named key if it exist, defaultValue otherwise.
Christopher Dunn0fd28752015-03-05 16:38:43 -0600523 /// \note deep copy
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000524 Value get(const CppTL::ConstString& key, const Value& defaultValue) const;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000525#endif
Christopher Dunn25342ba2015-03-02 18:05:26 -0600526 /// Most general and efficient version of isMember()const, get()const,
527 /// and operator[]const
Christopher Dunn89704032015-07-11 12:09:59 -0500528 /// \note As stated elsewhere, behavior is undefined if (end-begin) >= 2^30
529 Value const* find(char const* begin, char const* end) const;
Christopher Dunnc28610f2015-02-21 11:44:16 -0600530 /// Most general and efficient version of object-mutators.
Christopher Dunn89704032015-07-11 12:09:59 -0500531 /// \note As stated elsewhere, behavior is undefined if (end-begin) >= 2^30
Christopher Dunnc28610f2015-02-21 11:44:16 -0600532 /// \return non-zero, but JSON_ASSERT if this is neither object nor nullValue.
Christopher Dunn89704032015-07-11 12:09:59 -0500533 Value const* demand(char const* begin, char const* end);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000534 /// \brief Remove and return the named member.
535 ///
536 /// Do nothing if it did not exist.
537 /// \return the removed Value, or null.
538 /// \pre type() is objectValue or nullValue
539 /// \post type() is unchanged
Christopher Dunn76746b02015-01-21 16:01:30 -0600540 /// \deprecated
Wolfram Röslera06b3902017-10-18 07:19:27 +0200541 void removeMember(const char* key);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000542 /// Same as removeMember(const char*)
Christopher Dunn25342ba2015-03-02 18:05:26 -0600543 /// \param key may contain embedded nulls.
Christopher Dunn76746b02015-01-21 16:01:30 -0600544 /// \deprecated
Wolfram Röslera06b3902017-10-18 07:19:27 +0200545 void removeMember(const JSONCPP_STRING& key);
Christopher Dunn89704032015-07-11 12:09:59 -0500546 /// Same as removeMember(const char* begin, const char* end, Value* removed),
Christopher Dunn25342ba2015-03-02 18:05:26 -0600547 /// but 'key' is null-terminated.
548 bool removeMember(const char* key, Value* removed);
Christopher Dunn76746b02015-01-21 16:01:30 -0600549 /** \brief Remove the named map member.
550
551 Update 'removed' iff removed.
Christopher Dunn25342ba2015-03-02 18:05:26 -0600552 \param key may contain embedded nulls.
Christopher Dunn76746b02015-01-21 16:01:30 -0600553 \return true iff removed (no exceptions)
554 */
Christopher Dunnde5b7922016-03-06 11:19:46 -0600555 bool removeMember(JSONCPP_STRING const& key, Value* removed);
556 /// Same as removeMember(JSONCPP_STRING const& key, Value* removed)
Christopher Dunn89704032015-07-11 12:09:59 -0500557 bool removeMember(const char* begin, const char* end, Value* removed);
Christopher Dunn9de2c2d2015-01-20 16:15:40 -0600558 /** \brief Remove the indexed array element.
559
560 O(n) expensive operations.
561 Update 'removed' iff removed.
Christopher Dunne87e41c2015-01-20 16:24:11 -0600562 \return true iff removed (no exceptions)
Christopher Dunn9de2c2d2015-01-20 16:15:40 -0600563 */
564 bool removeIndex(ArrayIndex i, Value* removed);
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000565
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000566 /// Return true if the object has a member named key.
Christopher Dunnc28610f2015-02-21 11:44:16 -0600567 /// \note 'key' must be null-terminated.
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000568 bool isMember(const char* key) const;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000569 /// Return true if the object has a member named key.
Christopher Dunn25342ba2015-03-02 18:05:26 -0600570 /// \param key may contain embedded nulls.
Christopher Dunnde5b7922016-03-06 11:19:46 -0600571 bool isMember(const JSONCPP_STRING& key) const;
572 /// Same as isMember(JSONCPP_STRING const& key)const
Christopher Dunn89704032015-07-11 12:09:59 -0500573 bool isMember(const char* begin, const char* end) const;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000574#ifdef JSON_USE_CPPTL
575 /// Return true if the object has a member named key.
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000576 bool isMember(const CppTL::ConstString& key) const;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000577#endif
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000578
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000579 /// \brief Return a list of the member names.
580 ///
581 /// If null, return an empty list.
582 /// \pre type() is objectValue or nullValue
583 /// \post if type() was nullValue, it remains nullValue
584 Members getMemberNames() const;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000585
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000586 //# ifdef JSON_USE_CPPTL
587 // EnumMemberNames enumMemberNames() const;
588 // EnumValues enumValues() const;
589 //# endif
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000590
Christopher Dunn1e3149a2015-01-25 14:16:13 -0600591 /// \deprecated Always pass len.
Christopher Dunnde5b7922016-03-06 11:19:46 -0600592 JSONCPP_DEPRECATED("Use setComment(JSONCPP_STRING const&) instead.")
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000593 void setComment(const char* comment, CommentPlacement placement);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000594 /// Comments must be //... or /* ... */
Christopher Dunn1e3149a2015-01-25 14:16:13 -0600595 void setComment(const char* comment, size_t len, CommentPlacement placement);
596 /// Comments must be //... or /* ... */
Christopher Dunnde5b7922016-03-06 11:19:46 -0600597 void setComment(const JSONCPP_STRING& comment, CommentPlacement placement);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000598 bool hasComment(CommentPlacement placement) const;
599 /// Include delimiters and embedded newlines.
Christopher Dunnde5b7922016-03-06 11:19:46 -0600600 JSONCPP_STRING getComment(CommentPlacement placement) const;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000601
Christopher Dunnde5b7922016-03-06 11:19:46 -0600602 JSONCPP_STRING toStyledString() const;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000603
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000604 const_iterator begin() const;
605 const_iterator end() const;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000606
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000607 iterator begin();
608 iterator end();
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000609
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000610 // Accessors for the [start, limit) range of bytes within the JSON text from
611 // which this value was parsed, if any.
Christopher Dunnd4513fc2016-02-06 09:25:20 -0600612 void setOffsetStart(ptrdiff_t start);
613 void setOffsetLimit(ptrdiff_t limit);
614 ptrdiff_t getOffsetStart() const;
615 ptrdiff_t getOffsetLimit() const;
Aaron Jacobs68db6552014-04-23 23:41:12 +0000616
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000617private:
Billy Donahue8eb5d892014-11-10 01:35:42 -0500618 void initBasic(ValueType type, bool allocated = false);
Andrey Okoshkin9b569c82018-01-12 15:59:20 +0300619 void dupPayload(const Value& other);
Andrey Okoshkinc69148c2018-01-12 11:26:34 +0300620 void releasePayload();
Andrey Okoshkin9b569c82018-01-12 15:59:20 +0300621 void dupMeta(const Value& other);
Billy Donahue8eb5d892014-11-10 01:35:42 -0500622
Christopher Dunnc28610f2015-02-21 11:44:16 -0600623 Value& resolveReference(const char* key);
624 Value& resolveReference(const char* key, const char* end);
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000625
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000626 struct CommentInfo {
627 CommentInfo();
628 ~CommentInfo();
629
Christopher Dunn1e3149a2015-01-25 14:16:13 -0600630 void setComment(const char* text, size_t len);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000631
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000632 char* comment_;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000633 };
634
635 // struct MemberNamesTransform
636 //{
637 // typedef const char *result_type;
638 // const char *operator()( const CZString &name ) const
639 // {
640 // return name.c_str();
641 // }
642 //};
643
644 union ValueHolder {
645 LargestInt int_;
646 LargestUInt uint_;
647 double real_;
648 bool bool_;
Christopher Dunnc28610f2015-02-21 11:44:16 -0600649 char* string_; // actually ptr to unsigned, followed by str, unless !allocated_
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000650 ObjectValues* map_;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000651 } value_;
652 ValueType type_ : 8;
Christopher Dunn2bc61372015-01-24 13:42:37 -0600653 unsigned int allocated_ : 1; // Notes: if declared as bool, bitfield is useless.
Christopher Dunnc28610f2015-02-21 11:44:16 -0600654 // If not allocated_, string_ must be null-terminated.
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000655 CommentInfo* comments_;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000656
657 // [start, limit) byte offsets in the source JSON text from which this Value
658 // was extracted.
Christopher Dunnd4513fc2016-02-06 09:25:20 -0600659 ptrdiff_t start_;
660 ptrdiff_t limit_;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000661};
662
663/** \brief Experimental and untested: represents an element of the "path" to
664 * access a node.
665 */
666class JSON_API PathArgument {
667public:
668 friend class Path;
669
670 PathArgument();
671 PathArgument(ArrayIndex index);
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000672 PathArgument(const char* key);
Christopher Dunnde5b7922016-03-06 11:19:46 -0600673 PathArgument(const JSONCPP_STRING& key);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000674
675private:
676 enum Kind {
677 kindNone = 0,
678 kindIndex,
679 kindKey
680 };
Christopher Dunnde5b7922016-03-06 11:19:46 -0600681 JSONCPP_STRING key_;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000682 ArrayIndex index_;
683 Kind kind_;
684};
685
686/** \brief Experimental and untested: represents a "path" to access a node.
687 *
688 * Syntax:
689 * - "." => root node
690 * - ".[n]" => elements at index 'n' of root node (an array value)
691 * - ".name" => member named 'name' of root node (an object value)
692 * - ".name1.name2.name3"
693 * - ".[0][1][2].name1[3]"
694 * - ".%" => member name is provided as parameter
695 * - ".[%]" => index is provied as parameter
696 */
697class JSON_API Path {
698public:
Christopher Dunnde5b7922016-03-06 11:19:46 -0600699 Path(const JSONCPP_STRING& path,
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000700 const PathArgument& a1 = PathArgument(),
701 const PathArgument& a2 = PathArgument(),
702 const PathArgument& a3 = PathArgument(),
703 const PathArgument& a4 = PathArgument(),
704 const PathArgument& a5 = PathArgument());
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000705
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000706 const Value& resolve(const Value& root) const;
707 Value resolve(const Value& root, const Value& defaultValue) const;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000708 /// Creates the "path" to access the specified node and returns a reference on
709 /// the node.
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000710 Value& make(Value& root) const;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000711
712private:
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000713 typedef std::vector<const PathArgument*> InArgs;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000714 typedef std::vector<PathArgument> Args;
715
Christopher Dunnde5b7922016-03-06 11:19:46 -0600716 void makePath(const JSONCPP_STRING& path, const InArgs& in);
717 void addPathInArg(const JSONCPP_STRING& path,
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000718 const InArgs& in,
719 InArgs::const_iterator& itInArg,
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000720 PathArgument::Kind kind);
Christopher Dunnde5b7922016-03-06 11:19:46 -0600721 void invalidPath(const JSONCPP_STRING& path, int location);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000722
723 Args args_;
724};
725
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000726/** \brief base class for Value iterators.
727 *
728 */
729class JSON_API ValueIteratorBase {
730public:
731 typedef std::bidirectional_iterator_tag iterator_category;
732 typedef unsigned int size_t;
733 typedef int difference_type;
734 typedef ValueIteratorBase SelfType;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000735
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000736 bool operator==(const SelfType& other) const { return isEqual(other); }
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000737
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000738 bool operator!=(const SelfType& other) const { return !isEqual(other); }
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000739
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000740 difference_type operator-(const SelfType& other) const {
Kevin Grant4c5832a2015-02-14 20:53:35 -0800741 return other.computeDistance(*this);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000742 }
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000743
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000744 /// Return either the index or the member name of the referenced value as a
745 /// Value.
746 Value key() const;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000747
Christopher Dunned495ed2015-03-08 14:01:28 -0500748 /// Return the index of the referenced Value, or -1 if it is not an arrayValue.
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000749 UInt index() const;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000750
Christopher Dunned495ed2015-03-08 14:01:28 -0500751 /// Return the member name of the referenced Value, or "" if it is not an
752 /// objectValue.
753 /// \note Avoid `c_str()` on result, as embedded zeroes are possible.
Christopher Dunnde5b7922016-03-06 11:19:46 -0600754 JSONCPP_STRING name() const;
Christopher Dunned495ed2015-03-08 14:01:28 -0500755
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000756 /// Return the member name of the referenced Value. "" if it is not an
757 /// objectValue.
Christopher Dunnc28610f2015-02-21 11:44:16 -0600758 /// \deprecated This cannot be used for UTF-8 strings, since there can be embedded nulls.
Christopher Dunned495ed2015-03-08 14:01:28 -0500759 JSONCPP_DEPRECATED("Use `key = name();` instead.")
Christopher Dunnc28610f2015-02-21 11:44:16 -0600760 char const* memberName() const;
761 /// Return the member name of the referenced Value, or NULL if it is not an
762 /// objectValue.
Christopher Dunned495ed2015-03-08 14:01:28 -0500763 /// \note Better version than memberName(). Allows embedded nulls.
Christopher Dunnc28610f2015-02-21 11:44:16 -0600764 char const* memberName(char const** end) const;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000765
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000766protected:
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000767 Value& deref() const;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000768
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000769 void increment();
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000770
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000771 void decrement();
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000772
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000773 difference_type computeDistance(const SelfType& other) const;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000774
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000775 bool isEqual(const SelfType& other) const;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000776
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000777 void copy(const SelfType& other);
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000778
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000779private:
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000780 Value::ObjectValues::iterator current_;
781 // Indicates that iterator is for a null value.
782 bool isNull_;
Christopher Dunn2a10f4a2015-04-28 04:55:12 +0100783
784public:
785 // For some reason, BORLAND needs these at the end, rather
786 // than earlier. No idea why.
787 ValueIteratorBase();
788 explicit ValueIteratorBase(const Value::ObjectValues::iterator& current);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000789};
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000790
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000791/** \brief const iterator for object and array value.
792 *
793 */
794class JSON_API ValueConstIterator : public ValueIteratorBase {
795 friend class Value;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000796
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000797public:
798 typedef const Value value_type;
Christopher Dunnc28610f2015-02-21 11:44:16 -0600799 //typedef unsigned int size_t;
800 //typedef int difference_type;
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000801 typedef const Value& reference;
802 typedef const Value* pointer;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000803 typedef ValueConstIterator SelfType;
804
805 ValueConstIterator();
ycqiuc8a8cfc2015-10-06 16:46:19 +0800806 ValueConstIterator(ValueIterator const& other);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000807
808private:
809/*! \internal Use by Value to create an iterator.
810 */
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000811 explicit ValueConstIterator(const Value::ObjectValues::iterator& current);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000812public:
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000813 SelfType& operator=(const ValueIteratorBase& other);
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000814
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000815 SelfType operator++(int) {
816 SelfType temp(*this);
817 ++*this;
818 return temp;
819 }
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000820
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000821 SelfType operator--(int) {
822 SelfType temp(*this);
823 --*this;
824 return temp;
825 }
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000826
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000827 SelfType& operator--() {
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000828 decrement();
829 return *this;
830 }
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000831
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000832 SelfType& operator++() {
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000833 increment();
834 return *this;
835 }
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000836
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000837 reference operator*() const { return deref(); }
Braden McDorman540db3b2014-09-14 02:31:23 -0500838
839 pointer operator->() const { return &deref(); }
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000840};
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000841
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000842/** \brief Iterator for object and array value.
843 */
844class JSON_API ValueIterator : public ValueIteratorBase {
845 friend class Value;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000846
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000847public:
848 typedef Value value_type;
849 typedef unsigned int size_t;
850 typedef int difference_type;
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000851 typedef Value& reference;
852 typedef Value* pointer;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000853 typedef ValueIterator SelfType;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000854
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000855 ValueIterator();
ycqiuc8a8cfc2015-10-06 16:46:19 +0800856 explicit ValueIterator(const ValueConstIterator& other);
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000857 ValueIterator(const ValueIterator& other);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000858
859private:
860/*! \internal Use by Value to create an iterator.
861 */
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000862 explicit ValueIterator(const Value::ObjectValues::iterator& current);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000863public:
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000864 SelfType& operator=(const SelfType& other);
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000865
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000866 SelfType operator++(int) {
867 SelfType temp(*this);
868 ++*this;
869 return temp;
870 }
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000871
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000872 SelfType operator--(int) {
873 SelfType temp(*this);
874 --*this;
875 return temp;
876 }
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000877
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000878 SelfType& operator--() {
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000879 decrement();
880 return *this;
881 }
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000882
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000883 SelfType& operator++() {
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000884 increment();
885 return *this;
886 }
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000887
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000888 reference operator*() const { return deref(); }
Braden McDorman540db3b2014-09-14 02:31:23 -0500889
890 pointer operator->() const { return &deref(); }
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000891};
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000892
Billy Donahue1d956282018-03-06 12:51:58 -0500893inline void swap(Value& a, Value& b) { a.swap(b); }
894
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000895} // namespace Json
896
Sergiy80d6e666f2016-12-03 22:29:14 +0200897#pragma pack(pop)
datadiode9454e682015-01-20 15:25:04 -0600898
Baptiste Lepilleureafd7022013-05-08 20:21:11 +0000899#if defined(JSONCPP_DISABLE_DLL_INTERFACE_WARNING)
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000900#pragma warning(pop)
Baptiste Lepilleureafd7022013-05-08 20:21:11 +0000901#endif // if defined(JSONCPP_DISABLE_DLL_INTERFACE_WARNING)
902
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000903#endif // CPPTL_JSON_H_INCLUDED