blob: 9a2d10ddcd9c284ac42271bb6b11ec2c4eed1093 [file] [log] [blame]
Devin Jeanpierre59e4d352017-07-21 03:44:36 -07001// Copyright 2007-2010 Baptiste Lepilleur and The JsonCpp Authors
Baptiste Lepilleur7469f1d2010-04-20 21:35:19 +00002// Distributed under MIT license, or public domain if desired and
3// recognized in your jurisdiction.
4// See file LICENSE for detail or copy at http://jsoncpp.sourceforge.net/LICENSE
5
Christopher Dunn6d135cb2007-06-13 15:51:04 +00006#ifndef CPPTL_JSON_H_INCLUDED
Aaron Jacobs9fa4e842014-07-01 08:48:54 +10007#define CPPTL_JSON_H_INCLUDED
Christopher Dunn6d135cb2007-06-13 15:51:04 +00008
Baptiste Lepilleureadc4782011-05-02 21:09:30 +00009#if !defined(JSON_IS_AMALGAMATION)
Aaron Jacobs9fa4e842014-07-01 08:48:54 +100010#include "forwards.h"
Baptiste Lepilleureadc4782011-05-02 21:09:30 +000011#endif // if !defined(JSON_IS_AMALGAMATION)
Billy Donahue433107f2019-01-20 21:53:01 -050012#include <array>
Billy Donahueb5e1fe82018-05-20 16:55:27 -040013#include <exception>
Billy Donahue433107f2019-01-20 21:53:01 -050014#include <memory>
Aaron Jacobs9fa4e842014-07-01 08:48:54 +100015#include <string>
16#include <vector>
Christopher Dunn6d135cb2007-06-13 15:51:04 +000017
Aaron Jacobs9fa4e842014-07-01 08:48:54 +100018#ifndef JSON_USE_CPPTL_SMALLMAP
19#include <map>
20#else
21#include <cpptl/smallmap.h>
22#endif
23#ifdef JSON_USE_CPPTL
24#include <cpptl/forwards.h>
25#endif
Christopher Dunn6d135cb2007-06-13 15:51:04 +000026
Billy Donahueb5e1fe82018-05-20 16:55:27 -040027// Conditional NORETURN attribute on the throw functions would:
Dhruv Paranjape8996c372017-07-08 17:27:07 +053028// a) suppress false positives from static code analysis
Gauravd97ea5b2016-03-16 11:15:09 +053029// b) possibly improve optimization opportunities.
30#if !defined(JSONCPP_NORETURN)
Billy Donahueb5e1fe82018-05-20 16:55:27 -040031#if defined(_MSC_VER)
32#define JSONCPP_NORETURN __declspec(noreturn)
33#elif defined(__GNUC__)
34#define JSONCPP_NORETURN __attribute__((__noreturn__))
35#else
36#define JSONCPP_NORETURN
37#endif
Gauravd97ea5b2016-03-16 11:15:09 +053038#endif
39
Aaron Jacobs9fa4e842014-07-01 08:48:54 +100040// Disable warning C4251: <data member>: <type> needs to have dll-interface to
41// be used by...
Baptiste Lepilleureafd7022013-05-08 20:21:11 +000042#if defined(JSONCPP_DISABLE_DLL_INTERFACE_WARNING)
Aaron Jacobs9fa4e842014-07-01 08:48:54 +100043#pragma warning(push)
44#pragma warning(disable : 4251)
Baptiste Lepilleureafd7022013-05-08 20:21:11 +000045#endif // if defined(JSONCPP_DISABLE_DLL_INTERFACE_WARNING)
46
Sergiy80d6e666f2016-12-03 22:29:14 +020047#pragma pack(push, 8)
48
Christopher Dunn6d135cb2007-06-13 15:51:04 +000049/** \brief JSON (JavaScript Object Notation).
50 */
51namespace Json {
52
Christopher Dunn75279cc2015-03-08 12:20:06 -050053/** Base class for all exceptions we throw.
Christopher Dunn4e30c4f2015-03-08 12:56:32 -050054 *
55 * We use nothing but these internally. Of course, STL can throw others.
Christopher Dunn75279cc2015-03-08 12:20:06 -050056 */
Christopher Dunn949babd2015-07-23 00:19:12 -050057class JSON_API Exception : public std::exception {
58public:
Billy Donahue1c2ed7a2019-01-17 16:35:29 -050059 Exception(String msg);
Hans Johnson2853b1c2019-01-11 13:58:53 -060060 ~Exception() JSONCPP_NOEXCEPT override;
61 char const* what() const JSONCPP_NOEXCEPT override;
Billy Donahueb5e1fe82018-05-20 16:55:27 -040062
Christopher Dunn949babd2015-07-23 00:19:12 -050063protected:
Billy Donahue1c2ed7a2019-01-17 16:35:29 -050064 String msg_;
Christopher Dunn949babd2015-07-23 00:19:12 -050065};
66
Christopher Dunn53837942015-03-08 12:31:00 -050067/** Exceptions which the user cannot easily avoid.
68 *
Christopher Dunn4e30c4f2015-03-08 12:56:32 -050069 * E.g. out-of-memory (when we use malloc), stack-overflow, malicious input
Dhruv Paranjape8996c372017-07-08 17:27:07 +053070 *
Christopher Dunn4e30c4f2015-03-08 12:56:32 -050071 * \remark derived from Json::Exception
Christopher Dunn53837942015-03-08 12:31:00 -050072 */
Christopher Dunn949babd2015-07-23 00:19:12 -050073class JSON_API RuntimeError : public Exception {
74public:
Billy Donahue1c2ed7a2019-01-17 16:35:29 -050075 RuntimeError(String const& msg);
Christopher Dunn949babd2015-07-23 00:19:12 -050076};
77
Christopher Dunn4e30c4f2015-03-08 12:56:32 -050078/** Exceptions thrown by JSON_ASSERT/JSON_FAIL macros.
Christopher Dunn53837942015-03-08 12:31:00 -050079 *
80 * These are precondition-violations (user bugs) and internal errors (our bugs).
Dhruv Paranjape8996c372017-07-08 17:27:07 +053081 *
Christopher Dunn4e30c4f2015-03-08 12:56:32 -050082 * \remark derived from Json::Exception
Christopher Dunn53837942015-03-08 12:31:00 -050083 */
Christopher Dunn949babd2015-07-23 00:19:12 -050084class JSON_API LogicError : public Exception {
85public:
Billy Donahue1c2ed7a2019-01-17 16:35:29 -050086 LogicError(String const& msg);
Christopher Dunn949babd2015-07-23 00:19:12 -050087};
Christopher Dunn53837942015-03-08 12:31:00 -050088
Christopher Dunn4e30c4f2015-03-08 12:56:32 -050089/// used internally
Billy Donahue1c2ed7a2019-01-17 16:35:29 -050090JSONCPP_NORETURN void throwRuntimeError(String const& msg);
Christopher Dunn4e30c4f2015-03-08 12:56:32 -050091/// used internally
Billy Donahue1c2ed7a2019-01-17 16:35:29 -050092JSONCPP_NORETURN void throwLogicError(String const& msg);
Christopher Dunn75279cc2015-03-08 12:20:06 -050093
Aaron Jacobs9fa4e842014-07-01 08:48:54 +100094/** \brief Type of the value held by a Value object.
95 */
96enum ValueType {
97 nullValue = 0, ///< 'null' value
98 intValue, ///< signed integer value
99 uintValue, ///< unsigned integer value
100 realValue, ///< double value
101 stringValue, ///< UTF-8 string value
102 booleanValue, ///< bool value
103 arrayValue, ///< array value (ordered list)
104 objectValue ///< object value (collection of name/value pairs).
105};
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000106
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000107enum CommentPlacement {
108 commentBefore = 0, ///< a comment placed on the line before a value
109 commentAfterOnSameLine, ///< a comment just after a value on the same line
110 commentAfter, ///< a comment on the line after a value (only make sense for
Aaron Jacobs3a0c4fc2014-07-01 09:20:48 +1000111 /// root value)
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000112 numberOfCommentPlacement
113};
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000114
Mike Ra07fc532018-03-13 23:35:31 +0300115/** \brief Type of precision for formatting of real values.
116 */
117enum PrecisionType {
118 significantDigits = 0, ///< we set max number of significant digits in string
119 decimalPlaces ///< we set max number of digits after "." in string
120};
121
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000122//# ifdef JSON_USE_CPPTL
123// typedef CppTL::AnyEnumerator<const char *> EnumMemberNames;
124// typedef CppTL::AnyEnumerator<const Value &> EnumValues;
125//# endif
126
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000127/** \brief Lightweight wrapper to tag static string.
128 *
Josh Sorefe6a588a2017-12-03 11:54:29 -0500129 * Value constructor and objectValue member assignment takes advantage of the
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000130 * StaticString and avoid the cost of string duplication when storing the
131 * string or the member name.
132 *
133 * Example of usage:
134 * \code
135 * Json::Value aValue( StaticString("some text") );
136 * Json::Value object;
137 * static const StaticString code("code");
138 * object[code] = 1234;
139 * \endcode
140 */
141class JSON_API StaticString {
142public:
Christopher Dunnff617522015-03-06 10:31:46 -0600143 explicit StaticString(const char* czstring) : c_str_(czstring) {}
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000144
Christopher Dunnff617522015-03-06 10:31:46 -0600145 operator const char*() const { return c_str_; }
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000146
Christopher Dunnff617522015-03-06 10:31:46 -0600147 const char* c_str() const { return c_str_; }
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000148
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000149private:
Christopher Dunnff617522015-03-06 10:31:46 -0600150 const char* c_str_;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000151};
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000152
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000153/** \brief Represents a <a HREF="http://www.json.org">JSON</a> value.
154 *
155 * This class is a discriminated union wrapper that can represents a:
156 * - signed integer [range: Value::minInt - Value::maxInt]
157 * - unsigned integer (range: 0 - Value::maxUInt)
158 * - double
159 * - UTF-8 string
160 * - boolean
161 * - 'null'
162 * - an ordered list of Value
163 * - collection of name/value pairs (javascript object)
164 *
165 * The type of the held value is represented by a #ValueType and
166 * can be obtained using type().
167 *
Christopher Dunnc28610f2015-02-21 11:44:16 -0600168 * Values of an #objectValue or #arrayValue can be accessed using operator[]()
169 * methods.
170 * Non-const methods will automatically create the a #nullValue element
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000171 * if it does not exist.
Christopher Dunnc28610f2015-02-21 11:44:16 -0600172 * The sequence of an #arrayValue will be automatically resized and initialized
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000173 * with #nullValue. resize() can be used to enlarge or truncate an #arrayValue.
174 *
Christopher Dunnc28610f2015-02-21 11:44:16 -0600175 * The get() methods can be used to obtain default value in the case the
176 * required element does not exist.
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000177 *
Mathias L. Baumann08ddeed2018-12-12 17:59:43 +0100178 * It is possible to iterate over the list of member keys of an object using
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000179 * the getMemberNames() method.
Christopher Dunn25342ba2015-03-02 18:05:26 -0600180 *
Christopher Dunnc28610f2015-02-21 11:44:16 -0600181 * \note #Value string-length fit in size_t, but keys must be < 2^30.
182 * (The reason is an implementation detail.) A #CharReader will raise an
Christopher Dunn25342ba2015-03-02 18:05:26 -0600183 * exception if a bound is exceeded to avoid security holes in your app,
184 * but the Value API does *not* check bounds. That is the responsibility
185 * of the caller.
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000186 */
187class JSON_API Value {
188 friend class ValueIteratorBase;
Billy Donahueb5e1fe82018-05-20 16:55:27 -0400189
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000190public:
Billy Donahue1c2ed7a2019-01-17 16:35:29 -0500191 typedef std::vector<String> Members;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000192 typedef ValueIterator iterator;
193 typedef ValueConstIterator const_iterator;
194 typedef Json::UInt UInt;
195 typedef Json::Int Int;
196#if defined(JSON_HAS_INT64)
197 typedef Json::UInt64 UInt64;
198 typedef Json::Int64 Int64;
Baptiste Lepilleur842e9ac2010-12-27 17:45:23 +0000199#endif // defined(JSON_HAS_INT64)
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000200 typedef Json::LargestInt LargestInt;
201 typedef Json::LargestUInt LargestUInt;
202 typedef Json::ArrayIndex ArrayIndex;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000203
Wolfram Röslerff6b4492017-09-14 09:31:36 +0200204 // Required for boost integration, e. g. BOOST_TEST
205 typedef std::string value_type;
206
Billy Donahueb5e1fe82018-05-20 16:55:27 -0400207 static const Value& null; ///< We regret this reference to a global instance;
208 ///< prefer the simpler Value().
209 static const Value& nullRef; ///< just a kludge for binary-compatibility; same
210 ///< as null
Christopher Dunn0f288ae2016-06-26 18:47:43 -0500211 static Value const& nullSingleton(); ///< Prefer this to null or nullRef.
212
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000213 /// Minimum signed integer value that can be stored in a Json::Value.
214 static const LargestInt minLargestInt;
215 /// Maximum signed integer value that can be stored in a Json::Value.
216 static const LargestInt maxLargestInt;
217 /// Maximum unsigned integer value that can be stored in a Json::Value.
218 static const LargestUInt maxLargestUInt;
Baptiste Lepilleur842e9ac2010-12-27 17:45:23 +0000219
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000220 /// Minimum signed int value that can be stored in a Json::Value.
221 static const Int minInt;
222 /// Maximum signed int value that can be stored in a Json::Value.
223 static const Int maxInt;
224 /// Maximum unsigned int value that can be stored in a Json::Value.
225 static const UInt maxUInt;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000226
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000227#if defined(JSON_HAS_INT64)
228 /// Minimum signed 64 bits int value that can be stored in a Json::Value.
229 static const Int64 minInt64;
230 /// Maximum signed 64 bits int value that can be stored in a Json::Value.
231 static const Int64 maxInt64;
232 /// Maximum unsigned 64 bits int value that can be stored in a Json::Value.
233 static const UInt64 maxUInt64;
Aaron Jacobsf1053e72011-05-24 03:18:02 +0000234#endif // defined(JSON_HAS_INT64)
Baptiste Lepilleur842e9ac2010-12-27 17:45:23 +0000235
Mike Ra07fc532018-03-13 23:35:31 +0300236 /// Default precision for real value for string representation.
237 static const UInt defaultRealPrecision;
238
Darcy Beurle798f6ba2017-12-22 22:48:20 +0100239// Workaround for bug in the NVIDIAs CUDA 9.1 nvcc compiler
240// when using gcc and clang backend compilers. CZString
241// cannot be defined as private. See issue #486
242#ifdef __NVCC__
243public:
244#else
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000245private:
Darcy Beurle798f6ba2017-12-22 22:48:20 +0100246#endif
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000247#ifndef JSONCPP_DOC_EXCLUDE_IMPLEMENTATION
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000248 class CZString {
249 public:
Billy Donahueb5e1fe82018-05-20 16:55:27 -0400250 enum DuplicationPolicy { noDuplication = 0, duplicate, duplicateOnCopy };
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000251 CZString(ArrayIndex index);
Christopher Dunnc28610f2015-02-21 11:44:16 -0600252 CZString(char const* str, unsigned length, DuplicationPolicy allocate);
253 CZString(CZString const& other);
Motti2b008912015-04-20 17:44:47 +0300254#if JSON_HAS_RVALUE_REFERENCES
255 CZString(CZString&& other);
256#endif
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000257 ~CZString();
Dhruv Paranjape0ba8bd72017-07-08 17:47:13 +0530258 CZString& operator=(const CZString& other);
259
260#if JSON_HAS_RVALUE_REFERENCES
261 CZString& operator=(CZString&& other);
262#endif
263
Christopher Dunnc28610f2015-02-21 11:44:16 -0600264 bool operator<(CZString const& other) const;
265 bool operator==(CZString const& other) const;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000266 ArrayIndex index() const;
Billy Donahueb5e1fe82018-05-20 16:55:27 -0400267 // const char* c_str() const; ///< \deprecated
Christopher Dunnc28610f2015-02-21 11:44:16 -0600268 char const* data() const;
269 unsigned length() const;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000270 bool isStaticString() const;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000271
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000272 private:
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000273 void swap(CZString& other);
Christopher Dunnc28610f2015-02-21 11:44:16 -0600274
Christopher Dunn57ad0512015-03-02 12:10:35 -0600275 struct StringStorage {
Billy Donahueb5e1fe82018-05-20 16:55:27 -0400276 unsigned policy_ : 2;
277 unsigned length_ : 30; // 1GB max
Christopher Dunn57ad0512015-03-02 12:10:35 -0600278 };
279
Billy Donahueb5e1fe82018-05-20 16:55:27 -0400280 char const* cstr_; // actually, a prefixed string, unless policy is noDup
Christopher Dunn57ad0512015-03-02 12:10:35 -0600281 union {
282 ArrayIndex index_;
283 StringStorage storage_;
284 };
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000285 };
286
287public:
288#ifndef JSON_USE_CPPTL_SMALLMAP
289 typedef std::map<CZString, Value> ObjectValues;
290#else
291 typedef CppTL::SmallMap<CZString, Value> ObjectValues;
292#endif // ifndef JSON_USE_CPPTL_SMALLMAP
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000293#endif // ifndef JSONCPP_DOC_EXCLUDE_IMPLEMENTATION
294
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000295public:
296 /** \brief Create a default Value of the given type.
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000297
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000298 This is a very useful constructor.
299 To create an empty array, pass arrayValue.
300 To create an empty object, pass objectValue.
301 Another Value can then be set to this one by assignment.
302This is useful since clear() and resize() will not alter types.
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000303
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000304 Examples:
305\code
306Json::Value null_value; // null
307Json::Value arr_value(Json::arrayValue); // []
308Json::Value obj_value(Json::objectValue); // {}
309\endcode
310 */
311 Value(ValueType type = nullValue);
312 Value(Int value);
313 Value(UInt value);
Baptiste Lepilleur842e9ac2010-12-27 17:45:23 +0000314#if defined(JSON_HAS_INT64)
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000315 Value(Int64 value);
316 Value(UInt64 value);
Baptiste Lepilleur842e9ac2010-12-27 17:45:23 +0000317#endif // if defined(JSON_HAS_INT64)
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000318 Value(double value);
Christopher Dunn8a702972015-03-03 10:38:27 -0600319 Value(const char* value); ///< Copy til first 0. (NULL causes to seg-fault.)
Christopher Dunn89704032015-07-11 12:09:59 -0500320 Value(const char* begin, const char* end); ///< Copy all, incl zeroes.
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000321 /** \brief Constructs a value from a static string.
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000322
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000323 * Like other value string constructor but do not duplicate the string for
324 * internal storage. The given string must remain alive after the call to this
325 * constructor.
Christopher Dunnc28610f2015-02-21 11:44:16 -0600326 * \note This works only for null-terminated strings. (We cannot change the
327 * size of this class, so we have nowhere to store the length,
328 * which might be computed later for various operations.)
329 *
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000330 * Example of usage:
331 * \code
Christopher Dunnc28610f2015-02-21 11:44:16 -0600332 * static StaticString foo("some text");
333 * Json::Value aValue(foo);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000334 * \endcode
335 */
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000336 Value(const StaticString& value);
Billy Donahue1c2ed7a2019-01-17 16:35:29 -0500337 Value(const String& value); ///< Copy data() til size(). Embedded
338 ///< zeroes too.
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000339#ifdef JSON_USE_CPPTL
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000340 Value(const CppTL::ConstString& value);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000341#endif
342 Value(bool value);
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000343 Value(const Value& other);
Motti2b008912015-04-20 17:44:47 +0300344 Value(Value&& other);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000345 ~Value();
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000346
Billy Donahue0c1cc6e2019-01-20 23:59:16 -0500347 /// \note Overwrite existing comments. To preserve comments, use
Billy Donahueb5e1fe82018-05-20 16:55:27 -0400348 /// #swapPayload().
Billy Donahue0c1cc6e2019-01-20 23:59:16 -0500349 Value& operator=(const Value& other);
350 Value& operator=(Value&& other);
Dhruv Paranjape8996c372017-07-08 17:27:07 +0530351
Christopher Dunn66eb72f2015-01-20 11:02:22 -0600352 /// Swap everything.
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000353 void swap(Value& other);
Christopher Dunn66eb72f2015-01-20 11:02:22 -0600354 /// Swap values but leave comments and source offsets in place.
355 void swapPayload(Value& other);
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000356
Dhruv Paranjape8996c372017-07-08 17:27:07 +0530357 /// copy everything.
358 void copy(const Value& other);
359 /// copy values but leave comments and source offsets in place.
360 void copyPayload(const Value& other);
361
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000362 ValueType type() const;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000363
Christopher Dunn66eb72f2015-01-20 11:02:22 -0600364 /// Compare payload only, not comments etc.
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000365 bool operator<(const Value& other) const;
366 bool operator<=(const Value& other) const;
367 bool operator>=(const Value& other) const;
368 bool operator>(const Value& other) const;
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000369 bool operator==(const Value& other) const;
370 bool operator!=(const Value& other) const;
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000371 int compare(const Value& other) const;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000372
Christopher Dunn8a702972015-03-03 10:38:27 -0600373 const char* asCString() const; ///< Embedded zeroes could cause you trouble!
dawescae564652016-03-14 19:11:02 -0500374#if JSONCPP_USING_SECURE_MEMORY
Billy Donahueb5e1fe82018-05-20 16:55:27 -0400375 unsigned getCStringLength() const; // Allows you to understand the length of
376 // the CString
dawescae564652016-03-14 19:11:02 -0500377#endif
Billy Donahue1c2ed7a2019-01-17 16:35:29 -0500378 String asString() const; ///< Embedded zeroes are possible.
Christopher Dunnc28610f2015-02-21 11:44:16 -0600379 /** Get raw char* of string-value.
380 * \return false if !string. (Seg-fault if str or end are NULL.)
381 */
Billy Donahueb5e1fe82018-05-20 16:55:27 -0400382 bool getString(char const** begin, char const** end) const;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000383#ifdef JSON_USE_CPPTL
384 CppTL::ConstString asConstString() const;
385#endif
386 Int asInt() const;
387 UInt asUInt() const;
Aaron Jacobsf1053e72011-05-24 03:18:02 +0000388#if defined(JSON_HAS_INT64)
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000389 Int64 asInt64() const;
390 UInt64 asUInt64() const;
Aaron Jacobsf1053e72011-05-24 03:18:02 +0000391#endif // if defined(JSON_HAS_INT64)
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000392 LargestInt asLargestInt() const;
393 LargestUInt asLargestUInt() const;
394 float asFloat() const;
395 double asDouble() const;
396 bool asBool() const;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000397
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000398 bool isNull() const;
399 bool isBool() const;
400 bool isInt() const;
401 bool isInt64() const;
402 bool isUInt() const;
403 bool isUInt64() const;
404 bool isIntegral() const;
405 bool isDouble() const;
406 bool isNumeric() const;
407 bool isString() const;
408 bool isArray() const;
409 bool isObject() const;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000410
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000411 bool isConvertibleTo(ValueType other) const;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000412
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000413 /// Number of values in array or object
414 ArrayIndex size() const;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000415
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000416 /// \brief Return true if empty array, empty object, or null;
417 /// otherwise, false.
418 bool empty() const;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000419
Wolfram Rösler90794222017-12-05 18:18:55 +0100420 /// Return !isNull()
drgler04abe382018-01-13 15:28:19 +0100421 JSONCPP_OP_EXPLICIT operator bool() const;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000422
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000423 /// Remove all object members and array elements.
424 /// \pre type() is arrayValue, objectValue, or nullValue
425 /// \post type() is unchanged
426 void clear();
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000427
Marian Klymovfc201342018-06-02 20:15:26 +0300428 /// Resize the array to newSize elements.
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000429 /// New elements are initialized to null.
430 /// May only be called on nullValue or arrayValue.
431 /// \pre type() is arrayValue or nullValue
432 /// \post type() is arrayValue
Marian Klymovfc201342018-06-02 20:15:26 +0300433 void resize(ArrayIndex newSize);
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000434
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000435 /// Access an array element (zero based index ).
436 /// If the array contains less than index element, then null value are
437 /// inserted
438 /// in the array so that its size is index+1.
439 /// (You may need to say 'value[0u]' to get your compiler to distinguish
440 /// this from the operator[] which takes a string.)
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000441 Value& operator[](ArrayIndex index);
Baptiste Lepilleurfa130ef2010-12-24 12:47:14 +0000442
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000443 /// Access an array element (zero based index ).
444 /// If the array contains less than index element, then null value are
445 /// inserted
446 /// in the array so that its size is index+1.
447 /// (You may need to say 'value[0u]' to get your compiler to distinguish
448 /// this from the operator[] which takes a string.)
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000449 Value& operator[](int index);
Baptiste Lepilleurfa130ef2010-12-24 12:47:14 +0000450
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000451 /// Access an array element (zero based index )
452 /// (You may need to say 'value[0u]' to get your compiler to distinguish
453 /// this from the operator[] which takes a string.)
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000454 const Value& operator[](ArrayIndex index) const;
Baptiste Lepilleurfa130ef2010-12-24 12:47:14 +0000455
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000456 /// Access an array element (zero based index )
457 /// (You may need to say 'value[0u]' to get your compiler to distinguish
458 /// this from the operator[] which takes a string.)
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000459 const Value& operator[](int index) const;
Baptiste Lepilleurfa130ef2010-12-24 12:47:14 +0000460
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000461 /// If the array contains at least index+1 elements, returns the element
462 /// value,
463 /// otherwise returns defaultValue.
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000464 Value get(ArrayIndex index, const Value& defaultValue) const;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000465 /// Return true if index < size().
466 bool isValidIndex(ArrayIndex index) const;
467 /// \brief Append value to array at the end.
468 ///
469 /// Equivalent to jsonvalue[jsonvalue.size()] = value;
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000470 Value& append(const Value& value);
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000471
Dhruv Paranjape0ba8bd72017-07-08 17:47:13 +0530472#if JSON_HAS_RVALUE_REFERENCES
Dhruv Paranjape23c44d92017-07-08 17:30:47 +0530473 Value& append(Value&& value);
474#endif
475
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000476 /// Access an object value by name, create a null member if it does not exist.
Christopher Dunn25342ba2015-03-02 18:05:26 -0600477 /// \note Because of our implementation, keys are limited to 2^30 -1 chars.
478 /// Exceeding that will cause an exception.
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000479 Value& operator[](const char* key);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000480 /// Access an object value by name, returns null if there is no member with
481 /// that name.
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000482 const Value& operator[](const char* key) const;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000483 /// Access an object value by name, create a null member if it does not exist.
Christopher Dunn25342ba2015-03-02 18:05:26 -0600484 /// \param key may contain embedded nulls.
Billy Donahue1c2ed7a2019-01-17 16:35:29 -0500485 Value& operator[](const String& key);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000486 /// Access an object value by name, returns null if there is no member with
487 /// that name.
Christopher Dunn25342ba2015-03-02 18:05:26 -0600488 /// \param key may contain embedded nulls.
Billy Donahue1c2ed7a2019-01-17 16:35:29 -0500489 const Value& operator[](const String& key) const;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000490 /** \brief Access an object value by name, create a null member if it does not
491 exist.
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000492
Billy Donahueb5e1fe82018-05-20 16:55:27 -0400493 * If the object has no entry for that name, then the member name used to
494 store
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000495 * the new entry is not duplicated.
496 * Example of use:
497 * \code
498 * Json::Value object;
499 * static const StaticString code("code");
500 * object[code] = 1234;
501 * \endcode
502 */
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000503 Value& operator[](const StaticString& key);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000504#ifdef JSON_USE_CPPTL
505 /// Access an object value by name, create a null member if it does not exist.
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000506 Value& operator[](const CppTL::ConstString& key);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000507 /// Access an object value by name, returns null if there is no member with
508 /// that name.
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000509 const Value& operator[](const CppTL::ConstString& key) const;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000510#endif
511 /// Return the member named key if it exist, defaultValue otherwise.
Christopher Dunn0fd28752015-03-05 16:38:43 -0600512 /// \note deep copy
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000513 Value get(const char* key, const Value& defaultValue) const;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000514 /// Return the member named key if it exist, defaultValue otherwise.
Christopher Dunn0fd28752015-03-05 16:38:43 -0600515 /// \note deep copy
Christopher Dunn89704032015-07-11 12:09:59 -0500516 /// \note key may contain embedded nulls.
Billy Donahueb5e1fe82018-05-20 16:55:27 -0400517 Value
518 get(const char* begin, const char* end, const Value& defaultValue) const;
Christopher Dunn25342ba2015-03-02 18:05:26 -0600519 /// Return the member named key if it exist, defaultValue otherwise.
Christopher Dunn0fd28752015-03-05 16:38:43 -0600520 /// \note deep copy
Christopher Dunn25342ba2015-03-02 18:05:26 -0600521 /// \param key may contain embedded nulls.
Billy Donahue1c2ed7a2019-01-17 16:35:29 -0500522 Value get(const String& key, const Value& defaultValue) const;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000523#ifdef JSON_USE_CPPTL
524 /// Return the member named key if it exist, defaultValue otherwise.
Christopher Dunn0fd28752015-03-05 16:38:43 -0600525 /// \note deep copy
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000526 Value get(const CppTL::ConstString& key, const Value& defaultValue) const;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000527#endif
Christopher Dunn25342ba2015-03-02 18:05:26 -0600528 /// Most general and efficient version of isMember()const, get()const,
529 /// and operator[]const
Christopher Dunn89704032015-07-11 12:09:59 -0500530 /// \note As stated elsewhere, behavior is undefined if (end-begin) >= 2^30
531 Value const* find(char const* begin, char const* end) const;
Christopher Dunnc28610f2015-02-21 11:44:16 -0600532 /// Most general and efficient version of object-mutators.
Christopher Dunn89704032015-07-11 12:09:59 -0500533 /// \note As stated elsewhere, behavior is undefined if (end-begin) >= 2^30
Christopher Dunnc28610f2015-02-21 11:44:16 -0600534 /// \return non-zero, but JSON_ASSERT if this is neither object nor nullValue.
Christopher Dunn89704032015-07-11 12:09:59 -0500535 Value const* demand(char const* begin, char const* end);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000536 /// \brief Remove and return the named member.
537 ///
538 /// Do nothing if it did not exist.
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000539 /// \pre type() is objectValue or nullValue
540 /// \post type() is unchanged
Wolfram Röslera06b3902017-10-18 07:19:27 +0200541 void removeMember(const char* key);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000542 /// Same as removeMember(const char*)
Christopher Dunn25342ba2015-03-02 18:05:26 -0600543 /// \param key may contain embedded nulls.
Billy Donahue1c2ed7a2019-01-17 16:35:29 -0500544 void removeMember(const String& key);
Christopher Dunn89704032015-07-11 12:09:59 -0500545 /// Same as removeMember(const char* begin, const char* end, Value* removed),
Christopher Dunn25342ba2015-03-02 18:05:26 -0600546 /// but 'key' is null-terminated.
547 bool removeMember(const char* key, Value* removed);
Christopher Dunn76746b02015-01-21 16:01:30 -0600548 /** \brief Remove the named map member.
549
550 Update 'removed' iff removed.
Christopher Dunn25342ba2015-03-02 18:05:26 -0600551 \param key may contain embedded nulls.
Christopher Dunn76746b02015-01-21 16:01:30 -0600552 \return true iff removed (no exceptions)
553 */
Billy Donahue1c2ed7a2019-01-17 16:35:29 -0500554 bool removeMember(String const& key, Value* removed);
555 /// Same as removeMember(String const& key, Value* removed)
Christopher Dunn89704032015-07-11 12:09:59 -0500556 bool removeMember(const char* begin, const char* end, Value* removed);
Christopher Dunn9de2c2d2015-01-20 16:15:40 -0600557 /** \brief Remove the indexed array element.
558
559 O(n) expensive operations.
YantaoZhaoe32ee472018-07-03 21:29:18 +0800560 Update 'removed' iff removed.
Marian Klymovfc201342018-06-02 20:15:26 +0300561 \return true if removed (no exceptions)
Christopher Dunn9de2c2d2015-01-20 16:15:40 -0600562 */
Marian Klymovfc201342018-06-02 20:15:26 +0300563 bool removeIndex(ArrayIndex index, Value* removed);
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000564
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000565 /// Return true if the object has a member named key.
Christopher Dunnc28610f2015-02-21 11:44:16 -0600566 /// \note 'key' must be null-terminated.
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000567 bool isMember(const char* key) const;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000568 /// Return true if the object has a member named key.
Christopher Dunn25342ba2015-03-02 18:05:26 -0600569 /// \param key may contain embedded nulls.
Billy Donahue1c2ed7a2019-01-17 16:35:29 -0500570 bool isMember(const String& key) const;
571 /// Same as isMember(String const& key)const
Christopher Dunn89704032015-07-11 12:09:59 -0500572 bool isMember(const char* begin, const char* end) const;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000573#ifdef JSON_USE_CPPTL
574 /// Return true if the object has a member named key.
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000575 bool isMember(const CppTL::ConstString& key) const;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000576#endif
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000577
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000578 /// \brief Return a list of the member names.
579 ///
580 /// If null, return an empty list.
581 /// \pre type() is objectValue or nullValue
582 /// \post if type() was nullValue, it remains nullValue
583 Members getMemberNames() const;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000584
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000585 //# ifdef JSON_USE_CPPTL
586 // EnumMemberNames enumMemberNames() const;
587 // EnumValues enumValues() const;
588 //# endif
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000589
Christopher Dunn1e3149a2015-01-25 14:16:13 -0600590 /// \deprecated Always pass len.
Billy Donahue1c2ed7a2019-01-17 16:35:29 -0500591 JSONCPP_DEPRECATED("Use setComment(String const&) instead.")
Billy Donahue433107f2019-01-20 21:53:01 -0500592 void setComment(const char* comment, CommentPlacement placement) {
593 setComment(String(comment, strlen(comment)), placement);
594 }
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000595 /// Comments must be //... or /* ... */
Billy Donahue433107f2019-01-20 21:53:01 -0500596 void setComment(const char* comment, size_t len, CommentPlacement placement) {
597 setComment(String(comment, len), placement);
598 }
Christopher Dunn1e3149a2015-01-25 14:16:13 -0600599 /// Comments must be //... or /* ... */
Billy Donahue433107f2019-01-20 21:53:01 -0500600 void setComment(String comment, CommentPlacement placement);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000601 bool hasComment(CommentPlacement placement) const;
602 /// Include delimiters and embedded newlines.
Billy Donahue1c2ed7a2019-01-17 16:35:29 -0500603 String getComment(CommentPlacement placement) const;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000604
Billy Donahue1c2ed7a2019-01-17 16:35:29 -0500605 String toStyledString() const;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000606
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000607 const_iterator begin() const;
608 const_iterator end() const;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000609
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000610 iterator begin();
611 iterator end();
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000612
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000613 // Accessors for the [start, limit) range of bytes within the JSON text from
614 // which this value was parsed, if any.
Christopher Dunnd4513fc2016-02-06 09:25:20 -0600615 void setOffsetStart(ptrdiff_t start);
616 void setOffsetLimit(ptrdiff_t limit);
617 ptrdiff_t getOffsetStart() const;
618 ptrdiff_t getOffsetLimit() const;
Aaron Jacobs68db6552014-04-23 23:41:12 +0000619
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000620private:
Billy Donahue0c1cc6e2019-01-20 23:59:16 -0500621 void setType(ValueType v) { bits_.value_type_ = v; }
622 bool isAllocated() const { return bits_.allocated_; }
623 void setIsAllocated(bool v) { bits_.allocated_ = v; }
624
Billy Donahue8eb5d892014-11-10 01:35:42 -0500625 void initBasic(ValueType type, bool allocated = false);
Andrey Okoshkin9b569c82018-01-12 15:59:20 +0300626 void dupPayload(const Value& other);
Andrey Okoshkinc69148c2018-01-12 11:26:34 +0300627 void releasePayload();
Andrey Okoshkin9b569c82018-01-12 15:59:20 +0300628 void dupMeta(const Value& other);
Billy Donahue8eb5d892014-11-10 01:35:42 -0500629
Christopher Dunnc28610f2015-02-21 11:44:16 -0600630 Value& resolveReference(const char* key);
631 Value& resolveReference(const char* key, const char* end);
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000632
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000633 // struct MemberNamesTransform
634 //{
635 // typedef const char *result_type;
636 // const char *operator()( const CZString &name ) const
637 // {
638 // return name.c_str();
639 // }
640 //};
641
642 union ValueHolder {
643 LargestInt int_;
644 LargestUInt uint_;
645 double real_;
646 bool bool_;
Billy Donahue0c1cc6e2019-01-20 23:59:16 -0500647 char* string_; // if allocated_, ptr to { unsigned, char[] }.
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000648 ObjectValues* map_;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000649 } value_;
Billy Donahue0c1cc6e2019-01-20 23:59:16 -0500650
651 struct {
652 // Really a ValueType, but types should agree for bitfield packing.
653 unsigned int value_type_ : 8;
654 // Unless allocated_, string_ must be null-terminated.
655 unsigned int allocated_ : 1;
656 } bits_;
657
Billy Donahue433107f2019-01-20 21:53:01 -0500658 class Comments {
659 public:
660 Comments() = default;
661 Comments(const Comments& that);
662 Comments(Comments&&) = default;
663 Comments& operator=(const Comments& that);
664 Comments& operator=(Comments&&) = default;
665 bool has(CommentPlacement slot) const;
666 String get(CommentPlacement slot) const;
667 void set(CommentPlacement slot, String s);
668
669 private:
670 using Array = std::array<String, numberOfCommentPlacement>;
671 std::unique_ptr<Array> ptr_;
672 };
673 Comments comments_;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000674
675 // [start, limit) byte offsets in the source JSON text from which this Value
676 // was extracted.
Christopher Dunnd4513fc2016-02-06 09:25:20 -0600677 ptrdiff_t start_;
678 ptrdiff_t limit_;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000679};
680
681/** \brief Experimental and untested: represents an element of the "path" to
682 * access a node.
683 */
684class JSON_API PathArgument {
685public:
686 friend class Path;
687
688 PathArgument();
689 PathArgument(ArrayIndex index);
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000690 PathArgument(const char* key);
Billy Donahue1c2ed7a2019-01-17 16:35:29 -0500691 PathArgument(const String& key);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000692
693private:
Billy Donahueb5e1fe82018-05-20 16:55:27 -0400694 enum Kind { kindNone = 0, kindIndex, kindKey };
Billy Donahue1c2ed7a2019-01-17 16:35:29 -0500695 String key_;
Hans Johnsone817e4f2019-01-14 17:09:22 -0600696 ArrayIndex index_{};
Billy Donahue2b593a92019-01-18 03:46:57 -0500697 Kind kind_{kindNone};
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000698};
699
700/** \brief Experimental and untested: represents a "path" to access a node.
701 *
702 * Syntax:
703 * - "." => root node
704 * - ".[n]" => elements at index 'n' of root node (an array value)
705 * - ".name" => member named 'name' of root node (an object value)
706 * - ".name1.name2.name3"
707 * - ".[0][1][2].name1[3]"
708 * - ".%" => member name is provided as parameter
709 * - ".[%]" => index is provied as parameter
710 */
711class JSON_API Path {
712public:
Billy Donahue1c2ed7a2019-01-17 16:35:29 -0500713 Path(const String& path,
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000714 const PathArgument& a1 = PathArgument(),
715 const PathArgument& a2 = PathArgument(),
716 const PathArgument& a3 = PathArgument(),
717 const PathArgument& a4 = PathArgument(),
718 const PathArgument& a5 = PathArgument());
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000719
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000720 const Value& resolve(const Value& root) const;
721 Value resolve(const Value& root, const Value& defaultValue) const;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000722 /// Creates the "path" to access the specified node and returns a reference on
723 /// the node.
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000724 Value& make(Value& root) const;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000725
726private:
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000727 typedef std::vector<const PathArgument*> InArgs;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000728 typedef std::vector<PathArgument> Args;
729
Billy Donahue1c2ed7a2019-01-17 16:35:29 -0500730 void makePath(const String& path, const InArgs& in);
731 void addPathInArg(const String& path,
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000732 const InArgs& in,
733 InArgs::const_iterator& itInArg,
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000734 PathArgument::Kind kind);
Billy Donahue1c2ed7a2019-01-17 16:35:29 -0500735 static void invalidPath(const String& path, int location);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000736
737 Args args_;
738};
739
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000740/** \brief base class for Value iterators.
741 *
742 */
743class JSON_API ValueIteratorBase {
744public:
745 typedef std::bidirectional_iterator_tag iterator_category;
746 typedef unsigned int size_t;
747 typedef int difference_type;
748 typedef ValueIteratorBase SelfType;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000749
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000750 bool operator==(const SelfType& other) const { return isEqual(other); }
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 difference_type operator-(const SelfType& other) const {
Kevin Grant4c5832a2015-02-14 20:53:35 -0800755 return other.computeDistance(*this);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000756 }
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000757
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000758 /// Return either the index or the member name of the referenced value as a
759 /// Value.
760 Value key() const;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000761
Billy Donahueb5e1fe82018-05-20 16:55:27 -0400762 /// Return the index of the referenced Value, or -1 if it is not an
763 /// arrayValue.
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000764 UInt index() const;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000765
Christopher Dunned495ed2015-03-08 14:01:28 -0500766 /// Return the member name of the referenced Value, or "" if it is not an
767 /// objectValue.
768 /// \note Avoid `c_str()` on result, as embedded zeroes are possible.
Billy Donahue1c2ed7a2019-01-17 16:35:29 -0500769 String name() const;
Christopher Dunned495ed2015-03-08 14:01:28 -0500770
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000771 /// Return the member name of the referenced Value. "" if it is not an
772 /// objectValue.
Billy Donahueb5e1fe82018-05-20 16:55:27 -0400773 /// \deprecated This cannot be used for UTF-8 strings, since there can be
774 /// embedded nulls.
Christopher Dunned495ed2015-03-08 14:01:28 -0500775 JSONCPP_DEPRECATED("Use `key = name();` instead.")
Christopher Dunnc28610f2015-02-21 11:44:16 -0600776 char const* memberName() const;
777 /// Return the member name of the referenced Value, or NULL if it is not an
778 /// objectValue.
Christopher Dunned495ed2015-03-08 14:01:28 -0500779 /// \note Better version than memberName(). Allows embedded nulls.
Christopher Dunnc28610f2015-02-21 11:44:16 -0600780 char const* memberName(char const** end) const;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000781
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000782protected:
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000783 Value& deref() const;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000784
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000785 void increment();
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000786
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000787 void decrement();
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000788
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000789 difference_type computeDistance(const SelfType& other) const;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000790
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000791 bool isEqual(const SelfType& other) const;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000792
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000793 void copy(const SelfType& other);
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000794
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000795private:
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000796 Value::ObjectValues::iterator current_;
797 // Indicates that iterator is for a null value.
Billy Donahue2b593a92019-01-18 03:46:57 -0500798 bool isNull_{true};
Christopher Dunn2a10f4a2015-04-28 04:55:12 +0100799
800public:
801 // For some reason, BORLAND needs these at the end, rather
802 // than earlier. No idea why.
803 ValueIteratorBase();
804 explicit ValueIteratorBase(const Value::ObjectValues::iterator& current);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000805};
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000806
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000807/** \brief const iterator for object and array value.
808 *
809 */
810class JSON_API ValueConstIterator : public ValueIteratorBase {
811 friend class Value;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000812
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000813public:
814 typedef const Value value_type;
Billy Donahueb5e1fe82018-05-20 16:55:27 -0400815 // typedef unsigned int size_t;
816 // typedef int difference_type;
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000817 typedef const Value& reference;
818 typedef const Value* pointer;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000819 typedef ValueConstIterator SelfType;
820
821 ValueConstIterator();
ycqiuc8a8cfc2015-10-06 16:46:19 +0800822 ValueConstIterator(ValueIterator const& other);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000823
824private:
Billy Donahueb5e1fe82018-05-20 16:55:27 -0400825 /*! \internal Use by Value to create an iterator.
826 */
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000827 explicit ValueConstIterator(const Value::ObjectValues::iterator& current);
Billy Donahueb5e1fe82018-05-20 16:55:27 -0400828
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000829public:
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000830 SelfType& operator=(const ValueIteratorBase& other);
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000831
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000832 SelfType operator++(int) {
833 SelfType temp(*this);
834 ++*this;
835 return temp;
836 }
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000837
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000838 SelfType operator--(int) {
839 SelfType temp(*this);
840 --*this;
841 return temp;
842 }
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000843
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000844 SelfType& operator--() {
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000845 decrement();
846 return *this;
847 }
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000848
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000849 SelfType& operator++() {
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000850 increment();
851 return *this;
852 }
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000853
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000854 reference operator*() const { return deref(); }
Braden McDorman540db3b2014-09-14 02:31:23 -0500855
856 pointer operator->() const { return &deref(); }
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000857};
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000858
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000859/** \brief Iterator for object and array value.
860 */
861class JSON_API ValueIterator : public ValueIteratorBase {
862 friend class Value;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000863
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000864public:
865 typedef Value value_type;
866 typedef unsigned int size_t;
867 typedef int difference_type;
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000868 typedef Value& reference;
869 typedef Value* pointer;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000870 typedef ValueIterator SelfType;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000871
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000872 ValueIterator();
ycqiuc8a8cfc2015-10-06 16:46:19 +0800873 explicit ValueIterator(const ValueConstIterator& other);
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000874 ValueIterator(const ValueIterator& other);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000875
876private:
Billy Donahueb5e1fe82018-05-20 16:55:27 -0400877 /*! \internal Use by Value to create an iterator.
878 */
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000879 explicit ValueIterator(const Value::ObjectValues::iterator& current);
Billy Donahueb5e1fe82018-05-20 16:55:27 -0400880
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000881public:
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000882 SelfType& operator=(const SelfType& other);
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000883
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000884 SelfType operator++(int) {
885 SelfType temp(*this);
886 ++*this;
887 return temp;
888 }
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000889
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000890 SelfType operator--(int) {
891 SelfType temp(*this);
892 --*this;
893 return temp;
894 }
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000895
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000896 SelfType& operator--() {
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000897 decrement();
898 return *this;
899 }
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000900
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000901 SelfType& operator++() {
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000902 increment();
903 return *this;
904 }
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000905
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000906 reference operator*() const { return deref(); }
Braden McDorman540db3b2014-09-14 02:31:23 -0500907
908 pointer operator->() const { return &deref(); }
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000909};
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000910
Billy Donahue1d956282018-03-06 12:51:58 -0500911inline void swap(Value& a, Value& b) { a.swap(b); }
912
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000913} // namespace Json
914
Sergiy80d6e666f2016-12-03 22:29:14 +0200915#pragma pack(pop)
datadiode9454e682015-01-20 15:25:04 -0600916
Baptiste Lepilleureafd7022013-05-08 20:21:11 +0000917#if defined(JSONCPP_DISABLE_DLL_INTERFACE_WARNING)
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000918#pragma warning(pop)
Baptiste Lepilleureafd7022013-05-08 20:21:11 +0000919#endif // if defined(JSONCPP_DISABLE_DLL_INTERFACE_WARNING)
920
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000921#endif // CPPTL_JSON_H_INCLUDED