blob: c4b29b97d616ceb9460cbd14b1a6fd1755ea0bae [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
Jordan Bayles9704ced2019-11-14 09:38:11 -08006#ifndef JSON_H_INCLUDED
7#define 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)
es1x2ee3b1d2019-10-11 21:33:36 +030012
13// Conditional NORETURN attribute on the throw functions would:
14// a) suppress false positives from static code analysis
15// b) possibly improve optimization opportunities.
16#if !defined(JSONCPP_NORETURN)
17#if defined(_MSC_VER) && _MSC_VER == 1800
18#define JSONCPP_NORETURN __declspec(noreturn)
19#else
20#define JSONCPP_NORETURN [[noreturn]]
21#endif
22#endif
23
Billy Donahue433107f2019-01-20 21:53:01 -050024#include <array>
Billy Donahueb5e1fe82018-05-20 16:55:27 -040025#include <exception>
Jordan Bayles9704ced2019-11-14 09:38:11 -080026#include <map>
Billy Donahue433107f2019-01-20 21:53:01 -050027#include <memory>
Aaron Jacobs9fa4e842014-07-01 08:48:54 +100028#include <string>
29#include <vector>
Christopher Dunn6d135cb2007-06-13 15:51:04 +000030
Aaron Jacobs9fa4e842014-07-01 08:48:54 +100031// Disable warning C4251: <data member>: <type> needs to have dll-interface to
32// be used by...
Baptiste Lepilleureafd7022013-05-08 20:21:11 +000033#if defined(JSONCPP_DISABLE_DLL_INTERFACE_WARNING)
Aaron Jacobs9fa4e842014-07-01 08:48:54 +100034#pragma warning(push)
35#pragma warning(disable : 4251)
Baptiste Lepilleureafd7022013-05-08 20:21:11 +000036#endif // if defined(JSONCPP_DISABLE_DLL_INTERFACE_WARNING)
37
Sergiy80d6e666f2016-12-03 22:29:14 +020038#pragma pack(push, 8)
39
Christopher Dunn6d135cb2007-06-13 15:51:04 +000040/** \brief JSON (JavaScript Object Notation).
41 */
42namespace Json {
43
Jordan Bayles83cc9212019-06-06 13:41:47 -070044#if JSON_USE_EXCEPTION
Christopher Dunn75279cc2015-03-08 12:20:06 -050045/** Base class for all exceptions we throw.
Christopher Dunn4e30c4f2015-03-08 12:56:32 -050046 *
47 * We use nothing but these internally. Of course, STL can throw others.
Christopher Dunn75279cc2015-03-08 12:20:06 -050048 */
Christopher Dunn949babd2015-07-23 00:19:12 -050049class JSON_API Exception : public std::exception {
50public:
Billy Donahue1c2ed7a2019-01-17 16:35:29 -050051 Exception(String msg);
Hans Johnson2853b1c2019-01-11 13:58:53 -060052 ~Exception() JSONCPP_NOEXCEPT override;
53 char const* what() const JSONCPP_NOEXCEPT override;
Billy Donahueb5e1fe82018-05-20 16:55:27 -040054
Christopher Dunn949babd2015-07-23 00:19:12 -050055protected:
Billy Donahue1c2ed7a2019-01-17 16:35:29 -050056 String msg_;
Christopher Dunn949babd2015-07-23 00:19:12 -050057};
58
Christopher Dunn53837942015-03-08 12:31:00 -050059/** Exceptions which the user cannot easily avoid.
60 *
Christopher Dunn4e30c4f2015-03-08 12:56:32 -050061 * E.g. out-of-memory (when we use malloc), stack-overflow, malicious input
Dhruv Paranjape8996c372017-07-08 17:27:07 +053062 *
Christopher Dunn4e30c4f2015-03-08 12:56:32 -050063 * \remark derived from Json::Exception
Christopher Dunn53837942015-03-08 12:31:00 -050064 */
Christopher Dunn949babd2015-07-23 00:19:12 -050065class JSON_API RuntimeError : public Exception {
66public:
Billy Donahue1c2ed7a2019-01-17 16:35:29 -050067 RuntimeError(String const& msg);
Christopher Dunn949babd2015-07-23 00:19:12 -050068};
69
Christopher Dunn4e30c4f2015-03-08 12:56:32 -050070/** Exceptions thrown by JSON_ASSERT/JSON_FAIL macros.
Christopher Dunn53837942015-03-08 12:31:00 -050071 *
72 * These are precondition-violations (user bugs) and internal errors (our bugs).
Dhruv Paranjape8996c372017-07-08 17:27:07 +053073 *
Christopher Dunn4e30c4f2015-03-08 12:56:32 -050074 * \remark derived from Json::Exception
Christopher Dunn53837942015-03-08 12:31:00 -050075 */
Christopher Dunn949babd2015-07-23 00:19:12 -050076class JSON_API LogicError : public Exception {
77public:
Billy Donahue1c2ed7a2019-01-17 16:35:29 -050078 LogicError(String const& msg);
Christopher Dunn949babd2015-07-23 00:19:12 -050079};
Jordan Bayles83cc9212019-06-06 13:41:47 -070080#endif
Christopher Dunn53837942015-03-08 12:31:00 -050081
Christopher Dunn4e30c4f2015-03-08 12:56:32 -050082/// used internally
es1x2ee3b1d2019-10-11 21:33:36 +030083JSONCPP_NORETURN void throwRuntimeError(String const& msg);
Christopher Dunn4e30c4f2015-03-08 12:56:32 -050084/// used internally
es1x2ee3b1d2019-10-11 21:33:36 +030085JSONCPP_NORETURN void throwLogicError(String const& msg);
Christopher Dunn75279cc2015-03-08 12:20:06 -050086
Aaron Jacobs9fa4e842014-07-01 08:48:54 +100087/** \brief Type of the value held by a Value object.
88 */
89enum ValueType {
90 nullValue = 0, ///< 'null' value
91 intValue, ///< signed integer value
92 uintValue, ///< unsigned integer value
93 realValue, ///< double value
94 stringValue, ///< UTF-8 string value
95 booleanValue, ///< bool value
96 arrayValue, ///< array value (ordered list)
97 objectValue ///< object value (collection of name/value pairs).
98};
Christopher Dunn6d135cb2007-06-13 15:51:04 +000099
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000100enum CommentPlacement {
101 commentBefore = 0, ///< a comment placed on the line before a value
102 commentAfterOnSameLine, ///< a comment just after a value on the same line
103 commentAfter, ///< a comment on the line after a value (only make sense for
Aaron Jacobs3a0c4fc2014-07-01 09:20:48 +1000104 /// root value)
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000105 numberOfCommentPlacement
106};
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000107
Mike Ra07fc532018-03-13 23:35:31 +0300108/** \brief Type of precision for formatting of real values.
109 */
110enum PrecisionType {
111 significantDigits = 0, ///< we set max number of significant digits in string
112 decimalPlaces ///< we set max number of digits after "." in string
113};
114
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000115/** \brief Lightweight wrapper to tag static string.
116 *
Josh Sorefe6a588a2017-12-03 11:54:29 -0500117 * Value constructor and objectValue member assignment takes advantage of the
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000118 * StaticString and avoid the cost of string duplication when storing the
119 * string or the member name.
120 *
121 * Example of usage:
122 * \code
123 * Json::Value aValue( StaticString("some text") );
124 * Json::Value object;
125 * static const StaticString code("code");
126 * object[code] = 1234;
127 * \endcode
128 */
129class JSON_API StaticString {
130public:
Christopher Dunnff617522015-03-06 10:31:46 -0600131 explicit StaticString(const char* czstring) : c_str_(czstring) {}
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000132
Christopher Dunnff617522015-03-06 10:31:46 -0600133 operator const char*() const { return c_str_; }
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000134
Christopher Dunnff617522015-03-06 10:31:46 -0600135 const char* c_str() const { return c_str_; }
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000136
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000137private:
Christopher Dunnff617522015-03-06 10:31:46 -0600138 const char* c_str_;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000139};
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000140
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000141/** \brief Represents a <a HREF="http://www.json.org">JSON</a> value.
142 *
143 * This class is a discriminated union wrapper that can represents a:
144 * - signed integer [range: Value::minInt - Value::maxInt]
145 * - unsigned integer (range: 0 - Value::maxUInt)
146 * - double
147 * - UTF-8 string
148 * - boolean
149 * - 'null'
150 * - an ordered list of Value
151 * - collection of name/value pairs (javascript object)
152 *
153 * The type of the held value is represented by a #ValueType and
154 * can be obtained using type().
155 *
Christopher Dunnc28610f2015-02-21 11:44:16 -0600156 * Values of an #objectValue or #arrayValue can be accessed using operator[]()
157 * methods.
158 * Non-const methods will automatically create the a #nullValue element
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000159 * if it does not exist.
Christopher Dunnc28610f2015-02-21 11:44:16 -0600160 * The sequence of an #arrayValue will be automatically resized and initialized
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000161 * with #nullValue. resize() can be used to enlarge or truncate an #arrayValue.
162 *
Christopher Dunnc28610f2015-02-21 11:44:16 -0600163 * The get() methods can be used to obtain default value in the case the
164 * required element does not exist.
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000165 *
Mathias L. Baumann08ddeed2018-12-12 17:59:43 +0100166 * It is possible to iterate over the list of member keys of an object using
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000167 * the getMemberNames() method.
Christopher Dunn25342ba2015-03-02 18:05:26 -0600168 *
Christopher Dunnc28610f2015-02-21 11:44:16 -0600169 * \note #Value string-length fit in size_t, but keys must be < 2^30.
170 * (The reason is an implementation detail.) A #CharReader will raise an
Christopher Dunn25342ba2015-03-02 18:05:26 -0600171 * exception if a bound is exceeded to avoid security holes in your app,
172 * but the Value API does *not* check bounds. That is the responsibility
173 * of the caller.
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000174 */
175class JSON_API Value {
176 friend class ValueIteratorBase;
Billy Donahueb5e1fe82018-05-20 16:55:27 -0400177
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000178public:
Billy Donahue1c2ed7a2019-01-17 16:35:29 -0500179 typedef std::vector<String> Members;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000180 typedef ValueIterator iterator;
181 typedef ValueConstIterator const_iterator;
182 typedef Json::UInt UInt;
183 typedef Json::Int Int;
184#if defined(JSON_HAS_INT64)
185 typedef Json::UInt64 UInt64;
186 typedef Json::Int64 Int64;
Baptiste Lepilleur842e9ac2010-12-27 17:45:23 +0000187#endif // defined(JSON_HAS_INT64)
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000188 typedef Json::LargestInt LargestInt;
189 typedef Json::LargestUInt LargestUInt;
190 typedef Json::ArrayIndex ArrayIndex;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000191
Wolfram Röslerff6b4492017-09-14 09:31:36 +0200192 // Required for boost integration, e. g. BOOST_TEST
193 typedef std::string value_type;
194
Jordan Bayles25c57812019-07-11 14:27:29 -0700195#if JSON_USE_NULLREF
196 // Binary compatibility kludges, do not use.
197 static const Value& null;
198 static const Value& nullRef;
199#endif
200
201 // null and nullRef are deprecated, use this instead.
202 static Value const& nullSingleton();
Christopher Dunn0f288ae2016-06-26 18:47:43 -0500203
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000204 /// Minimum signed integer value that can be stored in a Json::Value.
Jordan Bayles81ae1d52019-09-16 12:37:14 -0700205 static constexpr LargestInt minLargestInt =
206 LargestInt(~(LargestUInt(-1) / 2));
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000207 /// Maximum signed integer value that can be stored in a Json::Value.
dota177ef0f9f2019-09-17 01:33:47 +0800208 static constexpr LargestInt maxLargestInt = LargestInt(LargestUInt(-1) / 2);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000209 /// Maximum unsigned integer value that can be stored in a Json::Value.
dota177ef0f9f2019-09-17 01:33:47 +0800210 static constexpr LargestUInt maxLargestUInt = LargestUInt(-1);
Baptiste Lepilleur842e9ac2010-12-27 17:45:23 +0000211
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000212 /// Minimum signed int value that can be stored in a Json::Value.
dota177ef0f9f2019-09-17 01:33:47 +0800213 static constexpr Int minInt = Int(~(UInt(-1) / 2));
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000214 /// Maximum signed int value that can be stored in a Json::Value.
dota177ef0f9f2019-09-17 01:33:47 +0800215 static constexpr Int maxInt = Int(UInt(-1) / 2);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000216 /// Maximum unsigned int value that can be stored in a Json::Value.
dota177ef0f9f2019-09-17 01:33:47 +0800217 static constexpr UInt maxUInt = UInt(-1);
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000218
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000219#if defined(JSON_HAS_INT64)
220 /// Minimum signed 64 bits int value that can be stored in a Json::Value.
dota177ef0f9f2019-09-17 01:33:47 +0800221 static constexpr Int64 minInt64 = Int64(~(UInt64(-1) / 2));
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000222 /// Maximum signed 64 bits int value that can be stored in a Json::Value.
dota177ef0f9f2019-09-17 01:33:47 +0800223 static constexpr Int64 maxInt64 = Int64(UInt64(-1) / 2);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000224 /// Maximum unsigned 64 bits int value that can be stored in a Json::Value.
dota177ef0f9f2019-09-17 01:33:47 +0800225 static constexpr UInt64 maxUInt64 = UInt64(-1);
Aaron Jacobsf1053e72011-05-24 03:18:02 +0000226#endif // defined(JSON_HAS_INT64)
Mike Ra07fc532018-03-13 23:35:31 +0300227 /// Default precision for real value for string representation.
dota177ef0f9f2019-09-17 01:33:47 +0800228 static constexpr UInt defaultRealPrecision = 17;
229 // The constant is hard-coded because some compiler have trouble
230 // converting Value::maxUInt64 to a double correctly (AIX/xlC).
231 // Assumes that UInt64 is a 64 bits integer.
232 static constexpr double maxUInt64AsDouble = 18446744073709551615.0;
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:
Billy Donahueb5e1fe82018-05-20 16:55:27 -0400244 enum DuplicationPolicy { noDuplication = 0, duplicate, duplicateOnCopy };
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000245 CZString(ArrayIndex index);
Christopher Dunnc28610f2015-02-21 11:44:16 -0600246 CZString(char const* str, unsigned length, DuplicationPolicy allocate);
247 CZString(CZString const& other);
Motti2b008912015-04-20 17:44:47 +0300248 CZString(CZString&& other);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000249 ~CZString();
Dhruv Paranjape0ba8bd72017-07-08 17:47:13 +0530250 CZString& operator=(const CZString& other);
Dhruv Paranjape0ba8bd72017-07-08 17:47:13 +0530251 CZString& operator=(CZString&& other);
Dhruv Paranjape0ba8bd72017-07-08 17:47:13 +0530252
Christopher Dunnc28610f2015-02-21 11:44:16 -0600253 bool operator<(CZString const& other) const;
254 bool operator==(CZString const& other) const;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000255 ArrayIndex index() const;
Billy Donahueb5e1fe82018-05-20 16:55:27 -0400256 // const char* c_str() const; ///< \deprecated
Christopher Dunnc28610f2015-02-21 11:44:16 -0600257 char const* data() const;
258 unsigned length() const;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000259 bool isStaticString() const;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000260
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000261 private:
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000262 void swap(CZString& other);
Christopher Dunnc28610f2015-02-21 11:44:16 -0600263
Christopher Dunn57ad0512015-03-02 12:10:35 -0600264 struct StringStorage {
Billy Donahueb5e1fe82018-05-20 16:55:27 -0400265 unsigned policy_ : 2;
266 unsigned length_ : 30; // 1GB max
Christopher Dunn57ad0512015-03-02 12:10:35 -0600267 };
268
Billy Donahueb5e1fe82018-05-20 16:55:27 -0400269 char const* cstr_; // actually, a prefixed string, unless policy is noDup
Christopher Dunn57ad0512015-03-02 12:10:35 -0600270 union {
271 ArrayIndex index_;
272 StringStorage storage_;
273 };
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000274 };
275
276public:
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000277 typedef std::map<CZString, Value> ObjectValues;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000278#endif // ifndef JSONCPP_DOC_EXCLUDE_IMPLEMENTATION
279
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000280public:
Billy Donahue483eba82019-07-14 18:41:48 -0400281 /**
282 * \brief Create a default Value of the given type.
283 *
284 * This is a very useful constructor.
285 * To create an empty array, pass arrayValue.
286 * To create an empty object, pass objectValue.
287 * Another Value can then be set to this one by assignment.
288 * This is useful since clear() and resize() will not alter types.
289 *
290 * Examples:
291 * \code
292 * Json::Value null_value; // null
293 * Json::Value arr_value(Json::arrayValue); // []
294 * Json::Value obj_value(Json::objectValue); // {}
295 * \endcode
296 */
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000297 Value(ValueType type = nullValue);
298 Value(Int value);
299 Value(UInt value);
Baptiste Lepilleur842e9ac2010-12-27 17:45:23 +0000300#if defined(JSON_HAS_INT64)
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000301 Value(Int64 value);
302 Value(UInt64 value);
Baptiste Lepilleur842e9ac2010-12-27 17:45:23 +0000303#endif // if defined(JSON_HAS_INT64)
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000304 Value(double value);
Christopher Dunn8a702972015-03-03 10:38:27 -0600305 Value(const char* value); ///< Copy til first 0. (NULL causes to seg-fault.)
Christopher Dunn89704032015-07-11 12:09:59 -0500306 Value(const char* begin, const char* end); ///< Copy all, incl zeroes.
Billy Donahue483eba82019-07-14 18:41:48 -0400307 /**
308 * \brief Constructs a value from a static string.
309 *
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000310 * Like other value string constructor but do not duplicate the string for
Billy Donahue483eba82019-07-14 18:41:48 -0400311 * internal storage. The given string must remain alive after the call to
312 * this constructor.
313 *
Christopher Dunnc28610f2015-02-21 11:44:16 -0600314 * \note This works only for null-terminated strings. (We cannot change the
Billy Donahue483eba82019-07-14 18:41:48 -0400315 * size of this class, so we have nowhere to store the length, which might be
316 * computed later for various operations.)
Christopher Dunnc28610f2015-02-21 11:44:16 -0600317 *
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000318 * Example of usage:
Billy Donahue483eba82019-07-14 18:41:48 -0400319 * \code
320 * static StaticString foo("some text");
321 * Json::Value aValue(foo);
322 * \endcode
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000323 */
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000324 Value(const StaticString& value);
Billy Donahue483eba82019-07-14 18:41:48 -0400325 Value(const String& value);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000326 Value(bool value);
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000327 Value(const Value& other);
Motti2b008912015-04-20 17:44:47 +0300328 Value(Value&& other);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000329 ~Value();
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000330
Billy Donahue0c1cc6e2019-01-20 23:59:16 -0500331 /// \note Overwrite existing comments. To preserve comments, use
Billy Donahueb5e1fe82018-05-20 16:55:27 -0400332 /// #swapPayload().
Billy Donahue0c1cc6e2019-01-20 23:59:16 -0500333 Value& operator=(const Value& other);
334 Value& operator=(Value&& other);
Dhruv Paranjape8996c372017-07-08 17:27:07 +0530335
Christopher Dunn66eb72f2015-01-20 11:02:22 -0600336 /// Swap everything.
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000337 void swap(Value& other);
Christopher Dunn66eb72f2015-01-20 11:02:22 -0600338 /// Swap values but leave comments and source offsets in place.
339 void swapPayload(Value& other);
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000340
Dhruv Paranjape8996c372017-07-08 17:27:07 +0530341 /// copy everything.
342 void copy(const Value& other);
343 /// copy values but leave comments and source offsets in place.
344 void copyPayload(const Value& other);
345
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000346 ValueType type() const;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000347
Christopher Dunn66eb72f2015-01-20 11:02:22 -0600348 /// Compare payload only, not comments etc.
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000349 bool operator<(const Value& other) const;
350 bool operator<=(const Value& other) const;
351 bool operator>=(const Value& other) const;
352 bool operator>(const Value& other) const;
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000353 bool operator==(const Value& other) const;
354 bool operator!=(const Value& other) const;
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000355 int compare(const Value& other) const;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000356
Christopher Dunn8a702972015-03-03 10:38:27 -0600357 const char* asCString() const; ///< Embedded zeroes could cause you trouble!
dawescae564652016-03-14 19:11:02 -0500358#if JSONCPP_USING_SECURE_MEMORY
Billy Donahueb5e1fe82018-05-20 16:55:27 -0400359 unsigned getCStringLength() const; // Allows you to understand the length of
360 // the CString
dawescae564652016-03-14 19:11:02 -0500361#endif
Billy Donahue1c2ed7a2019-01-17 16:35:29 -0500362 String asString() const; ///< Embedded zeroes are possible.
Christopher Dunnc28610f2015-02-21 11:44:16 -0600363 /** Get raw char* of string-value.
364 * \return false if !string. (Seg-fault if str or end are NULL.)
365 */
Billy Donahueb5e1fe82018-05-20 16:55:27 -0400366 bool getString(char const** begin, char const** end) const;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000367 Int asInt() const;
368 UInt asUInt() const;
Aaron Jacobsf1053e72011-05-24 03:18:02 +0000369#if defined(JSON_HAS_INT64)
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000370 Int64 asInt64() const;
371 UInt64 asUInt64() const;
Aaron Jacobsf1053e72011-05-24 03:18:02 +0000372#endif // if defined(JSON_HAS_INT64)
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000373 LargestInt asLargestInt() const;
374 LargestUInt asLargestUInt() const;
375 float asFloat() const;
376 double asDouble() const;
377 bool asBool() const;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000378
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000379 bool isNull() const;
380 bool isBool() const;
381 bool isInt() const;
382 bool isInt64() const;
383 bool isUInt() const;
384 bool isUInt64() const;
385 bool isIntegral() const;
386 bool isDouble() const;
387 bool isNumeric() const;
388 bool isString() const;
389 bool isArray() const;
390 bool isObject() const;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000391
Billy Donahue53c8e2c2019-11-11 22:43:52 -0500392 /// The `as<T>` and `is<T>` member function templates and specializations.
393 template <typename T> T as() const = delete;
394 template <typename T> bool is() const = delete;
395
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000396 bool isConvertibleTo(ValueType other) const;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000397
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000398 /// Number of values in array or object
399 ArrayIndex size() const;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000400
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000401 /// \brief Return true if empty array, empty object, or null;
402 /// otherwise, false.
403 bool empty() const;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000404
Wolfram Rösler90794222017-12-05 18:18:55 +0100405 /// Return !isNull()
drgler04abe382018-01-13 15:28:19 +0100406 JSONCPP_OP_EXPLICIT operator bool() const;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000407
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000408 /// Remove all object members and array elements.
409 /// \pre type() is arrayValue, objectValue, or nullValue
410 /// \post type() is unchanged
411 void clear();
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000412
Marian Klymovfc201342018-06-02 20:15:26 +0300413 /// Resize the array to newSize elements.
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000414 /// New elements are initialized to null.
415 /// May only be called on nullValue or arrayValue.
416 /// \pre type() is arrayValue or nullValue
417 /// \post type() is arrayValue
Marian Klymovfc201342018-06-02 20:15:26 +0300418 void resize(ArrayIndex newSize);
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000419
Billy Donahue483eba82019-07-14 18:41:48 -0400420 //@{
421 /// Access an array element (zero based index). If the array contains less
422 /// than index element, then null value are inserted in the array so that
423 /// its size is index+1.
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000424 /// (You may need to say 'value[0u]' to get your compiler to distinguish
Billy Donahue483eba82019-07-14 18:41:48 -0400425 /// this from the operator[] which takes a string.)
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000426 Value& operator[](ArrayIndex index);
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000427 Value& operator[](int index);
Billy Donahue483eba82019-07-14 18:41:48 -0400428 //@}
Baptiste Lepilleurfa130ef2010-12-24 12:47:14 +0000429
Billy Donahue483eba82019-07-14 18:41:48 -0400430 //@{
431 /// Access an array element (zero based index).
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000432 /// (You may need to say 'value[0u]' to get your compiler to distinguish
Billy Donahue483eba82019-07-14 18:41:48 -0400433 /// this from the operator[] which takes a string.)
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000434 const Value& operator[](ArrayIndex index) const;
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000435 const Value& operator[](int index) const;
Billy Donahue483eba82019-07-14 18:41:48 -0400436 //@}
Baptiste Lepilleurfa130ef2010-12-24 12:47:14 +0000437
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000438 /// If the array contains at least index+1 elements, returns the element
Billy Donahue483eba82019-07-14 18:41:48 -0400439 /// value, otherwise returns defaultValue.
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000440 Value get(ArrayIndex index, const Value& defaultValue) const;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000441 /// Return true if index < size().
442 bool isValidIndex(ArrayIndex index) const;
443 /// \brief Append value to array at the end.
444 ///
445 /// Equivalent to jsonvalue[jsonvalue.size()] = value;
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000446 Value& append(const Value& value);
Dhruv Paranjape23c44d92017-07-08 17:30:47 +0530447 Value& append(Value&& value);
Chena0bd9ad2019-12-03 09:13:42 +0800448
dota17bdacfd72019-10-16 14:33:53 +0800449 /// \brief Insert value in array at specific index
Chena0bd9ad2019-12-03 09:13:42 +0800450 bool insert(ArrayIndex index, const Value& newValue);
451 bool insert(ArrayIndex index, Value&& newValue);
Dhruv Paranjape23c44d92017-07-08 17:30:47 +0530452
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000453 /// Access an object value by name, create a null member if it does not exist.
Christopher Dunn25342ba2015-03-02 18:05:26 -0600454 /// \note Because of our implementation, keys are limited to 2^30 -1 chars.
Billy Donahue483eba82019-07-14 18:41:48 -0400455 /// Exceeding that will cause an exception.
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000456 Value& operator[](const char* key);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000457 /// Access an object value by name, returns null if there is no member with
458 /// that name.
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000459 const Value& operator[](const char* key) const;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000460 /// Access an object value by name, create a null member if it does not exist.
Christopher Dunn25342ba2015-03-02 18:05:26 -0600461 /// \param key may contain embedded nulls.
Billy Donahue1c2ed7a2019-01-17 16:35:29 -0500462 Value& operator[](const String& key);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000463 /// Access an object value by name, returns null if there is no member with
464 /// that name.
Christopher Dunn25342ba2015-03-02 18:05:26 -0600465 /// \param key may contain embedded nulls.
Billy Donahue1c2ed7a2019-01-17 16:35:29 -0500466 const Value& operator[](const String& key) const;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000467 /** \brief Access an object value by name, create a null member if it does not
Billy Donahue483eba82019-07-14 18:41:48 -0400468 * exist.
469 *
Billy Donahueb5e1fe82018-05-20 16:55:27 -0400470 * If the object has no entry for that name, then the member name used to
Billy Donahue483eba82019-07-14 18:41:48 -0400471 * store the new entry is not duplicated.
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000472 * Example of use:
Billy Donahue483eba82019-07-14 18:41:48 -0400473 * \code
474 * Json::Value object;
475 * static const StaticString code("code");
476 * object[code] = 1234;
477 * \endcode
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000478 */
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000479 Value& operator[](const StaticString& key);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000480 /// Return the member named key if it exist, defaultValue otherwise.
Christopher Dunn0fd28752015-03-05 16:38:43 -0600481 /// \note deep copy
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000482 Value get(const char* key, const Value& defaultValue) const;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000483 /// Return the member named key if it exist, defaultValue otherwise.
Christopher Dunn0fd28752015-03-05 16:38:43 -0600484 /// \note deep copy
Christopher Dunn89704032015-07-11 12:09:59 -0500485 /// \note key may contain embedded nulls.
Jordan Baylesf34bf242019-10-11 11:19:00 -0700486 Value get(const char* begin, const char* end,
487 const Value& defaultValue) const;
Christopher Dunn25342ba2015-03-02 18:05:26 -0600488 /// Return the member named key if it exist, defaultValue otherwise.
Christopher Dunn0fd28752015-03-05 16:38:43 -0600489 /// \note deep copy
Christopher Dunn25342ba2015-03-02 18:05:26 -0600490 /// \param key may contain embedded nulls.
Billy Donahue1c2ed7a2019-01-17 16:35:29 -0500491 Value get(const String& key, const Value& defaultValue) const;
Christopher Dunn25342ba2015-03-02 18:05:26 -0600492 /// Most general and efficient version of isMember()const, get()const,
493 /// and operator[]const
Christopher Dunn89704032015-07-11 12:09:59 -0500494 /// \note As stated elsewhere, behavior is undefined if (end-begin) >= 2^30
495 Value const* find(char const* begin, char const* end) const;
Christopher Dunnc28610f2015-02-21 11:44:16 -0600496 /// Most general and efficient version of object-mutators.
Christopher Dunn89704032015-07-11 12:09:59 -0500497 /// \note As stated elsewhere, behavior is undefined if (end-begin) >= 2^30
Christopher Dunnc28610f2015-02-21 11:44:16 -0600498 /// \return non-zero, but JSON_ASSERT if this is neither object nor nullValue.
Frank Richterd76fe562019-03-23 14:31:06 +0100499 Value* demand(char const* begin, char const* end);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000500 /// \brief Remove and return the named member.
501 ///
502 /// Do nothing if it did not exist.
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000503 /// \pre type() is objectValue or nullValue
504 /// \post type() is unchanged
Wolfram Röslera06b3902017-10-18 07:19:27 +0200505 void removeMember(const char* key);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000506 /// Same as removeMember(const char*)
Christopher Dunn25342ba2015-03-02 18:05:26 -0600507 /// \param key may contain embedded nulls.
Billy Donahue1c2ed7a2019-01-17 16:35:29 -0500508 void removeMember(const String& key);
Christopher Dunn89704032015-07-11 12:09:59 -0500509 /// Same as removeMember(const char* begin, const char* end, Value* removed),
Christopher Dunn25342ba2015-03-02 18:05:26 -0600510 /// but 'key' is null-terminated.
511 bool removeMember(const char* key, Value* removed);
Christopher Dunn76746b02015-01-21 16:01:30 -0600512 /** \brief Remove the named map member.
Billy Donahue483eba82019-07-14 18:41:48 -0400513 *
514 * Update 'removed' iff removed.
515 * \param key may contain embedded nulls.
516 * \return true iff removed (no exceptions)
517 */
Billy Donahue1c2ed7a2019-01-17 16:35:29 -0500518 bool removeMember(String const& key, Value* removed);
519 /// Same as removeMember(String const& key, Value* removed)
Christopher Dunn89704032015-07-11 12:09:59 -0500520 bool removeMember(const char* begin, const char* end, Value* removed);
Christopher Dunn9de2c2d2015-01-20 16:15:40 -0600521 /** \brief Remove the indexed array element.
Billy Donahue483eba82019-07-14 18:41:48 -0400522 *
523 * O(n) expensive operations.
524 * Update 'removed' iff removed.
525 * \return true if removed (no exceptions)
526 */
Marian Klymovfc201342018-06-02 20:15:26 +0300527 bool removeIndex(ArrayIndex index, Value* removed);
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000528
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000529 /// Return true if the object has a member named key.
Christopher Dunnc28610f2015-02-21 11:44:16 -0600530 /// \note 'key' must be null-terminated.
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000531 bool isMember(const char* key) const;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000532 /// Return true if the object has a member named key.
Christopher Dunn25342ba2015-03-02 18:05:26 -0600533 /// \param key may contain embedded nulls.
Billy Donahue1c2ed7a2019-01-17 16:35:29 -0500534 bool isMember(const String& key) const;
535 /// Same as isMember(String const& key)const
Christopher Dunn89704032015-07-11 12:09:59 -0500536 bool isMember(const char* begin, const char* end) const;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000537
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000538 /// \brief Return a list of the member names.
539 ///
540 /// If null, return an empty list.
541 /// \pre type() is objectValue or nullValue
542 /// \post if type() was nullValue, it remains nullValue
543 Members getMemberNames() const;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000544
Christopher Dunn1e3149a2015-01-25 14:16:13 -0600545 /// \deprecated Always pass len.
Jordan Baylesf34bf242019-10-11 11:19:00 -0700546 JSONCPP_DEPRECATED("Use setComment(String const&) instead.")
547 void setComment(const char* comment, CommentPlacement placement) {
Billy Donahue433107f2019-01-20 21:53:01 -0500548 setComment(String(comment, strlen(comment)), placement);
549 }
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000550 /// Comments must be //... or /* ... */
Billy Donahue433107f2019-01-20 21:53:01 -0500551 void setComment(const char* comment, size_t len, CommentPlacement placement) {
552 setComment(String(comment, len), placement);
553 }
Christopher Dunn1e3149a2015-01-25 14:16:13 -0600554 /// Comments must be //... or /* ... */
Billy Donahue433107f2019-01-20 21:53:01 -0500555 void setComment(String comment, CommentPlacement placement);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000556 bool hasComment(CommentPlacement placement) const;
557 /// Include delimiters and embedded newlines.
Billy Donahue1c2ed7a2019-01-17 16:35:29 -0500558 String getComment(CommentPlacement placement) const;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000559
Billy Donahue1c2ed7a2019-01-17 16:35:29 -0500560 String toStyledString() const;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000561
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000562 const_iterator begin() const;
563 const_iterator end() const;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000564
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000565 iterator begin();
566 iterator end();
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000567
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000568 // Accessors for the [start, limit) range of bytes within the JSON text from
569 // which this value was parsed, if any.
Christopher Dunnd4513fc2016-02-06 09:25:20 -0600570 void setOffsetStart(ptrdiff_t start);
571 void setOffsetLimit(ptrdiff_t limit);
572 ptrdiff_t getOffsetStart() const;
573 ptrdiff_t getOffsetLimit() const;
Aaron Jacobs68db6552014-04-23 23:41:12 +0000574
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000575private:
Jordan Baylesd5bd1a72019-06-24 14:06:45 -0700576 void setType(ValueType v) {
577 bits_.value_type_ = static_cast<unsigned char>(v);
578 }
Billy Donahue0c1cc6e2019-01-20 23:59:16 -0500579 bool isAllocated() const { return bits_.allocated_; }
580 void setIsAllocated(bool v) { bits_.allocated_ = v; }
581
Billy Donahue8eb5d892014-11-10 01:35:42 -0500582 void initBasic(ValueType type, bool allocated = false);
Andrey Okoshkin9b569c82018-01-12 15:59:20 +0300583 void dupPayload(const Value& other);
Andrey Okoshkinc69148c2018-01-12 11:26:34 +0300584 void releasePayload();
Andrey Okoshkin9b569c82018-01-12 15:59:20 +0300585 void dupMeta(const Value& other);
Billy Donahue8eb5d892014-11-10 01:35:42 -0500586
Christopher Dunnc28610f2015-02-21 11:44:16 -0600587 Value& resolveReference(const char* key);
588 Value& resolveReference(const char* key, const char* end);
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000589
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000590 // struct MemberNamesTransform
591 //{
592 // typedef const char *result_type;
593 // const char *operator()( const CZString &name ) const
594 // {
595 // return name.c_str();
596 // }
597 //};
598
599 union ValueHolder {
600 LargestInt int_;
601 LargestUInt uint_;
602 double real_;
603 bool bool_;
Billy Donahue0c1cc6e2019-01-20 23:59:16 -0500604 char* string_; // if allocated_, ptr to { unsigned, char[] }.
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000605 ObjectValues* map_;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000606 } value_;
Billy Donahue0c1cc6e2019-01-20 23:59:16 -0500607
608 struct {
609 // Really a ValueType, but types should agree for bitfield packing.
610 unsigned int value_type_ : 8;
611 // Unless allocated_, string_ must be null-terminated.
612 unsigned int allocated_ : 1;
613 } bits_;
614
Billy Donahue433107f2019-01-20 21:53:01 -0500615 class Comments {
616 public:
617 Comments() = default;
618 Comments(const Comments& that);
Billy Donahue00558b32019-01-21 16:42:25 -0500619 Comments(Comments&& that);
Billy Donahue433107f2019-01-20 21:53:01 -0500620 Comments& operator=(const Comments& that);
Billy Donahue00558b32019-01-21 16:42:25 -0500621 Comments& operator=(Comments&& that);
Billy Donahue433107f2019-01-20 21:53:01 -0500622 bool has(CommentPlacement slot) const;
623 String get(CommentPlacement slot) const;
Rosen Penevbcad4e42019-10-15 00:27:23 -0700624 void set(CommentPlacement slot, String comment);
Billy Donahue433107f2019-01-20 21:53:01 -0500625
626 private:
627 using Array = std::array<String, numberOfCommentPlacement>;
628 std::unique_ptr<Array> ptr_;
629 };
630 Comments comments_;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000631
632 // [start, limit) byte offsets in the source JSON text from which this Value
633 // was extracted.
Christopher Dunnd4513fc2016-02-06 09:25:20 -0600634 ptrdiff_t start_;
635 ptrdiff_t limit_;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000636};
637
Billy Donahue53c8e2c2019-11-11 22:43:52 -0500638template <> inline bool Value::as<bool>() const { return asBool(); }
639template <> inline bool Value::is<bool>() const { return isBool(); }
640
641template <> inline Int Value::as<Int>() const { return asInt(); }
642template <> inline bool Value::is<Int>() const { return isInt(); }
643
644template <> inline UInt Value::as<UInt>() const { return asUInt(); }
645template <> inline bool Value::is<UInt>() const { return isUInt(); }
646
647#if defined(JSON_HAS_INT64)
648template <> inline Int64 Value::as<Int64>() const { return asInt64(); }
649template <> inline bool Value::is<Int64>() const { return isInt64(); }
650
651template <> inline UInt64 Value::as<UInt64>() const { return asUInt64(); }
652template <> inline bool Value::is<UInt64>() const { return isUInt64(); }
653#endif
654
655template <> inline double Value::as<double>() const { return asDouble(); }
656template <> inline bool Value::is<double>() const { return isDouble(); }
657
658template <> inline String Value::as<String>() const { return asString(); }
659template <> inline bool Value::is<String>() const { return isString(); }
660
661/// These `as` specializations are type conversions, and do not have a
662/// corresponding `is`.
663template <> inline float Value::as<float>() const { return asFloat(); }
664template <> inline const char* Value::as<const char*>() const {
665 return asCString();
666}
Billy Donahue53c8e2c2019-11-11 22:43:52 -0500667
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000668/** \brief Experimental and untested: represents an element of the "path" to
669 * access a node.
670 */
671class JSON_API PathArgument {
672public:
673 friend class Path;
674
675 PathArgument();
676 PathArgument(ArrayIndex index);
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000677 PathArgument(const char* key);
Rosen Penevbcad4e42019-10-15 00:27:23 -0700678 PathArgument(String key);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000679
680private:
Billy Donahueb5e1fe82018-05-20 16:55:27 -0400681 enum Kind { kindNone = 0, kindIndex, kindKey };
Billy Donahue1c2ed7a2019-01-17 16:35:29 -0500682 String key_;
Hans Johnsone817e4f2019-01-14 17:09:22 -0600683 ArrayIndex index_{};
Billy Donahue2b593a92019-01-18 03:46:57 -0500684 Kind kind_{kindNone};
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000685};
686
687/** \brief Experimental and untested: represents a "path" to access a node.
688 *
689 * Syntax:
690 * - "." => root node
691 * - ".[n]" => elements at index 'n' of root node (an array value)
692 * - ".name" => member named 'name' of root node (an object value)
693 * - ".name1.name2.name3"
694 * - ".[0][1][2].name1[3]"
695 * - ".%" => member name is provided as parameter
aliha472adb62019-08-26 15:37:05 -0400696 * - ".[%]" => index is provided as parameter
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000697 */
698class JSON_API Path {
699public:
Jordan Baylesf34bf242019-10-11 11:19:00 -0700700 Path(const String& path, const PathArgument& a1 = PathArgument(),
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000701 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
Billy Donahue1c2ed7a2019-01-17 16:35:29 -0500716 void makePath(const String& path, const InArgs& in);
Jordan Baylesf34bf242019-10-11 11:19:00 -0700717 void addPathInArg(const String& path, const InArgs& in,
718 InArgs::const_iterator& itInArg, PathArgument::Kind kind);
Billy Donahue1c2ed7a2019-01-17 16:35:29 -0500719 static void invalidPath(const String& path, int location);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000720
721 Args args_;
722};
723
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000724/** \brief base class for Value iterators.
725 *
726 */
727class JSON_API ValueIteratorBase {
728public:
729 typedef std::bidirectional_iterator_tag iterator_category;
730 typedef unsigned int size_t;
731 typedef int difference_type;
732 typedef ValueIteratorBase SelfType;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000733
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000734 bool operator==(const SelfType& other) const { return isEqual(other); }
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 difference_type operator-(const SelfType& other) const {
Kevin Grant4c5832a2015-02-14 20:53:35 -0800739 return other.computeDistance(*this);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000740 }
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000741
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000742 /// Return either the index or the member name of the referenced value as a
743 /// Value.
744 Value key() const;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000745
Billy Donahueb5e1fe82018-05-20 16:55:27 -0400746 /// Return the index of the referenced Value, or -1 if it is not an
747 /// arrayValue.
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000748 UInt index() const;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000749
Christopher Dunned495ed2015-03-08 14:01:28 -0500750 /// Return the member name of the referenced Value, or "" if it is not an
751 /// objectValue.
752 /// \note Avoid `c_str()` on result, as embedded zeroes are possible.
Billy Donahue1c2ed7a2019-01-17 16:35:29 -0500753 String name() const;
Christopher Dunned495ed2015-03-08 14:01:28 -0500754
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000755 /// Return the member name of the referenced Value. "" if it is not an
756 /// objectValue.
Billy Donahueb5e1fe82018-05-20 16:55:27 -0400757 /// \deprecated This cannot be used for UTF-8 strings, since there can be
758 /// embedded nulls.
Jordan Baylesf34bf242019-10-11 11:19:00 -0700759 JSONCPP_DEPRECATED("Use `key = name();` instead.")
760 char const* memberName() const;
Christopher Dunnc28610f2015-02-21 11:44:16 -0600761 /// 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:
Hans Johnsonb0826932019-10-17 12:52:13 -0500767 /*! Internal utility functions to assist with implementing
768 * other iterator functions. The const and non-const versions
769 * of the "deref" protected methods expose the protected
770 * current_ member variable in a way that can often be
771 * optimized away by the compiler.
772 */
773 const Value& deref() const;
774 Value& deref();
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000775
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000776 void increment();
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000777
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000778 void decrement();
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000779
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000780 difference_type computeDistance(const SelfType& other) const;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000781
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000782 bool isEqual(const SelfType& other) const;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000783
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000784 void copy(const SelfType& other);
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000785
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000786private:
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000787 Value::ObjectValues::iterator current_;
788 // Indicates that iterator is for a null value.
Billy Donahue2b593a92019-01-18 03:46:57 -0500789 bool isNull_{true};
Christopher Dunn2a10f4a2015-04-28 04:55:12 +0100790
791public:
792 // For some reason, BORLAND needs these at the end, rather
793 // than earlier. No idea why.
794 ValueIteratorBase();
795 explicit ValueIteratorBase(const Value::ObjectValues::iterator& current);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000796};
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000797
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000798/** \brief const iterator for object and array value.
799 *
800 */
801class JSON_API ValueConstIterator : public ValueIteratorBase {
802 friend class Value;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000803
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000804public:
805 typedef const Value value_type;
Billy Donahueb5e1fe82018-05-20 16:55:27 -0400806 // typedef unsigned int size_t;
807 // typedef int difference_type;
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000808 typedef const Value& reference;
809 typedef const Value* pointer;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000810 typedef ValueConstIterator SelfType;
811
812 ValueConstIterator();
ycqiuc8a8cfc2015-10-06 16:46:19 +0800813 ValueConstIterator(ValueIterator const& other);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000814
815private:
Billy Donahueb5e1fe82018-05-20 16:55:27 -0400816 /*! \internal Use by Value to create an iterator.
817 */
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000818 explicit ValueConstIterator(const Value::ObjectValues::iterator& current);
Billy Donahueb5e1fe82018-05-20 16:55:27 -0400819
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000820public:
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000821 SelfType& operator=(const ValueIteratorBase& other);
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 Jacobs9fa4e842014-07-01 08:48:54 +1000829 SelfType operator--(int) {
830 SelfType temp(*this);
831 --*this;
832 return temp;
833 }
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000834
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000835 SelfType& operator--() {
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000836 decrement();
837 return *this;
838 }
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000839
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000840 SelfType& operator++() {
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000841 increment();
842 return *this;
843 }
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000844
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000845 reference operator*() const { return deref(); }
Braden McDorman540db3b2014-09-14 02:31:23 -0500846
847 pointer operator->() const { return &deref(); }
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000848};
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000849
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000850/** \brief Iterator for object and array value.
851 */
852class JSON_API ValueIterator : public ValueIteratorBase {
853 friend class Value;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000854
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000855public:
856 typedef Value value_type;
857 typedef unsigned int size_t;
858 typedef int difference_type;
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000859 typedef Value& reference;
860 typedef Value* pointer;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000861 typedef ValueIterator SelfType;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000862
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000863 ValueIterator();
ycqiuc8a8cfc2015-10-06 16:46:19 +0800864 explicit ValueIterator(const ValueConstIterator& other);
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000865 ValueIterator(const ValueIterator& other);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000866
867private:
Billy Donahueb5e1fe82018-05-20 16:55:27 -0400868 /*! \internal Use by Value to create an iterator.
869 */
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000870 explicit ValueIterator(const Value::ObjectValues::iterator& current);
Billy Donahueb5e1fe82018-05-20 16:55:27 -0400871
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000872public:
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000873 SelfType& operator=(const SelfType& other);
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 Jacobs9fa4e842014-07-01 08:48:54 +1000881 SelfType operator--(int) {
882 SelfType temp(*this);
883 --*this;
884 return temp;
885 }
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000886
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000887 SelfType& operator--() {
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000888 decrement();
889 return *this;
890 }
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000891
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000892 SelfType& operator++() {
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000893 increment();
894 return *this;
895 }
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000896
Hans Johnsonb0826932019-10-17 12:52:13 -0500897 /*! The return value of non-const iterators can be
898 * changed, so the these functions are not const
899 * because the returned references/pointers can be used
900 * to change state of the base class.
901 */
902 reference operator*() { return deref(); }
903 pointer operator->() { return &deref(); }
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000904};
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000905
Billy Donahue1d956282018-03-06 12:51:58 -0500906inline void swap(Value& a, Value& b) { a.swap(b); }
907
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000908} // namespace Json
909
Sergiy80d6e666f2016-12-03 22:29:14 +0200910#pragma pack(pop)
datadiode9454e682015-01-20 15:25:04 -0600911
Baptiste Lepilleureafd7022013-05-08 20:21:11 +0000912#if defined(JSONCPP_DISABLE_DLL_INTERFACE_WARNING)
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000913#pragma warning(pop)
Baptiste Lepilleureafd7022013-05-08 20:21:11 +0000914#endif // if defined(JSONCPP_DISABLE_DLL_INTERFACE_WARNING)
915
Jordan Bayles9704ced2019-11-14 09:38:11 -0800916#endif // JSON_H_INCLUDED