blob: 57ecb13f58217d8d74136fc61d1f0e35b0092a87 [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)
Riccardo Corsieab8ebe2021-01-04 15:10:05 +010053#pragma warning(disable : 4251 4275)
Baptiste Lepilleureafd7022013-05-08 20:21:11 +000054#endif // if defined(JSONCPP_DISABLE_DLL_INTERFACE_WARNING)
55
Jessica Clarke42e892d2022-01-12 21:27:16 +000056#pragma pack(push)
57#pragma pack()
Sergiy80d6e666f2016-12-03 22:29:14 +020058
Christopher Dunn6d135cb2007-06-13 15:51:04 +000059/** \brief JSON (JavaScript Object Notation).
60 */
61namespace Json {
62
Jordan Bayles83cc9212019-06-06 13:41:47 -070063#if JSON_USE_EXCEPTION
Christopher Dunn75279cc2015-03-08 12:20:06 -050064/** Base class for all exceptions we throw.
Christopher Dunn4e30c4f2015-03-08 12:56:32 -050065 *
66 * We use nothing but these internally. Of course, STL can throw others.
Christopher Dunn75279cc2015-03-08 12:20:06 -050067 */
Christopher Dunn949babd2015-07-23 00:19:12 -050068class JSON_API Exception : public std::exception {
69public:
Billy Donahue1c2ed7a2019-01-17 16:35:29 -050070 Exception(String msg);
Rosen Penev90ca6942020-04-11 22:26:04 -070071 ~Exception() noexcept override;
72 char const* what() const noexcept override;
Billy Donahueb5e1fe82018-05-20 16:55:27 -040073
Christopher Dunn949babd2015-07-23 00:19:12 -050074protected:
Billy Donahue1c2ed7a2019-01-17 16:35:29 -050075 String msg_;
Christopher Dunn949babd2015-07-23 00:19:12 -050076};
77
Christopher Dunn53837942015-03-08 12:31:00 -050078/** Exceptions which the user cannot easily avoid.
79 *
Christopher Dunn4e30c4f2015-03-08 12:56:32 -050080 * E.g. out-of-memory (when we use malloc), stack-overflow, malicious input
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 RuntimeError : public Exception {
85public:
Billy Donahue1c2ed7a2019-01-17 16:35:29 -050086 RuntimeError(String const& msg);
Christopher Dunn949babd2015-07-23 00:19:12 -050087};
88
Christopher Dunn4e30c4f2015-03-08 12:56:32 -050089/** Exceptions thrown by JSON_ASSERT/JSON_FAIL macros.
Christopher Dunn53837942015-03-08 12:31:00 -050090 *
91 * These are precondition-violations (user bugs) and internal errors (our bugs).
Dhruv Paranjape8996c372017-07-08 17:27:07 +053092 *
Christopher Dunn4e30c4f2015-03-08 12:56:32 -050093 * \remark derived from Json::Exception
Christopher Dunn53837942015-03-08 12:31:00 -050094 */
Christopher Dunn949babd2015-07-23 00:19:12 -050095class JSON_API LogicError : public Exception {
96public:
Billy Donahue1c2ed7a2019-01-17 16:35:29 -050097 LogicError(String const& msg);
Christopher Dunn949babd2015-07-23 00:19:12 -050098};
Jordan Bayles83cc9212019-06-06 13:41:47 -070099#endif
Christopher Dunn53837942015-03-08 12:31:00 -0500100
Christopher Dunn4e30c4f2015-03-08 12:56:32 -0500101/// used internally
es1x2ee3b1d2019-10-11 21:33:36 +0300102JSONCPP_NORETURN void throwRuntimeError(String const& msg);
Christopher Dunn4e30c4f2015-03-08 12:56:32 -0500103/// used internally
es1x2ee3b1d2019-10-11 21:33:36 +0300104JSONCPP_NORETURN void throwLogicError(String const& msg);
Christopher Dunn75279cc2015-03-08 12:20:06 -0500105
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000106/** \brief Type of the value held by a Value object.
107 */
108enum ValueType {
109 nullValue = 0, ///< 'null' value
110 intValue, ///< signed integer value
111 uintValue, ///< unsigned integer value
112 realValue, ///< double value
113 stringValue, ///< UTF-8 string value
114 booleanValue, ///< bool value
115 arrayValue, ///< array value (ordered list)
116 objectValue ///< object value (collection of name/value pairs).
117};
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000118
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000119enum CommentPlacement {
120 commentBefore = 0, ///< a comment placed on the line before a value
121 commentAfterOnSameLine, ///< a comment just after a value on the same line
122 commentAfter, ///< a comment on the line after a value (only make sense for
Aaron Jacobs3a0c4fc2014-07-01 09:20:48 +1000123 /// root value)
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000124 numberOfCommentPlacement
125};
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000126
Mike Ra07fc532018-03-13 23:35:31 +0300127/** \brief Type of precision for formatting of real values.
128 */
129enum PrecisionType {
130 significantDigits = 0, ///< we set max number of significant digits in string
131 decimalPlaces ///< we set max number of digits after "." in string
132};
133
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000134/** \brief Lightweight wrapper to tag static string.
135 *
Josh Sorefe6a588a2017-12-03 11:54:29 -0500136 * Value constructor and objectValue member assignment takes advantage of the
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000137 * StaticString and avoid the cost of string duplication when storing the
138 * string or the member name.
139 *
140 * Example of usage:
141 * \code
142 * Json::Value aValue( StaticString("some text") );
143 * Json::Value object;
144 * static const StaticString code("code");
145 * object[code] = 1234;
146 * \endcode
147 */
148class JSON_API StaticString {
149public:
Christopher Dunnff617522015-03-06 10:31:46 -0600150 explicit StaticString(const char* czstring) : c_str_(czstring) {}
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000151
Christopher Dunnff617522015-03-06 10:31:46 -0600152 operator const char*() const { return c_str_; }
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000153
Christopher Dunnff617522015-03-06 10:31:46 -0600154 const char* c_str() const { return c_str_; }
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000155
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000156private:
Christopher Dunnff617522015-03-06 10:31:46 -0600157 const char* c_str_;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000158};
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000159
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000160/** \brief Represents a <a HREF="http://www.json.org">JSON</a> value.
161 *
162 * This class is a discriminated union wrapper that can represents a:
163 * - signed integer [range: Value::minInt - Value::maxInt]
164 * - unsigned integer (range: 0 - Value::maxUInt)
165 * - double
166 * - UTF-8 string
167 * - boolean
168 * - 'null'
169 * - an ordered list of Value
170 * - collection of name/value pairs (javascript object)
171 *
172 * The type of the held value is represented by a #ValueType and
173 * can be obtained using type().
174 *
Christopher Dunnc28610f2015-02-21 11:44:16 -0600175 * Values of an #objectValue or #arrayValue can be accessed using operator[]()
176 * methods.
177 * Non-const methods will automatically create the a #nullValue element
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000178 * if it does not exist.
Christopher Dunnc28610f2015-02-21 11:44:16 -0600179 * The sequence of an #arrayValue will be automatically resized and initialized
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000180 * with #nullValue. resize() can be used to enlarge or truncate an #arrayValue.
181 *
Christopher Dunnc28610f2015-02-21 11:44:16 -0600182 * The get() methods can be used to obtain default value in the case the
183 * required element does not exist.
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000184 *
Mathias L. Baumann08ddeed2018-12-12 17:59:43 +0100185 * It is possible to iterate over the list of member keys of an object using
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000186 * the getMemberNames() method.
Christopher Dunn25342ba2015-03-02 18:05:26 -0600187 *
Christopher Dunnc28610f2015-02-21 11:44:16 -0600188 * \note #Value string-length fit in size_t, but keys must be < 2^30.
189 * (The reason is an implementation detail.) A #CharReader will raise an
Christopher Dunn25342ba2015-03-02 18:05:26 -0600190 * exception if a bound is exceeded to avoid security holes in your app,
191 * but the Value API does *not* check bounds. That is the responsibility
192 * of the caller.
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000193 */
194class JSON_API Value {
195 friend class ValueIteratorBase;
Billy Donahueb5e1fe82018-05-20 16:55:27 -0400196
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000197public:
Chen2983f5a2019-12-04 09:08:45 +0800198 using Members = std::vector<String>;
199 using iterator = ValueIterator;
200 using const_iterator = ValueConstIterator;
201 using UInt = Json::UInt;
202 using Int = Json::Int;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000203#if defined(JSON_HAS_INT64)
Chen2983f5a2019-12-04 09:08:45 +0800204 using UInt64 = Json::UInt64;
205 using Int64 = Json::Int64;
Baptiste Lepilleur842e9ac2010-12-27 17:45:23 +0000206#endif // defined(JSON_HAS_INT64)
Chen2983f5a2019-12-04 09:08:45 +0800207 using LargestInt = Json::LargestInt;
208 using LargestUInt = Json::LargestUInt;
209 using ArrayIndex = Json::ArrayIndex;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000210
Wolfram Röslerff6b4492017-09-14 09:31:36 +0200211 // Required for boost integration, e. g. BOOST_TEST
Chen2983f5a2019-12-04 09:08:45 +0800212 using value_type = std::string;
Wolfram Röslerff6b4492017-09-14 09:31:36 +0200213
Jordan Bayles25c57812019-07-11 14:27:29 -0700214#if JSON_USE_NULLREF
215 // Binary compatibility kludges, do not use.
216 static const Value& null;
217 static const Value& nullRef;
218#endif
219
220 // null and nullRef are deprecated, use this instead.
221 static Value const& nullSingleton();
Christopher Dunn0f288ae2016-06-26 18:47:43 -0500222
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000223 /// Minimum signed integer value that can be stored in a Json::Value.
Jordan Bayles81ae1d52019-09-16 12:37:14 -0700224 static constexpr LargestInt minLargestInt =
225 LargestInt(~(LargestUInt(-1) / 2));
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000226 /// Maximum signed integer value that can be stored in a Json::Value.
dota177ef0f9f2019-09-17 01:33:47 +0800227 static constexpr LargestInt maxLargestInt = LargestInt(LargestUInt(-1) / 2);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000228 /// Maximum unsigned integer value that can be stored in a Json::Value.
dota177ef0f9f2019-09-17 01:33:47 +0800229 static constexpr LargestUInt maxLargestUInt = LargestUInt(-1);
Baptiste Lepilleur842e9ac2010-12-27 17:45:23 +0000230
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000231 /// Minimum signed int value that can be stored in a Json::Value.
dota177ef0f9f2019-09-17 01:33:47 +0800232 static constexpr Int minInt = Int(~(UInt(-1) / 2));
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000233 /// Maximum signed int value that can be stored in a Json::Value.
dota177ef0f9f2019-09-17 01:33:47 +0800234 static constexpr Int maxInt = Int(UInt(-1) / 2);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000235 /// Maximum unsigned int value that can be stored in a Json::Value.
dota177ef0f9f2019-09-17 01:33:47 +0800236 static constexpr UInt maxUInt = UInt(-1);
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000237
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000238#if defined(JSON_HAS_INT64)
239 /// Minimum signed 64 bits int value that can be stored in a Json::Value.
dota177ef0f9f2019-09-17 01:33:47 +0800240 static constexpr Int64 minInt64 = Int64(~(UInt64(-1) / 2));
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000241 /// Maximum signed 64 bits int value that can be stored in a Json::Value.
dota177ef0f9f2019-09-17 01:33:47 +0800242 static constexpr Int64 maxInt64 = Int64(UInt64(-1) / 2);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000243 /// Maximum unsigned 64 bits int value that can be stored in a Json::Value.
dota177ef0f9f2019-09-17 01:33:47 +0800244 static constexpr UInt64 maxUInt64 = UInt64(-1);
Aaron Jacobsf1053e72011-05-24 03:18:02 +0000245#endif // defined(JSON_HAS_INT64)
Mike Ra07fc532018-03-13 23:35:31 +0300246 /// Default precision for real value for string representation.
dota177ef0f9f2019-09-17 01:33:47 +0800247 static constexpr UInt defaultRealPrecision = 17;
248 // The constant is hard-coded because some compiler have trouble
249 // converting Value::maxUInt64 to a double correctly (AIX/xlC).
250 // Assumes that UInt64 is a 64 bits integer.
251 static constexpr double maxUInt64AsDouble = 18446744073709551615.0;
Darcy Beurle798f6ba2017-12-22 22:48:20 +0100252// Workaround for bug in the NVIDIAs CUDA 9.1 nvcc compiler
253// when using gcc and clang backend compilers. CZString
254// cannot be defined as private. See issue #486
255#ifdef __NVCC__
256public:
257#else
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000258private:
Darcy Beurle798f6ba2017-12-22 22:48:20 +0100259#endif
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000260#ifndef JSONCPP_DOC_EXCLUDE_IMPLEMENTATION
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000261 class CZString {
262 public:
Billy Donahueb5e1fe82018-05-20 16:55:27 -0400263 enum DuplicationPolicy { noDuplication = 0, duplicate, duplicateOnCopy };
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000264 CZString(ArrayIndex index);
Christopher Dunnc28610f2015-02-21 11:44:16 -0600265 CZString(char const* str, unsigned length, DuplicationPolicy allocate);
266 CZString(CZString const& other);
Marcel Opprechtceae0e32020-11-06 22:22:26 +0100267 CZString(CZString&& other) noexcept;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000268 ~CZString();
Dhruv Paranjape0ba8bd72017-07-08 17:47:13 +0530269 CZString& operator=(const CZString& other);
Marcel Opprechtceae0e32020-11-06 22:22:26 +0100270 CZString& operator=(CZString&& other) noexcept;
Dhruv Paranjape0ba8bd72017-07-08 17:47:13 +0530271
Christopher Dunnc28610f2015-02-21 11:44:16 -0600272 bool operator<(CZString const& other) const;
273 bool operator==(CZString const& other) const;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000274 ArrayIndex index() const;
Billy Donahueb5e1fe82018-05-20 16:55:27 -0400275 // const char* c_str() const; ///< \deprecated
Christopher Dunnc28610f2015-02-21 11:44:16 -0600276 char const* data() const;
277 unsigned length() const;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000278 bool isStaticString() const;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000279
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000280 private:
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000281 void swap(CZString& other);
Christopher Dunnc28610f2015-02-21 11:44:16 -0600282
Christopher Dunn57ad0512015-03-02 12:10:35 -0600283 struct StringStorage {
Billy Donahueb5e1fe82018-05-20 16:55:27 -0400284 unsigned policy_ : 2;
285 unsigned length_ : 30; // 1GB max
Christopher Dunn57ad0512015-03-02 12:10:35 -0600286 };
287
Billy Donahueb5e1fe82018-05-20 16:55:27 -0400288 char const* cstr_; // actually, a prefixed string, unless policy is noDup
Christopher Dunn57ad0512015-03-02 12:10:35 -0600289 union {
290 ArrayIndex index_;
291 StringStorage storage_;
292 };
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000293 };
294
295public:
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000296 typedef std::map<CZString, Value> ObjectValues;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000297#endif // ifndef JSONCPP_DOC_EXCLUDE_IMPLEMENTATION
298
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000299public:
Billy Donahue483eba82019-07-14 18:41:48 -0400300 /**
301 * \brief Create a default Value of the given type.
302 *
303 * This is a very useful constructor.
304 * To create an empty array, pass arrayValue.
305 * To create an empty object, pass objectValue.
306 * Another Value can then be set to this one by assignment.
307 * This is useful since clear() and resize() will not alter types.
308 *
309 * Examples:
310 * \code
311 * Json::Value null_value; // null
312 * Json::Value arr_value(Json::arrayValue); // []
313 * Json::Value obj_value(Json::objectValue); // {}
314 * \endcode
315 */
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000316 Value(ValueType type = nullValue);
317 Value(Int value);
318 Value(UInt value);
Baptiste Lepilleur842e9ac2010-12-27 17:45:23 +0000319#if defined(JSON_HAS_INT64)
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000320 Value(Int64 value);
321 Value(UInt64 value);
Baptiste Lepilleur842e9ac2010-12-27 17:45:23 +0000322#endif // if defined(JSON_HAS_INT64)
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000323 Value(double value);
Christopher Dunn8a702972015-03-03 10:38:27 -0600324 Value(const char* value); ///< Copy til first 0. (NULL causes to seg-fault.)
Christopher Dunn89704032015-07-11 12:09:59 -0500325 Value(const char* begin, const char* end); ///< Copy all, incl zeroes.
Billy Donahue483eba82019-07-14 18:41:48 -0400326 /**
327 * \brief Constructs a value from a static string.
328 *
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000329 * Like other value string constructor but do not duplicate the string for
Billy Donahue483eba82019-07-14 18:41:48 -0400330 * internal storage. The given string must remain alive after the call to
331 * this constructor.
332 *
Christopher Dunnc28610f2015-02-21 11:44:16 -0600333 * \note This works only for null-terminated strings. (We cannot change the
Billy Donahue483eba82019-07-14 18:41:48 -0400334 * size of this class, so we have nowhere to store the length, which might be
335 * computed later for various operations.)
Christopher Dunnc28610f2015-02-21 11:44:16 -0600336 *
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000337 * Example of usage:
Billy Donahue483eba82019-07-14 18:41:48 -0400338 * \code
339 * static StaticString foo("some text");
340 * Json::Value aValue(foo);
341 * \endcode
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000342 */
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000343 Value(const StaticString& value);
Billy Donahue483eba82019-07-14 18:41:48 -0400344 Value(const String& value);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000345 Value(bool value);
nathanruizc8453d32020-06-24 07:52:28 +1000346 Value(std::nullptr_t ptr) = delete;
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000347 Value(const Value& other);
Marcel Opprechtceae0e32020-11-06 22:22:26 +0100348 Value(Value&& other) noexcept;
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);
Marcel Opprechtceae0e32020-11-06 22:22:26 +0100354 Value& operator=(Value&& other) noexcept;
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 Int asInt() const;
388 UInt asUInt() const;
Aaron Jacobsf1053e72011-05-24 03:18:02 +0000389#if defined(JSON_HAS_INT64)
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000390 Int64 asInt64() const;
391 UInt64 asUInt64() const;
Aaron Jacobsf1053e72011-05-24 03:18:02 +0000392#endif // if defined(JSON_HAS_INT64)
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000393 LargestInt asLargestInt() const;
394 LargestUInt asLargestUInt() const;
395 float asFloat() const;
396 double asDouble() const;
397 bool asBool() const;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000398
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000399 bool isNull() const;
400 bool isBool() const;
401 bool isInt() const;
402 bool isInt64() const;
403 bool isUInt() const;
404 bool isUInt64() const;
405 bool isIntegral() const;
406 bool isDouble() const;
407 bool isNumeric() const;
408 bool isString() const;
409 bool isArray() const;
410 bool isObject() const;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000411
Billy Donahue53c8e2c2019-11-11 22:43:52 -0500412 /// The `as<T>` and `is<T>` member function templates and specializations.
David Gobbidc180eb2020-02-13 14:22:49 -0700413 template <typename T> T as() const JSONCPP_TEMPLATE_DELETE;
414 template <typename T> bool is() const JSONCPP_TEMPLATE_DELETE;
Billy Donahue53c8e2c2019-11-11 22:43:52 -0500415
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000416 bool isConvertibleTo(ValueType other) const;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000417
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000418 /// Number of values in array or object
419 ArrayIndex size() const;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000420
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000421 /// \brief Return true if empty array, empty object, or null;
422 /// otherwise, false.
423 bool empty() const;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000424
Wolfram Rösler90794222017-12-05 18:18:55 +0100425 /// Return !isNull()
Rosen Penev90ca6942020-04-11 22:26:04 -0700426 explicit operator bool() const;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000427
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000428 /// Remove all object members and array elements.
429 /// \pre type() is arrayValue, objectValue, or nullValue
430 /// \post type() is unchanged
431 void clear();
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000432
Marian Klymovfc201342018-06-02 20:15:26 +0300433 /// Resize the array to newSize elements.
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000434 /// New elements are initialized to null.
435 /// May only be called on nullValue or arrayValue.
436 /// \pre type() is arrayValue or nullValue
437 /// \post type() is arrayValue
Marian Klymovfc201342018-06-02 20:15:26 +0300438 void resize(ArrayIndex newSize);
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000439
Billy Donahue483eba82019-07-14 18:41:48 -0400440 //@{
441 /// Access an array element (zero based index). If the array contains less
442 /// than index element, then null value are inserted in the array so that
443 /// its size is index+1.
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000444 /// (You may need to say 'value[0u]' to get your compiler to distinguish
Billy Donahue483eba82019-07-14 18:41:48 -0400445 /// this from the operator[] which takes a string.)
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000446 Value& operator[](ArrayIndex index);
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000447 Value& operator[](int index);
Billy Donahue483eba82019-07-14 18:41:48 -0400448 //@}
Baptiste Lepilleurfa130ef2010-12-24 12:47:14 +0000449
Billy Donahue483eba82019-07-14 18:41:48 -0400450 //@{
451 /// Access an array element (zero based index).
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000452 /// (You may need to say 'value[0u]' to get your compiler to distinguish
Billy Donahue483eba82019-07-14 18:41:48 -0400453 /// this from the operator[] which takes a string.)
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000454 const Value& operator[](ArrayIndex index) const;
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000455 const Value& operator[](int index) const;
Billy Donahue483eba82019-07-14 18:41:48 -0400456 //@}
Baptiste Lepilleurfa130ef2010-12-24 12:47:14 +0000457
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000458 /// If the array contains at least index+1 elements, returns the element
Billy Donahue483eba82019-07-14 18:41:48 -0400459 /// value, otherwise returns defaultValue.
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000460 Value get(ArrayIndex index, const Value& defaultValue) const;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000461 /// Return true if index < size().
462 bool isValidIndex(ArrayIndex index) const;
463 /// \brief Append value to array at the end.
464 ///
465 /// Equivalent to jsonvalue[jsonvalue.size()] = value;
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000466 Value& append(const Value& value);
Dhruv Paranjape23c44d92017-07-08 17:30:47 +0530467 Value& append(Value&& value);
Chena0bd9ad2019-12-03 09:13:42 +0800468
dota17bdacfd72019-10-16 14:33:53 +0800469 /// \brief Insert value in array at specific index
Chena0bd9ad2019-12-03 09:13:42 +0800470 bool insert(ArrayIndex index, const Value& newValue);
471 bool insert(ArrayIndex index, Value&& newValue);
Dhruv Paranjape23c44d92017-07-08 17:30:47 +0530472
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000473 /// Access an object value by name, create a null member if it does not exist.
Christopher Dunn25342ba2015-03-02 18:05:26 -0600474 /// \note Because of our implementation, keys are limited to 2^30 -1 chars.
Billy Donahue483eba82019-07-14 18:41:48 -0400475 /// Exceeding that will cause an exception.
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000476 Value& operator[](const char* key);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000477 /// Access an object value by name, returns null if there is no member with
478 /// that name.
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000479 const Value& operator[](const char* key) const;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000480 /// Access an object value by name, create a null member if it does not exist.
Christopher Dunn25342ba2015-03-02 18:05:26 -0600481 /// \param key may contain embedded nulls.
Billy Donahue1c2ed7a2019-01-17 16:35:29 -0500482 Value& operator[](const String& key);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000483 /// Access an object value by name, returns null if there is no member with
484 /// that name.
Christopher Dunn25342ba2015-03-02 18:05:26 -0600485 /// \param key may contain embedded nulls.
Billy Donahue1c2ed7a2019-01-17 16:35:29 -0500486 const Value& operator[](const String& key) const;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000487 /** \brief Access an object value by name, create a null member if it does not
Billy Donahue483eba82019-07-14 18:41:48 -0400488 * exist.
489 *
Billy Donahueb5e1fe82018-05-20 16:55:27 -0400490 * If the object has no entry for that name, then the member name used to
Billy Donahue483eba82019-07-14 18:41:48 -0400491 * store the new entry is not duplicated.
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000492 * Example of use:
Billy Donahue483eba82019-07-14 18:41:48 -0400493 * \code
494 * Json::Value object;
495 * static const StaticString code("code");
496 * object[code] = 1234;
497 * \endcode
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000498 */
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000499 Value& operator[](const StaticString& key);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000500 /// Return the member named key if it exist, defaultValue otherwise.
Christopher Dunn0fd28752015-03-05 16:38:43 -0600501 /// \note deep copy
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000502 Value get(const char* key, const Value& defaultValue) const;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000503 /// Return the member named key if it exist, defaultValue otherwise.
Christopher Dunn0fd28752015-03-05 16:38:43 -0600504 /// \note deep copy
Christopher Dunn89704032015-07-11 12:09:59 -0500505 /// \note key may contain embedded nulls.
Jordan Baylesf34bf242019-10-11 11:19:00 -0700506 Value get(const char* begin, const char* end,
507 const Value& defaultValue) const;
Christopher Dunn25342ba2015-03-02 18:05:26 -0600508 /// Return the member named key if it exist, defaultValue otherwise.
Christopher Dunn0fd28752015-03-05 16:38:43 -0600509 /// \note deep copy
Christopher Dunn25342ba2015-03-02 18:05:26 -0600510 /// \param key may contain embedded nulls.
Billy Donahue1c2ed7a2019-01-17 16:35:29 -0500511 Value get(const String& key, const Value& defaultValue) const;
Christopher Dunn25342ba2015-03-02 18:05:26 -0600512 /// Most general and efficient version of isMember()const, get()const,
513 /// and operator[]const
Christopher Dunn89704032015-07-11 12:09:59 -0500514 /// \note As stated elsewhere, behavior is undefined if (end-begin) >= 2^30
515 Value const* find(char const* begin, char const* end) const;
Christopher Dunnc28610f2015-02-21 11:44:16 -0600516 /// Most general and efficient version of object-mutators.
Christopher Dunn89704032015-07-11 12:09:59 -0500517 /// \note As stated elsewhere, behavior is undefined if (end-begin) >= 2^30
Christopher Dunnc28610f2015-02-21 11:44:16 -0600518 /// \return non-zero, but JSON_ASSERT if this is neither object nor nullValue.
Frank Richterd76fe562019-03-23 14:31:06 +0100519 Value* demand(char const* begin, char const* end);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000520 /// \brief Remove and return the named member.
521 ///
522 /// Do nothing if it did not exist.
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000523 /// \pre type() is objectValue or nullValue
524 /// \post type() is unchanged
Wolfram Röslera06b3902017-10-18 07:19:27 +0200525 void removeMember(const char* key);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000526 /// Same as removeMember(const char*)
Christopher Dunn25342ba2015-03-02 18:05:26 -0600527 /// \param key may contain embedded nulls.
Billy Donahue1c2ed7a2019-01-17 16:35:29 -0500528 void removeMember(const String& key);
Christopher Dunn89704032015-07-11 12:09:59 -0500529 /// Same as removeMember(const char* begin, const char* end, Value* removed),
Christopher Dunn25342ba2015-03-02 18:05:26 -0600530 /// but 'key' is null-terminated.
531 bool removeMember(const char* key, Value* removed);
Christopher Dunn76746b02015-01-21 16:01:30 -0600532 /** \brief Remove the named map member.
Billy Donahue483eba82019-07-14 18:41:48 -0400533 *
534 * Update 'removed' iff removed.
535 * \param key may contain embedded nulls.
536 * \return true iff removed (no exceptions)
537 */
Billy Donahue1c2ed7a2019-01-17 16:35:29 -0500538 bool removeMember(String const& key, Value* removed);
539 /// Same as removeMember(String const& key, Value* removed)
Christopher Dunn89704032015-07-11 12:09:59 -0500540 bool removeMember(const char* begin, const char* end, Value* removed);
Christopher Dunn9de2c2d2015-01-20 16:15:40 -0600541 /** \brief Remove the indexed array element.
Billy Donahue483eba82019-07-14 18:41:48 -0400542 *
543 * O(n) expensive operations.
544 * Update 'removed' iff removed.
545 * \return true if removed (no exceptions)
546 */
Marian Klymovfc201342018-06-02 20:15:26 +0300547 bool removeIndex(ArrayIndex index, Value* removed);
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000548
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000549 /// Return true if the object has a member named key.
Christopher Dunnc28610f2015-02-21 11:44:16 -0600550 /// \note 'key' must be null-terminated.
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000551 bool isMember(const char* key) const;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000552 /// Return true if the object has a member named key.
Christopher Dunn25342ba2015-03-02 18:05:26 -0600553 /// \param key may contain embedded nulls.
Billy Donahue1c2ed7a2019-01-17 16:35:29 -0500554 bool isMember(const String& key) const;
555 /// Same as isMember(String const& key)const
Christopher Dunn89704032015-07-11 12:09:59 -0500556 bool isMember(const char* begin, const char* end) const;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000557
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000558 /// \brief Return a list of the member names.
559 ///
560 /// If null, return an empty list.
561 /// \pre type() is objectValue or nullValue
562 /// \post if type() was nullValue, it remains nullValue
563 Members getMemberNames() const;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000564
Christopher Dunn1e3149a2015-01-25 14:16:13 -0600565 /// \deprecated Always pass len.
Jordan Baylesf34bf242019-10-11 11:19:00 -0700566 JSONCPP_DEPRECATED("Use setComment(String const&) instead.")
567 void setComment(const char* comment, CommentPlacement placement) {
Billy Donahue433107f2019-01-20 21:53:01 -0500568 setComment(String(comment, strlen(comment)), placement);
569 }
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000570 /// Comments must be //... or /* ... */
Billy Donahue433107f2019-01-20 21:53:01 -0500571 void setComment(const char* comment, size_t len, CommentPlacement placement) {
572 setComment(String(comment, len), placement);
573 }
Christopher Dunn1e3149a2015-01-25 14:16:13 -0600574 /// Comments must be //... or /* ... */
Billy Donahue433107f2019-01-20 21:53:01 -0500575 void setComment(String comment, CommentPlacement placement);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000576 bool hasComment(CommentPlacement placement) const;
577 /// Include delimiters and embedded newlines.
Billy Donahue1c2ed7a2019-01-17 16:35:29 -0500578 String getComment(CommentPlacement placement) const;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000579
Billy Donahue1c2ed7a2019-01-17 16:35:29 -0500580 String toStyledString() const;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000581
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000582 const_iterator begin() const;
583 const_iterator end() const;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000584
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000585 iterator begin();
586 iterator end();
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000587
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000588 // Accessors for the [start, limit) range of bytes within the JSON text from
589 // which this value was parsed, if any.
Christopher Dunnd4513fc2016-02-06 09:25:20 -0600590 void setOffsetStart(ptrdiff_t start);
591 void setOffsetLimit(ptrdiff_t limit);
592 ptrdiff_t getOffsetStart() const;
593 ptrdiff_t getOffsetLimit() const;
Aaron Jacobs68db6552014-04-23 23:41:12 +0000594
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000595private:
Jordan Baylesd5bd1a72019-06-24 14:06:45 -0700596 void setType(ValueType v) {
597 bits_.value_type_ = static_cast<unsigned char>(v);
598 }
Billy Donahue0c1cc6e2019-01-20 23:59:16 -0500599 bool isAllocated() const { return bits_.allocated_; }
600 void setIsAllocated(bool v) { bits_.allocated_ = v; }
601
Billy Donahue8eb5d892014-11-10 01:35:42 -0500602 void initBasic(ValueType type, bool allocated = false);
Andrey Okoshkin9b569c82018-01-12 15:59:20 +0300603 void dupPayload(const Value& other);
Andrey Okoshkinc69148c2018-01-12 11:26:34 +0300604 void releasePayload();
Andrey Okoshkin9b569c82018-01-12 15:59:20 +0300605 void dupMeta(const Value& other);
Billy Donahue8eb5d892014-11-10 01:35:42 -0500606
Christopher Dunnc28610f2015-02-21 11:44:16 -0600607 Value& resolveReference(const char* key);
608 Value& resolveReference(const char* key, const char* end);
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000609
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000610 // struct MemberNamesTransform
611 //{
612 // typedef const char *result_type;
613 // const char *operator()( const CZString &name ) const
614 // {
615 // return name.c_str();
616 // }
617 //};
618
619 union ValueHolder {
620 LargestInt int_;
621 LargestUInt uint_;
622 double real_;
623 bool bool_;
Billy Donahue0c1cc6e2019-01-20 23:59:16 -0500624 char* string_; // if allocated_, ptr to { unsigned, char[] }.
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000625 ObjectValues* map_;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000626 } value_;
Billy Donahue0c1cc6e2019-01-20 23:59:16 -0500627
628 struct {
629 // Really a ValueType, but types should agree for bitfield packing.
630 unsigned int value_type_ : 8;
631 // Unless allocated_, string_ must be null-terminated.
632 unsigned int allocated_ : 1;
633 } bits_;
634
Billy Donahue433107f2019-01-20 21:53:01 -0500635 class Comments {
636 public:
637 Comments() = default;
638 Comments(const Comments& that);
Marcel Opprechtceae0e32020-11-06 22:22:26 +0100639 Comments(Comments&& that) noexcept;
Billy Donahue433107f2019-01-20 21:53:01 -0500640 Comments& operator=(const Comments& that);
Marcel Opprechtceae0e32020-11-06 22:22:26 +0100641 Comments& operator=(Comments&& that) noexcept;
Billy Donahue433107f2019-01-20 21:53:01 -0500642 bool has(CommentPlacement slot) const;
643 String get(CommentPlacement slot) const;
Rosen Penevbcad4e42019-10-15 00:27:23 -0700644 void set(CommentPlacement slot, String comment);
Billy Donahue433107f2019-01-20 21:53:01 -0500645
646 private:
647 using Array = std::array<String, numberOfCommentPlacement>;
648 std::unique_ptr<Array> ptr_;
649 };
650 Comments comments_;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000651
652 // [start, limit) byte offsets in the source JSON text from which this Value
653 // was extracted.
Christopher Dunnd4513fc2016-02-06 09:25:20 -0600654 ptrdiff_t start_;
655 ptrdiff_t limit_;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000656};
657
Billy Donahue53c8e2c2019-11-11 22:43:52 -0500658template <> inline bool Value::as<bool>() const { return asBool(); }
659template <> inline bool Value::is<bool>() const { return isBool(); }
660
661template <> inline Int Value::as<Int>() const { return asInt(); }
662template <> inline bool Value::is<Int>() const { return isInt(); }
663
664template <> inline UInt Value::as<UInt>() const { return asUInt(); }
665template <> inline bool Value::is<UInt>() const { return isUInt(); }
666
667#if defined(JSON_HAS_INT64)
668template <> inline Int64 Value::as<Int64>() const { return asInt64(); }
669template <> inline bool Value::is<Int64>() const { return isInt64(); }
670
671template <> inline UInt64 Value::as<UInt64>() const { return asUInt64(); }
672template <> inline bool Value::is<UInt64>() const { return isUInt64(); }
673#endif
674
675template <> inline double Value::as<double>() const { return asDouble(); }
676template <> inline bool Value::is<double>() const { return isDouble(); }
677
678template <> inline String Value::as<String>() const { return asString(); }
679template <> inline bool Value::is<String>() const { return isString(); }
680
681/// These `as` specializations are type conversions, and do not have a
682/// corresponding `is`.
683template <> inline float Value::as<float>() const { return asFloat(); }
684template <> inline const char* Value::as<const char*>() const {
685 return asCString();
686}
Billy Donahue53c8e2c2019-11-11 22:43:52 -0500687
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000688/** \brief Experimental and untested: represents an element of the "path" to
689 * access a node.
690 */
691class JSON_API PathArgument {
692public:
693 friend class Path;
694
695 PathArgument();
696 PathArgument(ArrayIndex index);
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000697 PathArgument(const char* key);
Rosen Penevbcad4e42019-10-15 00:27:23 -0700698 PathArgument(String key);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000699
700private:
Billy Donahueb5e1fe82018-05-20 16:55:27 -0400701 enum Kind { kindNone = 0, kindIndex, kindKey };
Billy Donahue1c2ed7a2019-01-17 16:35:29 -0500702 String key_;
Hans Johnsone817e4f2019-01-14 17:09:22 -0600703 ArrayIndex index_{};
Billy Donahue2b593a92019-01-18 03:46:57 -0500704 Kind kind_{kindNone};
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000705};
706
707/** \brief Experimental and untested: represents a "path" to access a node.
708 *
709 * Syntax:
710 * - "." => root node
711 * - ".[n]" => elements at index 'n' of root node (an array value)
712 * - ".name" => member named 'name' of root node (an object value)
713 * - ".name1.name2.name3"
714 * - ".[0][1][2].name1[3]"
715 * - ".%" => member name is provided as parameter
aliha472adb62019-08-26 15:37:05 -0400716 * - ".[%]" => index is provided as parameter
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000717 */
718class JSON_API Path {
719public:
Jordan Baylesf34bf242019-10-11 11:19:00 -0700720 Path(const String& path, const PathArgument& a1 = PathArgument(),
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000721 const PathArgument& a2 = PathArgument(),
722 const PathArgument& a3 = PathArgument(),
723 const PathArgument& a4 = PathArgument(),
724 const PathArgument& a5 = PathArgument());
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000725
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000726 const Value& resolve(const Value& root) const;
727 Value resolve(const Value& root, const Value& defaultValue) const;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000728 /// Creates the "path" to access the specified node and returns a reference on
729 /// the node.
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000730 Value& make(Value& root) const;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000731
732private:
Chen2983f5a2019-12-04 09:08:45 +0800733 using InArgs = std::vector<const PathArgument*>;
734 using Args = std::vector<PathArgument>;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000735
Billy Donahue1c2ed7a2019-01-17 16:35:29 -0500736 void makePath(const String& path, const InArgs& in);
Jordan Baylesf34bf242019-10-11 11:19:00 -0700737 void addPathInArg(const String& path, const InArgs& in,
738 InArgs::const_iterator& itInArg, PathArgument::Kind kind);
Billy Donahue1c2ed7a2019-01-17 16:35:29 -0500739 static void invalidPath(const String& path, int location);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000740
741 Args args_;
742};
743
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000744/** \brief base class for Value iterators.
745 *
746 */
747class JSON_API ValueIteratorBase {
748public:
Chen2983f5a2019-12-04 09:08:45 +0800749 using iterator_category = std::bidirectional_iterator_tag;
750 using size_t = unsigned int;
751 using difference_type = int;
752 using SelfType = ValueIteratorBase;
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 bool operator!=(const SelfType& other) const { return !isEqual(other); }
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000757
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000758 difference_type operator-(const SelfType& other) const {
Kevin Grant4c5832a2015-02-14 20:53:35 -0800759 return other.computeDistance(*this);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000760 }
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000761
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000762 /// Return either the index or the member name of the referenced value as a
763 /// Value.
764 Value key() const;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000765
Billy Donahueb5e1fe82018-05-20 16:55:27 -0400766 /// Return the index of the referenced Value, or -1 if it is not an
767 /// arrayValue.
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000768 UInt index() const;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000769
Christopher Dunned495ed2015-03-08 14:01:28 -0500770 /// Return the member name of the referenced Value, or "" if it is not an
771 /// objectValue.
772 /// \note Avoid `c_str()` on result, as embedded zeroes are possible.
Billy Donahue1c2ed7a2019-01-17 16:35:29 -0500773 String name() const;
Christopher Dunned495ed2015-03-08 14:01:28 -0500774
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000775 /// Return the member name of the referenced Value. "" if it is not an
776 /// objectValue.
Billy Donahueb5e1fe82018-05-20 16:55:27 -0400777 /// \deprecated This cannot be used for UTF-8 strings, since there can be
778 /// embedded nulls.
Jordan Baylesf34bf242019-10-11 11:19:00 -0700779 JSONCPP_DEPRECATED("Use `key = name();` instead.")
780 char const* memberName() const;
Christopher Dunnc28610f2015-02-21 11:44:16 -0600781 /// Return the member name of the referenced Value, or NULL if it is not an
782 /// objectValue.
Christopher Dunned495ed2015-03-08 14:01:28 -0500783 /// \note Better version than memberName(). Allows embedded nulls.
Christopher Dunnc28610f2015-02-21 11:44:16 -0600784 char const* memberName(char const** end) const;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000785
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000786protected:
Hans Johnsonb0826932019-10-17 12:52:13 -0500787 /*! Internal utility functions to assist with implementing
788 * other iterator functions. The const and non-const versions
789 * of the "deref" protected methods expose the protected
790 * current_ member variable in a way that can often be
791 * optimized away by the compiler.
792 */
793 const Value& deref() const;
794 Value& deref();
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000795
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000796 void increment();
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000797
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000798 void decrement();
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000799
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000800 difference_type computeDistance(const SelfType& other) const;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000801
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000802 bool isEqual(const SelfType& other) const;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000803
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000804 void copy(const SelfType& other);
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000805
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000806private:
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000807 Value::ObjectValues::iterator current_;
808 // Indicates that iterator is for a null value.
Billy Donahue2b593a92019-01-18 03:46:57 -0500809 bool isNull_{true};
Christopher Dunn2a10f4a2015-04-28 04:55:12 +0100810
811public:
812 // For some reason, BORLAND needs these at the end, rather
813 // than earlier. No idea why.
814 ValueIteratorBase();
815 explicit ValueIteratorBase(const Value::ObjectValues::iterator& current);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000816};
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000817
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000818/** \brief const iterator for object and array value.
819 *
820 */
821class JSON_API ValueConstIterator : public ValueIteratorBase {
822 friend class Value;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000823
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000824public:
Chen2983f5a2019-12-04 09:08:45 +0800825 using value_type = const Value;
Billy Donahueb5e1fe82018-05-20 16:55:27 -0400826 // typedef unsigned int size_t;
827 // typedef int difference_type;
Chen2983f5a2019-12-04 09:08:45 +0800828 using reference = const Value&;
829 using pointer = const Value*;
830 using SelfType = ValueConstIterator;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000831
832 ValueConstIterator();
ycqiuc8a8cfc2015-10-06 16:46:19 +0800833 ValueConstIterator(ValueIterator const& other);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000834
835private:
Billy Donahueb5e1fe82018-05-20 16:55:27 -0400836 /*! \internal Use by Value to create an iterator.
837 */
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000838 explicit ValueConstIterator(const Value::ObjectValues::iterator& current);
Billy Donahueb5e1fe82018-05-20 16:55:27 -0400839
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000840public:
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000841 SelfType& operator=(const ValueIteratorBase& other);
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000842
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000843 SelfType operator++(int) {
844 SelfType temp(*this);
845 ++*this;
846 return temp;
847 }
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000848
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000849 SelfType operator--(int) {
850 SelfType temp(*this);
851 --*this;
852 return temp;
853 }
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000854
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000855 SelfType& operator--() {
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000856 decrement();
857 return *this;
858 }
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000859
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000860 SelfType& operator++() {
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000861 increment();
862 return *this;
863 }
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000864
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000865 reference operator*() const { return deref(); }
Braden McDorman540db3b2014-09-14 02:31:23 -0500866
867 pointer operator->() const { return &deref(); }
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000868};
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000869
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000870/** \brief Iterator for object and array value.
871 */
872class JSON_API ValueIterator : public ValueIteratorBase {
873 friend class Value;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000874
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000875public:
Chen2983f5a2019-12-04 09:08:45 +0800876 using value_type = Value;
877 using size_t = unsigned int;
878 using difference_type = int;
879 using reference = Value&;
880 using pointer = Value*;
881 using SelfType = ValueIterator;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000882
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000883 ValueIterator();
ycqiuc8a8cfc2015-10-06 16:46:19 +0800884 explicit ValueIterator(const ValueConstIterator& other);
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000885 ValueIterator(const ValueIterator& other);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000886
887private:
Billy Donahueb5e1fe82018-05-20 16:55:27 -0400888 /*! \internal Use by Value to create an iterator.
889 */
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000890 explicit ValueIterator(const Value::ObjectValues::iterator& current);
Billy Donahueb5e1fe82018-05-20 16:55:27 -0400891
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000892public:
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000893 SelfType& operator=(const SelfType& other);
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000894
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000895 SelfType operator++(int) {
896 SelfType temp(*this);
897 ++*this;
898 return temp;
899 }
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000900
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000901 SelfType operator--(int) {
902 SelfType temp(*this);
903 --*this;
904 return temp;
905 }
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000906
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000907 SelfType& operator--() {
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000908 decrement();
909 return *this;
910 }
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000911
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000912 SelfType& operator++() {
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000913 increment();
914 return *this;
915 }
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000916
Hans Johnsonb0826932019-10-17 12:52:13 -0500917 /*! The return value of non-const iterators can be
918 * changed, so the these functions are not const
919 * because the returned references/pointers can be used
920 * to change state of the base class.
921 */
Billy Donahuefe9663e2020-12-21 04:41:37 -0500922 reference operator*() const { return const_cast<reference>(deref()); }
923 pointer operator->() const { return const_cast<pointer>(&deref()); }
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000924};
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000925
Billy Donahue1d956282018-03-06 12:51:58 -0500926inline void swap(Value& a, Value& b) { a.swap(b); }
927
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000928} // namespace Json
929
Sergiy80d6e666f2016-12-03 22:29:14 +0200930#pragma pack(pop)
datadiode9454e682015-01-20 15:25:04 -0600931
Baptiste Lepilleureafd7022013-05-08 20:21:11 +0000932#if defined(JSONCPP_DISABLE_DLL_INTERFACE_WARNING)
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000933#pragma warning(pop)
Baptiste Lepilleureafd7022013-05-08 20:21:11 +0000934#endif // if defined(JSONCPP_DISABLE_DLL_INTERFACE_WARNING)
935
Jordan Bayles9704ced2019-11-14 09:38:11 -0800936#endif // JSON_H_INCLUDED