blob: c9729241af94f216fbef3dc8a35a20176c6e7cd6 [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 Donahue433107f2019-01-20 21:53:01 -050012#include <array>
Billy Donahueb5e1fe82018-05-20 16:55:27 -040013#include <exception>
Billy Donahue433107f2019-01-20 21:53:01 -050014#include <memory>
Aaron Jacobs9fa4e842014-07-01 08:48:54 +100015#include <string>
16#include <vector>
Christopher Dunn6d135cb2007-06-13 15:51:04 +000017
Aaron Jacobs9fa4e842014-07-01 08:48:54 +100018#ifndef JSON_USE_CPPTL_SMALLMAP
19#include <map>
20#else
21#include <cpptl/smallmap.h>
22#endif
23#ifdef JSON_USE_CPPTL
24#include <cpptl/forwards.h>
25#endif
Christopher Dunn6d135cb2007-06-13 15:51:04 +000026
Aaron Jacobs9fa4e842014-07-01 08:48:54 +100027// Disable warning C4251: <data member>: <type> needs to have dll-interface to
28// be used by...
Baptiste Lepilleureafd7022013-05-08 20:21:11 +000029#if defined(JSONCPP_DISABLE_DLL_INTERFACE_WARNING)
Aaron Jacobs9fa4e842014-07-01 08:48:54 +100030#pragma warning(push)
31#pragma warning(disable : 4251)
Baptiste Lepilleureafd7022013-05-08 20:21:11 +000032#endif // if defined(JSONCPP_DISABLE_DLL_INTERFACE_WARNING)
33
Sergiy80d6e666f2016-12-03 22:29:14 +020034#pragma pack(push, 8)
35
Christopher Dunn6d135cb2007-06-13 15:51:04 +000036/** \brief JSON (JavaScript Object Notation).
37 */
38namespace Json {
39
Jordan Bayles83cc9212019-06-06 13:41:47 -070040#if JSON_USE_EXCEPTION
Christopher Dunn75279cc2015-03-08 12:20:06 -050041/** Base class for all exceptions we throw.
Christopher Dunn4e30c4f2015-03-08 12:56:32 -050042 *
43 * We use nothing but these internally. Of course, STL can throw others.
Christopher Dunn75279cc2015-03-08 12:20:06 -050044 */
Christopher Dunn949babd2015-07-23 00:19:12 -050045class JSON_API Exception : public std::exception {
46public:
Billy Donahue1c2ed7a2019-01-17 16:35:29 -050047 Exception(String msg);
Hans Johnson2853b1c2019-01-11 13:58:53 -060048 ~Exception() JSONCPP_NOEXCEPT override;
49 char const* what() const JSONCPP_NOEXCEPT override;
Billy Donahueb5e1fe82018-05-20 16:55:27 -040050
Christopher Dunn949babd2015-07-23 00:19:12 -050051protected:
Billy Donahue1c2ed7a2019-01-17 16:35:29 -050052 String msg_;
Christopher Dunn949babd2015-07-23 00:19:12 -050053};
54
Christopher Dunn53837942015-03-08 12:31:00 -050055/** Exceptions which the user cannot easily avoid.
56 *
Christopher Dunn4e30c4f2015-03-08 12:56:32 -050057 * E.g. out-of-memory (when we use malloc), stack-overflow, malicious input
Dhruv Paranjape8996c372017-07-08 17:27:07 +053058 *
Christopher Dunn4e30c4f2015-03-08 12:56:32 -050059 * \remark derived from Json::Exception
Christopher Dunn53837942015-03-08 12:31:00 -050060 */
Christopher Dunn949babd2015-07-23 00:19:12 -050061class JSON_API RuntimeError : public Exception {
62public:
Billy Donahue1c2ed7a2019-01-17 16:35:29 -050063 RuntimeError(String const& msg);
Christopher Dunn949babd2015-07-23 00:19:12 -050064};
65
Christopher Dunn4e30c4f2015-03-08 12:56:32 -050066/** Exceptions thrown by JSON_ASSERT/JSON_FAIL macros.
Christopher Dunn53837942015-03-08 12:31:00 -050067 *
68 * These are precondition-violations (user bugs) and internal errors (our bugs).
Dhruv Paranjape8996c372017-07-08 17:27:07 +053069 *
Christopher Dunn4e30c4f2015-03-08 12:56:32 -050070 * \remark derived from Json::Exception
Christopher Dunn53837942015-03-08 12:31:00 -050071 */
Christopher Dunn949babd2015-07-23 00:19:12 -050072class JSON_API LogicError : public Exception {
73public:
Billy Donahue1c2ed7a2019-01-17 16:35:29 -050074 LogicError(String const& msg);
Christopher Dunn949babd2015-07-23 00:19:12 -050075};
Jordan Bayles83cc9212019-06-06 13:41:47 -070076#endif
Christopher Dunn53837942015-03-08 12:31:00 -050077
Christopher Dunn4e30c4f2015-03-08 12:56:32 -050078/// used internally
Jordan Bayles9ef812a2019-07-09 15:03:39 -070079[[noreturn]] void throwRuntimeError(String const& msg);
Christopher Dunn4e30c4f2015-03-08 12:56:32 -050080/// used internally
Jordan Bayles9ef812a2019-07-09 15:03:39 -070081[[noreturn]] void throwLogicError(String const& msg);
Christopher Dunn75279cc2015-03-08 12:20:06 -050082
Aaron Jacobs9fa4e842014-07-01 08:48:54 +100083/** \brief Type of the value held by a Value object.
84 */
85enum ValueType {
86 nullValue = 0, ///< 'null' value
87 intValue, ///< signed integer value
88 uintValue, ///< unsigned integer value
89 realValue, ///< double value
90 stringValue, ///< UTF-8 string value
91 booleanValue, ///< bool value
92 arrayValue, ///< array value (ordered list)
93 objectValue ///< object value (collection of name/value pairs).
94};
Christopher Dunn6d135cb2007-06-13 15:51:04 +000095
Aaron Jacobs9fa4e842014-07-01 08:48:54 +100096enum CommentPlacement {
97 commentBefore = 0, ///< a comment placed on the line before a value
98 commentAfterOnSameLine, ///< a comment just after a value on the same line
99 commentAfter, ///< a comment on the line after a value (only make sense for
Aaron Jacobs3a0c4fc2014-07-01 09:20:48 +1000100 /// root value)
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000101 numberOfCommentPlacement
102};
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000103
Mike Ra07fc532018-03-13 23:35:31 +0300104/** \brief Type of precision for formatting of real values.
105 */
106enum PrecisionType {
107 significantDigits = 0, ///< we set max number of significant digits in string
108 decimalPlaces ///< we set max number of digits after "." in string
109};
110
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000111//# ifdef JSON_USE_CPPTL
112// typedef CppTL::AnyEnumerator<const char *> EnumMemberNames;
113// typedef CppTL::AnyEnumerator<const Value &> EnumValues;
114//# endif
115
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000116/** \brief Lightweight wrapper to tag static string.
117 *
Josh Sorefe6a588a2017-12-03 11:54:29 -0500118 * Value constructor and objectValue member assignment takes advantage of the
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000119 * StaticString and avoid the cost of string duplication when storing the
120 * string or the member name.
121 *
122 * Example of usage:
123 * \code
124 * Json::Value aValue( StaticString("some text") );
125 * Json::Value object;
126 * static const StaticString code("code");
127 * object[code] = 1234;
128 * \endcode
129 */
130class JSON_API StaticString {
131public:
Christopher Dunnff617522015-03-06 10:31:46 -0600132 explicit StaticString(const char* czstring) : c_str_(czstring) {}
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000133
Christopher Dunnff617522015-03-06 10:31:46 -0600134 operator const char*() const { return c_str_; }
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000135
Christopher Dunnff617522015-03-06 10:31:46 -0600136 const char* c_str() const { return c_str_; }
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000137
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000138private:
Christopher Dunnff617522015-03-06 10:31:46 -0600139 const char* c_str_;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000140};
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000141
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000142/** \brief Represents a <a HREF="http://www.json.org">JSON</a> value.
143 *
144 * This class is a discriminated union wrapper that can represents a:
145 * - signed integer [range: Value::minInt - Value::maxInt]
146 * - unsigned integer (range: 0 - Value::maxUInt)
147 * - double
148 * - UTF-8 string
149 * - boolean
150 * - 'null'
151 * - an ordered list of Value
152 * - collection of name/value pairs (javascript object)
153 *
154 * The type of the held value is represented by a #ValueType and
155 * can be obtained using type().
156 *
Christopher Dunnc28610f2015-02-21 11:44:16 -0600157 * Values of an #objectValue or #arrayValue can be accessed using operator[]()
158 * methods.
159 * Non-const methods will automatically create the a #nullValue element
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000160 * if it does not exist.
Christopher Dunnc28610f2015-02-21 11:44:16 -0600161 * The sequence of an #arrayValue will be automatically resized and initialized
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000162 * with #nullValue. resize() can be used to enlarge or truncate an #arrayValue.
163 *
Christopher Dunnc28610f2015-02-21 11:44:16 -0600164 * The get() methods can be used to obtain default value in the case the
165 * required element does not exist.
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000166 *
Mathias L. Baumann08ddeed2018-12-12 17:59:43 +0100167 * It is possible to iterate over the list of member keys of an object using
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000168 * the getMemberNames() method.
Christopher Dunn25342ba2015-03-02 18:05:26 -0600169 *
Christopher Dunnc28610f2015-02-21 11:44:16 -0600170 * \note #Value string-length fit in size_t, but keys must be < 2^30.
171 * (The reason is an implementation detail.) A #CharReader will raise an
Christopher Dunn25342ba2015-03-02 18:05:26 -0600172 * exception if a bound is exceeded to avoid security holes in your app,
173 * but the Value API does *not* check bounds. That is the responsibility
174 * of the caller.
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000175 */
176class JSON_API Value {
177 friend class ValueIteratorBase;
Billy Donahueb5e1fe82018-05-20 16:55:27 -0400178
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000179public:
Billy Donahue1c2ed7a2019-01-17 16:35:29 -0500180 typedef std::vector<String> Members;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000181 typedef ValueIterator iterator;
182 typedef ValueConstIterator const_iterator;
183 typedef Json::UInt UInt;
184 typedef Json::Int Int;
185#if defined(JSON_HAS_INT64)
186 typedef Json::UInt64 UInt64;
187 typedef Json::Int64 Int64;
Baptiste Lepilleur842e9ac2010-12-27 17:45:23 +0000188#endif // defined(JSON_HAS_INT64)
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000189 typedef Json::LargestInt LargestInt;
190 typedef Json::LargestUInt LargestUInt;
191 typedef Json::ArrayIndex ArrayIndex;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000192
Wolfram Röslerff6b4492017-09-14 09:31:36 +0200193 // Required for boost integration, e. g. BOOST_TEST
194 typedef std::string value_type;
195
Jordan Bayles25c57812019-07-11 14:27:29 -0700196#if JSON_USE_NULLREF
197 // Binary compatibility kludges, do not use.
198 static const Value& null;
199 static const Value& nullRef;
200#endif
201
202 // null and nullRef are deprecated, use this instead.
203 static Value const& nullSingleton();
Christopher Dunn0f288ae2016-06-26 18:47:43 -0500204
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000205 /// Minimum signed integer value that can be stored in a Json::Value.
dota177ef0f9f2019-09-17 01:33:47 +0800206 static constexpr LargestInt minLargestInt = 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:
277#ifndef JSON_USE_CPPTL_SMALLMAP
278 typedef std::map<CZString, Value> ObjectValues;
279#else
280 typedef CppTL::SmallMap<CZString, Value> ObjectValues;
281#endif // ifndef JSON_USE_CPPTL_SMALLMAP
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000282#endif // ifndef JSONCPP_DOC_EXCLUDE_IMPLEMENTATION
283
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000284public:
Billy Donahue483eba82019-07-14 18:41:48 -0400285 /**
286 * \brief Create a default Value of the given type.
287 *
288 * This is a very useful constructor.
289 * To create an empty array, pass arrayValue.
290 * To create an empty object, pass objectValue.
291 * Another Value can then be set to this one by assignment.
292 * This is useful since clear() and resize() will not alter types.
293 *
294 * Examples:
295 * \code
296 * Json::Value null_value; // null
297 * Json::Value arr_value(Json::arrayValue); // []
298 * Json::Value obj_value(Json::objectValue); // {}
299 * \endcode
300 */
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000301 Value(ValueType type = nullValue);
302 Value(Int value);
303 Value(UInt value);
Baptiste Lepilleur842e9ac2010-12-27 17:45:23 +0000304#if defined(JSON_HAS_INT64)
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000305 Value(Int64 value);
306 Value(UInt64 value);
Baptiste Lepilleur842e9ac2010-12-27 17:45:23 +0000307#endif // if defined(JSON_HAS_INT64)
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000308 Value(double value);
Christopher Dunn8a702972015-03-03 10:38:27 -0600309 Value(const char* value); ///< Copy til first 0. (NULL causes to seg-fault.)
Christopher Dunn89704032015-07-11 12:09:59 -0500310 Value(const char* begin, const char* end); ///< Copy all, incl zeroes.
Billy Donahue483eba82019-07-14 18:41:48 -0400311 /**
312 * \brief Constructs a value from a static string.
313 *
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000314 * Like other value string constructor but do not duplicate the string for
Billy Donahue483eba82019-07-14 18:41:48 -0400315 * internal storage. The given string must remain alive after the call to
316 * this constructor.
317 *
Christopher Dunnc28610f2015-02-21 11:44:16 -0600318 * \note This works only for null-terminated strings. (We cannot change the
Billy Donahue483eba82019-07-14 18:41:48 -0400319 * size of this class, so we have nowhere to store the length, which might be
320 * computed later for various operations.)
Christopher Dunnc28610f2015-02-21 11:44:16 -0600321 *
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000322 * Example of usage:
Billy Donahue483eba82019-07-14 18:41:48 -0400323 * \code
324 * static StaticString foo("some text");
325 * Json::Value aValue(foo);
326 * \endcode
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000327 */
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000328 Value(const StaticString& value);
Billy Donahue483eba82019-07-14 18:41:48 -0400329 Value(const String& value);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000330#ifdef JSON_USE_CPPTL
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000331 Value(const CppTL::ConstString& value);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000332#endif
333 Value(bool value);
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000334 Value(const Value& other);
Motti2b008912015-04-20 17:44:47 +0300335 Value(Value&& other);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000336 ~Value();
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000337
Billy Donahue0c1cc6e2019-01-20 23:59:16 -0500338 /// \note Overwrite existing comments. To preserve comments, use
Billy Donahueb5e1fe82018-05-20 16:55:27 -0400339 /// #swapPayload().
Billy Donahue0c1cc6e2019-01-20 23:59:16 -0500340 Value& operator=(const Value& other);
341 Value& operator=(Value&& other);
Dhruv Paranjape8996c372017-07-08 17:27:07 +0530342
Christopher Dunn66eb72f2015-01-20 11:02:22 -0600343 /// Swap everything.
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000344 void swap(Value& other);
Christopher Dunn66eb72f2015-01-20 11:02:22 -0600345 /// Swap values but leave comments and source offsets in place.
346 void swapPayload(Value& other);
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000347
Dhruv Paranjape8996c372017-07-08 17:27:07 +0530348 /// copy everything.
349 void copy(const Value& other);
350 /// copy values but leave comments and source offsets in place.
351 void copyPayload(const Value& other);
352
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000353 ValueType type() const;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000354
Christopher Dunn66eb72f2015-01-20 11:02:22 -0600355 /// Compare payload only, not comments etc.
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000356 bool operator<(const Value& other) const;
357 bool operator<=(const Value& other) const;
358 bool operator>=(const Value& other) const;
359 bool operator>(const Value& other) const;
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000360 bool operator==(const Value& other) const;
361 bool operator!=(const Value& other) const;
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000362 int compare(const Value& other) const;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000363
Christopher Dunn8a702972015-03-03 10:38:27 -0600364 const char* asCString() const; ///< Embedded zeroes could cause you trouble!
dawescae564652016-03-14 19:11:02 -0500365#if JSONCPP_USING_SECURE_MEMORY
Billy Donahueb5e1fe82018-05-20 16:55:27 -0400366 unsigned getCStringLength() const; // Allows you to understand the length of
367 // the CString
dawescae564652016-03-14 19:11:02 -0500368#endif
Billy Donahue1c2ed7a2019-01-17 16:35:29 -0500369 String asString() const; ///< Embedded zeroes are possible.
Christopher Dunnc28610f2015-02-21 11:44:16 -0600370 /** Get raw char* of string-value.
371 * \return false if !string. (Seg-fault if str or end are NULL.)
372 */
Billy Donahueb5e1fe82018-05-20 16:55:27 -0400373 bool getString(char const** begin, char const** end) const;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000374#ifdef JSON_USE_CPPTL
375 CppTL::ConstString asConstString() const;
376#endif
377 Int asInt() const;
378 UInt asUInt() const;
Aaron Jacobsf1053e72011-05-24 03:18:02 +0000379#if defined(JSON_HAS_INT64)
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000380 Int64 asInt64() const;
381 UInt64 asUInt64() const;
Aaron Jacobsf1053e72011-05-24 03:18:02 +0000382#endif // if defined(JSON_HAS_INT64)
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000383 LargestInt asLargestInt() const;
384 LargestUInt asLargestUInt() const;
385 float asFloat() const;
386 double asDouble() const;
387 bool asBool() const;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000388
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000389 bool isNull() const;
390 bool isBool() const;
391 bool isInt() const;
392 bool isInt64() const;
393 bool isUInt() const;
394 bool isUInt64() const;
395 bool isIntegral() const;
396 bool isDouble() const;
397 bool isNumeric() const;
398 bool isString() const;
399 bool isArray() const;
400 bool isObject() const;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000401
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000402 bool isConvertibleTo(ValueType other) const;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000403
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000404 /// Number of values in array or object
405 ArrayIndex size() const;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000406
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000407 /// \brief Return true if empty array, empty object, or null;
408 /// otherwise, false.
409 bool empty() const;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000410
Wolfram Rösler90794222017-12-05 18:18:55 +0100411 /// Return !isNull()
drgler04abe382018-01-13 15:28:19 +0100412 JSONCPP_OP_EXPLICIT operator bool() const;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000413
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000414 /// Remove all object members and array elements.
415 /// \pre type() is arrayValue, objectValue, or nullValue
416 /// \post type() is unchanged
417 void clear();
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000418
Marian Klymovfc201342018-06-02 20:15:26 +0300419 /// Resize the array to newSize elements.
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000420 /// New elements are initialized to null.
421 /// May only be called on nullValue or arrayValue.
422 /// \pre type() is arrayValue or nullValue
423 /// \post type() is arrayValue
Marian Klymovfc201342018-06-02 20:15:26 +0300424 void resize(ArrayIndex newSize);
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000425
Billy Donahue483eba82019-07-14 18:41:48 -0400426 //@{
427 /// Access an array element (zero based index). If the array contains less
428 /// than index element, then null value are inserted in the array so that
429 /// its size is index+1.
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000430 /// (You may need to say 'value[0u]' to get your compiler to distinguish
Billy Donahue483eba82019-07-14 18:41:48 -0400431 /// this from the operator[] which takes a string.)
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000432 Value& operator[](ArrayIndex index);
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000433 Value& operator[](int index);
Billy Donahue483eba82019-07-14 18:41:48 -0400434 //@}
Baptiste Lepilleurfa130ef2010-12-24 12:47:14 +0000435
Billy Donahue483eba82019-07-14 18:41:48 -0400436 //@{
437 /// Access an array element (zero based index).
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000438 /// (You may need to say 'value[0u]' to get your compiler to distinguish
Billy Donahue483eba82019-07-14 18:41:48 -0400439 /// this from the operator[] which takes a string.)
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000440 const Value& operator[](ArrayIndex index) const;
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000441 const Value& operator[](int index) const;
Billy Donahue483eba82019-07-14 18:41:48 -0400442 //@}
Baptiste Lepilleurfa130ef2010-12-24 12:47:14 +0000443
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000444 /// If the array contains at least index+1 elements, returns the element
Billy Donahue483eba82019-07-14 18:41:48 -0400445 /// value, otherwise returns defaultValue.
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000446 Value get(ArrayIndex index, const Value& defaultValue) const;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000447 /// Return true if index < size().
448 bool isValidIndex(ArrayIndex index) const;
449 /// \brief Append value to array at the end.
450 ///
451 /// Equivalent to jsonvalue[jsonvalue.size()] = value;
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000452 Value& append(const Value& value);
Dhruv Paranjape23c44d92017-07-08 17:30:47 +0530453 Value& append(Value&& value);
Dhruv Paranjape23c44d92017-07-08 17:30:47 +0530454
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000455 /// Access an object value by name, create a null member if it does not exist.
Christopher Dunn25342ba2015-03-02 18:05:26 -0600456 /// \note Because of our implementation, keys are limited to 2^30 -1 chars.
Billy Donahue483eba82019-07-14 18:41:48 -0400457 /// Exceeding that will cause an exception.
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000458 Value& operator[](const char* key);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000459 /// Access an object value by name, returns null if there is no member with
460 /// that name.
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000461 const Value& operator[](const char* key) const;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000462 /// Access an object value by name, create a null member if it does not exist.
Christopher Dunn25342ba2015-03-02 18:05:26 -0600463 /// \param key may contain embedded nulls.
Billy Donahue1c2ed7a2019-01-17 16:35:29 -0500464 Value& operator[](const String& key);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000465 /// Access an object value by name, returns null if there is no member with
466 /// that name.
Christopher Dunn25342ba2015-03-02 18:05:26 -0600467 /// \param key may contain embedded nulls.
Billy Donahue1c2ed7a2019-01-17 16:35:29 -0500468 const Value& operator[](const String& key) const;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000469 /** \brief Access an object value by name, create a null member if it does not
Billy Donahue483eba82019-07-14 18:41:48 -0400470 * exist.
471 *
Billy Donahueb5e1fe82018-05-20 16:55:27 -0400472 * If the object has no entry for that name, then the member name used to
Billy Donahue483eba82019-07-14 18:41:48 -0400473 * store the new entry is not duplicated.
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000474 * Example of use:
Billy Donahue483eba82019-07-14 18:41:48 -0400475 * \code
476 * Json::Value object;
477 * static const StaticString code("code");
478 * object[code] = 1234;
479 * \endcode
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000480 */
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000481 Value& operator[](const StaticString& key);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000482#ifdef JSON_USE_CPPTL
483 /// Access an object value by name, create a null member if it does not exist.
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000484 Value& operator[](const CppTL::ConstString& key);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000485 /// Access an object value by name, returns null if there is no member with
486 /// that name.
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000487 const Value& operator[](const CppTL::ConstString& key) const;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000488#endif
489 /// Return the member named key if it exist, defaultValue otherwise.
Christopher Dunn0fd28752015-03-05 16:38:43 -0600490 /// \note deep copy
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000491 Value get(const char* key, const Value& defaultValue) const;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000492 /// Return the member named key if it exist, defaultValue otherwise.
Christopher Dunn0fd28752015-03-05 16:38:43 -0600493 /// \note deep copy
Christopher Dunn89704032015-07-11 12:09:59 -0500494 /// \note key may contain embedded nulls.
Billy Donahueb5e1fe82018-05-20 16:55:27 -0400495 Value
496 get(const char* begin, const char* end, const Value& defaultValue) const;
Christopher Dunn25342ba2015-03-02 18:05:26 -0600497 /// Return the member named key if it exist, defaultValue otherwise.
Christopher Dunn0fd28752015-03-05 16:38:43 -0600498 /// \note deep copy
Christopher Dunn25342ba2015-03-02 18:05:26 -0600499 /// \param key may contain embedded nulls.
Billy Donahue1c2ed7a2019-01-17 16:35:29 -0500500 Value get(const String& key, const Value& defaultValue) const;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000501#ifdef JSON_USE_CPPTL
502 /// Return the member named key if it exist, defaultValue otherwise.
Christopher Dunn0fd28752015-03-05 16:38:43 -0600503 /// \note deep copy
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000504 Value get(const CppTL::ConstString& key, const Value& defaultValue) const;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000505#endif
Christopher Dunn25342ba2015-03-02 18:05:26 -0600506 /// Most general and efficient version of isMember()const, get()const,
507 /// and operator[]const
Christopher Dunn89704032015-07-11 12:09:59 -0500508 /// \note As stated elsewhere, behavior is undefined if (end-begin) >= 2^30
509 Value const* find(char const* begin, char const* end) const;
Christopher Dunnc28610f2015-02-21 11:44:16 -0600510 /// Most general and efficient version of object-mutators.
Christopher Dunn89704032015-07-11 12:09:59 -0500511 /// \note As stated elsewhere, behavior is undefined if (end-begin) >= 2^30
Christopher Dunnc28610f2015-02-21 11:44:16 -0600512 /// \return non-zero, but JSON_ASSERT if this is neither object nor nullValue.
Frank Richterd76fe562019-03-23 14:31:06 +0100513 Value* demand(char const* begin, char const* end);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000514 /// \brief Remove and return the named member.
515 ///
516 /// Do nothing if it did not exist.
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000517 /// \pre type() is objectValue or nullValue
518 /// \post type() is unchanged
Wolfram Röslera06b3902017-10-18 07:19:27 +0200519 void removeMember(const char* key);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000520 /// Same as removeMember(const char*)
Christopher Dunn25342ba2015-03-02 18:05:26 -0600521 /// \param key may contain embedded nulls.
Billy Donahue1c2ed7a2019-01-17 16:35:29 -0500522 void removeMember(const String& key);
Christopher Dunn89704032015-07-11 12:09:59 -0500523 /// Same as removeMember(const char* begin, const char* end, Value* removed),
Christopher Dunn25342ba2015-03-02 18:05:26 -0600524 /// but 'key' is null-terminated.
525 bool removeMember(const char* key, Value* removed);
Christopher Dunn76746b02015-01-21 16:01:30 -0600526 /** \brief Remove the named map member.
Billy Donahue483eba82019-07-14 18:41:48 -0400527 *
528 * Update 'removed' iff removed.
529 * \param key may contain embedded nulls.
530 * \return true iff removed (no exceptions)
531 */
Billy Donahue1c2ed7a2019-01-17 16:35:29 -0500532 bool removeMember(String const& key, Value* removed);
533 /// Same as removeMember(String const& key, Value* removed)
Christopher Dunn89704032015-07-11 12:09:59 -0500534 bool removeMember(const char* begin, const char* end, Value* removed);
Christopher Dunn9de2c2d2015-01-20 16:15:40 -0600535 /** \brief Remove the indexed array element.
Billy Donahue483eba82019-07-14 18:41:48 -0400536 *
537 * O(n) expensive operations.
538 * Update 'removed' iff removed.
539 * \return true if removed (no exceptions)
540 */
Marian Klymovfc201342018-06-02 20:15:26 +0300541 bool removeIndex(ArrayIndex index, Value* removed);
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000542
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000543 /// Return true if the object has a member named key.
Christopher Dunnc28610f2015-02-21 11:44:16 -0600544 /// \note 'key' must be null-terminated.
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000545 bool isMember(const char* key) const;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000546 /// Return true if the object has a member named key.
Christopher Dunn25342ba2015-03-02 18:05:26 -0600547 /// \param key may contain embedded nulls.
Billy Donahue1c2ed7a2019-01-17 16:35:29 -0500548 bool isMember(const String& key) const;
549 /// Same as isMember(String const& key)const
Christopher Dunn89704032015-07-11 12:09:59 -0500550 bool isMember(const char* begin, const char* end) const;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000551#ifdef JSON_USE_CPPTL
552 /// Return true if the object has a member named key.
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000553 bool isMember(const CppTL::ConstString& key) const;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000554#endif
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000555
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000556 /// \brief Return a list of the member names.
557 ///
558 /// If null, return an empty list.
559 /// \pre type() is objectValue or nullValue
560 /// \post if type() was nullValue, it remains nullValue
561 Members getMemberNames() const;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000562
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000563 //# ifdef JSON_USE_CPPTL
564 // EnumMemberNames enumMemberNames() const;
565 // EnumValues enumValues() const;
566 //# endif
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000567
Christopher Dunn1e3149a2015-01-25 14:16:13 -0600568 /// \deprecated Always pass len.
dota17b27c83f2019-07-18 04:35:33 +0800569 [[deprecated("Use setComment(String const&) instead.")]]
Billy Donahue433107f2019-01-20 21:53:01 -0500570 void setComment(const char* comment, CommentPlacement placement) {
571 setComment(String(comment, strlen(comment)), placement);
572 }
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000573 /// Comments must be //... or /* ... */
Billy Donahue433107f2019-01-20 21:53:01 -0500574 void setComment(const char* comment, size_t len, CommentPlacement placement) {
575 setComment(String(comment, len), placement);
576 }
Christopher Dunn1e3149a2015-01-25 14:16:13 -0600577 /// Comments must be //... or /* ... */
Billy Donahue433107f2019-01-20 21:53:01 -0500578 void setComment(String comment, CommentPlacement placement);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000579 bool hasComment(CommentPlacement placement) const;
580 /// Include delimiters and embedded newlines.
Billy Donahue1c2ed7a2019-01-17 16:35:29 -0500581 String getComment(CommentPlacement placement) const;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000582
Billy Donahue1c2ed7a2019-01-17 16:35:29 -0500583 String toStyledString() const;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000584
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000585 const_iterator begin() const;
586 const_iterator end() const;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000587
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000588 iterator begin();
589 iterator end();
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000590
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000591 // Accessors for the [start, limit) range of bytes within the JSON text from
592 // which this value was parsed, if any.
Christopher Dunnd4513fc2016-02-06 09:25:20 -0600593 void setOffsetStart(ptrdiff_t start);
594 void setOffsetLimit(ptrdiff_t limit);
595 ptrdiff_t getOffsetStart() const;
596 ptrdiff_t getOffsetLimit() const;
Aaron Jacobs68db6552014-04-23 23:41:12 +0000597
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000598private:
Jordan Baylesd5bd1a72019-06-24 14:06:45 -0700599 void setType(ValueType v) {
600 bits_.value_type_ = static_cast<unsigned char>(v);
601 }
Billy Donahue0c1cc6e2019-01-20 23:59:16 -0500602 bool isAllocated() const { return bits_.allocated_; }
603 void setIsAllocated(bool v) { bits_.allocated_ = v; }
604
Billy Donahue8eb5d892014-11-10 01:35:42 -0500605 void initBasic(ValueType type, bool allocated = false);
Andrey Okoshkin9b569c82018-01-12 15:59:20 +0300606 void dupPayload(const Value& other);
Andrey Okoshkinc69148c2018-01-12 11:26:34 +0300607 void releasePayload();
Andrey Okoshkin9b569c82018-01-12 15:59:20 +0300608 void dupMeta(const Value& other);
Billy Donahue8eb5d892014-11-10 01:35:42 -0500609
Christopher Dunnc28610f2015-02-21 11:44:16 -0600610 Value& resolveReference(const char* key);
611 Value& resolveReference(const char* key, const char* end);
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000612
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000613 // struct MemberNamesTransform
614 //{
615 // typedef const char *result_type;
616 // const char *operator()( const CZString &name ) const
617 // {
618 // return name.c_str();
619 // }
620 //};
621
622 union ValueHolder {
623 LargestInt int_;
624 LargestUInt uint_;
625 double real_;
626 bool bool_;
Billy Donahue0c1cc6e2019-01-20 23:59:16 -0500627 char* string_; // if allocated_, ptr to { unsigned, char[] }.
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000628 ObjectValues* map_;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000629 } value_;
Billy Donahue0c1cc6e2019-01-20 23:59:16 -0500630
631 struct {
632 // Really a ValueType, but types should agree for bitfield packing.
633 unsigned int value_type_ : 8;
634 // Unless allocated_, string_ must be null-terminated.
635 unsigned int allocated_ : 1;
636 } bits_;
637
Billy Donahue433107f2019-01-20 21:53:01 -0500638 class Comments {
639 public:
640 Comments() = default;
641 Comments(const Comments& that);
Billy Donahue00558b32019-01-21 16:42:25 -0500642 Comments(Comments&& that);
Billy Donahue433107f2019-01-20 21:53:01 -0500643 Comments& operator=(const Comments& that);
Billy Donahue00558b32019-01-21 16:42:25 -0500644 Comments& operator=(Comments&& that);
Billy Donahue433107f2019-01-20 21:53:01 -0500645 bool has(CommentPlacement slot) const;
646 String get(CommentPlacement slot) const;
647 void set(CommentPlacement slot, String s);
648
649 private:
650 using Array = std::array<String, numberOfCommentPlacement>;
651 std::unique_ptr<Array> ptr_;
652 };
653 Comments comments_;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000654
655 // [start, limit) byte offsets in the source JSON text from which this Value
656 // was extracted.
Christopher Dunnd4513fc2016-02-06 09:25:20 -0600657 ptrdiff_t start_;
658 ptrdiff_t limit_;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000659};
660
661/** \brief Experimental and untested: represents an element of the "path" to
662 * access a node.
663 */
664class JSON_API PathArgument {
665public:
666 friend class Path;
667
668 PathArgument();
669 PathArgument(ArrayIndex index);
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000670 PathArgument(const char* key);
Billy Donahue1c2ed7a2019-01-17 16:35:29 -0500671 PathArgument(const String& key);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000672
673private:
Billy Donahueb5e1fe82018-05-20 16:55:27 -0400674 enum Kind { kindNone = 0, kindIndex, kindKey };
Billy Donahue1c2ed7a2019-01-17 16:35:29 -0500675 String key_;
Hans Johnsone817e4f2019-01-14 17:09:22 -0600676 ArrayIndex index_{};
Billy Donahue2b593a92019-01-18 03:46:57 -0500677 Kind kind_{kindNone};
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000678};
679
680/** \brief Experimental and untested: represents a "path" to access a node.
681 *
682 * Syntax:
683 * - "." => root node
684 * - ".[n]" => elements at index 'n' of root node (an array value)
685 * - ".name" => member named 'name' of root node (an object value)
686 * - ".name1.name2.name3"
687 * - ".[0][1][2].name1[3]"
688 * - ".%" => member name is provided as parameter
aliha472adb62019-08-26 15:37:05 -0400689 * - ".[%]" => index is provided as parameter
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000690 */
691class JSON_API Path {
692public:
Billy Donahue1c2ed7a2019-01-17 16:35:29 -0500693 Path(const String& path,
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000694 const PathArgument& a1 = PathArgument(),
695 const PathArgument& a2 = PathArgument(),
696 const PathArgument& a3 = PathArgument(),
697 const PathArgument& a4 = PathArgument(),
698 const PathArgument& a5 = PathArgument());
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000699
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000700 const Value& resolve(const Value& root) const;
701 Value resolve(const Value& root, const Value& defaultValue) const;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000702 /// Creates the "path" to access the specified node and returns a reference on
703 /// the node.
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000704 Value& make(Value& root) const;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000705
706private:
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000707 typedef std::vector<const PathArgument*> InArgs;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000708 typedef std::vector<PathArgument> Args;
709
Billy Donahue1c2ed7a2019-01-17 16:35:29 -0500710 void makePath(const String& path, const InArgs& in);
711 void addPathInArg(const String& path,
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000712 const InArgs& in,
713 InArgs::const_iterator& itInArg,
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000714 PathArgument::Kind kind);
Billy Donahue1c2ed7a2019-01-17 16:35:29 -0500715 static void invalidPath(const String& path, int location);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000716
717 Args args_;
718};
719
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000720/** \brief base class for Value iterators.
721 *
722 */
723class JSON_API ValueIteratorBase {
724public:
725 typedef std::bidirectional_iterator_tag iterator_category;
726 typedef unsigned int size_t;
727 typedef int difference_type;
728 typedef ValueIteratorBase SelfType;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000729
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000730 bool operator==(const SelfType& other) const { return isEqual(other); }
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000731
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000732 bool operator!=(const SelfType& other) const { return !isEqual(other); }
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000733
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000734 difference_type operator-(const SelfType& other) const {
Kevin Grant4c5832a2015-02-14 20:53:35 -0800735 return other.computeDistance(*this);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000736 }
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000737
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000738 /// Return either the index or the member name of the referenced value as a
739 /// Value.
740 Value key() const;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000741
Billy Donahueb5e1fe82018-05-20 16:55:27 -0400742 /// Return the index of the referenced Value, or -1 if it is not an
743 /// arrayValue.
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000744 UInt index() const;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000745
Christopher Dunned495ed2015-03-08 14:01:28 -0500746 /// Return the member name of the referenced Value, or "" if it is not an
747 /// objectValue.
748 /// \note Avoid `c_str()` on result, as embedded zeroes are possible.
Billy Donahue1c2ed7a2019-01-17 16:35:29 -0500749 String name() const;
Christopher Dunned495ed2015-03-08 14:01:28 -0500750
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000751 /// Return the member name of the referenced Value. "" if it is not an
752 /// objectValue.
Billy Donahueb5e1fe82018-05-20 16:55:27 -0400753 /// \deprecated This cannot be used for UTF-8 strings, since there can be
754 /// embedded nulls.
dota17b27c83f2019-07-18 04:35:33 +0800755 [[deprecated("Use `key = name();` instead.")]]
Christopher Dunnc28610f2015-02-21 11:44:16 -0600756 char const* memberName() const;
757 /// Return the member name of the referenced Value, or NULL if it is not an
758 /// objectValue.
Christopher Dunned495ed2015-03-08 14:01:28 -0500759 /// \note Better version than memberName(). Allows embedded nulls.
Christopher Dunnc28610f2015-02-21 11:44:16 -0600760 char const* memberName(char const** end) const;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000761
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000762protected:
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000763 Value& deref() const;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000764
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000765 void increment();
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000766
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000767 void decrement();
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000768
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000769 difference_type computeDistance(const SelfType& other) const;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000770
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000771 bool isEqual(const SelfType& other) const;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000772
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000773 void copy(const SelfType& other);
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000774
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000775private:
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000776 Value::ObjectValues::iterator current_;
777 // Indicates that iterator is for a null value.
Billy Donahue2b593a92019-01-18 03:46:57 -0500778 bool isNull_{true};
Christopher Dunn2a10f4a2015-04-28 04:55:12 +0100779
780public:
781 // For some reason, BORLAND needs these at the end, rather
782 // than earlier. No idea why.
783 ValueIteratorBase();
784 explicit ValueIteratorBase(const Value::ObjectValues::iterator& current);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000785};
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000786
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000787/** \brief const iterator for object and array value.
788 *
789 */
790class JSON_API ValueConstIterator : public ValueIteratorBase {
791 friend class Value;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000792
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000793public:
794 typedef const Value value_type;
Billy Donahueb5e1fe82018-05-20 16:55:27 -0400795 // typedef unsigned int size_t;
796 // typedef int difference_type;
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000797 typedef const Value& reference;
798 typedef const Value* pointer;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000799 typedef ValueConstIterator SelfType;
800
801 ValueConstIterator();
ycqiuc8a8cfc2015-10-06 16:46:19 +0800802 ValueConstIterator(ValueIterator const& other);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000803
804private:
Billy Donahueb5e1fe82018-05-20 16:55:27 -0400805 /*! \internal Use by Value to create an iterator.
806 */
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000807 explicit ValueConstIterator(const Value::ObjectValues::iterator& current);
Billy Donahueb5e1fe82018-05-20 16:55:27 -0400808
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000809public:
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000810 SelfType& operator=(const ValueIteratorBase& other);
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000811
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000812 SelfType operator++(int) {
813 SelfType temp(*this);
814 ++*this;
815 return temp;
816 }
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000817
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000818 SelfType operator--(int) {
819 SelfType temp(*this);
820 --*this;
821 return temp;
822 }
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000823
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000824 SelfType& operator--() {
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000825 decrement();
826 return *this;
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 increment();
831 return *this;
832 }
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000833
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000834 reference operator*() const { return deref(); }
Braden McDorman540db3b2014-09-14 02:31:23 -0500835
836 pointer operator->() const { return &deref(); }
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000837};
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000838
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000839/** \brief Iterator for object and array value.
840 */
841class JSON_API ValueIterator : public ValueIteratorBase {
842 friend class Value;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000843
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000844public:
845 typedef Value value_type;
846 typedef unsigned int size_t;
847 typedef int difference_type;
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000848 typedef Value& reference;
849 typedef Value* pointer;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000850 typedef ValueIterator SelfType;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000851
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000852 ValueIterator();
ycqiuc8a8cfc2015-10-06 16:46:19 +0800853 explicit ValueIterator(const ValueConstIterator& other);
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000854 ValueIterator(const ValueIterator& other);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000855
856private:
Billy Donahueb5e1fe82018-05-20 16:55:27 -0400857 /*! \internal Use by Value to create an iterator.
858 */
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000859 explicit ValueIterator(const Value::ObjectValues::iterator& current);
Billy Donahueb5e1fe82018-05-20 16:55:27 -0400860
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000861public:
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000862 SelfType& operator=(const SelfType& other);
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000863
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000864 SelfType operator++(int) {
865 SelfType temp(*this);
866 ++*this;
867 return temp;
868 }
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000869
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000870 SelfType operator--(int) {
871 SelfType temp(*this);
872 --*this;
873 return temp;
874 }
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000875
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000876 SelfType& operator--() {
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000877 decrement();
878 return *this;
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 increment();
883 return *this;
884 }
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000885
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000886 reference operator*() const { return deref(); }
Braden McDorman540db3b2014-09-14 02:31:23 -0500887
888 pointer operator->() const { return &deref(); }
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000889};
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000890
Billy Donahue1d956282018-03-06 12:51:58 -0500891inline void swap(Value& a, Value& b) { a.swap(b); }
892
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000893} // namespace Json
894
Sergiy80d6e666f2016-12-03 22:29:14 +0200895#pragma pack(pop)
datadiode9454e682015-01-20 15:25:04 -0600896
Baptiste Lepilleureafd7022013-05-08 20:21:11 +0000897#if defined(JSONCPP_DISABLE_DLL_INTERFACE_WARNING)
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000898#pragma warning(pop)
Baptiste Lepilleureafd7022013-05-08 20:21:11 +0000899#endif // if defined(JSONCPP_DISABLE_DLL_INTERFACE_WARNING)
900
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000901#endif // CPPTL_JSON_H_INCLUDED