blob: bea2a568e11650ee13648d62d083d2ceedeea2fd [file] [log] [blame]
Devin Jeanpierre59e4d352017-07-21 03:44:36 -07001// Copyright 2007-2010 Baptiste Lepilleur and The JsonCpp Authors
Baptiste Lepilleur7469f1d2010-04-20 21:35:19 +00002// Distributed under MIT license, or public domain if desired and
3// recognized in your jurisdiction.
4// See file LICENSE for detail or copy at http://jsoncpp.sourceforge.net/LICENSE
5
Jordan Bayles9704ced2019-11-14 09:38:11 -08006#ifndef JSON_H_INCLUDED
7#define JSON_H_INCLUDED
Christopher Dunn6d135cb2007-06-13 15:51:04 +00008
Baptiste Lepilleureadc4782011-05-02 21:09:30 +00009#if !defined(JSON_IS_AMALGAMATION)
Aaron Jacobs9fa4e842014-07-01 08:48:54 +100010#include "forwards.h"
Baptiste Lepilleureadc4782011-05-02 21:09:30 +000011#endif // if !defined(JSON_IS_AMALGAMATION)
es1x2ee3b1d2019-10-11 21:33:36 +030012
13// Conditional NORETURN attribute on the throw functions would:
14// a) suppress false positives from static code analysis
15// b) possibly improve optimization opportunities.
16#if !defined(JSONCPP_NORETURN)
17#if defined(_MSC_VER) && _MSC_VER == 1800
18#define JSONCPP_NORETURN __declspec(noreturn)
19#else
20#define JSONCPP_NORETURN [[noreturn]]
21#endif
22#endif
23
David Gobbidc180eb2020-02-13 14:22:49 -070024// Support for '= delete' with template declarations was a late addition
25// to the c++11 standard and is rejected by clang 3.8 and Apple clang 8.2
26// even though these declare themselves to be c++11 compilers.
27#if !defined(JSONCPP_TEMPLATE_DELETE)
28#if defined(__clang__) && defined(__apple_build_version__)
29#if __apple_build_version__ <= 8000042
30#define JSONCPP_TEMPLATE_DELETE
31#endif
32#elif defined(__clang__)
33#if __clang_major__ == 3 && __clang_minor__ <= 8
34#define JSONCPP_TEMPLATE_DELETE
35#endif
36#endif
37#if !defined(JSONCPP_TEMPLATE_DELETE)
38#define JSONCPP_TEMPLATE_DELETE = delete
39#endif
40#endif
41
Billy Donahue433107f2019-01-20 21:53:01 -050042#include <array>
Billy Donahueb5e1fe82018-05-20 16:55:27 -040043#include <exception>
Jordan Bayles9704ced2019-11-14 09:38:11 -080044#include <map>
Billy Donahue433107f2019-01-20 21:53:01 -050045#include <memory>
Aaron Jacobs9fa4e842014-07-01 08:48:54 +100046#include <string>
47#include <vector>
Christopher Dunn6d135cb2007-06-13 15:51:04 +000048
Aaron Jacobs9fa4e842014-07-01 08:48:54 +100049// Disable warning C4251: <data member>: <type> needs to have dll-interface to
50// be used by...
Baptiste Lepilleureafd7022013-05-08 20:21:11 +000051#if defined(JSONCPP_DISABLE_DLL_INTERFACE_WARNING)
Aaron Jacobs9fa4e842014-07-01 08:48:54 +100052#pragma warning(push)
53#pragma warning(disable : 4251)
Baptiste Lepilleureafd7022013-05-08 20:21:11 +000054#endif // if defined(JSONCPP_DISABLE_DLL_INTERFACE_WARNING)
55
Sergiy80d6e666f2016-12-03 22:29:14 +020056#pragma pack(push, 8)
57
Christopher Dunn6d135cb2007-06-13 15:51:04 +000058/** \brief JSON (JavaScript Object Notation).
59 */
60namespace Json {
61
Jordan Bayles83cc9212019-06-06 13:41:47 -070062#if JSON_USE_EXCEPTION
Christopher Dunn75279cc2015-03-08 12:20:06 -050063/** Base class for all exceptions we throw.
Christopher Dunn4e30c4f2015-03-08 12:56:32 -050064 *
65 * We use nothing but these internally. Of course, STL can throw others.
Christopher Dunn75279cc2015-03-08 12:20:06 -050066 */
Christopher Dunn949babd2015-07-23 00:19:12 -050067class JSON_API Exception : public std::exception {
68public:
Billy Donahue1c2ed7a2019-01-17 16:35:29 -050069 Exception(String msg);
Hans Johnson2853b1c2019-01-11 13:58:53 -060070 ~Exception() JSONCPP_NOEXCEPT override;
71 char const* what() const JSONCPP_NOEXCEPT override;
Billy Donahueb5e1fe82018-05-20 16:55:27 -040072
Christopher Dunn949babd2015-07-23 00:19:12 -050073protected:
Billy Donahue1c2ed7a2019-01-17 16:35:29 -050074 String msg_;
Christopher Dunn949babd2015-07-23 00:19:12 -050075};
76
Christopher Dunn53837942015-03-08 12:31:00 -050077/** Exceptions which the user cannot easily avoid.
78 *
Christopher Dunn4e30c4f2015-03-08 12:56:32 -050079 * E.g. out-of-memory (when we use malloc), stack-overflow, malicious input
Dhruv Paranjape8996c372017-07-08 17:27:07 +053080 *
Christopher Dunn4e30c4f2015-03-08 12:56:32 -050081 * \remark derived from Json::Exception
Christopher Dunn53837942015-03-08 12:31:00 -050082 */
Christopher Dunn949babd2015-07-23 00:19:12 -050083class JSON_API RuntimeError : public Exception {
84public:
Billy Donahue1c2ed7a2019-01-17 16:35:29 -050085 RuntimeError(String const& msg);
Christopher Dunn949babd2015-07-23 00:19:12 -050086};
87
Christopher Dunn4e30c4f2015-03-08 12:56:32 -050088/** Exceptions thrown by JSON_ASSERT/JSON_FAIL macros.
Christopher Dunn53837942015-03-08 12:31:00 -050089 *
90 * These are precondition-violations (user bugs) and internal errors (our bugs).
Dhruv Paranjape8996c372017-07-08 17:27:07 +053091 *
Christopher Dunn4e30c4f2015-03-08 12:56:32 -050092 * \remark derived from Json::Exception
Christopher Dunn53837942015-03-08 12:31:00 -050093 */
Christopher Dunn949babd2015-07-23 00:19:12 -050094class JSON_API LogicError : public Exception {
95public:
Billy Donahue1c2ed7a2019-01-17 16:35:29 -050096 LogicError(String const& msg);
Christopher Dunn949babd2015-07-23 00:19:12 -050097};
Jordan Bayles83cc9212019-06-06 13:41:47 -070098#endif
Christopher Dunn53837942015-03-08 12:31:00 -050099
Christopher Dunn4e30c4f2015-03-08 12:56:32 -0500100/// used internally
es1x2ee3b1d2019-10-11 21:33:36 +0300101JSONCPP_NORETURN void throwRuntimeError(String const& msg);
Christopher Dunn4e30c4f2015-03-08 12:56:32 -0500102/// used internally
es1x2ee3b1d2019-10-11 21:33:36 +0300103JSONCPP_NORETURN void throwLogicError(String const& msg);
Christopher Dunn75279cc2015-03-08 12:20:06 -0500104
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000105/** \brief Type of the value held by a Value object.
106 */
107enum ValueType {
108 nullValue = 0, ///< 'null' value
109 intValue, ///< signed integer value
110 uintValue, ///< unsigned integer value
111 realValue, ///< double value
112 stringValue, ///< UTF-8 string value
113 booleanValue, ///< bool value
114 arrayValue, ///< array value (ordered list)
115 objectValue ///< object value (collection of name/value pairs).
116};
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000117
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000118enum CommentPlacement {
119 commentBefore = 0, ///< a comment placed on the line before a value
120 commentAfterOnSameLine, ///< a comment just after a value on the same line
121 commentAfter, ///< a comment on the line after a value (only make sense for
Aaron Jacobs3a0c4fc2014-07-01 09:20:48 +1000122 /// root value)
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000123 numberOfCommentPlacement
124};
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000125
Mike Ra07fc532018-03-13 23:35:31 +0300126/** \brief Type of precision for formatting of real values.
127 */
128enum PrecisionType {
129 significantDigits = 0, ///< we set max number of significant digits in string
130 decimalPlaces ///< we set max number of digits after "." in string
131};
132
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000133/** \brief Lightweight wrapper to tag static string.
134 *
Josh Sorefe6a588a2017-12-03 11:54:29 -0500135 * Value constructor and objectValue member assignment takes advantage of the
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000136 * StaticString and avoid the cost of string duplication when storing the
137 * string or the member name.
138 *
139 * Example of usage:
140 * \code
141 * Json::Value aValue( StaticString("some text") );
142 * Json::Value object;
143 * static const StaticString code("code");
144 * object[code] = 1234;
145 * \endcode
146 */
147class JSON_API StaticString {
148public:
Christopher Dunnff617522015-03-06 10:31:46 -0600149 explicit StaticString(const char* czstring) : c_str_(czstring) {}
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000150
Christopher Dunnff617522015-03-06 10:31:46 -0600151 operator const char*() const { return c_str_; }
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000152
Christopher Dunnff617522015-03-06 10:31:46 -0600153 const char* c_str() const { return c_str_; }
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000154
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000155private:
Christopher Dunnff617522015-03-06 10:31:46 -0600156 const char* c_str_;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000157};
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000158
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000159/** \brief Represents a <a HREF="http://www.json.org">JSON</a> value.
160 *
161 * This class is a discriminated union wrapper that can represents a:
162 * - signed integer [range: Value::minInt - Value::maxInt]
163 * - unsigned integer (range: 0 - Value::maxUInt)
164 * - double
165 * - UTF-8 string
166 * - boolean
167 * - 'null'
168 * - an ordered list of Value
169 * - collection of name/value pairs (javascript object)
170 *
171 * The type of the held value is represented by a #ValueType and
172 * can be obtained using type().
173 *
Christopher Dunnc28610f2015-02-21 11:44:16 -0600174 * Values of an #objectValue or #arrayValue can be accessed using operator[]()
175 * methods.
176 * Non-const methods will automatically create the a #nullValue element
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000177 * if it does not exist.
Christopher Dunnc28610f2015-02-21 11:44:16 -0600178 * The sequence of an #arrayValue will be automatically resized and initialized
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000179 * with #nullValue. resize() can be used to enlarge or truncate an #arrayValue.
180 *
Christopher Dunnc28610f2015-02-21 11:44:16 -0600181 * The get() methods can be used to obtain default value in the case the
182 * required element does not exist.
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000183 *
Mathias L. Baumann08ddeed2018-12-12 17:59:43 +0100184 * It is possible to iterate over the list of member keys of an object using
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000185 * the getMemberNames() method.
Christopher Dunn25342ba2015-03-02 18:05:26 -0600186 *
Christopher Dunnc28610f2015-02-21 11:44:16 -0600187 * \note #Value string-length fit in size_t, but keys must be < 2^30.
188 * (The reason is an implementation detail.) A #CharReader will raise an
Christopher Dunn25342ba2015-03-02 18:05:26 -0600189 * exception if a bound is exceeded to avoid security holes in your app,
190 * but the Value API does *not* check bounds. That is the responsibility
191 * of the caller.
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000192 */
193class JSON_API Value {
194 friend class ValueIteratorBase;
Billy Donahueb5e1fe82018-05-20 16:55:27 -0400195
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000196public:
Chen2983f5a2019-12-04 09:08:45 +0800197 using Members = std::vector<String>;
198 using iterator = ValueIterator;
199 using const_iterator = ValueConstIterator;
200 using UInt = Json::UInt;
201 using Int = Json::Int;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000202#if defined(JSON_HAS_INT64)
Chen2983f5a2019-12-04 09:08:45 +0800203 using UInt64 = Json::UInt64;
204 using Int64 = Json::Int64;
Baptiste Lepilleur842e9ac2010-12-27 17:45:23 +0000205#endif // defined(JSON_HAS_INT64)
Chen2983f5a2019-12-04 09:08:45 +0800206 using LargestInt = Json::LargestInt;
207 using LargestUInt = Json::LargestUInt;
208 using ArrayIndex = Json::ArrayIndex;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000209
Wolfram Röslerff6b4492017-09-14 09:31:36 +0200210 // Required for boost integration, e. g. BOOST_TEST
Chen2983f5a2019-12-04 09:08:45 +0800211 using value_type = std::string;
Wolfram Röslerff6b4492017-09-14 09:31:36 +0200212
Jordan Bayles25c57812019-07-11 14:27:29 -0700213#if JSON_USE_NULLREF
214 // Binary compatibility kludges, do not use.
215 static const Value& null;
216 static const Value& nullRef;
217#endif
218
219 // null and nullRef are deprecated, use this instead.
220 static Value const& nullSingleton();
Christopher Dunn0f288ae2016-06-26 18:47:43 -0500221
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000222 /// Minimum signed integer value that can be stored in a Json::Value.
Jordan Bayles81ae1d52019-09-16 12:37:14 -0700223 static constexpr LargestInt minLargestInt =
224 LargestInt(~(LargestUInt(-1) / 2));
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000225 /// Maximum signed integer value that can be stored in a Json::Value.
dota177ef0f9f2019-09-17 01:33:47 +0800226 static constexpr LargestInt maxLargestInt = LargestInt(LargestUInt(-1) / 2);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000227 /// Maximum unsigned integer value that can be stored in a Json::Value.
dota177ef0f9f2019-09-17 01:33:47 +0800228 static constexpr LargestUInt maxLargestUInt = LargestUInt(-1);
Baptiste Lepilleur842e9ac2010-12-27 17:45:23 +0000229
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000230 /// Minimum signed int value that can be stored in a Json::Value.
dota177ef0f9f2019-09-17 01:33:47 +0800231 static constexpr Int minInt = Int(~(UInt(-1) / 2));
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000232 /// Maximum signed int value that can be stored in a Json::Value.
dota177ef0f9f2019-09-17 01:33:47 +0800233 static constexpr Int maxInt = Int(UInt(-1) / 2);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000234 /// Maximum unsigned int value that can be stored in a Json::Value.
dota177ef0f9f2019-09-17 01:33:47 +0800235 static constexpr UInt maxUInt = UInt(-1);
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000236
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000237#if defined(JSON_HAS_INT64)
238 /// Minimum signed 64 bits int value that can be stored in a Json::Value.
dota177ef0f9f2019-09-17 01:33:47 +0800239 static constexpr Int64 minInt64 = Int64(~(UInt64(-1) / 2));
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000240 /// Maximum signed 64 bits int value that can be stored in a Json::Value.
dota177ef0f9f2019-09-17 01:33:47 +0800241 static constexpr Int64 maxInt64 = Int64(UInt64(-1) / 2);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000242 /// Maximum unsigned 64 bits int value that can be stored in a Json::Value.
dota177ef0f9f2019-09-17 01:33:47 +0800243 static constexpr UInt64 maxUInt64 = UInt64(-1);
Aaron Jacobsf1053e72011-05-24 03:18:02 +0000244#endif // defined(JSON_HAS_INT64)
Mike Ra07fc532018-03-13 23:35:31 +0300245 /// Default precision for real value for string representation.
dota177ef0f9f2019-09-17 01:33:47 +0800246 static constexpr UInt defaultRealPrecision = 17;
247 // The constant is hard-coded because some compiler have trouble
248 // converting Value::maxUInt64 to a double correctly (AIX/xlC).
249 // Assumes that UInt64 is a 64 bits integer.
250 static constexpr double maxUInt64AsDouble = 18446744073709551615.0;
Darcy Beurle798f6ba2017-12-22 22:48:20 +0100251// Workaround for bug in the NVIDIAs CUDA 9.1 nvcc compiler
252// when using gcc and clang backend compilers. CZString
253// cannot be defined as private. See issue #486
254#ifdef __NVCC__
255public:
256#else
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000257private:
Darcy Beurle798f6ba2017-12-22 22:48:20 +0100258#endif
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000259#ifndef JSONCPP_DOC_EXCLUDE_IMPLEMENTATION
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000260 class CZString {
261 public:
Billy Donahueb5e1fe82018-05-20 16:55:27 -0400262 enum DuplicationPolicy { noDuplication = 0, duplicate, duplicateOnCopy };
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000263 CZString(ArrayIndex index);
Christopher Dunnc28610f2015-02-21 11:44:16 -0600264 CZString(char const* str, unsigned length, DuplicationPolicy allocate);
265 CZString(CZString const& other);
Motti2b008912015-04-20 17:44:47 +0300266 CZString(CZString&& other);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000267 ~CZString();
Dhruv Paranjape0ba8bd72017-07-08 17:47:13 +0530268 CZString& operator=(const CZString& other);
Dhruv Paranjape0ba8bd72017-07-08 17:47:13 +0530269 CZString& operator=(CZString&& other);
Dhruv Paranjape0ba8bd72017-07-08 17:47:13 +0530270
Christopher Dunnc28610f2015-02-21 11:44:16 -0600271 bool operator<(CZString const& other) const;
272 bool operator==(CZString const& other) const;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000273 ArrayIndex index() const;
Billy Donahueb5e1fe82018-05-20 16:55:27 -0400274 // const char* c_str() const; ///< \deprecated
Christopher Dunnc28610f2015-02-21 11:44:16 -0600275 char const* data() const;
276 unsigned length() const;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000277 bool isStaticString() const;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000278
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000279 private:
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000280 void swap(CZString& other);
Christopher Dunnc28610f2015-02-21 11:44:16 -0600281
Christopher Dunn57ad0512015-03-02 12:10:35 -0600282 struct StringStorage {
Billy Donahueb5e1fe82018-05-20 16:55:27 -0400283 unsigned policy_ : 2;
284 unsigned length_ : 30; // 1GB max
Christopher Dunn57ad0512015-03-02 12:10:35 -0600285 };
286
Billy Donahueb5e1fe82018-05-20 16:55:27 -0400287 char const* cstr_; // actually, a prefixed string, unless policy is noDup
Christopher Dunn57ad0512015-03-02 12:10:35 -0600288 union {
289 ArrayIndex index_;
290 StringStorage storage_;
291 };
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000292 };
293
294public:
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000295 typedef std::map<CZString, Value> ObjectValues;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000296#endif // ifndef JSONCPP_DOC_EXCLUDE_IMPLEMENTATION
297
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000298public:
Billy Donahue483eba82019-07-14 18:41:48 -0400299 /**
300 * \brief Create a default Value of the given type.
301 *
302 * This is a very useful constructor.
303 * To create an empty array, pass arrayValue.
304 * To create an empty object, pass objectValue.
305 * Another Value can then be set to this one by assignment.
306 * This is useful since clear() and resize() will not alter types.
307 *
308 * Examples:
309 * \code
310 * Json::Value null_value; // null
311 * Json::Value arr_value(Json::arrayValue); // []
312 * Json::Value obj_value(Json::objectValue); // {}
313 * \endcode
314 */
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000315 Value(ValueType type = nullValue);
316 Value(Int value);
317 Value(UInt value);
Baptiste Lepilleur842e9ac2010-12-27 17:45:23 +0000318#if defined(JSON_HAS_INT64)
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000319 Value(Int64 value);
320 Value(UInt64 value);
Baptiste Lepilleur842e9ac2010-12-27 17:45:23 +0000321#endif // if defined(JSON_HAS_INT64)
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000322 Value(double value);
Christopher Dunn8a702972015-03-03 10:38:27 -0600323 Value(const char* value); ///< Copy til first 0. (NULL causes to seg-fault.)
Christopher Dunn89704032015-07-11 12:09:59 -0500324 Value(const char* begin, const char* end); ///< Copy all, incl zeroes.
Billy Donahue483eba82019-07-14 18:41:48 -0400325 /**
326 * \brief Constructs a value from a static string.
327 *
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000328 * Like other value string constructor but do not duplicate the string for
Billy Donahue483eba82019-07-14 18:41:48 -0400329 * internal storage. The given string must remain alive after the call to
330 * this constructor.
331 *
Christopher Dunnc28610f2015-02-21 11:44:16 -0600332 * \note This works only for null-terminated strings. (We cannot change the
Billy Donahue483eba82019-07-14 18:41:48 -0400333 * size of this class, so we have nowhere to store the length, which might be
334 * computed later for various operations.)
Christopher Dunnc28610f2015-02-21 11:44:16 -0600335 *
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000336 * Example of usage:
Billy Donahue483eba82019-07-14 18:41:48 -0400337 * \code
338 * static StaticString foo("some text");
339 * Json::Value aValue(foo);
340 * \endcode
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000341 */
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000342 Value(const StaticString& value);
Billy Donahue483eba82019-07-14 18:41:48 -0400343 Value(const String& value);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000344 Value(bool value);
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000345 Value(const Value& other);
Motti2b008912015-04-20 17:44:47 +0300346 Value(Value&& other);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000347 ~Value();
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000348
Billy Donahue0c1cc6e2019-01-20 23:59:16 -0500349 /// \note Overwrite existing comments. To preserve comments, use
Billy Donahueb5e1fe82018-05-20 16:55:27 -0400350 /// #swapPayload().
Billy Donahue0c1cc6e2019-01-20 23:59:16 -0500351 Value& operator=(const Value& other);
352 Value& operator=(Value&& other);
Dhruv Paranjape8996c372017-07-08 17:27:07 +0530353
Christopher Dunn66eb72f2015-01-20 11:02:22 -0600354 /// Swap everything.
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000355 void swap(Value& other);
Christopher Dunn66eb72f2015-01-20 11:02:22 -0600356 /// Swap values but leave comments and source offsets in place.
357 void swapPayload(Value& other);
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000358
Dhruv Paranjape8996c372017-07-08 17:27:07 +0530359 /// copy everything.
360 void copy(const Value& other);
361 /// copy values but leave comments and source offsets in place.
362 void copyPayload(const Value& other);
363
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000364 ValueType type() const;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000365
Christopher Dunn66eb72f2015-01-20 11:02:22 -0600366 /// Compare payload only, not comments etc.
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000367 bool operator<(const Value& other) const;
368 bool operator<=(const Value& other) const;
369 bool operator>=(const Value& other) const;
370 bool operator>(const Value& other) const;
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000371 bool operator==(const Value& other) const;
372 bool operator!=(const Value& other) const;
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000373 int compare(const Value& other) const;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000374
Christopher Dunn8a702972015-03-03 10:38:27 -0600375 const char* asCString() const; ///< Embedded zeroes could cause you trouble!
dawescae564652016-03-14 19:11:02 -0500376#if JSONCPP_USING_SECURE_MEMORY
Billy Donahueb5e1fe82018-05-20 16:55:27 -0400377 unsigned getCStringLength() const; // Allows you to understand the length of
378 // the CString
dawescae564652016-03-14 19:11:02 -0500379#endif
Billy Donahue1c2ed7a2019-01-17 16:35:29 -0500380 String asString() const; ///< Embedded zeroes are possible.
Christopher Dunnc28610f2015-02-21 11:44:16 -0600381 /** Get raw char* of string-value.
382 * \return false if !string. (Seg-fault if str or end are NULL.)
383 */
Billy Donahueb5e1fe82018-05-20 16:55:27 -0400384 bool getString(char const** begin, char const** end) const;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000385 Int asInt() const;
386 UInt asUInt() const;
Aaron Jacobsf1053e72011-05-24 03:18:02 +0000387#if defined(JSON_HAS_INT64)
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000388 Int64 asInt64() const;
389 UInt64 asUInt64() const;
Aaron Jacobsf1053e72011-05-24 03:18:02 +0000390#endif // if defined(JSON_HAS_INT64)
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000391 LargestInt asLargestInt() const;
392 LargestUInt asLargestUInt() const;
393 float asFloat() const;
394 double asDouble() const;
395 bool asBool() const;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000396
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000397 bool isNull() const;
398 bool isBool() const;
399 bool isInt() const;
400 bool isInt64() const;
401 bool isUInt() const;
402 bool isUInt64() const;
403 bool isIntegral() const;
404 bool isDouble() const;
405 bool isNumeric() const;
406 bool isString() const;
407 bool isArray() const;
408 bool isObject() const;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000409
Billy Donahue53c8e2c2019-11-11 22:43:52 -0500410 /// The `as<T>` and `is<T>` member function templates and specializations.
David Gobbidc180eb2020-02-13 14:22:49 -0700411 template <typename T> T as() const JSONCPP_TEMPLATE_DELETE;
412 template <typename T> bool is() const JSONCPP_TEMPLATE_DELETE;
Billy Donahue53c8e2c2019-11-11 22:43:52 -0500413
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000414 bool isConvertibleTo(ValueType other) const;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000415
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000416 /// Number of values in array or object
417 ArrayIndex size() const;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000418
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000419 /// \brief Return true if empty array, empty object, or null;
420 /// otherwise, false.
421 bool empty() const;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000422
Wolfram Rösler90794222017-12-05 18:18:55 +0100423 /// Return !isNull()
drgler04abe382018-01-13 15:28:19 +0100424 JSONCPP_OP_EXPLICIT operator bool() const;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000425
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000426 /// Remove all object members and array elements.
427 /// \pre type() is arrayValue, objectValue, or nullValue
428 /// \post type() is unchanged
429 void clear();
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000430
Marian Klymovfc201342018-06-02 20:15:26 +0300431 /// Resize the array to newSize elements.
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000432 /// New elements are initialized to null.
433 /// May only be called on nullValue or arrayValue.
434 /// \pre type() is arrayValue or nullValue
435 /// \post type() is arrayValue
Marian Klymovfc201342018-06-02 20:15:26 +0300436 void resize(ArrayIndex newSize);
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000437
Billy Donahue483eba82019-07-14 18:41:48 -0400438 //@{
439 /// Access an array element (zero based index). If the array contains less
440 /// than index element, then null value are inserted in the array so that
441 /// its size is index+1.
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000442 /// (You may need to say 'value[0u]' to get your compiler to distinguish
Billy Donahue483eba82019-07-14 18:41:48 -0400443 /// this from the operator[] which takes a string.)
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000444 Value& operator[](ArrayIndex index);
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000445 Value& operator[](int index);
Billy Donahue483eba82019-07-14 18:41:48 -0400446 //@}
Baptiste Lepilleurfa130ef2010-12-24 12:47:14 +0000447
Billy Donahue483eba82019-07-14 18:41:48 -0400448 //@{
449 /// Access an array element (zero based index).
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000450 /// (You may need to say 'value[0u]' to get your compiler to distinguish
Billy Donahue483eba82019-07-14 18:41:48 -0400451 /// this from the operator[] which takes a string.)
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000452 const Value& operator[](ArrayIndex index) const;
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000453 const Value& operator[](int index) const;
Billy Donahue483eba82019-07-14 18:41:48 -0400454 //@}
Baptiste Lepilleurfa130ef2010-12-24 12:47:14 +0000455
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000456 /// If the array contains at least index+1 elements, returns the element
Billy Donahue483eba82019-07-14 18:41:48 -0400457 /// value, otherwise returns defaultValue.
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000458 Value get(ArrayIndex index, const Value& defaultValue) const;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000459 /// Return true if index < size().
460 bool isValidIndex(ArrayIndex index) const;
461 /// \brief Append value to array at the end.
462 ///
463 /// Equivalent to jsonvalue[jsonvalue.size()] = value;
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000464 Value& append(const Value& value);
Dhruv Paranjape23c44d92017-07-08 17:30:47 +0530465 Value& append(Value&& value);
Chena0bd9ad2019-12-03 09:13:42 +0800466
dota17bdacfd72019-10-16 14:33:53 +0800467 /// \brief Insert value in array at specific index
Chena0bd9ad2019-12-03 09:13:42 +0800468 bool insert(ArrayIndex index, const Value& newValue);
469 bool insert(ArrayIndex index, Value&& newValue);
Dhruv Paranjape23c44d92017-07-08 17:30:47 +0530470
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000471 /// Access an object value by name, create a null member if it does not exist.
Christopher Dunn25342ba2015-03-02 18:05:26 -0600472 /// \note Because of our implementation, keys are limited to 2^30 -1 chars.
Billy Donahue483eba82019-07-14 18:41:48 -0400473 /// Exceeding that will cause an exception.
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000474 Value& operator[](const char* key);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000475 /// Access an object value by name, returns null if there is no member with
476 /// that name.
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000477 const Value& operator[](const char* key) const;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000478 /// Access an object value by name, create a null member if it does not exist.
Christopher Dunn25342ba2015-03-02 18:05:26 -0600479 /// \param key may contain embedded nulls.
Billy Donahue1c2ed7a2019-01-17 16:35:29 -0500480 Value& operator[](const String& key);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000481 /// Access an object value by name, returns null if there is no member with
482 /// that name.
Christopher Dunn25342ba2015-03-02 18:05:26 -0600483 /// \param key may contain embedded nulls.
Billy Donahue1c2ed7a2019-01-17 16:35:29 -0500484 const Value& operator[](const String& key) const;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000485 /** \brief Access an object value by name, create a null member if it does not
Billy Donahue483eba82019-07-14 18:41:48 -0400486 * exist.
487 *
Billy Donahueb5e1fe82018-05-20 16:55:27 -0400488 * If the object has no entry for that name, then the member name used to
Billy Donahue483eba82019-07-14 18:41:48 -0400489 * store the new entry is not duplicated.
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000490 * Example of use:
Billy Donahue483eba82019-07-14 18:41:48 -0400491 * \code
492 * Json::Value object;
493 * static const StaticString code("code");
494 * object[code] = 1234;
495 * \endcode
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000496 */
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000497 Value& operator[](const StaticString& key);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000498 /// Return the member named key if it exist, defaultValue otherwise.
Christopher Dunn0fd28752015-03-05 16:38:43 -0600499 /// \note deep copy
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000500 Value get(const char* key, const Value& defaultValue) const;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000501 /// Return the member named key if it exist, defaultValue otherwise.
Christopher Dunn0fd28752015-03-05 16:38:43 -0600502 /// \note deep copy
Christopher Dunn89704032015-07-11 12:09:59 -0500503 /// \note key may contain embedded nulls.
Jordan Baylesf34bf242019-10-11 11:19:00 -0700504 Value get(const char* begin, const char* end,
505 const Value& defaultValue) const;
Christopher Dunn25342ba2015-03-02 18:05:26 -0600506 /// Return the member named key if it exist, defaultValue otherwise.
Christopher Dunn0fd28752015-03-05 16:38:43 -0600507 /// \note deep copy
Christopher Dunn25342ba2015-03-02 18:05:26 -0600508 /// \param key may contain embedded nulls.
Billy Donahue1c2ed7a2019-01-17 16:35:29 -0500509 Value get(const String& key, const Value& defaultValue) const;
Christopher Dunn25342ba2015-03-02 18:05:26 -0600510 /// Most general and efficient version of isMember()const, get()const,
511 /// and operator[]const
Christopher Dunn89704032015-07-11 12:09:59 -0500512 /// \note As stated elsewhere, behavior is undefined if (end-begin) >= 2^30
513 Value const* find(char const* begin, char const* end) const;
Christopher Dunnc28610f2015-02-21 11:44:16 -0600514 /// Most general and efficient version of object-mutators.
Christopher Dunn89704032015-07-11 12:09:59 -0500515 /// \note As stated elsewhere, behavior is undefined if (end-begin) >= 2^30
Christopher Dunnc28610f2015-02-21 11:44:16 -0600516 /// \return non-zero, but JSON_ASSERT if this is neither object nor nullValue.
Frank Richterd76fe562019-03-23 14:31:06 +0100517 Value* demand(char const* begin, char const* end);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000518 /// \brief Remove and return the named member.
519 ///
520 /// Do nothing if it did not exist.
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000521 /// \pre type() is objectValue or nullValue
522 /// \post type() is unchanged
Wolfram Röslera06b3902017-10-18 07:19:27 +0200523 void removeMember(const char* key);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000524 /// Same as removeMember(const char*)
Christopher Dunn25342ba2015-03-02 18:05:26 -0600525 /// \param key may contain embedded nulls.
Billy Donahue1c2ed7a2019-01-17 16:35:29 -0500526 void removeMember(const String& key);
Christopher Dunn89704032015-07-11 12:09:59 -0500527 /// Same as removeMember(const char* begin, const char* end, Value* removed),
Christopher Dunn25342ba2015-03-02 18:05:26 -0600528 /// but 'key' is null-terminated.
529 bool removeMember(const char* key, Value* removed);
Christopher Dunn76746b02015-01-21 16:01:30 -0600530 /** \brief Remove the named map member.
Billy Donahue483eba82019-07-14 18:41:48 -0400531 *
532 * Update 'removed' iff removed.
533 * \param key may contain embedded nulls.
534 * \return true iff removed (no exceptions)
535 */
Billy Donahue1c2ed7a2019-01-17 16:35:29 -0500536 bool removeMember(String const& key, Value* removed);
537 /// Same as removeMember(String const& key, Value* removed)
Christopher Dunn89704032015-07-11 12:09:59 -0500538 bool removeMember(const char* begin, const char* end, Value* removed);
Christopher Dunn9de2c2d2015-01-20 16:15:40 -0600539 /** \brief Remove the indexed array element.
Billy Donahue483eba82019-07-14 18:41:48 -0400540 *
541 * O(n) expensive operations.
542 * Update 'removed' iff removed.
543 * \return true if removed (no exceptions)
544 */
Marian Klymovfc201342018-06-02 20:15:26 +0300545 bool removeIndex(ArrayIndex index, Value* removed);
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000546
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000547 /// Return true if the object has a member named key.
Christopher Dunnc28610f2015-02-21 11:44:16 -0600548 /// \note 'key' must be null-terminated.
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000549 bool isMember(const char* key) const;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000550 /// Return true if the object has a member named key.
Christopher Dunn25342ba2015-03-02 18:05:26 -0600551 /// \param key may contain embedded nulls.
Billy Donahue1c2ed7a2019-01-17 16:35:29 -0500552 bool isMember(const String& key) const;
553 /// Same as isMember(String const& key)const
Christopher Dunn89704032015-07-11 12:09:59 -0500554 bool isMember(const char* begin, const char* end) const;
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
Christopher Dunn1e3149a2015-01-25 14:16:13 -0600563 /// \deprecated Always pass len.
Jordan Baylesf34bf242019-10-11 11:19:00 -0700564 JSONCPP_DEPRECATED("Use setComment(String const&) instead.")
565 void setComment(const char* comment, CommentPlacement placement) {
Billy Donahue433107f2019-01-20 21:53:01 -0500566 setComment(String(comment, strlen(comment)), placement);
567 }
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000568 /// Comments must be //... or /* ... */
Billy Donahue433107f2019-01-20 21:53:01 -0500569 void setComment(const char* comment, size_t len, CommentPlacement placement) {
570 setComment(String(comment, len), placement);
571 }
Christopher Dunn1e3149a2015-01-25 14:16:13 -0600572 /// Comments must be //... or /* ... */
Billy Donahue433107f2019-01-20 21:53:01 -0500573 void setComment(String comment, CommentPlacement placement);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000574 bool hasComment(CommentPlacement placement) const;
575 /// Include delimiters and embedded newlines.
Billy Donahue1c2ed7a2019-01-17 16:35:29 -0500576 String getComment(CommentPlacement placement) const;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000577
Billy Donahue1c2ed7a2019-01-17 16:35:29 -0500578 String toStyledString() const;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000579
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000580 const_iterator begin() const;
581 const_iterator end() const;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000582
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000583 iterator begin();
584 iterator end();
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000585
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000586 // Accessors for the [start, limit) range of bytes within the JSON text from
587 // which this value was parsed, if any.
Christopher Dunnd4513fc2016-02-06 09:25:20 -0600588 void setOffsetStart(ptrdiff_t start);
589 void setOffsetLimit(ptrdiff_t limit);
590 ptrdiff_t getOffsetStart() const;
591 ptrdiff_t getOffsetLimit() const;
Aaron Jacobs68db6552014-04-23 23:41:12 +0000592
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000593private:
Jordan Baylesd5bd1a72019-06-24 14:06:45 -0700594 void setType(ValueType v) {
595 bits_.value_type_ = static_cast<unsigned char>(v);
596 }
Billy Donahue0c1cc6e2019-01-20 23:59:16 -0500597 bool isAllocated() const { return bits_.allocated_; }
598 void setIsAllocated(bool v) { bits_.allocated_ = v; }
599
Billy Donahue8eb5d892014-11-10 01:35:42 -0500600 void initBasic(ValueType type, bool allocated = false);
Andrey Okoshkin9b569c82018-01-12 15:59:20 +0300601 void dupPayload(const Value& other);
Andrey Okoshkinc69148c2018-01-12 11:26:34 +0300602 void releasePayload();
Andrey Okoshkin9b569c82018-01-12 15:59:20 +0300603 void dupMeta(const Value& other);
Billy Donahue8eb5d892014-11-10 01:35:42 -0500604
Christopher Dunnc28610f2015-02-21 11:44:16 -0600605 Value& resolveReference(const char* key);
606 Value& resolveReference(const char* key, const char* end);
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000607
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000608 // struct MemberNamesTransform
609 //{
610 // typedef const char *result_type;
611 // const char *operator()( const CZString &name ) const
612 // {
613 // return name.c_str();
614 // }
615 //};
616
617 union ValueHolder {
618 LargestInt int_;
619 LargestUInt uint_;
620 double real_;
621 bool bool_;
Billy Donahue0c1cc6e2019-01-20 23:59:16 -0500622 char* string_; // if allocated_, ptr to { unsigned, char[] }.
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000623 ObjectValues* map_;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000624 } value_;
Billy Donahue0c1cc6e2019-01-20 23:59:16 -0500625
626 struct {
627 // Really a ValueType, but types should agree for bitfield packing.
628 unsigned int value_type_ : 8;
629 // Unless allocated_, string_ must be null-terminated.
630 unsigned int allocated_ : 1;
631 } bits_;
632
Billy Donahue433107f2019-01-20 21:53:01 -0500633 class Comments {
634 public:
635 Comments() = default;
636 Comments(const Comments& that);
Billy Donahue00558b32019-01-21 16:42:25 -0500637 Comments(Comments&& that);
Billy Donahue433107f2019-01-20 21:53:01 -0500638 Comments& operator=(const Comments& that);
Billy Donahue00558b32019-01-21 16:42:25 -0500639 Comments& operator=(Comments&& that);
Billy Donahue433107f2019-01-20 21:53:01 -0500640 bool has(CommentPlacement slot) const;
641 String get(CommentPlacement slot) const;
Rosen Penevbcad4e42019-10-15 00:27:23 -0700642 void set(CommentPlacement slot, String comment);
Billy Donahue433107f2019-01-20 21:53:01 -0500643
644 private:
645 using Array = std::array<String, numberOfCommentPlacement>;
646 std::unique_ptr<Array> ptr_;
647 };
648 Comments comments_;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000649
650 // [start, limit) byte offsets in the source JSON text from which this Value
651 // was extracted.
Christopher Dunnd4513fc2016-02-06 09:25:20 -0600652 ptrdiff_t start_;
653 ptrdiff_t limit_;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000654};
655
Billy Donahue53c8e2c2019-11-11 22:43:52 -0500656template <> inline bool Value::as<bool>() const { return asBool(); }
657template <> inline bool Value::is<bool>() const { return isBool(); }
658
659template <> inline Int Value::as<Int>() const { return asInt(); }
660template <> inline bool Value::is<Int>() const { return isInt(); }
661
662template <> inline UInt Value::as<UInt>() const { return asUInt(); }
663template <> inline bool Value::is<UInt>() const { return isUInt(); }
664
665#if defined(JSON_HAS_INT64)
666template <> inline Int64 Value::as<Int64>() const { return asInt64(); }
667template <> inline bool Value::is<Int64>() const { return isInt64(); }
668
669template <> inline UInt64 Value::as<UInt64>() const { return asUInt64(); }
670template <> inline bool Value::is<UInt64>() const { return isUInt64(); }
671#endif
672
673template <> inline double Value::as<double>() const { return asDouble(); }
674template <> inline bool Value::is<double>() const { return isDouble(); }
675
676template <> inline String Value::as<String>() const { return asString(); }
677template <> inline bool Value::is<String>() const { return isString(); }
678
679/// These `as` specializations are type conversions, and do not have a
680/// corresponding `is`.
681template <> inline float Value::as<float>() const { return asFloat(); }
682template <> inline const char* Value::as<const char*>() const {
683 return asCString();
684}
Billy Donahue53c8e2c2019-11-11 22:43:52 -0500685
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000686/** \brief Experimental and untested: represents an element of the "path" to
687 * access a node.
688 */
689class JSON_API PathArgument {
690public:
691 friend class Path;
692
693 PathArgument();
694 PathArgument(ArrayIndex index);
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000695 PathArgument(const char* key);
Rosen Penevbcad4e42019-10-15 00:27:23 -0700696 PathArgument(String key);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000697
698private:
Billy Donahueb5e1fe82018-05-20 16:55:27 -0400699 enum Kind { kindNone = 0, kindIndex, kindKey };
Billy Donahue1c2ed7a2019-01-17 16:35:29 -0500700 String key_;
Hans Johnsone817e4f2019-01-14 17:09:22 -0600701 ArrayIndex index_{};
Billy Donahue2b593a92019-01-18 03:46:57 -0500702 Kind kind_{kindNone};
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000703};
704
705/** \brief Experimental and untested: represents a "path" to access a node.
706 *
707 * Syntax:
708 * - "." => root node
709 * - ".[n]" => elements at index 'n' of root node (an array value)
710 * - ".name" => member named 'name' of root node (an object value)
711 * - ".name1.name2.name3"
712 * - ".[0][1][2].name1[3]"
713 * - ".%" => member name is provided as parameter
aliha472adb62019-08-26 15:37:05 -0400714 * - ".[%]" => index is provided as parameter
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000715 */
716class JSON_API Path {
717public:
Jordan Baylesf34bf242019-10-11 11:19:00 -0700718 Path(const String& path, const PathArgument& a1 = PathArgument(),
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000719 const PathArgument& a2 = PathArgument(),
720 const PathArgument& a3 = PathArgument(),
721 const PathArgument& a4 = PathArgument(),
722 const PathArgument& a5 = PathArgument());
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000723
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000724 const Value& resolve(const Value& root) const;
725 Value resolve(const Value& root, const Value& defaultValue) const;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000726 /// Creates the "path" to access the specified node and returns a reference on
727 /// the node.
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000728 Value& make(Value& root) const;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000729
730private:
Chen2983f5a2019-12-04 09:08:45 +0800731 using InArgs = std::vector<const PathArgument*>;
732 using Args = std::vector<PathArgument>;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000733
Billy Donahue1c2ed7a2019-01-17 16:35:29 -0500734 void makePath(const String& path, const InArgs& in);
Jordan Baylesf34bf242019-10-11 11:19:00 -0700735 void addPathInArg(const String& path, const InArgs& in,
736 InArgs::const_iterator& itInArg, PathArgument::Kind kind);
Billy Donahue1c2ed7a2019-01-17 16:35:29 -0500737 static void invalidPath(const String& path, int location);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000738
739 Args args_;
740};
741
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000742/** \brief base class for Value iterators.
743 *
744 */
745class JSON_API ValueIteratorBase {
746public:
Chen2983f5a2019-12-04 09:08:45 +0800747 using iterator_category = std::bidirectional_iterator_tag;
748 using size_t = unsigned int;
749 using difference_type = int;
750 using SelfType = ValueIteratorBase;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000751
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000752 bool operator==(const SelfType& other) const { return isEqual(other); }
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000753
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000754 bool operator!=(const SelfType& other) const { return !isEqual(other); }
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000755
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000756 difference_type operator-(const SelfType& other) const {
Kevin Grant4c5832a2015-02-14 20:53:35 -0800757 return other.computeDistance(*this);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000758 }
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000759
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000760 /// Return either the index or the member name of the referenced value as a
761 /// Value.
762 Value key() const;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000763
Billy Donahueb5e1fe82018-05-20 16:55:27 -0400764 /// Return the index of the referenced Value, or -1 if it is not an
765 /// arrayValue.
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000766 UInt index() const;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000767
Christopher Dunned495ed2015-03-08 14:01:28 -0500768 /// Return the member name of the referenced Value, or "" if it is not an
769 /// objectValue.
770 /// \note Avoid `c_str()` on result, as embedded zeroes are possible.
Billy Donahue1c2ed7a2019-01-17 16:35:29 -0500771 String name() const;
Christopher Dunned495ed2015-03-08 14:01:28 -0500772
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000773 /// Return the member name of the referenced Value. "" if it is not an
774 /// objectValue.
Billy Donahueb5e1fe82018-05-20 16:55:27 -0400775 /// \deprecated This cannot be used for UTF-8 strings, since there can be
776 /// embedded nulls.
Jordan Baylesf34bf242019-10-11 11:19:00 -0700777 JSONCPP_DEPRECATED("Use `key = name();` instead.")
778 char const* memberName() const;
Christopher Dunnc28610f2015-02-21 11:44:16 -0600779 /// Return the member name of the referenced Value, or NULL if it is not an
780 /// objectValue.
Christopher Dunned495ed2015-03-08 14:01:28 -0500781 /// \note Better version than memberName(). Allows embedded nulls.
Christopher Dunnc28610f2015-02-21 11:44:16 -0600782 char const* memberName(char const** end) const;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000783
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000784protected:
Hans Johnsonb0826932019-10-17 12:52:13 -0500785 /*! Internal utility functions to assist with implementing
786 * other iterator functions. The const and non-const versions
787 * of the "deref" protected methods expose the protected
788 * current_ member variable in a way that can often be
789 * optimized away by the compiler.
790 */
791 const Value& deref() const;
792 Value& deref();
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000793
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000794 void increment();
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000795
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000796 void decrement();
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000797
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000798 difference_type computeDistance(const SelfType& other) const;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000799
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000800 bool isEqual(const SelfType& other) const;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000801
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000802 void copy(const SelfType& other);
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000803
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000804private:
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000805 Value::ObjectValues::iterator current_;
806 // Indicates that iterator is for a null value.
Billy Donahue2b593a92019-01-18 03:46:57 -0500807 bool isNull_{true};
Christopher Dunn2a10f4a2015-04-28 04:55:12 +0100808
809public:
810 // For some reason, BORLAND needs these at the end, rather
811 // than earlier. No idea why.
812 ValueIteratorBase();
813 explicit ValueIteratorBase(const Value::ObjectValues::iterator& current);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000814};
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000815
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000816/** \brief const iterator for object and array value.
817 *
818 */
819class JSON_API ValueConstIterator : public ValueIteratorBase {
820 friend class Value;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000821
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000822public:
Chen2983f5a2019-12-04 09:08:45 +0800823 using value_type = const Value;
Billy Donahueb5e1fe82018-05-20 16:55:27 -0400824 // typedef unsigned int size_t;
825 // typedef int difference_type;
Chen2983f5a2019-12-04 09:08:45 +0800826 using reference = const Value&;
827 using pointer = const Value*;
828 using SelfType = ValueConstIterator;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000829
830 ValueConstIterator();
ycqiuc8a8cfc2015-10-06 16:46:19 +0800831 ValueConstIterator(ValueIterator const& other);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000832
833private:
Billy Donahueb5e1fe82018-05-20 16:55:27 -0400834 /*! \internal Use by Value to create an iterator.
835 */
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000836 explicit ValueConstIterator(const Value::ObjectValues::iterator& current);
Billy Donahueb5e1fe82018-05-20 16:55:27 -0400837
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000838public:
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000839 SelfType& operator=(const ValueIteratorBase& other);
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000840
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000841 SelfType operator++(int) {
842 SelfType temp(*this);
843 ++*this;
844 return temp;
845 }
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000846
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000847 SelfType operator--(int) {
848 SelfType temp(*this);
849 --*this;
850 return temp;
851 }
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000852
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000853 SelfType& operator--() {
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000854 decrement();
855 return *this;
856 }
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000857
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000858 SelfType& operator++() {
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000859 increment();
860 return *this;
861 }
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000862
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000863 reference operator*() const { return deref(); }
Braden McDorman540db3b2014-09-14 02:31:23 -0500864
865 pointer operator->() const { return &deref(); }
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000866};
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000867
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000868/** \brief Iterator for object and array value.
869 */
870class JSON_API ValueIterator : public ValueIteratorBase {
871 friend class Value;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000872
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000873public:
Chen2983f5a2019-12-04 09:08:45 +0800874 using value_type = Value;
875 using size_t = unsigned int;
876 using difference_type = int;
877 using reference = Value&;
878 using pointer = Value*;
879 using SelfType = ValueIterator;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000880
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000881 ValueIterator();
ycqiuc8a8cfc2015-10-06 16:46:19 +0800882 explicit ValueIterator(const ValueConstIterator& other);
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000883 ValueIterator(const ValueIterator& other);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000884
885private:
Billy Donahueb5e1fe82018-05-20 16:55:27 -0400886 /*! \internal Use by Value to create an iterator.
887 */
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000888 explicit ValueIterator(const Value::ObjectValues::iterator& current);
Billy Donahueb5e1fe82018-05-20 16:55:27 -0400889
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000890public:
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000891 SelfType& operator=(const SelfType& other);
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000892
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000893 SelfType operator++(int) {
894 SelfType temp(*this);
895 ++*this;
896 return temp;
897 }
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000898
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000899 SelfType operator--(int) {
900 SelfType temp(*this);
901 --*this;
902 return temp;
903 }
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000904
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000905 SelfType& operator--() {
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000906 decrement();
907 return *this;
908 }
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000909
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000910 SelfType& operator++() {
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000911 increment();
912 return *this;
913 }
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000914
Hans Johnsonb0826932019-10-17 12:52:13 -0500915 /*! The return value of non-const iterators can be
916 * changed, so the these functions are not const
917 * because the returned references/pointers can be used
918 * to change state of the base class.
919 */
920 reference operator*() { return deref(); }
921 pointer operator->() { return &deref(); }
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000922};
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000923
Billy Donahue1d956282018-03-06 12:51:58 -0500924inline void swap(Value& a, Value& b) { a.swap(b); }
925
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000926} // namespace Json
927
Sergiy80d6e666f2016-12-03 22:29:14 +0200928#pragma pack(pop)
datadiode9454e682015-01-20 15:25:04 -0600929
Baptiste Lepilleureafd7022013-05-08 20:21:11 +0000930#if defined(JSONCPP_DISABLE_DLL_INTERFACE_WARNING)
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000931#pragma warning(pop)
Baptiste Lepilleureafd7022013-05-08 20:21:11 +0000932#endif // if defined(JSONCPP_DISABLE_DLL_INTERFACE_WARNING)
933
Jordan Bayles9704ced2019-11-14 09:38:11 -0800934#endif // JSON_H_INCLUDED