blob: 8321de7bf92fcfdef8bcbd960e3a6576d847b68e [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)
Billy Donahueb5e1fe82018-05-20 16:55:27 -040012#include <exception>
Aaron Jacobs9fa4e842014-07-01 08:48:54 +100013#include <string>
14#include <vector>
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
Billy Donahueb5e1fe82018-05-20 16:55:27 -040025// 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)
Billy Donahueb5e1fe82018-05-20 16:55:27 -040029#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
Gauravd97ea5b2016-03-16 11:15:09 +053036#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:
Hans Johnsonb5093e82019-01-14 17:09:19 -060057 Exception(JSONCPP_STRING msg);
Hans Johnson2853b1c2019-01-11 13:58:53 -060058 ~Exception() JSONCPP_NOEXCEPT override;
59 char const* what() const JSONCPP_NOEXCEPT override;
Billy Donahueb5e1fe82018-05-20 16:55:27 -040060
Christopher Dunn949babd2015-07-23 00:19:12 -050061protected:
Christopher Dunnde5b7922016-03-06 11:19:46 -060062 JSONCPP_STRING msg_;
Christopher Dunn949babd2015-07-23 00:19:12 -050063};
64
Christopher Dunn53837942015-03-08 12:31:00 -050065/** Exceptions which the user cannot easily avoid.
66 *
Christopher Dunn4e30c4f2015-03-08 12:56:32 -050067 * E.g. out-of-memory (when we use malloc), stack-overflow, malicious input
Dhruv Paranjape8996c372017-07-08 17:27:07 +053068 *
Christopher Dunn4e30c4f2015-03-08 12:56:32 -050069 * \remark derived from Json::Exception
Christopher Dunn53837942015-03-08 12:31:00 -050070 */
Christopher Dunn949babd2015-07-23 00:19:12 -050071class JSON_API RuntimeError : public Exception {
72public:
Christopher Dunnde5b7922016-03-06 11:19:46 -060073 RuntimeError(JSONCPP_STRING const& msg);
Christopher Dunn949babd2015-07-23 00:19:12 -050074};
75
Christopher Dunn4e30c4f2015-03-08 12:56:32 -050076/** Exceptions thrown by JSON_ASSERT/JSON_FAIL macros.
Christopher Dunn53837942015-03-08 12:31:00 -050077 *
78 * These are precondition-violations (user bugs) and internal errors (our bugs).
Dhruv Paranjape8996c372017-07-08 17:27:07 +053079 *
Christopher Dunn4e30c4f2015-03-08 12:56:32 -050080 * \remark derived from Json::Exception
Christopher Dunn53837942015-03-08 12:31:00 -050081 */
Christopher Dunn949babd2015-07-23 00:19:12 -050082class JSON_API LogicError : public Exception {
83public:
Christopher Dunnde5b7922016-03-06 11:19:46 -060084 LogicError(JSONCPP_STRING const& msg);
Christopher Dunn949babd2015-07-23 00:19:12 -050085};
Christopher Dunn53837942015-03-08 12:31:00 -050086
Christopher Dunn4e30c4f2015-03-08 12:56:32 -050087/// used internally
Gauravd97ea5b2016-03-16 11:15:09 +053088JSONCPP_NORETURN void throwRuntimeError(JSONCPP_STRING const& msg);
Christopher Dunn4e30c4f2015-03-08 12:56:32 -050089/// used internally
Gauravd97ea5b2016-03-16 11:15:09 +053090JSONCPP_NORETURN void throwLogicError(JSONCPP_STRING const& msg);
Christopher Dunn75279cc2015-03-08 12:20:06 -050091
Aaron Jacobs9fa4e842014-07-01 08:48:54 +100092/** \brief Type of the value held by a Value object.
93 */
94enum ValueType {
95 nullValue = 0, ///< 'null' value
96 intValue, ///< signed integer value
97 uintValue, ///< unsigned integer value
98 realValue, ///< double value
99 stringValue, ///< UTF-8 string value
100 booleanValue, ///< bool value
101 arrayValue, ///< array value (ordered list)
102 objectValue ///< object value (collection of name/value pairs).
103};
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000104
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000105enum CommentPlacement {
106 commentBefore = 0, ///< a comment placed on the line before a value
107 commentAfterOnSameLine, ///< a comment just after a value on the same line
108 commentAfter, ///< a comment on the line after a value (only make sense for
Aaron Jacobs3a0c4fc2014-07-01 09:20:48 +1000109 /// root value)
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000110 numberOfCommentPlacement
111};
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000112
Mike Ra07fc532018-03-13 23:35:31 +0300113/** \brief Type of precision for formatting of real values.
114 */
115enum PrecisionType {
116 significantDigits = 0, ///< we set max number of significant digits in string
117 decimalPlaces ///< we set max number of digits after "." in string
118};
119
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000120//# ifdef JSON_USE_CPPTL
121// typedef CppTL::AnyEnumerator<const char *> EnumMemberNames;
122// typedef CppTL::AnyEnumerator<const Value &> EnumValues;
123//# endif
124
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000125/** \brief Lightweight wrapper to tag static string.
126 *
Josh Sorefe6a588a2017-12-03 11:54:29 -0500127 * Value constructor and objectValue member assignment takes advantage of the
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000128 * StaticString and avoid the cost of string duplication when storing the
129 * string or the member name.
130 *
131 * Example of usage:
132 * \code
133 * Json::Value aValue( StaticString("some text") );
134 * Json::Value object;
135 * static const StaticString code("code");
136 * object[code] = 1234;
137 * \endcode
138 */
139class JSON_API StaticString {
140public:
Christopher Dunnff617522015-03-06 10:31:46 -0600141 explicit StaticString(const char* czstring) : c_str_(czstring) {}
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000142
Christopher Dunnff617522015-03-06 10:31:46 -0600143 operator const char*() const { return c_str_; }
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000144
Christopher Dunnff617522015-03-06 10:31:46 -0600145 const char* c_str() const { return c_str_; }
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000146
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000147private:
Christopher Dunnff617522015-03-06 10:31:46 -0600148 const char* c_str_;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000149};
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000150
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000151/** \brief Represents a <a HREF="http://www.json.org">JSON</a> value.
152 *
153 * This class is a discriminated union wrapper that can represents a:
154 * - signed integer [range: Value::minInt - Value::maxInt]
155 * - unsigned integer (range: 0 - Value::maxUInt)
156 * - double
157 * - UTF-8 string
158 * - boolean
159 * - 'null'
160 * - an ordered list of Value
161 * - collection of name/value pairs (javascript object)
162 *
163 * The type of the held value is represented by a #ValueType and
164 * can be obtained using type().
165 *
Christopher Dunnc28610f2015-02-21 11:44:16 -0600166 * Values of an #objectValue or #arrayValue can be accessed using operator[]()
167 * methods.
168 * Non-const methods will automatically create the a #nullValue element
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000169 * if it does not exist.
Christopher Dunnc28610f2015-02-21 11:44:16 -0600170 * The sequence of an #arrayValue will be automatically resized and initialized
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000171 * with #nullValue. resize() can be used to enlarge or truncate an #arrayValue.
172 *
Christopher Dunnc28610f2015-02-21 11:44:16 -0600173 * The get() methods can be used to obtain default value in the case the
174 * required element does not exist.
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000175 *
Mathias L. Baumann08ddeed2018-12-12 17:59:43 +0100176 * It is possible to iterate over the list of member keys of an object using
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000177 * the getMemberNames() method.
Christopher Dunn25342ba2015-03-02 18:05:26 -0600178 *
Christopher Dunnc28610f2015-02-21 11:44:16 -0600179 * \note #Value string-length fit in size_t, but keys must be < 2^30.
180 * (The reason is an implementation detail.) A #CharReader will raise an
Christopher Dunn25342ba2015-03-02 18:05:26 -0600181 * exception if a bound is exceeded to avoid security holes in your app,
182 * but the Value API does *not* check bounds. That is the responsibility
183 * of the caller.
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000184 */
185class JSON_API Value {
186 friend class ValueIteratorBase;
Billy Donahueb5e1fe82018-05-20 16:55:27 -0400187
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000188public:
Christopher Dunnde5b7922016-03-06 11:19:46 -0600189 typedef std::vector<JSONCPP_STRING> Members;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000190 typedef ValueIterator iterator;
191 typedef ValueConstIterator const_iterator;
192 typedef Json::UInt UInt;
193 typedef Json::Int Int;
194#if defined(JSON_HAS_INT64)
195 typedef Json::UInt64 UInt64;
196 typedef Json::Int64 Int64;
Baptiste Lepilleur842e9ac2010-12-27 17:45:23 +0000197#endif // defined(JSON_HAS_INT64)
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000198 typedef Json::LargestInt LargestInt;
199 typedef Json::LargestUInt LargestUInt;
200 typedef Json::ArrayIndex ArrayIndex;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000201
Wolfram Röslerff6b4492017-09-14 09:31:36 +0200202 // Required for boost integration, e. g. BOOST_TEST
203 typedef std::string value_type;
204
Billy Donahueb5e1fe82018-05-20 16:55:27 -0400205 static const Value& null; ///< We regret this reference to a global instance;
206 ///< prefer the simpler Value().
207 static const Value& nullRef; ///< just a kludge for binary-compatibility; same
208 ///< as null
Christopher Dunn0f288ae2016-06-26 18:47:43 -0500209 static Value const& nullSingleton(); ///< Prefer this to null or nullRef.
210
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000211 /// Minimum signed integer value that can be stored in a Json::Value.
212 static const LargestInt minLargestInt;
213 /// Maximum signed integer value that can be stored in a Json::Value.
214 static const LargestInt maxLargestInt;
215 /// Maximum unsigned integer value that can be stored in a Json::Value.
216 static const LargestUInt maxLargestUInt;
Baptiste Lepilleur842e9ac2010-12-27 17:45:23 +0000217
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000218 /// Minimum signed int value that can be stored in a Json::Value.
219 static const Int minInt;
220 /// Maximum signed int value that can be stored in a Json::Value.
221 static const Int maxInt;
222 /// Maximum unsigned int value that can be stored in a Json::Value.
223 static const UInt maxUInt;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000224
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000225#if defined(JSON_HAS_INT64)
226 /// Minimum signed 64 bits int value that can be stored in a Json::Value.
227 static const Int64 minInt64;
228 /// Maximum signed 64 bits int value that can be stored in a Json::Value.
229 static const Int64 maxInt64;
230 /// Maximum unsigned 64 bits int value that can be stored in a Json::Value.
231 static const UInt64 maxUInt64;
Aaron Jacobsf1053e72011-05-24 03:18:02 +0000232#endif // defined(JSON_HAS_INT64)
Baptiste Lepilleur842e9ac2010-12-27 17:45:23 +0000233
Mike Ra07fc532018-03-13 23:35:31 +0300234 /// Default precision for real value for string representation.
235 static const UInt defaultRealPrecision;
236
Darcy Beurle798f6ba2017-12-22 22:48:20 +0100237// Workaround for bug in the NVIDIAs CUDA 9.1 nvcc compiler
238// when using gcc and clang backend compilers. CZString
239// cannot be defined as private. See issue #486
240#ifdef __NVCC__
241public:
242#else
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000243private:
Darcy Beurle798f6ba2017-12-22 22:48:20 +0100244#endif
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000245#ifndef JSONCPP_DOC_EXCLUDE_IMPLEMENTATION
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000246 class CZString {
247 public:
Billy Donahueb5e1fe82018-05-20 16:55:27 -0400248 enum DuplicationPolicy { noDuplication = 0, duplicate, duplicateOnCopy };
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000249 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;
Billy Donahueb5e1fe82018-05-20 16:55:27 -0400265 // const char* c_str() const; ///< \deprecated
Christopher Dunnc28610f2015-02-21 11:44:16 -0600266 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 {
Billy Donahueb5e1fe82018-05-20 16:55:27 -0400274 unsigned policy_ : 2;
275 unsigned length_ : 30; // 1GB max
Christopher Dunn57ad0512015-03-02 12:10:35 -0600276 };
277
Billy Donahueb5e1fe82018-05-20 16:55:27 -0400278 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);
Billy Donahueb5e1fe82018-05-20 16:55:27 -0400335 Value(const JSONCPP_STRING& value); ///< Copy data() til size(). Embedded
336 ///< zeroes too.
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000337#ifdef JSON_USE_CPPTL
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000338 Value(const CppTL::ConstString& value);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000339#endif
340 Value(bool value);
Christopher Dunn66eb72f2015-01-20 11:02:22 -0600341 /// Deep copy.
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000342 Value(const Value& other);
Motti2b008912015-04-20 17:44:47 +0300343#if JSON_HAS_RVALUE_REFERENCES
344 /// Move constructor
345 Value(Value&& other);
346#endif
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000347 ~Value();
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000348
Christopher Dunn7f439f42015-03-06 09:22:57 -0600349 /// Deep copy, then swap(other).
Billy Donahueb5e1fe82018-05-20 16:55:27 -0400350 /// \note Over-write existing comments. To preserve comments, use
351 /// #swapPayload().
Александр Малинин6a15ca62017-07-31 15:29:02 +0300352 Value& operator=(Value other);
Dhruv Paranjape8996c372017-07-08 17:27:07 +0530353
Christopher Dunn66eb72f2015-01-20 11:02:22 -0600354 /// Swap everything.
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000355 void swap(Value& other);
Christopher Dunn66eb72f2015-01-20 11:02:22 -0600356 /// Swap values but leave comments and source offsets in place.
357 void swapPayload(Value& other);
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000358
Dhruv Paranjape8996c372017-07-08 17:27:07 +0530359 /// copy everything.
360 void copy(const Value& other);
361 /// copy values but leave comments and source offsets in place.
362 void copyPayload(const Value& other);
363
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000364 ValueType type() const;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000365
Christopher Dunn66eb72f2015-01-20 11:02:22 -0600366 /// Compare payload only, not comments etc.
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000367 bool operator<(const Value& other) const;
368 bool operator<=(const Value& other) const;
369 bool operator>=(const Value& other) const;
370 bool operator>(const Value& other) const;
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000371 bool operator==(const Value& other) const;
372 bool operator!=(const Value& other) const;
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000373 int compare(const Value& other) const;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000374
Christopher Dunn8a702972015-03-03 10:38:27 -0600375 const char* asCString() const; ///< Embedded zeroes could cause you trouble!
dawescae564652016-03-14 19:11:02 -0500376#if JSONCPP_USING_SECURE_MEMORY
Billy Donahueb5e1fe82018-05-20 16:55:27 -0400377 unsigned getCStringLength() const; // Allows you to understand the length of
378 // the CString
dawescae564652016-03-14 19:11:02 -0500379#endif
Christopher Dunnde5b7922016-03-06 11:19:46 -0600380 JSONCPP_STRING asString() const; ///< Embedded zeroes are possible.
Christopher Dunnc28610f2015-02-21 11:44:16 -0600381 /** Get raw char* of string-value.
382 * \return false if !string. (Seg-fault if str or end are NULL.)
383 */
Billy Donahueb5e1fe82018-05-20 16:55:27 -0400384 bool getString(char const** begin, char const** end) const;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000385#ifdef JSON_USE_CPPTL
386 CppTL::ConstString asConstString() const;
387#endif
388 Int asInt() const;
389 UInt asUInt() const;
Aaron Jacobsf1053e72011-05-24 03:18:02 +0000390#if defined(JSON_HAS_INT64)
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000391 Int64 asInt64() const;
392 UInt64 asUInt64() const;
Aaron Jacobsf1053e72011-05-24 03:18:02 +0000393#endif // if defined(JSON_HAS_INT64)
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000394 LargestInt asLargestInt() const;
395 LargestUInt asLargestUInt() const;
396 float asFloat() const;
397 double asDouble() const;
398 bool asBool() const;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000399
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000400 bool isNull() const;
401 bool isBool() const;
402 bool isInt() const;
403 bool isInt64() const;
404 bool isUInt() const;
405 bool isUInt64() const;
406 bool isIntegral() const;
407 bool isDouble() const;
408 bool isNumeric() const;
409 bool isString() const;
410 bool isArray() const;
411 bool isObject() const;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000412
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000413 bool isConvertibleTo(ValueType other) const;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000414
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000415 /// Number of values in array or object
416 ArrayIndex size() const;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000417
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000418 /// \brief Return true if empty array, empty object, or null;
419 /// otherwise, false.
420 bool empty() const;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000421
Wolfram Rösler90794222017-12-05 18:18:55 +0100422 /// Return !isNull()
drgler04abe382018-01-13 15:28:19 +0100423 JSONCPP_OP_EXPLICIT operator bool() const;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000424
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000425 /// Remove all object members and array elements.
426 /// \pre type() is arrayValue, objectValue, or nullValue
427 /// \post type() is unchanged
428 void clear();
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000429
Marian Klymovfc201342018-06-02 20:15:26 +0300430 /// Resize the array to newSize elements.
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000431 /// New elements are initialized to null.
432 /// May only be called on nullValue or arrayValue.
433 /// \pre type() is arrayValue or nullValue
434 /// \post type() is arrayValue
Marian Klymovfc201342018-06-02 20:15:26 +0300435 void resize(ArrayIndex newSize);
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000436
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000437 /// Access an array element (zero based index ).
438 /// If the array contains less than index element, then null value are
439 /// inserted
440 /// in the array so that its size is index+1.
441 /// (You may need to say 'value[0u]' to get your compiler to distinguish
442 /// this from the operator[] which takes a string.)
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000443 Value& operator[](ArrayIndex index);
Baptiste Lepilleurfa130ef2010-12-24 12:47:14 +0000444
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000445 /// Access an array element (zero based index ).
446 /// If the array contains less than index element, then null value are
447 /// inserted
448 /// in the array so that its size is index+1.
449 /// (You may need to say 'value[0u]' to get your compiler to distinguish
450 /// this from the operator[] which takes a string.)
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000451 Value& operator[](int index);
Baptiste Lepilleurfa130ef2010-12-24 12:47:14 +0000452
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000453 /// Access an array element (zero based index )
454 /// (You may need to say 'value[0u]' to get your compiler to distinguish
455 /// this from the operator[] which takes a string.)
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000456 const Value& operator[](ArrayIndex index) const;
Baptiste Lepilleurfa130ef2010-12-24 12:47:14 +0000457
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000458 /// Access an array element (zero based index )
459 /// (You may need to say 'value[0u]' to get your compiler to distinguish
460 /// this from the operator[] which takes a string.)
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000461 const Value& operator[](int index) const;
Baptiste Lepilleurfa130ef2010-12-24 12:47:14 +0000462
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000463 /// If the array contains at least index+1 elements, returns the element
464 /// value,
465 /// otherwise returns defaultValue.
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000466 Value get(ArrayIndex index, const Value& defaultValue) const;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000467 /// Return true if index < size().
468 bool isValidIndex(ArrayIndex index) const;
469 /// \brief Append value to array at the end.
470 ///
471 /// Equivalent to jsonvalue[jsonvalue.size()] = value;
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000472 Value& append(const Value& value);
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000473
Dhruv Paranjape0ba8bd72017-07-08 17:47:13 +0530474#if JSON_HAS_RVALUE_REFERENCES
Dhruv Paranjape23c44d92017-07-08 17:30:47 +0530475 Value& append(Value&& value);
476#endif
477
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000478 /// Access an object value by name, create a null member if it does not exist.
Christopher Dunn25342ba2015-03-02 18:05:26 -0600479 /// \note Because of our implementation, keys are limited to 2^30 -1 chars.
480 /// Exceeding that will cause an exception.
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000481 Value& operator[](const char* key);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000482 /// Access an object value by name, returns null if there is no member with
483 /// that name.
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000484 const Value& operator[](const char* key) const;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000485 /// Access an object value by name, create a null member if it does not exist.
Christopher Dunn25342ba2015-03-02 18:05:26 -0600486 /// \param key may contain embedded nulls.
Christopher Dunnde5b7922016-03-06 11:19:46 -0600487 Value& operator[](const JSONCPP_STRING& key);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000488 /// Access an object value by name, returns null if there is no member with
489 /// that name.
Christopher Dunn25342ba2015-03-02 18:05:26 -0600490 /// \param key may contain embedded nulls.
Christopher Dunnde5b7922016-03-06 11:19:46 -0600491 const Value& operator[](const JSONCPP_STRING& key) const;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000492 /** \brief Access an object value by name, create a null member if it does not
493 exist.
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000494
Billy Donahueb5e1fe82018-05-20 16:55:27 -0400495 * If the object has no entry for that name, then the member name used to
496 store
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000497 * the new entry is not duplicated.
498 * Example of use:
499 * \code
500 * Json::Value object;
501 * static const StaticString code("code");
502 * object[code] = 1234;
503 * \endcode
504 */
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000505 Value& operator[](const StaticString& key);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000506#ifdef JSON_USE_CPPTL
507 /// Access an object value by name, create a null member if it does not exist.
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000508 Value& operator[](const CppTL::ConstString& key);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000509 /// Access an object value by name, returns null if there is no member with
510 /// that name.
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000511 const Value& operator[](const CppTL::ConstString& key) const;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000512#endif
513 /// Return the member named key if it exist, defaultValue otherwise.
Christopher Dunn0fd28752015-03-05 16:38:43 -0600514 /// \note deep copy
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000515 Value get(const char* key, const Value& defaultValue) const;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000516 /// Return the member named key if it exist, defaultValue otherwise.
Christopher Dunn0fd28752015-03-05 16:38:43 -0600517 /// \note deep copy
Christopher Dunn89704032015-07-11 12:09:59 -0500518 /// \note key may contain embedded nulls.
Billy Donahueb5e1fe82018-05-20 16:55:27 -0400519 Value
520 get(const char* begin, const char* end, const Value& defaultValue) const;
Christopher Dunn25342ba2015-03-02 18:05:26 -0600521 /// Return the member named key if it exist, defaultValue otherwise.
Christopher Dunn0fd28752015-03-05 16:38:43 -0600522 /// \note deep copy
Christopher Dunn25342ba2015-03-02 18:05:26 -0600523 /// \param key may contain embedded nulls.
Christopher Dunnde5b7922016-03-06 11:19:46 -0600524 Value get(const JSONCPP_STRING& key, const Value& defaultValue) const;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000525#ifdef JSON_USE_CPPTL
526 /// Return the member named key if it exist, defaultValue otherwise.
Christopher Dunn0fd28752015-03-05 16:38:43 -0600527 /// \note deep copy
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000528 Value get(const CppTL::ConstString& key, const Value& defaultValue) const;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000529#endif
Christopher Dunn25342ba2015-03-02 18:05:26 -0600530 /// Most general and efficient version of isMember()const, get()const,
531 /// and operator[]const
Christopher Dunn89704032015-07-11 12:09:59 -0500532 /// \note As stated elsewhere, behavior is undefined if (end-begin) >= 2^30
533 Value const* find(char const* begin, char const* end) const;
Christopher Dunnc28610f2015-02-21 11:44:16 -0600534 /// Most general and efficient version of object-mutators.
Christopher Dunn89704032015-07-11 12:09:59 -0500535 /// \note As stated elsewhere, behavior is undefined if (end-begin) >= 2^30
Christopher Dunnc28610f2015-02-21 11:44:16 -0600536 /// \return non-zero, but JSON_ASSERT if this is neither object nor nullValue.
Christopher Dunn89704032015-07-11 12:09:59 -0500537 Value const* demand(char const* begin, char const* end);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000538 /// \brief Remove and return the named member.
539 ///
540 /// Do nothing if it did not exist.
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000541 /// \pre type() is objectValue or nullValue
542 /// \post type() is unchanged
Wolfram Röslera06b3902017-10-18 07:19:27 +0200543 void removeMember(const char* key);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000544 /// Same as removeMember(const char*)
Christopher Dunn25342ba2015-03-02 18:05:26 -0600545 /// \param key may contain embedded nulls.
Wolfram Röslera06b3902017-10-18 07:19:27 +0200546 void removeMember(const JSONCPP_STRING& key);
Christopher Dunn89704032015-07-11 12:09:59 -0500547 /// Same as removeMember(const char* begin, const char* end, Value* removed),
Christopher Dunn25342ba2015-03-02 18:05:26 -0600548 /// but 'key' is null-terminated.
549 bool removeMember(const char* key, Value* removed);
Christopher Dunn76746b02015-01-21 16:01:30 -0600550 /** \brief Remove the named map member.
551
552 Update 'removed' iff removed.
Christopher Dunn25342ba2015-03-02 18:05:26 -0600553 \param key may contain embedded nulls.
Christopher Dunn76746b02015-01-21 16:01:30 -0600554 \return true iff removed (no exceptions)
555 */
Christopher Dunnde5b7922016-03-06 11:19:46 -0600556 bool removeMember(JSONCPP_STRING const& key, Value* removed);
557 /// Same as removeMember(JSONCPP_STRING const& key, Value* removed)
Christopher Dunn89704032015-07-11 12:09:59 -0500558 bool removeMember(const char* begin, const char* end, Value* removed);
Christopher Dunn9de2c2d2015-01-20 16:15:40 -0600559 /** \brief Remove the indexed array element.
560
561 O(n) expensive operations.
YantaoZhaoe32ee472018-07-03 21:29:18 +0800562 Update 'removed' iff removed.
Marian Klymovfc201342018-06-02 20:15:26 +0300563 \return true if removed (no exceptions)
Christopher Dunn9de2c2d2015-01-20 16:15:40 -0600564 */
Marian Klymovfc201342018-06-02 20:15:26 +0300565 bool removeIndex(ArrayIndex index, Value* removed);
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000566
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000567 /// Return true if the object has a member named key.
Christopher Dunnc28610f2015-02-21 11:44:16 -0600568 /// \note 'key' must be null-terminated.
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000569 bool isMember(const char* key) const;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000570 /// Return true if the object has a member named key.
Christopher Dunn25342ba2015-03-02 18:05:26 -0600571 /// \param key may contain embedded nulls.
Christopher Dunnde5b7922016-03-06 11:19:46 -0600572 bool isMember(const JSONCPP_STRING& key) const;
573 /// Same as isMember(JSONCPP_STRING const& key)const
Christopher Dunn89704032015-07-11 12:09:59 -0500574 bool isMember(const char* begin, const char* end) const;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000575#ifdef JSON_USE_CPPTL
576 /// Return true if the object has a member named key.
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000577 bool isMember(const CppTL::ConstString& key) const;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000578#endif
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000579
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000580 /// \brief Return a list of the member names.
581 ///
582 /// If null, return an empty list.
583 /// \pre type() is objectValue or nullValue
584 /// \post if type() was nullValue, it remains nullValue
585 Members getMemberNames() const;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000586
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000587 //# ifdef JSON_USE_CPPTL
588 // EnumMemberNames enumMemberNames() const;
589 // EnumValues enumValues() const;
590 //# endif
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000591
Christopher Dunn1e3149a2015-01-25 14:16:13 -0600592 /// \deprecated Always pass len.
Christopher Dunnde5b7922016-03-06 11:19:46 -0600593 JSONCPP_DEPRECATED("Use setComment(JSONCPP_STRING const&) instead.")
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000594 void setComment(const char* comment, CommentPlacement placement);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000595 /// Comments must be //... or /* ... */
Christopher Dunn1e3149a2015-01-25 14:16:13 -0600596 void setComment(const char* comment, size_t len, CommentPlacement placement);
597 /// Comments must be //... or /* ... */
Christopher Dunnde5b7922016-03-06 11:19:46 -0600598 void setComment(const JSONCPP_STRING& comment, CommentPlacement placement);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000599 bool hasComment(CommentPlacement placement) const;
600 /// Include delimiters and embedded newlines.
Christopher Dunnde5b7922016-03-06 11:19:46 -0600601 JSONCPP_STRING getComment(CommentPlacement placement) const;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000602
Christopher Dunnde5b7922016-03-06 11:19:46 -0600603 JSONCPP_STRING toStyledString() const;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000604
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000605 const_iterator begin() const;
606 const_iterator end() const;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000607
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000608 iterator begin();
609 iterator end();
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000610
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000611 // Accessors for the [start, limit) range of bytes within the JSON text from
612 // which this value was parsed, if any.
Christopher Dunnd4513fc2016-02-06 09:25:20 -0600613 void setOffsetStart(ptrdiff_t start);
614 void setOffsetLimit(ptrdiff_t limit);
615 ptrdiff_t getOffsetStart() const;
616 ptrdiff_t getOffsetLimit() const;
Aaron Jacobs68db6552014-04-23 23:41:12 +0000617
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000618private:
Billy Donahue8eb5d892014-11-10 01:35:42 -0500619 void initBasic(ValueType type, bool allocated = false);
Andrey Okoshkin9b569c82018-01-12 15:59:20 +0300620 void dupPayload(const Value& other);
Andrey Okoshkinc69148c2018-01-12 11:26:34 +0300621 void releasePayload();
Andrey Okoshkin9b569c82018-01-12 15:59:20 +0300622 void dupMeta(const Value& other);
Billy Donahue8eb5d892014-11-10 01:35:42 -0500623
Christopher Dunnc28610f2015-02-21 11:44:16 -0600624 Value& resolveReference(const char* key);
625 Value& resolveReference(const char* key, const char* end);
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000626
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000627 struct CommentInfo {
628 CommentInfo();
629 ~CommentInfo();
630
Christopher Dunn1e3149a2015-01-25 14:16:13 -0600631 void setComment(const char* text, size_t len);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000632
Hans Johnsone817e4f2019-01-14 17:09:22 -0600633 char* comment_{nullptr};
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000634 };
635
636 // struct MemberNamesTransform
637 //{
638 // typedef const char *result_type;
639 // const char *operator()( const CZString &name ) const
640 // {
641 // return name.c_str();
642 // }
643 //};
644
645 union ValueHolder {
646 LargestInt int_;
647 LargestUInt uint_;
648 double real_;
649 bool bool_;
Billy Donahueb5e1fe82018-05-20 16:55:27 -0400650 char* string_; // actually ptr to unsigned, followed by str, unless
651 // !allocated_
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000652 ObjectValues* map_;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000653 } value_;
654 ValueType type_ : 8;
Billy Donahueb5e1fe82018-05-20 16:55:27 -0400655 unsigned int allocated_ : 1; // Notes: if declared as bool, bitfield is
656 // useless. If not allocated_, string_ must be
657 // null-terminated.
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000658 CommentInfo* comments_;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000659
660 // [start, limit) byte offsets in the source JSON text from which this Value
661 // was extracted.
Christopher Dunnd4513fc2016-02-06 09:25:20 -0600662 ptrdiff_t start_;
663 ptrdiff_t limit_;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000664};
665
666/** \brief Experimental and untested: represents an element of the "path" to
667 * access a node.
668 */
669class JSON_API PathArgument {
670public:
671 friend class Path;
672
673 PathArgument();
674 PathArgument(ArrayIndex index);
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000675 PathArgument(const char* key);
Christopher Dunnde5b7922016-03-06 11:19:46 -0600676 PathArgument(const JSONCPP_STRING& key);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000677
678private:
Billy Donahueb5e1fe82018-05-20 16:55:27 -0400679 enum Kind { kindNone = 0, kindIndex, kindKey };
Christopher Dunnde5b7922016-03-06 11:19:46 -0600680 JSONCPP_STRING key_;
Hans Johnsone817e4f2019-01-14 17:09:22 -0600681 ArrayIndex index_{};
682 Kind kind_{kindNone};
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000683};
684
685/** \brief Experimental and untested: represents a "path" to access a node.
686 *
687 * Syntax:
688 * - "." => root node
689 * - ".[n]" => elements at index 'n' of root node (an array value)
690 * - ".name" => member named 'name' of root node (an object value)
691 * - ".name1.name2.name3"
692 * - ".[0][1][2].name1[3]"
693 * - ".%" => member name is provided as parameter
694 * - ".[%]" => index is provied as parameter
695 */
696class JSON_API Path {
697public:
Christopher Dunnde5b7922016-03-06 11:19:46 -0600698 Path(const JSONCPP_STRING& path,
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000699 const PathArgument& a1 = PathArgument(),
700 const PathArgument& a2 = PathArgument(),
701 const PathArgument& a3 = PathArgument(),
702 const PathArgument& a4 = PathArgument(),
703 const PathArgument& a5 = PathArgument());
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000704
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000705 const Value& resolve(const Value& root) const;
706 Value resolve(const Value& root, const Value& defaultValue) const;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000707 /// Creates the "path" to access the specified node and returns a reference on
708 /// the node.
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000709 Value& make(Value& root) const;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000710
711private:
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000712 typedef std::vector<const PathArgument*> InArgs;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000713 typedef std::vector<PathArgument> Args;
714
Christopher Dunnde5b7922016-03-06 11:19:46 -0600715 void makePath(const JSONCPP_STRING& path, const InArgs& in);
716 void addPathInArg(const JSONCPP_STRING& path,
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000717 const InArgs& in,
718 InArgs::const_iterator& itInArg,
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000719 PathArgument::Kind kind);
Marian Klymov48112c82018-06-02 19:43:31 +0300720 static void invalidPath(const JSONCPP_STRING& path, int location);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000721
722 Args args_;
723};
724
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000725/** \brief base class for Value iterators.
726 *
727 */
728class JSON_API ValueIteratorBase {
729public:
730 typedef std::bidirectional_iterator_tag iterator_category;
731 typedef unsigned int size_t;
732 typedef int difference_type;
733 typedef ValueIteratorBase SelfType;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000734
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000735 bool operator==(const SelfType& other) const { return isEqual(other); }
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000736
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000737 bool operator!=(const SelfType& other) const { return !isEqual(other); }
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000738
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000739 difference_type operator-(const SelfType& other) const {
Kevin Grant4c5832a2015-02-14 20:53:35 -0800740 return other.computeDistance(*this);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000741 }
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000742
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000743 /// Return either the index or the member name of the referenced value as a
744 /// Value.
745 Value key() const;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000746
Billy Donahueb5e1fe82018-05-20 16:55:27 -0400747 /// Return the index of the referenced Value, or -1 if it is not an
748 /// 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.
Billy Donahueb5e1fe82018-05-20 16:55:27 -0400758 /// \deprecated This cannot be used for UTF-8 strings, since there can be
759 /// embedded nulls.
Christopher Dunned495ed2015-03-08 14:01:28 -0500760 JSONCPP_DEPRECATED("Use `key = name();` instead.")
Christopher Dunnc28610f2015-02-21 11:44:16 -0600761 char const* memberName() const;
762 /// Return the member name of the referenced Value, or NULL if it is not an
763 /// objectValue.
Christopher Dunned495ed2015-03-08 14:01:28 -0500764 /// \note Better version than memberName(). Allows embedded nulls.
Christopher Dunnc28610f2015-02-21 11:44:16 -0600765 char const* memberName(char const** end) const;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000766
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000767protected:
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000768 Value& deref() const;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000769
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000770 void increment();
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000771
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000772 void decrement();
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000773
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000774 difference_type computeDistance(const SelfType& other) const;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000775
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000776 bool isEqual(const SelfType& other) const;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000777
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000778 void copy(const SelfType& other);
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000779
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000780private:
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000781 Value::ObjectValues::iterator current_;
782 // Indicates that iterator is for a null value.
Hans Johnsone817e4f2019-01-14 17:09:22 -0600783 bool isNull_{true};
Christopher Dunn2a10f4a2015-04-28 04:55:12 +0100784
785public:
786 // For some reason, BORLAND needs these at the end, rather
787 // than earlier. No idea why.
788 ValueIteratorBase();
789 explicit ValueIteratorBase(const Value::ObjectValues::iterator& current);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000790};
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000791
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000792/** \brief const iterator for object and array value.
793 *
794 */
795class JSON_API ValueConstIterator : public ValueIteratorBase {
796 friend class Value;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000797
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000798public:
799 typedef const Value value_type;
Billy Donahueb5e1fe82018-05-20 16:55:27 -0400800 // typedef unsigned int size_t;
801 // typedef int difference_type;
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000802 typedef const Value& reference;
803 typedef const Value* pointer;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000804 typedef ValueConstIterator SelfType;
805
806 ValueConstIterator();
ycqiuc8a8cfc2015-10-06 16:46:19 +0800807 ValueConstIterator(ValueIterator const& other);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000808
809private:
Billy Donahueb5e1fe82018-05-20 16:55:27 -0400810 /*! \internal Use by Value to create an iterator.
811 */
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000812 explicit ValueConstIterator(const Value::ObjectValues::iterator& current);
Billy Donahueb5e1fe82018-05-20 16:55:27 -0400813
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000814public:
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000815 SelfType& operator=(const ValueIteratorBase& other);
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000816
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000817 SelfType operator++(int) {
818 SelfType temp(*this);
819 ++*this;
820 return temp;
821 }
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000822
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000823 SelfType operator--(int) {
824 SelfType temp(*this);
825 --*this;
826 return temp;
827 }
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000828
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000829 SelfType& operator--() {
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000830 decrement();
831 return *this;
832 }
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000833
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000834 SelfType& operator++() {
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000835 increment();
836 return *this;
837 }
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000838
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000839 reference operator*() const { return deref(); }
Braden McDorman540db3b2014-09-14 02:31:23 -0500840
841 pointer operator->() const { return &deref(); }
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000842};
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000843
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000844/** \brief Iterator for object and array value.
845 */
846class JSON_API ValueIterator : public ValueIteratorBase {
847 friend class Value;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000848
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000849public:
850 typedef Value value_type;
851 typedef unsigned int size_t;
852 typedef int difference_type;
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000853 typedef Value& reference;
854 typedef Value* pointer;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000855 typedef ValueIterator SelfType;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000856
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000857 ValueIterator();
ycqiuc8a8cfc2015-10-06 16:46:19 +0800858 explicit ValueIterator(const ValueConstIterator& other);
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000859 ValueIterator(const ValueIterator& other);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000860
861private:
Billy Donahueb5e1fe82018-05-20 16:55:27 -0400862 /*! \internal Use by Value to create an iterator.
863 */
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000864 explicit ValueIterator(const Value::ObjectValues::iterator& current);
Billy Donahueb5e1fe82018-05-20 16:55:27 -0400865
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000866public:
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000867 SelfType& operator=(const SelfType& other);
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000868
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000869 SelfType operator++(int) {
870 SelfType temp(*this);
871 ++*this;
872 return temp;
873 }
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000874
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000875 SelfType operator--(int) {
876 SelfType temp(*this);
877 --*this;
878 return temp;
879 }
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000880
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000881 SelfType& operator--() {
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000882 decrement();
883 return *this;
884 }
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000885
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000886 SelfType& operator++() {
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000887 increment();
888 return *this;
889 }
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000890
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000891 reference operator*() const { return deref(); }
Braden McDorman540db3b2014-09-14 02:31:23 -0500892
893 pointer operator->() const { return &deref(); }
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000894};
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000895
Billy Donahue1d956282018-03-06 12:51:58 -0500896inline void swap(Value& a, Value& b) { a.swap(b); }
897
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000898} // namespace Json
899
Sergiy80d6e666f2016-12-03 22:29:14 +0200900#pragma pack(pop)
datadiode9454e682015-01-20 15:25:04 -0600901
Baptiste Lepilleureafd7022013-05-08 20:21:11 +0000902#if defined(JSONCPP_DISABLE_DLL_INTERFACE_WARNING)
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000903#pragma warning(pop)
Baptiste Lepilleureafd7022013-05-08 20:21:11 +0000904#endif // if defined(JSONCPP_DISABLE_DLL_INTERFACE_WARNING)
905
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000906#endif // CPPTL_JSON_H_INCLUDED