blob: d0af7116b46de21e42716ab72508afa32f0146bc [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)
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>
Billy Donahue433107f2019-01-20 21:53:01 -050026#include <memory>
Aaron Jacobs9fa4e842014-07-01 08:48:54 +100027#include <string>
28#include <vector>
Christopher Dunn6d135cb2007-06-13 15:51:04 +000029
Aaron Jacobs9fa4e842014-07-01 08:48:54 +100030#ifndef JSON_USE_CPPTL_SMALLMAP
31#include <map>
32#else
33#include <cpptl/smallmap.h>
34#endif
35#ifdef JSON_USE_CPPTL
36#include <cpptl/forwards.h>
37#endif
Christopher Dunn6d135cb2007-06-13 15:51:04 +000038
Aaron Jacobs9fa4e842014-07-01 08:48:54 +100039// Disable warning C4251: <data member>: <type> needs to have dll-interface to
40// be used by...
Baptiste Lepilleureafd7022013-05-08 20:21:11 +000041#if defined(JSONCPP_DISABLE_DLL_INTERFACE_WARNING)
Aaron Jacobs9fa4e842014-07-01 08:48:54 +100042#pragma warning(push)
43#pragma warning(disable : 4251)
Baptiste Lepilleureafd7022013-05-08 20:21:11 +000044#endif // if defined(JSONCPP_DISABLE_DLL_INTERFACE_WARNING)
45
Sergiy80d6e666f2016-12-03 22:29:14 +020046#pragma pack(push, 8)
47
Christopher Dunn6d135cb2007-06-13 15:51:04 +000048/** \brief JSON (JavaScript Object Notation).
49 */
50namespace Json {
51
Jordan Bayles83cc9212019-06-06 13:41:47 -070052#if JSON_USE_EXCEPTION
Christopher Dunn75279cc2015-03-08 12:20:06 -050053/** Base class for all exceptions we throw.
Christopher Dunn4e30c4f2015-03-08 12:56:32 -050054 *
55 * We use nothing but these internally. Of course, STL can throw others.
Christopher Dunn75279cc2015-03-08 12:20:06 -050056 */
Christopher Dunn949babd2015-07-23 00:19:12 -050057class JSON_API Exception : public std::exception {
58public:
Billy Donahue1c2ed7a2019-01-17 16:35:29 -050059 Exception(String msg);
Hans Johnson2853b1c2019-01-11 13:58:53 -060060 ~Exception() JSONCPP_NOEXCEPT override;
61 char const* what() const JSONCPP_NOEXCEPT override;
Billy Donahueb5e1fe82018-05-20 16:55:27 -040062
Christopher Dunn949babd2015-07-23 00:19:12 -050063protected:
Billy Donahue1c2ed7a2019-01-17 16:35:29 -050064 String msg_;
Christopher Dunn949babd2015-07-23 00:19:12 -050065};
66
Christopher Dunn53837942015-03-08 12:31:00 -050067/** Exceptions which the user cannot easily avoid.
68 *
Christopher Dunn4e30c4f2015-03-08 12:56:32 -050069 * E.g. out-of-memory (when we use malloc), stack-overflow, malicious input
Dhruv Paranjape8996c372017-07-08 17:27:07 +053070 *
Christopher Dunn4e30c4f2015-03-08 12:56:32 -050071 * \remark derived from Json::Exception
Christopher Dunn53837942015-03-08 12:31:00 -050072 */
Christopher Dunn949babd2015-07-23 00:19:12 -050073class JSON_API RuntimeError : public Exception {
74public:
Billy Donahue1c2ed7a2019-01-17 16:35:29 -050075 RuntimeError(String const& msg);
Christopher Dunn949babd2015-07-23 00:19:12 -050076};
77
Christopher Dunn4e30c4f2015-03-08 12:56:32 -050078/** Exceptions thrown by JSON_ASSERT/JSON_FAIL macros.
Christopher Dunn53837942015-03-08 12:31:00 -050079 *
80 * These are precondition-violations (user bugs) and internal errors (our bugs).
Dhruv Paranjape8996c372017-07-08 17:27:07 +053081 *
Christopher Dunn4e30c4f2015-03-08 12:56:32 -050082 * \remark derived from Json::Exception
Christopher Dunn53837942015-03-08 12:31:00 -050083 */
Christopher Dunn949babd2015-07-23 00:19:12 -050084class JSON_API LogicError : public Exception {
85public:
Billy Donahue1c2ed7a2019-01-17 16:35:29 -050086 LogicError(String const& msg);
Christopher Dunn949babd2015-07-23 00:19:12 -050087};
Jordan Bayles83cc9212019-06-06 13:41:47 -070088#endif
Christopher Dunn53837942015-03-08 12:31:00 -050089
Christopher Dunn4e30c4f2015-03-08 12:56:32 -050090/// used internally
es1x2ee3b1d2019-10-11 21:33:36 +030091JSONCPP_NORETURN void throwRuntimeError(String const& msg);
Christopher Dunn4e30c4f2015-03-08 12:56:32 -050092/// used internally
es1x2ee3b1d2019-10-11 21:33:36 +030093JSONCPP_NORETURN void throwLogicError(String const& msg);
Christopher Dunn75279cc2015-03-08 12:20:06 -050094
Aaron Jacobs9fa4e842014-07-01 08:48:54 +100095/** \brief Type of the value held by a Value object.
96 */
97enum ValueType {
98 nullValue = 0, ///< 'null' value
99 intValue, ///< signed integer value
100 uintValue, ///< unsigned integer value
101 realValue, ///< double value
102 stringValue, ///< UTF-8 string value
103 booleanValue, ///< bool value
104 arrayValue, ///< array value (ordered list)
105 objectValue ///< object value (collection of name/value pairs).
106};
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000107
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000108enum CommentPlacement {
109 commentBefore = 0, ///< a comment placed on the line before a value
110 commentAfterOnSameLine, ///< a comment just after a value on the same line
111 commentAfter, ///< a comment on the line after a value (only make sense for
Aaron Jacobs3a0c4fc2014-07-01 09:20:48 +1000112 /// root value)
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000113 numberOfCommentPlacement
114};
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000115
Mike Ra07fc532018-03-13 23:35:31 +0300116/** \brief Type of precision for formatting of real values.
117 */
118enum PrecisionType {
119 significantDigits = 0, ///< we set max number of significant digits in string
120 decimalPlaces ///< we set max number of digits after "." in string
121};
122
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000123//# ifdef JSON_USE_CPPTL
124// typedef CppTL::AnyEnumerator<const char *> EnumMemberNames;
125// typedef CppTL::AnyEnumerator<const Value &> EnumValues;
126//# endif
127
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000128/** \brief Lightweight wrapper to tag static string.
129 *
Josh Sorefe6a588a2017-12-03 11:54:29 -0500130 * Value constructor and objectValue member assignment takes advantage of the
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000131 * StaticString and avoid the cost of string duplication when storing the
132 * string or the member name.
133 *
134 * Example of usage:
135 * \code
136 * Json::Value aValue( StaticString("some text") );
137 * Json::Value object;
138 * static const StaticString code("code");
139 * object[code] = 1234;
140 * \endcode
141 */
142class JSON_API StaticString {
143public:
Christopher Dunnff617522015-03-06 10:31:46 -0600144 explicit StaticString(const char* czstring) : c_str_(czstring) {}
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000145
Christopher Dunnff617522015-03-06 10:31:46 -0600146 operator const char*() const { return c_str_; }
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000147
Christopher Dunnff617522015-03-06 10:31:46 -0600148 const char* c_str() const { return c_str_; }
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000149
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000150private:
Christopher Dunnff617522015-03-06 10:31:46 -0600151 const char* c_str_;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000152};
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000153
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000154/** \brief Represents a <a HREF="http://www.json.org">JSON</a> value.
155 *
156 * This class is a discriminated union wrapper that can represents a:
157 * - signed integer [range: Value::minInt - Value::maxInt]
158 * - unsigned integer (range: 0 - Value::maxUInt)
159 * - double
160 * - UTF-8 string
161 * - boolean
162 * - 'null'
163 * - an ordered list of Value
164 * - collection of name/value pairs (javascript object)
165 *
166 * The type of the held value is represented by a #ValueType and
167 * can be obtained using type().
168 *
Christopher Dunnc28610f2015-02-21 11:44:16 -0600169 * Values of an #objectValue or #arrayValue can be accessed using operator[]()
170 * methods.
171 * Non-const methods will automatically create the a #nullValue element
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000172 * if it does not exist.
Christopher Dunnc28610f2015-02-21 11:44:16 -0600173 * The sequence of an #arrayValue will be automatically resized and initialized
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000174 * with #nullValue. resize() can be used to enlarge or truncate an #arrayValue.
175 *
Christopher Dunnc28610f2015-02-21 11:44:16 -0600176 * The get() methods can be used to obtain default value in the case the
177 * required element does not exist.
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000178 *
Mathias L. Baumann08ddeed2018-12-12 17:59:43 +0100179 * It is possible to iterate over the list of member keys of an object using
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000180 * the getMemberNames() method.
Christopher Dunn25342ba2015-03-02 18:05:26 -0600181 *
Christopher Dunnc28610f2015-02-21 11:44:16 -0600182 * \note #Value string-length fit in size_t, but keys must be < 2^30.
183 * (The reason is an implementation detail.) A #CharReader will raise an
Christopher Dunn25342ba2015-03-02 18:05:26 -0600184 * exception if a bound is exceeded to avoid security holes in your app,
185 * but the Value API does *not* check bounds. That is the responsibility
186 * of the caller.
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000187 */
188class JSON_API Value {
189 friend class ValueIteratorBase;
Billy Donahueb5e1fe82018-05-20 16:55:27 -0400190
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000191public:
Billy Donahue1c2ed7a2019-01-17 16:35:29 -0500192 typedef std::vector<String> Members;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000193 typedef ValueIterator iterator;
194 typedef ValueConstIterator const_iterator;
195 typedef Json::UInt UInt;
196 typedef Json::Int Int;
197#if defined(JSON_HAS_INT64)
198 typedef Json::UInt64 UInt64;
199 typedef Json::Int64 Int64;
Baptiste Lepilleur842e9ac2010-12-27 17:45:23 +0000200#endif // defined(JSON_HAS_INT64)
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000201 typedef Json::LargestInt LargestInt;
202 typedef Json::LargestUInt LargestUInt;
203 typedef Json::ArrayIndex ArrayIndex;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000204
Wolfram Röslerff6b4492017-09-14 09:31:36 +0200205 // Required for boost integration, e. g. BOOST_TEST
206 typedef std::string value_type;
207
Jordan Bayles25c57812019-07-11 14:27:29 -0700208#if JSON_USE_NULLREF
209 // Binary compatibility kludges, do not use.
210 static const Value& null;
211 static const Value& nullRef;
212#endif
213
214 // null and nullRef are deprecated, use this instead.
215 static Value const& nullSingleton();
Christopher Dunn0f288ae2016-06-26 18:47:43 -0500216
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000217 /// Minimum signed integer value that can be stored in a Json::Value.
Jordan Bayles81ae1d52019-09-16 12:37:14 -0700218 static constexpr LargestInt minLargestInt =
219 LargestInt(~(LargestUInt(-1) / 2));
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000220 /// Maximum signed integer value that can be stored in a Json::Value.
dota177ef0f9f2019-09-17 01:33:47 +0800221 static constexpr LargestInt maxLargestInt = LargestInt(LargestUInt(-1) / 2);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000222 /// Maximum unsigned integer value that can be stored in a Json::Value.
dota177ef0f9f2019-09-17 01:33:47 +0800223 static constexpr LargestUInt maxLargestUInt = LargestUInt(-1);
Baptiste Lepilleur842e9ac2010-12-27 17:45:23 +0000224
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000225 /// Minimum signed int value that can be stored in a Json::Value.
dota177ef0f9f2019-09-17 01:33:47 +0800226 static constexpr Int minInt = Int(~(UInt(-1) / 2));
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000227 /// Maximum signed int value that can be stored in a Json::Value.
dota177ef0f9f2019-09-17 01:33:47 +0800228 static constexpr Int maxInt = Int(UInt(-1) / 2);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000229 /// Maximum unsigned int value that can be stored in a Json::Value.
dota177ef0f9f2019-09-17 01:33:47 +0800230 static constexpr UInt maxUInt = UInt(-1);
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000231
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000232#if defined(JSON_HAS_INT64)
233 /// Minimum signed 64 bits int value that can be stored in a Json::Value.
dota177ef0f9f2019-09-17 01:33:47 +0800234 static constexpr Int64 minInt64 = Int64(~(UInt64(-1) / 2));
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000235 /// Maximum signed 64 bits int value that can be stored in a Json::Value.
dota177ef0f9f2019-09-17 01:33:47 +0800236 static constexpr Int64 maxInt64 = Int64(UInt64(-1) / 2);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000237 /// Maximum unsigned 64 bits int value that can be stored in a Json::Value.
dota177ef0f9f2019-09-17 01:33:47 +0800238 static constexpr UInt64 maxUInt64 = UInt64(-1);
Aaron Jacobsf1053e72011-05-24 03:18:02 +0000239#endif // defined(JSON_HAS_INT64)
Mike Ra07fc532018-03-13 23:35:31 +0300240 /// Default precision for real value for string representation.
dota177ef0f9f2019-09-17 01:33:47 +0800241 static constexpr UInt defaultRealPrecision = 17;
242 // The constant is hard-coded because some compiler have trouble
243 // converting Value::maxUInt64 to a double correctly (AIX/xlC).
244 // Assumes that UInt64 is a 64 bits integer.
245 static constexpr double maxUInt64AsDouble = 18446744073709551615.0;
Darcy Beurle798f6ba2017-12-22 22:48:20 +0100246// Workaround for bug in the NVIDIAs CUDA 9.1 nvcc compiler
247// when using gcc and clang backend compilers. CZString
248// cannot be defined as private. See issue #486
249#ifdef __NVCC__
250public:
251#else
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000252private:
Darcy Beurle798f6ba2017-12-22 22:48:20 +0100253#endif
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000254#ifndef JSONCPP_DOC_EXCLUDE_IMPLEMENTATION
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000255 class CZString {
256 public:
Billy Donahueb5e1fe82018-05-20 16:55:27 -0400257 enum DuplicationPolicy { noDuplication = 0, duplicate, duplicateOnCopy };
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000258 CZString(ArrayIndex index);
Christopher Dunnc28610f2015-02-21 11:44:16 -0600259 CZString(char const* str, unsigned length, DuplicationPolicy allocate);
260 CZString(CZString const& other);
Motti2b008912015-04-20 17:44:47 +0300261 CZString(CZString&& other);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000262 ~CZString();
Dhruv Paranjape0ba8bd72017-07-08 17:47:13 +0530263 CZString& operator=(const CZString& other);
Dhruv Paranjape0ba8bd72017-07-08 17:47:13 +0530264 CZString& operator=(CZString&& other);
Dhruv Paranjape0ba8bd72017-07-08 17:47:13 +0530265
Christopher Dunnc28610f2015-02-21 11:44:16 -0600266 bool operator<(CZString const& other) const;
267 bool operator==(CZString const& other) const;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000268 ArrayIndex index() const;
Billy Donahueb5e1fe82018-05-20 16:55:27 -0400269 // const char* c_str() const; ///< \deprecated
Christopher Dunnc28610f2015-02-21 11:44:16 -0600270 char const* data() const;
271 unsigned length() const;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000272 bool isStaticString() const;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000273
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000274 private:
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000275 void swap(CZString& other);
Christopher Dunnc28610f2015-02-21 11:44:16 -0600276
Christopher Dunn57ad0512015-03-02 12:10:35 -0600277 struct StringStorage {
Billy Donahueb5e1fe82018-05-20 16:55:27 -0400278 unsigned policy_ : 2;
279 unsigned length_ : 30; // 1GB max
Christopher Dunn57ad0512015-03-02 12:10:35 -0600280 };
281
Billy Donahueb5e1fe82018-05-20 16:55:27 -0400282 char const* cstr_; // actually, a prefixed string, unless policy is noDup
Christopher Dunn57ad0512015-03-02 12:10:35 -0600283 union {
284 ArrayIndex index_;
285 StringStorage storage_;
286 };
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000287 };
288
289public:
290#ifndef JSON_USE_CPPTL_SMALLMAP
291 typedef std::map<CZString, Value> ObjectValues;
292#else
293 typedef CppTL::SmallMap<CZString, Value> ObjectValues;
294#endif // ifndef JSON_USE_CPPTL_SMALLMAP
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000295#endif // ifndef JSONCPP_DOC_EXCLUDE_IMPLEMENTATION
296
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000297public:
Billy Donahue483eba82019-07-14 18:41:48 -0400298 /**
299 * \brief Create a default Value of the given type.
300 *
301 * This is a very useful constructor.
302 * To create an empty array, pass arrayValue.
303 * To create an empty object, pass objectValue.
304 * Another Value can then be set to this one by assignment.
305 * This is useful since clear() and resize() will not alter types.
306 *
307 * Examples:
308 * \code
309 * Json::Value null_value; // null
310 * Json::Value arr_value(Json::arrayValue); // []
311 * Json::Value obj_value(Json::objectValue); // {}
312 * \endcode
313 */
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000314 Value(ValueType type = nullValue);
315 Value(Int value);
316 Value(UInt value);
Baptiste Lepilleur842e9ac2010-12-27 17:45:23 +0000317#if defined(JSON_HAS_INT64)
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000318 Value(Int64 value);
319 Value(UInt64 value);
Baptiste Lepilleur842e9ac2010-12-27 17:45:23 +0000320#endif // if defined(JSON_HAS_INT64)
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000321 Value(double value);
Christopher Dunn8a702972015-03-03 10:38:27 -0600322 Value(const char* value); ///< Copy til first 0. (NULL causes to seg-fault.)
Christopher Dunn89704032015-07-11 12:09:59 -0500323 Value(const char* begin, const char* end); ///< Copy all, incl zeroes.
Billy Donahue483eba82019-07-14 18:41:48 -0400324 /**
325 * \brief Constructs a value from a static string.
326 *
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000327 * Like other value string constructor but do not duplicate the string for
Billy Donahue483eba82019-07-14 18:41:48 -0400328 * internal storage. The given string must remain alive after the call to
329 * this constructor.
330 *
Christopher Dunnc28610f2015-02-21 11:44:16 -0600331 * \note This works only for null-terminated strings. (We cannot change the
Billy Donahue483eba82019-07-14 18:41:48 -0400332 * size of this class, so we have nowhere to store the length, which might be
333 * computed later for various operations.)
Christopher Dunnc28610f2015-02-21 11:44:16 -0600334 *
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000335 * Example of usage:
Billy Donahue483eba82019-07-14 18:41:48 -0400336 * \code
337 * static StaticString foo("some text");
338 * Json::Value aValue(foo);
339 * \endcode
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000340 */
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000341 Value(const StaticString& value);
Billy Donahue483eba82019-07-14 18:41:48 -0400342 Value(const String& value);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000343#ifdef JSON_USE_CPPTL
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000344 Value(const CppTL::ConstString& value);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000345#endif
346 Value(bool value);
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000347 Value(const Value& other);
Motti2b008912015-04-20 17:44:47 +0300348 Value(Value&& other);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000349 ~Value();
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000350
Billy Donahue0c1cc6e2019-01-20 23:59:16 -0500351 /// \note Overwrite existing comments. To preserve comments, use
Billy Donahueb5e1fe82018-05-20 16:55:27 -0400352 /// #swapPayload().
Billy Donahue0c1cc6e2019-01-20 23:59:16 -0500353 Value& operator=(const Value& other);
354 Value& operator=(Value&& other);
Dhruv Paranjape8996c372017-07-08 17:27:07 +0530355
Christopher Dunn66eb72f2015-01-20 11:02:22 -0600356 /// Swap everything.
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000357 void swap(Value& other);
Christopher Dunn66eb72f2015-01-20 11:02:22 -0600358 /// Swap values but leave comments and source offsets in place.
359 void swapPayload(Value& other);
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000360
Dhruv Paranjape8996c372017-07-08 17:27:07 +0530361 /// copy everything.
362 void copy(const Value& other);
363 /// copy values but leave comments and source offsets in place.
364 void copyPayload(const Value& other);
365
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000366 ValueType type() const;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000367
Christopher Dunn66eb72f2015-01-20 11:02:22 -0600368 /// Compare payload only, not comments etc.
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000369 bool operator<(const Value& other) const;
370 bool operator<=(const Value& other) const;
371 bool operator>=(const Value& other) const;
372 bool operator>(const Value& other) const;
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000373 bool operator==(const Value& other) const;
374 bool operator!=(const Value& other) const;
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000375 int compare(const Value& other) const;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000376
Christopher Dunn8a702972015-03-03 10:38:27 -0600377 const char* asCString() const; ///< Embedded zeroes could cause you trouble!
dawescae564652016-03-14 19:11:02 -0500378#if JSONCPP_USING_SECURE_MEMORY
Billy Donahueb5e1fe82018-05-20 16:55:27 -0400379 unsigned getCStringLength() const; // Allows you to understand the length of
380 // the CString
dawescae564652016-03-14 19:11:02 -0500381#endif
Billy Donahue1c2ed7a2019-01-17 16:35:29 -0500382 String asString() const; ///< Embedded zeroes are possible.
Christopher Dunnc28610f2015-02-21 11:44:16 -0600383 /** Get raw char* of string-value.
384 * \return false if !string. (Seg-fault if str or end are NULL.)
385 */
Billy Donahueb5e1fe82018-05-20 16:55:27 -0400386 bool getString(char const** begin, char const** end) const;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000387#ifdef JSON_USE_CPPTL
388 CppTL::ConstString asConstString() const;
389#endif
390 Int asInt() const;
391 UInt asUInt() const;
Aaron Jacobsf1053e72011-05-24 03:18:02 +0000392#if defined(JSON_HAS_INT64)
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000393 Int64 asInt64() const;
394 UInt64 asUInt64() const;
Aaron Jacobsf1053e72011-05-24 03:18:02 +0000395#endif // if defined(JSON_HAS_INT64)
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000396 LargestInt asLargestInt() const;
397 LargestUInt asLargestUInt() const;
398 float asFloat() const;
399 double asDouble() const;
400 bool asBool() const;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000401
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000402 bool isNull() const;
403 bool isBool() const;
404 bool isInt() const;
405 bool isInt64() const;
406 bool isUInt() const;
407 bool isUInt64() const;
408 bool isIntegral() const;
409 bool isDouble() const;
410 bool isNumeric() const;
411 bool isString() const;
412 bool isArray() const;
413 bool isObject() const;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000414
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000415 bool isConvertibleTo(ValueType other) const;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000416
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000417 /// Number of values in array or object
418 ArrayIndex size() const;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000419
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000420 /// \brief Return true if empty array, empty object, or null;
421 /// otherwise, false.
422 bool empty() const;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000423
Wolfram Rösler90794222017-12-05 18:18:55 +0100424 /// Return !isNull()
drgler04abe382018-01-13 15:28:19 +0100425 JSONCPP_OP_EXPLICIT operator bool() const;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000426
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000427 /// Remove all object members and array elements.
428 /// \pre type() is arrayValue, objectValue, or nullValue
429 /// \post type() is unchanged
430 void clear();
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000431
Marian Klymovfc201342018-06-02 20:15:26 +0300432 /// Resize the array to newSize elements.
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000433 /// New elements are initialized to null.
434 /// May only be called on nullValue or arrayValue.
435 /// \pre type() is arrayValue or nullValue
436 /// \post type() is arrayValue
Marian Klymovfc201342018-06-02 20:15:26 +0300437 void resize(ArrayIndex newSize);
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000438
Billy Donahue483eba82019-07-14 18:41:48 -0400439 //@{
440 /// Access an array element (zero based index). If the array contains less
441 /// than index element, then null value are inserted in the array so that
442 /// its size is index+1.
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000443 /// (You may need to say 'value[0u]' to get your compiler to distinguish
Billy Donahue483eba82019-07-14 18:41:48 -0400444 /// this from the operator[] which takes a string.)
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000445 Value& operator[](ArrayIndex index);
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000446 Value& operator[](int index);
Billy Donahue483eba82019-07-14 18:41:48 -0400447 //@}
Baptiste Lepilleurfa130ef2010-12-24 12:47:14 +0000448
Billy Donahue483eba82019-07-14 18:41:48 -0400449 //@{
450 /// Access an array element (zero based index).
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000451 /// (You may need to say 'value[0u]' to get your compiler to distinguish
Billy Donahue483eba82019-07-14 18:41:48 -0400452 /// this from the operator[] which takes a string.)
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000453 const Value& operator[](ArrayIndex index) const;
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000454 const Value& operator[](int index) const;
Billy Donahue483eba82019-07-14 18:41:48 -0400455 //@}
Baptiste Lepilleurfa130ef2010-12-24 12:47:14 +0000456
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000457 /// If the array contains at least index+1 elements, returns the element
Billy Donahue483eba82019-07-14 18:41:48 -0400458 /// value, otherwise returns defaultValue.
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000459 Value get(ArrayIndex index, const Value& defaultValue) const;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000460 /// Return true if index < size().
461 bool isValidIndex(ArrayIndex index) const;
462 /// \brief Append value to array at the end.
463 ///
464 /// Equivalent to jsonvalue[jsonvalue.size()] = value;
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000465 Value& append(const Value& value);
Dhruv Paranjape23c44d92017-07-08 17:30:47 +0530466 Value& append(Value&& value);
dota17bdacfd72019-10-16 14:33:53 +0800467 /// \brief Insert value in array at specific index
468 bool insert(ArrayIndex index, Value newValue);
Dhruv Paranjape23c44d92017-07-08 17:30:47 +0530469
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000470 /// Access an object value by name, create a null member if it does not exist.
Christopher Dunn25342ba2015-03-02 18:05:26 -0600471 /// \note Because of our implementation, keys are limited to 2^30 -1 chars.
Billy Donahue483eba82019-07-14 18:41:48 -0400472 /// Exceeding that will cause an exception.
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000473 Value& operator[](const char* key);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000474 /// Access an object value by name, returns null if there is no member with
475 /// that name.
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000476 const Value& operator[](const char* key) const;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000477 /// Access an object value by name, create a null member if it does not exist.
Christopher Dunn25342ba2015-03-02 18:05:26 -0600478 /// \param key may contain embedded nulls.
Billy Donahue1c2ed7a2019-01-17 16:35:29 -0500479 Value& operator[](const String& key);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000480 /// Access an object value by name, returns null if there is no member with
481 /// that name.
Christopher Dunn25342ba2015-03-02 18:05:26 -0600482 /// \param key may contain embedded nulls.
Billy Donahue1c2ed7a2019-01-17 16:35:29 -0500483 const Value& operator[](const String& key) const;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000484 /** \brief Access an object value by name, create a null member if it does not
Billy Donahue483eba82019-07-14 18:41:48 -0400485 * exist.
486 *
Billy Donahueb5e1fe82018-05-20 16:55:27 -0400487 * If the object has no entry for that name, then the member name used to
Billy Donahue483eba82019-07-14 18:41:48 -0400488 * store the new entry is not duplicated.
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000489 * Example of use:
Billy Donahue483eba82019-07-14 18:41:48 -0400490 * \code
491 * Json::Value object;
492 * static const StaticString code("code");
493 * object[code] = 1234;
494 * \endcode
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000495 */
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000496 Value& operator[](const StaticString& key);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000497#ifdef JSON_USE_CPPTL
498 /// Access an object value by name, create a null member if it does not exist.
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000499 Value& operator[](const CppTL::ConstString& key);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000500 /// Access an object value by name, returns null if there is no member with
501 /// that name.
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000502 const Value& operator[](const CppTL::ConstString& key) const;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000503#endif
504 /// Return the member named key if it exist, defaultValue otherwise.
Christopher Dunn0fd28752015-03-05 16:38:43 -0600505 /// \note deep copy
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000506 Value get(const char* key, const Value& defaultValue) const;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000507 /// Return the member named key if it exist, defaultValue otherwise.
Christopher Dunn0fd28752015-03-05 16:38:43 -0600508 /// \note deep copy
Christopher Dunn89704032015-07-11 12:09:59 -0500509 /// \note key may contain embedded nulls.
Jordan Baylesf34bf242019-10-11 11:19:00 -0700510 Value get(const char* begin, const char* end,
511 const Value& defaultValue) const;
Christopher Dunn25342ba2015-03-02 18:05:26 -0600512 /// Return the member named key if it exist, defaultValue otherwise.
Christopher Dunn0fd28752015-03-05 16:38:43 -0600513 /// \note deep copy
Christopher Dunn25342ba2015-03-02 18:05:26 -0600514 /// \param key may contain embedded nulls.
Billy Donahue1c2ed7a2019-01-17 16:35:29 -0500515 Value get(const String& key, const Value& defaultValue) const;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000516#ifdef JSON_USE_CPPTL
517 /// Return the member named key if it exist, defaultValue otherwise.
Christopher Dunn0fd28752015-03-05 16:38:43 -0600518 /// \note deep copy
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000519 Value get(const CppTL::ConstString& key, const Value& defaultValue) const;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000520#endif
Christopher Dunn25342ba2015-03-02 18:05:26 -0600521 /// Most general and efficient version of isMember()const, get()const,
522 /// and operator[]const
Christopher Dunn89704032015-07-11 12:09:59 -0500523 /// \note As stated elsewhere, behavior is undefined if (end-begin) >= 2^30
524 Value const* find(char const* begin, char const* end) const;
Christopher Dunnc28610f2015-02-21 11:44:16 -0600525 /// Most general and efficient version of object-mutators.
Christopher Dunn89704032015-07-11 12:09:59 -0500526 /// \note As stated elsewhere, behavior is undefined if (end-begin) >= 2^30
Christopher Dunnc28610f2015-02-21 11:44:16 -0600527 /// \return non-zero, but JSON_ASSERT if this is neither object nor nullValue.
Frank Richterd76fe562019-03-23 14:31:06 +0100528 Value* demand(char const* begin, char const* end);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000529 /// \brief Remove and return the named member.
530 ///
531 /// Do nothing if it did not exist.
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000532 /// \pre type() is objectValue or nullValue
533 /// \post type() is unchanged
Wolfram Röslera06b3902017-10-18 07:19:27 +0200534 void removeMember(const char* key);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000535 /// Same as removeMember(const char*)
Christopher Dunn25342ba2015-03-02 18:05:26 -0600536 /// \param key may contain embedded nulls.
Billy Donahue1c2ed7a2019-01-17 16:35:29 -0500537 void removeMember(const String& key);
Christopher Dunn89704032015-07-11 12:09:59 -0500538 /// Same as removeMember(const char* begin, const char* end, Value* removed),
Christopher Dunn25342ba2015-03-02 18:05:26 -0600539 /// but 'key' is null-terminated.
540 bool removeMember(const char* key, Value* removed);
Christopher Dunn76746b02015-01-21 16:01:30 -0600541 /** \brief Remove the named map member.
Billy Donahue483eba82019-07-14 18:41:48 -0400542 *
543 * Update 'removed' iff removed.
544 * \param key may contain embedded nulls.
545 * \return true iff removed (no exceptions)
546 */
Billy Donahue1c2ed7a2019-01-17 16:35:29 -0500547 bool removeMember(String const& key, Value* removed);
548 /// Same as removeMember(String const& key, Value* removed)
Christopher Dunn89704032015-07-11 12:09:59 -0500549 bool removeMember(const char* begin, const char* end, Value* removed);
Christopher Dunn9de2c2d2015-01-20 16:15:40 -0600550 /** \brief Remove the indexed array element.
Billy Donahue483eba82019-07-14 18:41:48 -0400551 *
552 * O(n) expensive operations.
553 * Update 'removed' iff removed.
554 * \return true if removed (no exceptions)
555 */
Marian Klymovfc201342018-06-02 20:15:26 +0300556 bool removeIndex(ArrayIndex index, Value* removed);
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000557
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000558 /// Return true if the object has a member named key.
Christopher Dunnc28610f2015-02-21 11:44:16 -0600559 /// \note 'key' must be null-terminated.
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000560 bool isMember(const char* key) const;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000561 /// Return true if the object has a member named key.
Christopher Dunn25342ba2015-03-02 18:05:26 -0600562 /// \param key may contain embedded nulls.
Billy Donahue1c2ed7a2019-01-17 16:35:29 -0500563 bool isMember(const String& key) const;
564 /// Same as isMember(String const& key)const
Christopher Dunn89704032015-07-11 12:09:59 -0500565 bool isMember(const char* begin, const char* end) const;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000566#ifdef JSON_USE_CPPTL
567 /// Return true if the object has a member named key.
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000568 bool isMember(const CppTL::ConstString& key) const;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000569#endif
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000570
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000571 /// \brief Return a list of the member names.
572 ///
573 /// If null, return an empty list.
574 /// \pre type() is objectValue or nullValue
575 /// \post if type() was nullValue, it remains nullValue
576 Members getMemberNames() const;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000577
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000578 //# ifdef JSON_USE_CPPTL
579 // EnumMemberNames enumMemberNames() const;
580 // EnumValues enumValues() const;
581 //# endif
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000582
Christopher Dunn1e3149a2015-01-25 14:16:13 -0600583 /// \deprecated Always pass len.
Jordan Baylesf34bf242019-10-11 11:19:00 -0700584 JSONCPP_DEPRECATED("Use setComment(String const&) instead.")
585 void setComment(const char* comment, CommentPlacement placement) {
Billy Donahue433107f2019-01-20 21:53:01 -0500586 setComment(String(comment, strlen(comment)), placement);
587 }
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000588 /// Comments must be //... or /* ... */
Billy Donahue433107f2019-01-20 21:53:01 -0500589 void setComment(const char* comment, size_t len, CommentPlacement placement) {
590 setComment(String(comment, len), placement);
591 }
Christopher Dunn1e3149a2015-01-25 14:16:13 -0600592 /// Comments must be //... or /* ... */
Billy Donahue433107f2019-01-20 21:53:01 -0500593 void setComment(String comment, CommentPlacement placement);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000594 bool hasComment(CommentPlacement placement) const;
595 /// Include delimiters and embedded newlines.
Billy Donahue1c2ed7a2019-01-17 16:35:29 -0500596 String getComment(CommentPlacement placement) const;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000597
Billy Donahue1c2ed7a2019-01-17 16:35:29 -0500598 String toStyledString() const;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000599
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000600 const_iterator begin() const;
601 const_iterator end() const;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000602
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000603 iterator begin();
604 iterator end();
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000605
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000606 // Accessors for the [start, limit) range of bytes within the JSON text from
607 // which this value was parsed, if any.
Christopher Dunnd4513fc2016-02-06 09:25:20 -0600608 void setOffsetStart(ptrdiff_t start);
609 void setOffsetLimit(ptrdiff_t limit);
610 ptrdiff_t getOffsetStart() const;
611 ptrdiff_t getOffsetLimit() const;
Aaron Jacobs68db6552014-04-23 23:41:12 +0000612
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000613private:
Jordan Baylesd5bd1a72019-06-24 14:06:45 -0700614 void setType(ValueType v) {
615 bits_.value_type_ = static_cast<unsigned char>(v);
616 }
Billy Donahue0c1cc6e2019-01-20 23:59:16 -0500617 bool isAllocated() const { return bits_.allocated_; }
618 void setIsAllocated(bool v) { bits_.allocated_ = v; }
619
Billy Donahue8eb5d892014-11-10 01:35:42 -0500620 void initBasic(ValueType type, bool allocated = false);
Andrey Okoshkin9b569c82018-01-12 15:59:20 +0300621 void dupPayload(const Value& other);
Andrey Okoshkinc69148c2018-01-12 11:26:34 +0300622 void releasePayload();
Andrey Okoshkin9b569c82018-01-12 15:59:20 +0300623 void dupMeta(const Value& other);
Billy Donahue8eb5d892014-11-10 01:35:42 -0500624
Christopher Dunnc28610f2015-02-21 11:44:16 -0600625 Value& resolveReference(const char* key);
626 Value& resolveReference(const char* key, const char* end);
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000627
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000628 // struct MemberNamesTransform
629 //{
630 // typedef const char *result_type;
631 // const char *operator()( const CZString &name ) const
632 // {
633 // return name.c_str();
634 // }
635 //};
636
637 union ValueHolder {
638 LargestInt int_;
639 LargestUInt uint_;
640 double real_;
641 bool bool_;
Billy Donahue0c1cc6e2019-01-20 23:59:16 -0500642 char* string_; // if allocated_, ptr to { unsigned, char[] }.
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000643 ObjectValues* map_;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000644 } value_;
Billy Donahue0c1cc6e2019-01-20 23:59:16 -0500645
646 struct {
647 // Really a ValueType, but types should agree for bitfield packing.
648 unsigned int value_type_ : 8;
649 // Unless allocated_, string_ must be null-terminated.
650 unsigned int allocated_ : 1;
651 } bits_;
652
Billy Donahue433107f2019-01-20 21:53:01 -0500653 class Comments {
654 public:
655 Comments() = default;
656 Comments(const Comments& that);
Billy Donahue00558b32019-01-21 16:42:25 -0500657 Comments(Comments&& that);
Billy Donahue433107f2019-01-20 21:53:01 -0500658 Comments& operator=(const Comments& that);
Billy Donahue00558b32019-01-21 16:42:25 -0500659 Comments& operator=(Comments&& that);
Billy Donahue433107f2019-01-20 21:53:01 -0500660 bool has(CommentPlacement slot) const;
661 String get(CommentPlacement slot) const;
Rosen Penevbcad4e42019-10-15 00:27:23 -0700662 void set(CommentPlacement slot, String comment);
Billy Donahue433107f2019-01-20 21:53:01 -0500663
664 private:
665 using Array = std::array<String, numberOfCommentPlacement>;
666 std::unique_ptr<Array> ptr_;
667 };
668 Comments comments_;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000669
670 // [start, limit) byte offsets in the source JSON text from which this Value
671 // was extracted.
Christopher Dunnd4513fc2016-02-06 09:25:20 -0600672 ptrdiff_t start_;
673 ptrdiff_t limit_;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000674};
675
676/** \brief Experimental and untested: represents an element of the "path" to
677 * access a node.
678 */
679class JSON_API PathArgument {
680public:
681 friend class Path;
682
683 PathArgument();
684 PathArgument(ArrayIndex index);
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000685 PathArgument(const char* key);
Rosen Penevbcad4e42019-10-15 00:27:23 -0700686 PathArgument(String key);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000687
688private:
Billy Donahueb5e1fe82018-05-20 16:55:27 -0400689 enum Kind { kindNone = 0, kindIndex, kindKey };
Billy Donahue1c2ed7a2019-01-17 16:35:29 -0500690 String key_;
Hans Johnsone817e4f2019-01-14 17:09:22 -0600691 ArrayIndex index_{};
Billy Donahue2b593a92019-01-18 03:46:57 -0500692 Kind kind_{kindNone};
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000693};
694
695/** \brief Experimental and untested: represents a "path" to access a node.
696 *
697 * Syntax:
698 * - "." => root node
699 * - ".[n]" => elements at index 'n' of root node (an array value)
700 * - ".name" => member named 'name' of root node (an object value)
701 * - ".name1.name2.name3"
702 * - ".[0][1][2].name1[3]"
703 * - ".%" => member name is provided as parameter
aliha472adb62019-08-26 15:37:05 -0400704 * - ".[%]" => index is provided as parameter
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000705 */
706class JSON_API Path {
707public:
Jordan Baylesf34bf242019-10-11 11:19:00 -0700708 Path(const String& path, const PathArgument& a1 = PathArgument(),
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000709 const PathArgument& a2 = PathArgument(),
710 const PathArgument& a3 = PathArgument(),
711 const PathArgument& a4 = PathArgument(),
712 const PathArgument& a5 = PathArgument());
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000713
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000714 const Value& resolve(const Value& root) const;
715 Value resolve(const Value& root, const Value& defaultValue) const;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000716 /// Creates the "path" to access the specified node and returns a reference on
717 /// the node.
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000718 Value& make(Value& root) const;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000719
720private:
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000721 typedef std::vector<const PathArgument*> InArgs;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000722 typedef std::vector<PathArgument> Args;
723
Billy Donahue1c2ed7a2019-01-17 16:35:29 -0500724 void makePath(const String& path, const InArgs& in);
Jordan Baylesf34bf242019-10-11 11:19:00 -0700725 void addPathInArg(const String& path, const InArgs& in,
726 InArgs::const_iterator& itInArg, PathArgument::Kind kind);
Billy Donahue1c2ed7a2019-01-17 16:35:29 -0500727 static void invalidPath(const String& path, int location);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000728
729 Args args_;
730};
731
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000732/** \brief base class for Value iterators.
733 *
734 */
735class JSON_API ValueIteratorBase {
736public:
737 typedef std::bidirectional_iterator_tag iterator_category;
738 typedef unsigned int size_t;
739 typedef int difference_type;
740 typedef ValueIteratorBase SelfType;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000741
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000742 bool operator==(const SelfType& other) const { return isEqual(other); }
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000743
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000744 bool operator!=(const SelfType& other) const { return !isEqual(other); }
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000745
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000746 difference_type operator-(const SelfType& other) const {
Kevin Grant4c5832a2015-02-14 20:53:35 -0800747 return other.computeDistance(*this);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000748 }
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000749
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000750 /// Return either the index or the member name of the referenced value as a
751 /// Value.
752 Value key() const;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000753
Billy Donahueb5e1fe82018-05-20 16:55:27 -0400754 /// Return the index of the referenced Value, or -1 if it is not an
755 /// arrayValue.
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000756 UInt index() const;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000757
Christopher Dunned495ed2015-03-08 14:01:28 -0500758 /// Return the member name of the referenced Value, or "" if it is not an
759 /// objectValue.
760 /// \note Avoid `c_str()` on result, as embedded zeroes are possible.
Billy Donahue1c2ed7a2019-01-17 16:35:29 -0500761 String name() const;
Christopher Dunned495ed2015-03-08 14:01:28 -0500762
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000763 /// Return the member name of the referenced Value. "" if it is not an
764 /// objectValue.
Billy Donahueb5e1fe82018-05-20 16:55:27 -0400765 /// \deprecated This cannot be used for UTF-8 strings, since there can be
766 /// embedded nulls.
Jordan Baylesf34bf242019-10-11 11:19:00 -0700767 JSONCPP_DEPRECATED("Use `key = name();` instead.")
768 char const* memberName() const;
Christopher Dunnc28610f2015-02-21 11:44:16 -0600769 /// Return the member name of the referenced Value, or NULL if it is not an
770 /// objectValue.
Christopher Dunned495ed2015-03-08 14:01:28 -0500771 /// \note Better version than memberName(). Allows embedded nulls.
Christopher Dunnc28610f2015-02-21 11:44:16 -0600772 char const* memberName(char const** end) const;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000773
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000774protected:
Hans Johnsonb0826932019-10-17 12:52:13 -0500775 /*! Internal utility functions to assist with implementing
776 * other iterator functions. The const and non-const versions
777 * of the "deref" protected methods expose the protected
778 * current_ member variable in a way that can often be
779 * optimized away by the compiler.
780 */
781 const Value& deref() const;
782 Value& deref();
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000783
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000784 void increment();
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000785
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000786 void decrement();
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000787
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000788 difference_type computeDistance(const SelfType& other) const;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000789
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000790 bool isEqual(const SelfType& other) const;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000791
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000792 void copy(const SelfType& other);
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000793
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000794private:
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000795 Value::ObjectValues::iterator current_;
796 // Indicates that iterator is for a null value.
Billy Donahue2b593a92019-01-18 03:46:57 -0500797 bool isNull_{true};
Christopher Dunn2a10f4a2015-04-28 04:55:12 +0100798
799public:
800 // For some reason, BORLAND needs these at the end, rather
801 // than earlier. No idea why.
802 ValueIteratorBase();
803 explicit ValueIteratorBase(const Value::ObjectValues::iterator& current);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000804};
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000805
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000806/** \brief const iterator for object and array value.
807 *
808 */
809class JSON_API ValueConstIterator : public ValueIteratorBase {
810 friend class Value;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000811
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000812public:
813 typedef const Value value_type;
Billy Donahueb5e1fe82018-05-20 16:55:27 -0400814 // typedef unsigned int size_t;
815 // typedef int difference_type;
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000816 typedef const Value& reference;
817 typedef const Value* pointer;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000818 typedef ValueConstIterator SelfType;
819
820 ValueConstIterator();
ycqiuc8a8cfc2015-10-06 16:46:19 +0800821 ValueConstIterator(ValueIterator const& other);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000822
823private:
Billy Donahueb5e1fe82018-05-20 16:55:27 -0400824 /*! \internal Use by Value to create an iterator.
825 */
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000826 explicit ValueConstIterator(const Value::ObjectValues::iterator& current);
Billy Donahueb5e1fe82018-05-20 16:55:27 -0400827
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000828public:
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000829 SelfType& operator=(const ValueIteratorBase& other);
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000830
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000831 SelfType operator++(int) {
832 SelfType temp(*this);
833 ++*this;
834 return temp;
835 }
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000836
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000837 SelfType operator--(int) {
838 SelfType temp(*this);
839 --*this;
840 return temp;
841 }
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000842
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000843 SelfType& operator--() {
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000844 decrement();
845 return *this;
846 }
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000847
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000848 SelfType& operator++() {
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000849 increment();
850 return *this;
851 }
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000852
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000853 reference operator*() const { return deref(); }
Braden McDorman540db3b2014-09-14 02:31:23 -0500854
855 pointer operator->() const { return &deref(); }
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000856};
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000857
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000858/** \brief Iterator for object and array value.
859 */
860class JSON_API ValueIterator : public ValueIteratorBase {
861 friend class Value;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000862
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000863public:
864 typedef Value value_type;
865 typedef unsigned int size_t;
866 typedef int difference_type;
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000867 typedef Value& reference;
868 typedef Value* pointer;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000869 typedef ValueIterator SelfType;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000870
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000871 ValueIterator();
ycqiuc8a8cfc2015-10-06 16:46:19 +0800872 explicit ValueIterator(const ValueConstIterator& other);
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000873 ValueIterator(const ValueIterator& other);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000874
875private:
Billy Donahueb5e1fe82018-05-20 16:55:27 -0400876 /*! \internal Use by Value to create an iterator.
877 */
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000878 explicit ValueIterator(const Value::ObjectValues::iterator& current);
Billy Donahueb5e1fe82018-05-20 16:55:27 -0400879
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000880public:
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000881 SelfType& operator=(const SelfType& other);
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000882
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000883 SelfType operator++(int) {
884 SelfType temp(*this);
885 ++*this;
886 return temp;
887 }
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000888
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000889 SelfType operator--(int) {
890 SelfType temp(*this);
891 --*this;
892 return temp;
893 }
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000894
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000895 SelfType& operator--() {
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000896 decrement();
897 return *this;
898 }
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000899
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000900 SelfType& operator++() {
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000901 increment();
902 return *this;
903 }
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000904
Hans Johnsonb0826932019-10-17 12:52:13 -0500905 /*! The return value of non-const iterators can be
906 * changed, so the these functions are not const
907 * because the returned references/pointers can be used
908 * to change state of the base class.
909 */
910 reference operator*() { return deref(); }
911 pointer operator->() { return &deref(); }
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000912};
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000913
Billy Donahue1d956282018-03-06 12:51:58 -0500914inline void swap(Value& a, Value& b) { a.swap(b); }
915
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000916} // namespace Json
917
Sergiy80d6e666f2016-12-03 22:29:14 +0200918#pragma pack(pop)
datadiode9454e682015-01-20 15:25:04 -0600919
Baptiste Lepilleureafd7022013-05-08 20:21:11 +0000920#if defined(JSONCPP_DISABLE_DLL_INTERFACE_WARNING)
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000921#pragma warning(pop)
Baptiste Lepilleureafd7022013-05-08 20:21:11 +0000922#endif // if defined(JSONCPP_DISABLE_DLL_INTERFACE_WARNING)
923
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000924#endif // CPPTL_JSON_H_INCLUDED