blob: 277cd451dc106841d684b39ddd8a0943f457705a [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 CZString(CZString&& other);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000255 ~CZString();
Dhruv Paranjape0ba8bd72017-07-08 17:47:13 +0530256 CZString& operator=(const CZString& other);
Dhruv Paranjape0ba8bd72017-07-08 17:47:13 +0530257 CZString& operator=(CZString&& other);
Dhruv Paranjape0ba8bd72017-07-08 17:47:13 +0530258
Christopher Dunnc28610f2015-02-21 11:44:16 -0600259 bool operator<(CZString const& other) const;
260 bool operator==(CZString const& other) const;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000261 ArrayIndex index() const;
Billy Donahueb5e1fe82018-05-20 16:55:27 -0400262 // const char* c_str() const; ///< \deprecated
Christopher Dunnc28610f2015-02-21 11:44:16 -0600263 char const* data() const;
264 unsigned length() const;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000265 bool isStaticString() const;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000266
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000267 private:
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000268 void swap(CZString& other);
Christopher Dunnc28610f2015-02-21 11:44:16 -0600269
Christopher Dunn57ad0512015-03-02 12:10:35 -0600270 struct StringStorage {
Billy Donahueb5e1fe82018-05-20 16:55:27 -0400271 unsigned policy_ : 2;
272 unsigned length_ : 30; // 1GB max
Christopher Dunn57ad0512015-03-02 12:10:35 -0600273 };
274
Billy Donahueb5e1fe82018-05-20 16:55:27 -0400275 char const* cstr_; // actually, a prefixed string, unless policy is noDup
Christopher Dunn57ad0512015-03-02 12:10:35 -0600276 union {
277 ArrayIndex index_;
278 StringStorage storage_;
279 };
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000280 };
281
282public:
283#ifndef JSON_USE_CPPTL_SMALLMAP
284 typedef std::map<CZString, Value> ObjectValues;
285#else
286 typedef CppTL::SmallMap<CZString, Value> ObjectValues;
287#endif // ifndef JSON_USE_CPPTL_SMALLMAP
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000288#endif // ifndef JSONCPP_DOC_EXCLUDE_IMPLEMENTATION
289
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000290public:
291 /** \brief Create a default Value of the given type.
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000292
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000293 This is a very useful constructor.
294 To create an empty array, pass arrayValue.
295 To create an empty object, pass objectValue.
296 Another Value can then be set to this one by assignment.
297This is useful since clear() and resize() will not alter types.
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000298
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000299 Examples:
300\code
301Json::Value null_value; // null
302Json::Value arr_value(Json::arrayValue); // []
303Json::Value obj_value(Json::objectValue); // {}
304\endcode
305 */
306 Value(ValueType type = nullValue);
307 Value(Int value);
308 Value(UInt value);
Baptiste Lepilleur842e9ac2010-12-27 17:45:23 +0000309#if defined(JSON_HAS_INT64)
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000310 Value(Int64 value);
311 Value(UInt64 value);
Baptiste Lepilleur842e9ac2010-12-27 17:45:23 +0000312#endif // if defined(JSON_HAS_INT64)
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000313 Value(double value);
Christopher Dunn8a702972015-03-03 10:38:27 -0600314 Value(const char* value); ///< Copy til first 0. (NULL causes to seg-fault.)
Christopher Dunn89704032015-07-11 12:09:59 -0500315 Value(const char* begin, const char* end); ///< Copy all, incl zeroes.
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000316 /** \brief Constructs a value from a static string.
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000317
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000318 * Like other value string constructor but do not duplicate the string for
319 * internal storage. The given string must remain alive after the call to this
320 * constructor.
Christopher Dunnc28610f2015-02-21 11:44:16 -0600321 * \note This works only for null-terminated strings. (We cannot change the
322 * size of this class, so we have nowhere to store the length,
323 * which might be computed later for various operations.)
324 *
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000325 * Example of usage:
326 * \code
Christopher Dunnc28610f2015-02-21 11:44:16 -0600327 * static StaticString foo("some text");
328 * Json::Value aValue(foo);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000329 * \endcode
330 */
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000331 Value(const StaticString& value);
Billy Donahue1c2ed7a2019-01-17 16:35:29 -0500332 Value(const String& value); ///< Copy data() til size(). Embedded
333 ///< zeroes too.
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000334#ifdef JSON_USE_CPPTL
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000335 Value(const CppTL::ConstString& value);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000336#endif
337 Value(bool value);
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000338 Value(const Value& other);
Motti2b008912015-04-20 17:44:47 +0300339 Value(Value&& other);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000340 ~Value();
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000341
Billy Donahue0c1cc6e2019-01-20 23:59:16 -0500342 /// \note Overwrite existing comments. To preserve comments, use
Billy Donahueb5e1fe82018-05-20 16:55:27 -0400343 /// #swapPayload().
Billy Donahue0c1cc6e2019-01-20 23:59:16 -0500344 Value& operator=(const Value& other);
345 Value& operator=(Value&& other);
Dhruv Paranjape8996c372017-07-08 17:27:07 +0530346
Christopher Dunn66eb72f2015-01-20 11:02:22 -0600347 /// Swap everything.
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000348 void swap(Value& other);
Christopher Dunn66eb72f2015-01-20 11:02:22 -0600349 /// Swap values but leave comments and source offsets in place.
350 void swapPayload(Value& other);
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000351
Dhruv Paranjape8996c372017-07-08 17:27:07 +0530352 /// copy everything.
353 void copy(const Value& other);
354 /// copy values but leave comments and source offsets in place.
355 void copyPayload(const Value& other);
356
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000357 ValueType type() const;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000358
Christopher Dunn66eb72f2015-01-20 11:02:22 -0600359 /// Compare payload only, not comments etc.
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000360 bool operator<(const Value& other) const;
361 bool operator<=(const Value& other) const;
362 bool operator>=(const Value& other) const;
363 bool operator>(const Value& other) const;
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000364 bool operator==(const Value& other) const;
365 bool operator!=(const Value& other) const;
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000366 int compare(const Value& other) const;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000367
Christopher Dunn8a702972015-03-03 10:38:27 -0600368 const char* asCString() const; ///< Embedded zeroes could cause you trouble!
dawescae564652016-03-14 19:11:02 -0500369#if JSONCPP_USING_SECURE_MEMORY
Billy Donahueb5e1fe82018-05-20 16:55:27 -0400370 unsigned getCStringLength() const; // Allows you to understand the length of
371 // the CString
dawescae564652016-03-14 19:11:02 -0500372#endif
Billy Donahue1c2ed7a2019-01-17 16:35:29 -0500373 String asString() const; ///< Embedded zeroes are possible.
Christopher Dunnc28610f2015-02-21 11:44:16 -0600374 /** Get raw char* of string-value.
375 * \return false if !string. (Seg-fault if str or end are NULL.)
376 */
Billy Donahueb5e1fe82018-05-20 16:55:27 -0400377 bool getString(char const** begin, char const** end) const;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000378#ifdef JSON_USE_CPPTL
379 CppTL::ConstString asConstString() const;
380#endif
381 Int asInt() const;
382 UInt asUInt() const;
Aaron Jacobsf1053e72011-05-24 03:18:02 +0000383#if defined(JSON_HAS_INT64)
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000384 Int64 asInt64() const;
385 UInt64 asUInt64() const;
Aaron Jacobsf1053e72011-05-24 03:18:02 +0000386#endif // if defined(JSON_HAS_INT64)
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000387 LargestInt asLargestInt() const;
388 LargestUInt asLargestUInt() const;
389 float asFloat() const;
390 double asDouble() const;
391 bool asBool() const;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000392
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000393 bool isNull() const;
394 bool isBool() const;
395 bool isInt() const;
396 bool isInt64() const;
397 bool isUInt() const;
398 bool isUInt64() const;
399 bool isIntegral() const;
400 bool isDouble() const;
401 bool isNumeric() const;
402 bool isString() const;
403 bool isArray() const;
404 bool isObject() const;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000405
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000406 bool isConvertibleTo(ValueType other) const;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000407
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000408 /// Number of values in array or object
409 ArrayIndex size() const;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000410
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000411 /// \brief Return true if empty array, empty object, or null;
412 /// otherwise, false.
413 bool empty() const;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000414
Wolfram Rösler90794222017-12-05 18:18:55 +0100415 /// Return !isNull()
drgler04abe382018-01-13 15:28:19 +0100416 JSONCPP_OP_EXPLICIT operator bool() const;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000417
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000418 /// Remove all object members and array elements.
419 /// \pre type() is arrayValue, objectValue, or nullValue
420 /// \post type() is unchanged
421 void clear();
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000422
Marian Klymovfc201342018-06-02 20:15:26 +0300423 /// Resize the array to newSize elements.
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000424 /// New elements are initialized to null.
425 /// May only be called on nullValue or arrayValue.
426 /// \pre type() is arrayValue or nullValue
427 /// \post type() is arrayValue
Marian Klymovfc201342018-06-02 20:15:26 +0300428 void resize(ArrayIndex newSize);
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000429
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000430 /// Access an array element (zero based index ).
431 /// If the array contains less than index element, then null value are
432 /// inserted
433 /// in the array so that its size is index+1.
434 /// (You may need to say 'value[0u]' to get your compiler to distinguish
435 /// this from the operator[] which takes a string.)
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000436 Value& operator[](ArrayIndex index);
Baptiste Lepilleurfa130ef2010-12-24 12:47:14 +0000437
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000438 /// Access an array element (zero based index ).
439 /// If the array contains less than index element, then null value are
440 /// inserted
441 /// in the array so that its size is index+1.
442 /// (You may need to say 'value[0u]' to get your compiler to distinguish
443 /// this from the operator[] which takes a string.)
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000444 Value& operator[](int index);
Baptiste Lepilleurfa130ef2010-12-24 12:47:14 +0000445
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000446 /// Access an array element (zero based index )
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 const Value& operator[](ArrayIndex index) const;
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[](int index) const;
Baptiste Lepilleurfa130ef2010-12-24 12:47:14 +0000455
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000456 /// If the array contains at least index+1 elements, returns the element
457 /// value,
458 /// otherwise returns defaultValue.
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000459 Value get(ArrayIndex index, const Value& defaultValue) const;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000460 /// Return true if index < size().
461 bool isValidIndex(ArrayIndex index) const;
462 /// \brief Append value to array at the end.
463 ///
464 /// Equivalent to jsonvalue[jsonvalue.size()] = value;
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000465 Value& append(const Value& value);
Dhruv Paranjape23c44d92017-07-08 17:30:47 +0530466 Value& append(Value&& value);
Dhruv Paranjape23c44d92017-07-08 17:30:47 +0530467
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000468 /// Access an object value by name, create a null member if it does not exist.
Christopher Dunn25342ba2015-03-02 18:05:26 -0600469 /// \note Because of our implementation, keys are limited to 2^30 -1 chars.
470 /// Exceeding that will cause an exception.
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000471 Value& operator[](const char* key);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000472 /// Access an object value by name, returns null if there is no member with
473 /// that name.
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000474 const Value& operator[](const char* key) const;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000475 /// Access an object value by name, create a null member if it does not exist.
Christopher Dunn25342ba2015-03-02 18:05:26 -0600476 /// \param key may contain embedded nulls.
Billy Donahue1c2ed7a2019-01-17 16:35:29 -0500477 Value& operator[](const String& key);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000478 /// Access an object value by name, returns null if there is no member with
479 /// that name.
Christopher Dunn25342ba2015-03-02 18:05:26 -0600480 /// \param key may contain embedded nulls.
Billy Donahue1c2ed7a2019-01-17 16:35:29 -0500481 const Value& operator[](const String& key) const;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000482 /** \brief Access an object value by name, create a null member if it does not
483 exist.
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000484
Billy Donahueb5e1fe82018-05-20 16:55:27 -0400485 * If the object has no entry for that name, then the member name used to
486 store
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000487 * the new entry is not duplicated.
488 * Example of use:
489 * \code
490 * Json::Value object;
491 * static const StaticString code("code");
492 * object[code] = 1234;
493 * \endcode
494 */
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000495 Value& operator[](const StaticString& key);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000496#ifdef JSON_USE_CPPTL
497 /// Access an object value by name, create a null member if it does not exist.
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000498 Value& operator[](const CppTL::ConstString& key);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000499 /// Access an object value by name, returns null if there is no member with
500 /// that name.
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000501 const Value& operator[](const CppTL::ConstString& key) const;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000502#endif
503 /// Return the member named key if it exist, defaultValue otherwise.
Christopher Dunn0fd28752015-03-05 16:38:43 -0600504 /// \note deep copy
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000505 Value get(const char* key, const Value& defaultValue) const;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000506 /// Return the member named key if it exist, defaultValue otherwise.
Christopher Dunn0fd28752015-03-05 16:38:43 -0600507 /// \note deep copy
Christopher Dunn89704032015-07-11 12:09:59 -0500508 /// \note key may contain embedded nulls.
Billy Donahueb5e1fe82018-05-20 16:55:27 -0400509 Value
510 get(const char* begin, const char* end, const Value& defaultValue) const;
Christopher Dunn25342ba2015-03-02 18:05:26 -0600511 /// Return the member named key if it exist, defaultValue otherwise.
Christopher Dunn0fd28752015-03-05 16:38:43 -0600512 /// \note deep copy
Christopher Dunn25342ba2015-03-02 18:05:26 -0600513 /// \param key may contain embedded nulls.
Billy Donahue1c2ed7a2019-01-17 16:35:29 -0500514 Value get(const String& key, const Value& defaultValue) const;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000515#ifdef JSON_USE_CPPTL
516 /// Return the member named key if it exist, defaultValue otherwise.
Christopher Dunn0fd28752015-03-05 16:38:43 -0600517 /// \note deep copy
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000518 Value get(const CppTL::ConstString& key, const Value& defaultValue) const;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000519#endif
Christopher Dunn25342ba2015-03-02 18:05:26 -0600520 /// Most general and efficient version of isMember()const, get()const,
521 /// and operator[]const
Christopher Dunn89704032015-07-11 12:09:59 -0500522 /// \note As stated elsewhere, behavior is undefined if (end-begin) >= 2^30
523 Value const* find(char const* begin, char const* end) const;
Christopher Dunnc28610f2015-02-21 11:44:16 -0600524 /// Most general and efficient version of object-mutators.
Christopher Dunn89704032015-07-11 12:09:59 -0500525 /// \note As stated elsewhere, behavior is undefined if (end-begin) >= 2^30
Christopher Dunnc28610f2015-02-21 11:44:16 -0600526 /// \return non-zero, but JSON_ASSERT if this is neither object nor nullValue.
Frank Richterd76fe562019-03-23 14:31:06 +0100527 Value* demand(char const* begin, char const* end);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000528 /// \brief Remove and return the named member.
529 ///
530 /// Do nothing if it did not exist.
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000531 /// \pre type() is objectValue or nullValue
532 /// \post type() is unchanged
Wolfram Röslera06b3902017-10-18 07:19:27 +0200533 void removeMember(const char* key);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000534 /// Same as removeMember(const char*)
Christopher Dunn25342ba2015-03-02 18:05:26 -0600535 /// \param key may contain embedded nulls.
Billy Donahue1c2ed7a2019-01-17 16:35:29 -0500536 void removeMember(const String& key);
Christopher Dunn89704032015-07-11 12:09:59 -0500537 /// Same as removeMember(const char* begin, const char* end, Value* removed),
Christopher Dunn25342ba2015-03-02 18:05:26 -0600538 /// but 'key' is null-terminated.
539 bool removeMember(const char* key, Value* removed);
Christopher Dunn76746b02015-01-21 16:01:30 -0600540 /** \brief Remove the named map member.
541
542 Update 'removed' iff removed.
Christopher Dunn25342ba2015-03-02 18:05:26 -0600543 \param key may contain embedded nulls.
Christopher Dunn76746b02015-01-21 16:01:30 -0600544 \return true iff removed (no exceptions)
545 */
Billy Donahue1c2ed7a2019-01-17 16:35:29 -0500546 bool removeMember(String const& key, Value* removed);
547 /// Same as removeMember(String const& key, Value* removed)
Christopher Dunn89704032015-07-11 12:09:59 -0500548 bool removeMember(const char* begin, const char* end, Value* removed);
Christopher Dunn9de2c2d2015-01-20 16:15:40 -0600549 /** \brief Remove the indexed array element.
550
551 O(n) expensive operations.
YantaoZhaoe32ee472018-07-03 21:29:18 +0800552 Update 'removed' iff removed.
Marian Klymovfc201342018-06-02 20:15:26 +0300553 \return true if removed (no exceptions)
Christopher Dunn9de2c2d2015-01-20 16:15:40 -0600554 */
Marian Klymovfc201342018-06-02 20:15:26 +0300555 bool removeIndex(ArrayIndex index, Value* removed);
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000556
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000557 /// Return true if the object has a member named key.
Christopher Dunnc28610f2015-02-21 11:44:16 -0600558 /// \note 'key' must be null-terminated.
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000559 bool isMember(const char* key) const;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000560 /// Return true if the object has a member named key.
Christopher Dunn25342ba2015-03-02 18:05:26 -0600561 /// \param key may contain embedded nulls.
Billy Donahue1c2ed7a2019-01-17 16:35:29 -0500562 bool isMember(const String& key) const;
563 /// Same as isMember(String const& key)const
Christopher Dunn89704032015-07-11 12:09:59 -0500564 bool isMember(const char* begin, const char* end) const;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000565#ifdef JSON_USE_CPPTL
566 /// Return true if the object has a member named key.
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000567 bool isMember(const CppTL::ConstString& key) const;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000568#endif
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000569
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000570 /// \brief Return a list of the member names.
571 ///
572 /// If null, return an empty list.
573 /// \pre type() is objectValue or nullValue
574 /// \post if type() was nullValue, it remains nullValue
575 Members getMemberNames() const;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000576
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000577 //# ifdef JSON_USE_CPPTL
578 // EnumMemberNames enumMemberNames() const;
579 // EnumValues enumValues() const;
580 //# endif
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000581
Christopher Dunn1e3149a2015-01-25 14:16:13 -0600582 /// \deprecated Always pass len.
Billy Donahue1c2ed7a2019-01-17 16:35:29 -0500583 JSONCPP_DEPRECATED("Use setComment(String const&) instead.")
Billy Donahue433107f2019-01-20 21:53:01 -0500584 void setComment(const char* comment, CommentPlacement placement) {
585 setComment(String(comment, strlen(comment)), placement);
586 }
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000587 /// Comments must be //... or /* ... */
Billy Donahue433107f2019-01-20 21:53:01 -0500588 void setComment(const char* comment, size_t len, CommentPlacement placement) {
589 setComment(String(comment, len), placement);
590 }
Christopher Dunn1e3149a2015-01-25 14:16:13 -0600591 /// Comments must be //... or /* ... */
Billy Donahue433107f2019-01-20 21:53:01 -0500592 void setComment(String comment, CommentPlacement placement);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000593 bool hasComment(CommentPlacement placement) const;
594 /// Include delimiters and embedded newlines.
Billy Donahue1c2ed7a2019-01-17 16:35:29 -0500595 String getComment(CommentPlacement placement) const;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000596
Billy Donahue1c2ed7a2019-01-17 16:35:29 -0500597 String toStyledString() const;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000598
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000599 const_iterator begin() const;
600 const_iterator end() const;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000601
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000602 iterator begin();
603 iterator end();
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000604
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000605 // Accessors for the [start, limit) range of bytes within the JSON text from
606 // which this value was parsed, if any.
Christopher Dunnd4513fc2016-02-06 09:25:20 -0600607 void setOffsetStart(ptrdiff_t start);
608 void setOffsetLimit(ptrdiff_t limit);
609 ptrdiff_t getOffsetStart() const;
610 ptrdiff_t getOffsetLimit() const;
Aaron Jacobs68db6552014-04-23 23:41:12 +0000611
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000612private:
Billy Donahue0c1cc6e2019-01-20 23:59:16 -0500613 void setType(ValueType v) { bits_.value_type_ = v; }
614 bool isAllocated() const { return bits_.allocated_; }
615 void setIsAllocated(bool v) { bits_.allocated_ = v; }
616
Billy Donahue8eb5d892014-11-10 01:35:42 -0500617 void initBasic(ValueType type, bool allocated = false);
Andrey Okoshkin9b569c82018-01-12 15:59:20 +0300618 void dupPayload(const Value& other);
Andrey Okoshkinc69148c2018-01-12 11:26:34 +0300619 void releasePayload();
Andrey Okoshkin9b569c82018-01-12 15:59:20 +0300620 void dupMeta(const Value& other);
Billy Donahue8eb5d892014-11-10 01:35:42 -0500621
Christopher Dunnc28610f2015-02-21 11:44:16 -0600622 Value& resolveReference(const char* key);
623 Value& resolveReference(const char* key, const char* end);
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000624
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000625 // struct MemberNamesTransform
626 //{
627 // typedef const char *result_type;
628 // const char *operator()( const CZString &name ) const
629 // {
630 // return name.c_str();
631 // }
632 //};
633
634 union ValueHolder {
635 LargestInt int_;
636 LargestUInt uint_;
637 double real_;
638 bool bool_;
Billy Donahue0c1cc6e2019-01-20 23:59:16 -0500639 char* string_; // if allocated_, ptr to { unsigned, char[] }.
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000640 ObjectValues* map_;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000641 } value_;
Billy Donahue0c1cc6e2019-01-20 23:59:16 -0500642
643 struct {
644 // Really a ValueType, but types should agree for bitfield packing.
645 unsigned int value_type_ : 8;
646 // Unless allocated_, string_ must be null-terminated.
647 unsigned int allocated_ : 1;
648 } bits_;
649
Billy Donahue433107f2019-01-20 21:53:01 -0500650 class Comments {
651 public:
652 Comments() = default;
653 Comments(const Comments& that);
Billy Donahue00558b32019-01-21 16:42:25 -0500654 Comments(Comments&& that);
Billy Donahue433107f2019-01-20 21:53:01 -0500655 Comments& operator=(const Comments& that);
Billy Donahue00558b32019-01-21 16:42:25 -0500656 Comments& operator=(Comments&& that);
Billy Donahue433107f2019-01-20 21:53:01 -0500657 bool has(CommentPlacement slot) const;
658 String get(CommentPlacement slot) const;
659 void set(CommentPlacement slot, String s);
660
661 private:
662 using Array = std::array<String, numberOfCommentPlacement>;
663 std::unique_ptr<Array> ptr_;
664 };
665 Comments comments_;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000666
667 // [start, limit) byte offsets in the source JSON text from which this Value
668 // was extracted.
Christopher Dunnd4513fc2016-02-06 09:25:20 -0600669 ptrdiff_t start_;
670 ptrdiff_t limit_;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000671};
672
673/** \brief Experimental and untested: represents an element of the "path" to
674 * access a node.
675 */
676class JSON_API PathArgument {
677public:
678 friend class Path;
679
680 PathArgument();
681 PathArgument(ArrayIndex index);
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000682 PathArgument(const char* key);
Billy Donahue1c2ed7a2019-01-17 16:35:29 -0500683 PathArgument(const String& key);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000684
685private:
Billy Donahueb5e1fe82018-05-20 16:55:27 -0400686 enum Kind { kindNone = 0, kindIndex, kindKey };
Billy Donahue1c2ed7a2019-01-17 16:35:29 -0500687 String key_;
Hans Johnsone817e4f2019-01-14 17:09:22 -0600688 ArrayIndex index_{};
Billy Donahue2b593a92019-01-18 03:46:57 -0500689 Kind kind_{kindNone};
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000690};
691
692/** \brief Experimental and untested: represents a "path" to access a node.
693 *
694 * Syntax:
695 * - "." => root node
696 * - ".[n]" => elements at index 'n' of root node (an array value)
697 * - ".name" => member named 'name' of root node (an object value)
698 * - ".name1.name2.name3"
699 * - ".[0][1][2].name1[3]"
700 * - ".%" => member name is provided as parameter
701 * - ".[%]" => index is provied as parameter
702 */
703class JSON_API Path {
704public:
Billy Donahue1c2ed7a2019-01-17 16:35:29 -0500705 Path(const String& path,
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000706 const PathArgument& a1 = PathArgument(),
707 const PathArgument& a2 = PathArgument(),
708 const PathArgument& a3 = PathArgument(),
709 const PathArgument& a4 = PathArgument(),
710 const PathArgument& a5 = PathArgument());
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000711
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000712 const Value& resolve(const Value& root) const;
713 Value resolve(const Value& root, const Value& defaultValue) const;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000714 /// Creates the "path" to access the specified node and returns a reference on
715 /// the node.
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000716 Value& make(Value& root) const;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000717
718private:
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000719 typedef std::vector<const PathArgument*> InArgs;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000720 typedef std::vector<PathArgument> Args;
721
Billy Donahue1c2ed7a2019-01-17 16:35:29 -0500722 void makePath(const String& path, const InArgs& in);
723 void addPathInArg(const String& path,
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000724 const InArgs& in,
725 InArgs::const_iterator& itInArg,
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000726 PathArgument::Kind kind);
Billy Donahue1c2ed7a2019-01-17 16:35:29 -0500727 static void invalidPath(const String& path, int location);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000728
729 Args args_;
730};
731
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000732/** \brief base class for Value iterators.
733 *
734 */
735class JSON_API ValueIteratorBase {
736public:
737 typedef std::bidirectional_iterator_tag iterator_category;
738 typedef unsigned int size_t;
739 typedef int difference_type;
740 typedef ValueIteratorBase SelfType;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000741
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000742 bool operator==(const SelfType& other) const { return isEqual(other); }
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000743
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000744 bool operator!=(const SelfType& other) const { return !isEqual(other); }
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000745
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000746 difference_type operator-(const SelfType& other) const {
Kevin Grant4c5832a2015-02-14 20:53:35 -0800747 return other.computeDistance(*this);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000748 }
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000749
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000750 /// Return either the index or the member name of the referenced value as a
751 /// Value.
752 Value key() const;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000753
Billy Donahueb5e1fe82018-05-20 16:55:27 -0400754 /// Return the index of the referenced Value, or -1 if it is not an
755 /// arrayValue.
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000756 UInt index() const;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000757
Christopher Dunned495ed2015-03-08 14:01:28 -0500758 /// Return the member name of the referenced Value, or "" if it is not an
759 /// objectValue.
760 /// \note Avoid `c_str()` on result, as embedded zeroes are possible.
Billy Donahue1c2ed7a2019-01-17 16:35:29 -0500761 String name() const;
Christopher Dunned495ed2015-03-08 14:01:28 -0500762
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000763 /// Return the member name of the referenced Value. "" if it is not an
764 /// objectValue.
Billy Donahueb5e1fe82018-05-20 16:55:27 -0400765 /// \deprecated This cannot be used for UTF-8 strings, since there can be
766 /// embedded nulls.
Christopher Dunned495ed2015-03-08 14:01:28 -0500767 JSONCPP_DEPRECATED("Use `key = name();` instead.")
Christopher Dunnc28610f2015-02-21 11:44:16 -0600768 char const* memberName() const;
769 /// Return the member name of the referenced Value, or NULL if it is not an
770 /// objectValue.
Christopher Dunned495ed2015-03-08 14:01:28 -0500771 /// \note Better version than memberName(). Allows embedded nulls.
Christopher Dunnc28610f2015-02-21 11:44:16 -0600772 char const* memberName(char const** end) const;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000773
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000774protected:
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000775 Value& deref() const;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000776
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000777 void increment();
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000778
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000779 void decrement();
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000780
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000781 difference_type computeDistance(const SelfType& other) const;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000782
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000783 bool isEqual(const SelfType& other) const;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000784
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000785 void copy(const SelfType& other);
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000786
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000787private:
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000788 Value::ObjectValues::iterator current_;
789 // Indicates that iterator is for a null value.
Billy Donahue2b593a92019-01-18 03:46:57 -0500790 bool isNull_{true};
Christopher Dunn2a10f4a2015-04-28 04:55:12 +0100791
792public:
793 // For some reason, BORLAND needs these at the end, rather
794 // than earlier. No idea why.
795 ValueIteratorBase();
796 explicit ValueIteratorBase(const Value::ObjectValues::iterator& current);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000797};
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000798
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000799/** \brief const iterator for object and array value.
800 *
801 */
802class JSON_API ValueConstIterator : public ValueIteratorBase {
803 friend class Value;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000804
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000805public:
806 typedef const Value value_type;
Billy Donahueb5e1fe82018-05-20 16:55:27 -0400807 // typedef unsigned int size_t;
808 // typedef int difference_type;
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000809 typedef const Value& reference;
810 typedef const Value* pointer;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000811 typedef ValueConstIterator SelfType;
812
813 ValueConstIterator();
ycqiuc8a8cfc2015-10-06 16:46:19 +0800814 ValueConstIterator(ValueIterator const& other);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000815
816private:
Billy Donahueb5e1fe82018-05-20 16:55:27 -0400817 /*! \internal Use by Value to create an iterator.
818 */
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000819 explicit ValueConstIterator(const Value::ObjectValues::iterator& current);
Billy Donahueb5e1fe82018-05-20 16:55:27 -0400820
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000821public:
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000822 SelfType& operator=(const ValueIteratorBase& other);
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000823
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000824 SelfType operator++(int) {
825 SelfType temp(*this);
826 ++*this;
827 return temp;
828 }
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000829
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000830 SelfType operator--(int) {
831 SelfType temp(*this);
832 --*this;
833 return temp;
834 }
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000835
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000836 SelfType& operator--() {
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000837 decrement();
838 return *this;
839 }
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000840
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000841 SelfType& operator++() {
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000842 increment();
843 return *this;
844 }
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000845
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000846 reference operator*() const { return deref(); }
Braden McDorman540db3b2014-09-14 02:31:23 -0500847
848 pointer operator->() const { return &deref(); }
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000849};
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000850
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000851/** \brief Iterator for object and array value.
852 */
853class JSON_API ValueIterator : public ValueIteratorBase {
854 friend class Value;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000855
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000856public:
857 typedef Value value_type;
858 typedef unsigned int size_t;
859 typedef int difference_type;
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000860 typedef Value& reference;
861 typedef Value* pointer;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000862 typedef ValueIterator SelfType;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000863
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000864 ValueIterator();
ycqiuc8a8cfc2015-10-06 16:46:19 +0800865 explicit ValueIterator(const ValueConstIterator& other);
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000866 ValueIterator(const ValueIterator& other);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000867
868private:
Billy Donahueb5e1fe82018-05-20 16:55:27 -0400869 /*! \internal Use by Value to create an iterator.
870 */
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000871 explicit ValueIterator(const Value::ObjectValues::iterator& current);
Billy Donahueb5e1fe82018-05-20 16:55:27 -0400872
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000873public:
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000874 SelfType& operator=(const SelfType& other);
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000875
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000876 SelfType operator++(int) {
877 SelfType temp(*this);
878 ++*this;
879 return temp;
880 }
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000881
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000882 SelfType operator--(int) {
883 SelfType temp(*this);
884 --*this;
885 return temp;
886 }
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000887
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000888 SelfType& operator--() {
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000889 decrement();
890 return *this;
891 }
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000892
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000893 SelfType& operator++() {
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000894 increment();
895 return *this;
896 }
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000897
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000898 reference operator*() const { return deref(); }
Braden McDorman540db3b2014-09-14 02:31:23 -0500899
900 pointer operator->() const { return &deref(); }
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000901};
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000902
Billy Donahue1d956282018-03-06 12:51:58 -0500903inline void swap(Value& a, Value& b) { a.swap(b); }
904
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000905} // namespace Json
906
Sergiy80d6e666f2016-12-03 22:29:14 +0200907#pragma pack(pop)
datadiode9454e682015-01-20 15:25:04 -0600908
Baptiste Lepilleureafd7022013-05-08 20:21:11 +0000909#if defined(JSONCPP_DISABLE_DLL_INTERFACE_WARNING)
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000910#pragma warning(pop)
Baptiste Lepilleureafd7022013-05-08 20:21:11 +0000911#endif // if defined(JSONCPP_DISABLE_DLL_INTERFACE_WARNING)
912
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000913#endif // CPPTL_JSON_H_INCLUDED