blob: 59bae07da526246d5f8d948267ded1e971fbc5a3 [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
Jordan Bayles83cc9212019-06-06 13:41:47 -070053#if JSON_USE_EXCEPTION
Christopher Dunn75279cc2015-03-08 12:20:06 -050054/** Base class for all exceptions we throw.
Christopher Dunn4e30c4f2015-03-08 12:56:32 -050055 *
56 * We use nothing but these internally. Of course, STL can throw others.
Christopher Dunn75279cc2015-03-08 12:20:06 -050057 */
Christopher Dunn949babd2015-07-23 00:19:12 -050058class JSON_API Exception : public std::exception {
59public:
Billy Donahue1c2ed7a2019-01-17 16:35:29 -050060 Exception(String msg);
Hans Johnson2853b1c2019-01-11 13:58:53 -060061 ~Exception() JSONCPP_NOEXCEPT override;
62 char const* what() const JSONCPP_NOEXCEPT override;
Billy Donahueb5e1fe82018-05-20 16:55:27 -040063
Christopher Dunn949babd2015-07-23 00:19:12 -050064protected:
Billy Donahue1c2ed7a2019-01-17 16:35:29 -050065 String msg_;
Christopher Dunn949babd2015-07-23 00:19:12 -050066};
67
Christopher Dunn53837942015-03-08 12:31:00 -050068/** Exceptions which the user cannot easily avoid.
69 *
Christopher Dunn4e30c4f2015-03-08 12:56:32 -050070 * E.g. out-of-memory (when we use malloc), stack-overflow, malicious input
Dhruv Paranjape8996c372017-07-08 17:27:07 +053071 *
Christopher Dunn4e30c4f2015-03-08 12:56:32 -050072 * \remark derived from Json::Exception
Christopher Dunn53837942015-03-08 12:31:00 -050073 */
Christopher Dunn949babd2015-07-23 00:19:12 -050074class JSON_API RuntimeError : public Exception {
75public:
Billy Donahue1c2ed7a2019-01-17 16:35:29 -050076 RuntimeError(String const& msg);
Christopher Dunn949babd2015-07-23 00:19:12 -050077};
78
Christopher Dunn4e30c4f2015-03-08 12:56:32 -050079/** Exceptions thrown by JSON_ASSERT/JSON_FAIL macros.
Christopher Dunn53837942015-03-08 12:31:00 -050080 *
81 * These are precondition-violations (user bugs) and internal errors (our bugs).
Dhruv Paranjape8996c372017-07-08 17:27:07 +053082 *
Christopher Dunn4e30c4f2015-03-08 12:56:32 -050083 * \remark derived from Json::Exception
Christopher Dunn53837942015-03-08 12:31:00 -050084 */
Christopher Dunn949babd2015-07-23 00:19:12 -050085class JSON_API LogicError : public Exception {
86public:
Billy Donahue1c2ed7a2019-01-17 16:35:29 -050087 LogicError(String const& msg);
Christopher Dunn949babd2015-07-23 00:19:12 -050088};
Jordan Bayles83cc9212019-06-06 13:41:47 -070089#endif
Christopher Dunn53837942015-03-08 12:31:00 -050090
Christopher Dunn4e30c4f2015-03-08 12:56:32 -050091/// used internally
Billy Donahue1c2ed7a2019-01-17 16:35:29 -050092JSONCPP_NORETURN void throwRuntimeError(String const& msg);
Christopher Dunn4e30c4f2015-03-08 12:56:32 -050093/// used internally
Billy Donahue1c2ed7a2019-01-17 16:35:29 -050094JSONCPP_NORETURN void throwLogicError(String const& msg);
Christopher Dunn75279cc2015-03-08 12:20:06 -050095
Aaron Jacobs9fa4e842014-07-01 08:48:54 +100096/** \brief Type of the value held by a Value object.
97 */
98enum ValueType {
99 nullValue = 0, ///< 'null' value
100 intValue, ///< signed integer value
101 uintValue, ///< unsigned integer value
102 realValue, ///< double value
103 stringValue, ///< UTF-8 string value
104 booleanValue, ///< bool value
105 arrayValue, ///< array value (ordered list)
106 objectValue ///< object value (collection of name/value pairs).
107};
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000108
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000109enum CommentPlacement {
110 commentBefore = 0, ///< a comment placed on the line before a value
111 commentAfterOnSameLine, ///< a comment just after a value on the same line
112 commentAfter, ///< a comment on the line after a value (only make sense for
Aaron Jacobs3a0c4fc2014-07-01 09:20:48 +1000113 /// root value)
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000114 numberOfCommentPlacement
115};
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000116
Mike Ra07fc532018-03-13 23:35:31 +0300117/** \brief Type of precision for formatting of real values.
118 */
119enum PrecisionType {
120 significantDigits = 0, ///< we set max number of significant digits in string
121 decimalPlaces ///< we set max number of digits after "." in string
122};
123
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000124//# ifdef JSON_USE_CPPTL
125// typedef CppTL::AnyEnumerator<const char *> EnumMemberNames;
126// typedef CppTL::AnyEnumerator<const Value &> EnumValues;
127//# endif
128
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000129/** \brief Lightweight wrapper to tag static string.
130 *
Josh Sorefe6a588a2017-12-03 11:54:29 -0500131 * Value constructor and objectValue member assignment takes advantage of the
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000132 * StaticString and avoid the cost of string duplication when storing the
133 * string or the member name.
134 *
135 * Example of usage:
136 * \code
137 * Json::Value aValue( StaticString("some text") );
138 * Json::Value object;
139 * static const StaticString code("code");
140 * object[code] = 1234;
141 * \endcode
142 */
143class JSON_API StaticString {
144public:
Christopher Dunnff617522015-03-06 10:31:46 -0600145 explicit StaticString(const char* czstring) : c_str_(czstring) {}
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000146
Christopher Dunnff617522015-03-06 10:31:46 -0600147 operator const char*() const { return c_str_; }
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000148
Christopher Dunnff617522015-03-06 10:31:46 -0600149 const char* c_str() const { return c_str_; }
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000150
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000151private:
Christopher Dunnff617522015-03-06 10:31:46 -0600152 const char* c_str_;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000153};
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000154
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000155/** \brief Represents a <a HREF="http://www.json.org">JSON</a> value.
156 *
157 * This class is a discriminated union wrapper that can represents a:
158 * - signed integer [range: Value::minInt - Value::maxInt]
159 * - unsigned integer (range: 0 - Value::maxUInt)
160 * - double
161 * - UTF-8 string
162 * - boolean
163 * - 'null'
164 * - an ordered list of Value
165 * - collection of name/value pairs (javascript object)
166 *
167 * The type of the held value is represented by a #ValueType and
168 * can be obtained using type().
169 *
Christopher Dunnc28610f2015-02-21 11:44:16 -0600170 * Values of an #objectValue or #arrayValue can be accessed using operator[]()
171 * methods.
172 * Non-const methods will automatically create the a #nullValue element
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000173 * if it does not exist.
Christopher Dunnc28610f2015-02-21 11:44:16 -0600174 * The sequence of an #arrayValue will be automatically resized and initialized
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000175 * with #nullValue. resize() can be used to enlarge or truncate an #arrayValue.
176 *
Christopher Dunnc28610f2015-02-21 11:44:16 -0600177 * The get() methods can be used to obtain default value in the case the
178 * required element does not exist.
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000179 *
Mathias L. Baumann08ddeed2018-12-12 17:59:43 +0100180 * It is possible to iterate over the list of member keys of an object using
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000181 * the getMemberNames() method.
Christopher Dunn25342ba2015-03-02 18:05:26 -0600182 *
Christopher Dunnc28610f2015-02-21 11:44:16 -0600183 * \note #Value string-length fit in size_t, but keys must be < 2^30.
184 * (The reason is an implementation detail.) A #CharReader will raise an
Christopher Dunn25342ba2015-03-02 18:05:26 -0600185 * exception if a bound is exceeded to avoid security holes in your app,
186 * but the Value API does *not* check bounds. That is the responsibility
187 * of the caller.
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000188 */
189class JSON_API Value {
190 friend class ValueIteratorBase;
Billy Donahueb5e1fe82018-05-20 16:55:27 -0400191
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000192public:
Billy Donahue1c2ed7a2019-01-17 16:35:29 -0500193 typedef std::vector<String> Members;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000194 typedef ValueIterator iterator;
195 typedef ValueConstIterator const_iterator;
196 typedef Json::UInt UInt;
197 typedef Json::Int Int;
198#if defined(JSON_HAS_INT64)
199 typedef Json::UInt64 UInt64;
200 typedef Json::Int64 Int64;
Baptiste Lepilleur842e9ac2010-12-27 17:45:23 +0000201#endif // defined(JSON_HAS_INT64)
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000202 typedef Json::LargestInt LargestInt;
203 typedef Json::LargestUInt LargestUInt;
204 typedef Json::ArrayIndex ArrayIndex;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000205
Wolfram Röslerff6b4492017-09-14 09:31:36 +0200206 // Required for boost integration, e. g. BOOST_TEST
207 typedef std::string value_type;
208
Billy Donahueb5e1fe82018-05-20 16:55:27 -0400209 static const Value& null; ///< We regret this reference to a global instance;
210 ///< prefer the simpler Value().
211 static const Value& nullRef; ///< just a kludge for binary-compatibility; same
212 ///< as null
Christopher Dunn0f288ae2016-06-26 18:47:43 -0500213 static Value const& nullSingleton(); ///< Prefer this to null or nullRef.
214
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000215 /// Minimum signed integer value that can be stored in a Json::Value.
216 static const LargestInt minLargestInt;
217 /// Maximum signed integer value that can be stored in a Json::Value.
218 static const LargestInt maxLargestInt;
219 /// Maximum unsigned integer value that can be stored in a Json::Value.
220 static const LargestUInt maxLargestUInt;
Baptiste Lepilleur842e9ac2010-12-27 17:45:23 +0000221
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000222 /// Minimum signed int value that can be stored in a Json::Value.
223 static const Int minInt;
224 /// Maximum signed int value that can be stored in a Json::Value.
225 static const Int maxInt;
226 /// Maximum unsigned int value that can be stored in a Json::Value.
227 static const UInt maxUInt;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000228
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000229#if defined(JSON_HAS_INT64)
230 /// Minimum signed 64 bits int value that can be stored in a Json::Value.
231 static const Int64 minInt64;
232 /// Maximum signed 64 bits int value that can be stored in a Json::Value.
233 static const Int64 maxInt64;
234 /// Maximum unsigned 64 bits int value that can be stored in a Json::Value.
235 static const UInt64 maxUInt64;
Aaron Jacobsf1053e72011-05-24 03:18:02 +0000236#endif // defined(JSON_HAS_INT64)
Baptiste Lepilleur842e9ac2010-12-27 17:45:23 +0000237
Mike Ra07fc532018-03-13 23:35:31 +0300238 /// Default precision for real value for string representation.
239 static const UInt defaultRealPrecision;
240
Darcy Beurle798f6ba2017-12-22 22:48:20 +0100241// Workaround for bug in the NVIDIAs CUDA 9.1 nvcc compiler
242// when using gcc and clang backend compilers. CZString
243// cannot be defined as private. See issue #486
244#ifdef __NVCC__
245public:
246#else
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000247private:
Darcy Beurle798f6ba2017-12-22 22:48:20 +0100248#endif
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000249#ifndef JSONCPP_DOC_EXCLUDE_IMPLEMENTATION
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000250 class CZString {
251 public:
Billy Donahueb5e1fe82018-05-20 16:55:27 -0400252 enum DuplicationPolicy { noDuplication = 0, duplicate, duplicateOnCopy };
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000253 CZString(ArrayIndex index);
Christopher Dunnc28610f2015-02-21 11:44:16 -0600254 CZString(char const* str, unsigned length, DuplicationPolicy allocate);
255 CZString(CZString const& other);
Motti2b008912015-04-20 17:44:47 +0300256 CZString(CZString&& other);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000257 ~CZString();
Dhruv Paranjape0ba8bd72017-07-08 17:47:13 +0530258 CZString& operator=(const CZString& other);
Dhruv Paranjape0ba8bd72017-07-08 17:47:13 +0530259 CZString& operator=(CZString&& other);
Dhruv Paranjape0ba8bd72017-07-08 17:47:13 +0530260
Christopher Dunnc28610f2015-02-21 11:44:16 -0600261 bool operator<(CZString const& other) const;
262 bool operator==(CZString const& other) const;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000263 ArrayIndex index() const;
Billy Donahueb5e1fe82018-05-20 16:55:27 -0400264 // const char* c_str() const; ///< \deprecated
Christopher Dunnc28610f2015-02-21 11:44:16 -0600265 char const* data() const;
266 unsigned length() const;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000267 bool isStaticString() const;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000268
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000269 private:
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000270 void swap(CZString& other);
Christopher Dunnc28610f2015-02-21 11:44:16 -0600271
Christopher Dunn57ad0512015-03-02 12:10:35 -0600272 struct StringStorage {
Billy Donahueb5e1fe82018-05-20 16:55:27 -0400273 unsigned policy_ : 2;
274 unsigned length_ : 30; // 1GB max
Christopher Dunn57ad0512015-03-02 12:10:35 -0600275 };
276
Billy Donahueb5e1fe82018-05-20 16:55:27 -0400277 char const* cstr_; // actually, a prefixed string, unless policy is noDup
Christopher Dunn57ad0512015-03-02 12:10:35 -0600278 union {
279 ArrayIndex index_;
280 StringStorage storage_;
281 };
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000282 };
283
284public:
285#ifndef JSON_USE_CPPTL_SMALLMAP
286 typedef std::map<CZString, Value> ObjectValues;
287#else
288 typedef CppTL::SmallMap<CZString, Value> ObjectValues;
289#endif // ifndef JSON_USE_CPPTL_SMALLMAP
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000290#endif // ifndef JSONCPP_DOC_EXCLUDE_IMPLEMENTATION
291
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000292public:
293 /** \brief Create a default Value of the given type.
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000294
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000295 This is a very useful constructor.
296 To create an empty array, pass arrayValue.
297 To create an empty object, pass objectValue.
298 Another Value can then be set to this one by assignment.
299This is useful since clear() and resize() will not alter types.
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000300
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000301 Examples:
302\code
303Json::Value null_value; // null
304Json::Value arr_value(Json::arrayValue); // []
305Json::Value obj_value(Json::objectValue); // {}
306\endcode
307 */
308 Value(ValueType type = nullValue);
309 Value(Int value);
310 Value(UInt value);
Baptiste Lepilleur842e9ac2010-12-27 17:45:23 +0000311#if defined(JSON_HAS_INT64)
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000312 Value(Int64 value);
313 Value(UInt64 value);
Baptiste Lepilleur842e9ac2010-12-27 17:45:23 +0000314#endif // if defined(JSON_HAS_INT64)
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000315 Value(double value);
Christopher Dunn8a702972015-03-03 10:38:27 -0600316 Value(const char* value); ///< Copy til first 0. (NULL causes to seg-fault.)
Christopher Dunn89704032015-07-11 12:09:59 -0500317 Value(const char* begin, const char* end); ///< Copy all, incl zeroes.
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000318 /** \brief Constructs a value from a static string.
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000319
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000320 * Like other value string constructor but do not duplicate the string for
321 * internal storage. The given string must remain alive after the call to this
322 * constructor.
Christopher Dunnc28610f2015-02-21 11:44:16 -0600323 * \note This works only for null-terminated strings. (We cannot change the
324 * size of this class, so we have nowhere to store the length,
325 * which might be computed later for various operations.)
326 *
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000327 * Example of usage:
328 * \code
Christopher Dunnc28610f2015-02-21 11:44:16 -0600329 * static StaticString foo("some text");
330 * Json::Value aValue(foo);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000331 * \endcode
332 */
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000333 Value(const StaticString& value);
Billy Donahue1c2ed7a2019-01-17 16:35:29 -0500334 Value(const String& value); ///< Copy data() til size(). Embedded
335 ///< zeroes too.
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000336#ifdef JSON_USE_CPPTL
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000337 Value(const CppTL::ConstString& value);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000338#endif
339 Value(bool value);
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000340 Value(const Value& other);
Motti2b008912015-04-20 17:44:47 +0300341 Value(Value&& other);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000342 ~Value();
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000343
Billy Donahue0c1cc6e2019-01-20 23:59:16 -0500344 /// \note Overwrite existing comments. To preserve comments, use
Billy Donahueb5e1fe82018-05-20 16:55:27 -0400345 /// #swapPayload().
Billy Donahue0c1cc6e2019-01-20 23:59:16 -0500346 Value& operator=(const Value& other);
347 Value& operator=(Value&& other);
Dhruv Paranjape8996c372017-07-08 17:27:07 +0530348
Christopher Dunn66eb72f2015-01-20 11:02:22 -0600349 /// Swap everything.
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000350 void swap(Value& other);
Christopher Dunn66eb72f2015-01-20 11:02:22 -0600351 /// Swap values but leave comments and source offsets in place.
352 void swapPayload(Value& other);
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000353
Dhruv Paranjape8996c372017-07-08 17:27:07 +0530354 /// copy everything.
355 void copy(const Value& other);
356 /// copy values but leave comments and source offsets in place.
357 void copyPayload(const Value& other);
358
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000359 ValueType type() const;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000360
Christopher Dunn66eb72f2015-01-20 11:02:22 -0600361 /// Compare payload only, not comments etc.
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000362 bool operator<(const Value& other) const;
363 bool operator<=(const Value& other) const;
364 bool operator>=(const Value& other) const;
365 bool operator>(const Value& other) const;
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000366 bool operator==(const Value& other) const;
367 bool operator!=(const Value& other) const;
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000368 int compare(const Value& other) const;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000369
Christopher Dunn8a702972015-03-03 10:38:27 -0600370 const char* asCString() const; ///< Embedded zeroes could cause you trouble!
dawescae564652016-03-14 19:11:02 -0500371#if JSONCPP_USING_SECURE_MEMORY
Billy Donahueb5e1fe82018-05-20 16:55:27 -0400372 unsigned getCStringLength() const; // Allows you to understand the length of
373 // the CString
dawescae564652016-03-14 19:11:02 -0500374#endif
Billy Donahue1c2ed7a2019-01-17 16:35:29 -0500375 String asString() const; ///< Embedded zeroes are possible.
Christopher Dunnc28610f2015-02-21 11:44:16 -0600376 /** Get raw char* of string-value.
377 * \return false if !string. (Seg-fault if str or end are NULL.)
378 */
Billy Donahueb5e1fe82018-05-20 16:55:27 -0400379 bool getString(char const** begin, char const** end) const;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000380#ifdef JSON_USE_CPPTL
381 CppTL::ConstString asConstString() const;
382#endif
383 Int asInt() const;
384 UInt asUInt() const;
Aaron Jacobsf1053e72011-05-24 03:18:02 +0000385#if defined(JSON_HAS_INT64)
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000386 Int64 asInt64() const;
387 UInt64 asUInt64() const;
Aaron Jacobsf1053e72011-05-24 03:18:02 +0000388#endif // if defined(JSON_HAS_INT64)
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000389 LargestInt asLargestInt() const;
390 LargestUInt asLargestUInt() const;
391 float asFloat() const;
392 double asDouble() const;
393 bool asBool() const;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000394
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000395 bool isNull() const;
396 bool isBool() const;
397 bool isInt() const;
398 bool isInt64() const;
399 bool isUInt() const;
400 bool isUInt64() const;
401 bool isIntegral() const;
402 bool isDouble() const;
403 bool isNumeric() const;
404 bool isString() const;
405 bool isArray() const;
406 bool isObject() const;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000407
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000408 bool isConvertibleTo(ValueType other) const;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000409
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000410 /// Number of values in array or object
411 ArrayIndex size() const;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000412
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000413 /// \brief Return true if empty array, empty object, or null;
414 /// otherwise, false.
415 bool empty() const;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000416
Wolfram Rösler90794222017-12-05 18:18:55 +0100417 /// Return !isNull()
drgler04abe382018-01-13 15:28:19 +0100418 JSONCPP_OP_EXPLICIT operator bool() const;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000419
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000420 /// Remove all object members and array elements.
421 /// \pre type() is arrayValue, objectValue, or nullValue
422 /// \post type() is unchanged
423 void clear();
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000424
Marian Klymovfc201342018-06-02 20:15:26 +0300425 /// Resize the array to newSize elements.
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000426 /// New elements are initialized to null.
427 /// May only be called on nullValue or arrayValue.
428 /// \pre type() is arrayValue or nullValue
429 /// \post type() is arrayValue
Marian Klymovfc201342018-06-02 20:15:26 +0300430 void resize(ArrayIndex newSize);
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000431
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000432 /// Access an array element (zero based index ).
433 /// If the array contains less than index element, then null value are
434 /// inserted
435 /// in the array so that its size is index+1.
436 /// (You may need to say 'value[0u]' to get your compiler to distinguish
437 /// this from the operator[] which takes a string.)
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000438 Value& operator[](ArrayIndex index);
Baptiste Lepilleurfa130ef2010-12-24 12:47:14 +0000439
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000440 /// Access an array element (zero based index ).
441 /// If the array contains less than index element, then null value are
442 /// inserted
443 /// in the array so that its size is index+1.
444 /// (You may need to say 'value[0u]' to get your compiler to distinguish
445 /// this from the operator[] which takes a string.)
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000446 Value& operator[](int index);
Baptiste Lepilleurfa130ef2010-12-24 12:47:14 +0000447
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000448 /// Access an array element (zero based index )
449 /// (You may need to say 'value[0u]' to get your compiler to distinguish
450 /// this from the operator[] which takes a string.)
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000451 const Value& operator[](ArrayIndex index) const;
Baptiste Lepilleurfa130ef2010-12-24 12:47:14 +0000452
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000453 /// Access an array element (zero based index )
454 /// (You may need to say 'value[0u]' to get your compiler to distinguish
455 /// this from the operator[] which takes a string.)
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000456 const Value& operator[](int index) const;
Baptiste Lepilleurfa130ef2010-12-24 12:47:14 +0000457
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000458 /// If the array contains at least index+1 elements, returns the element
459 /// value,
460 /// otherwise returns defaultValue.
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000461 Value get(ArrayIndex index, const Value& defaultValue) const;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000462 /// Return true if index < size().
463 bool isValidIndex(ArrayIndex index) const;
464 /// \brief Append value to array at the end.
465 ///
466 /// Equivalent to jsonvalue[jsonvalue.size()] = value;
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000467 Value& append(const Value& value);
Dhruv Paranjape23c44d92017-07-08 17:30:47 +0530468 Value& append(Value&& value);
Dhruv Paranjape23c44d92017-07-08 17:30:47 +0530469
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000470 /// Access an object value by name, create a null member if it does not exist.
Christopher Dunn25342ba2015-03-02 18:05:26 -0600471 /// \note Because of our implementation, keys are limited to 2^30 -1 chars.
472 /// Exceeding that will cause an exception.
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000473 Value& operator[](const char* key);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000474 /// Access an object value by name, returns null if there is no member with
475 /// that name.
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000476 const Value& operator[](const char* key) const;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000477 /// Access an object value by name, create a null member if it does not exist.
Christopher Dunn25342ba2015-03-02 18:05:26 -0600478 /// \param key may contain embedded nulls.
Billy Donahue1c2ed7a2019-01-17 16:35:29 -0500479 Value& operator[](const String& key);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000480 /// Access an object value by name, returns null if there is no member with
481 /// that name.
Christopher Dunn25342ba2015-03-02 18:05:26 -0600482 /// \param key may contain embedded nulls.
Billy Donahue1c2ed7a2019-01-17 16:35:29 -0500483 const Value& operator[](const String& key) const;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000484 /** \brief Access an object value by name, create a null member if it does not
485 exist.
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000486
Billy Donahueb5e1fe82018-05-20 16:55:27 -0400487 * If the object has no entry for that name, then the member name used to
488 store
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000489 * the new entry is not duplicated.
490 * Example of use:
491 * \code
492 * Json::Value object;
493 * static const StaticString code("code");
494 * object[code] = 1234;
495 * \endcode
496 */
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000497 Value& operator[](const StaticString& key);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000498#ifdef JSON_USE_CPPTL
499 /// Access an object value by name, create a null member if it does not exist.
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000500 Value& operator[](const CppTL::ConstString& key);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000501 /// Access an object value by name, returns null if there is no member with
502 /// that name.
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000503 const Value& operator[](const CppTL::ConstString& key) const;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000504#endif
505 /// Return the member named key if it exist, defaultValue otherwise.
Christopher Dunn0fd28752015-03-05 16:38:43 -0600506 /// \note deep copy
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000507 Value get(const char* key, const Value& defaultValue) const;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000508 /// Return the member named key if it exist, defaultValue otherwise.
Christopher Dunn0fd28752015-03-05 16:38:43 -0600509 /// \note deep copy
Christopher Dunn89704032015-07-11 12:09:59 -0500510 /// \note key may contain embedded nulls.
Billy Donahueb5e1fe82018-05-20 16:55:27 -0400511 Value
512 get(const char* begin, const char* end, const Value& defaultValue) const;
Christopher Dunn25342ba2015-03-02 18:05:26 -0600513 /// Return the member named key if it exist, defaultValue otherwise.
Christopher Dunn0fd28752015-03-05 16:38:43 -0600514 /// \note deep copy
Christopher Dunn25342ba2015-03-02 18:05:26 -0600515 /// \param key may contain embedded nulls.
Billy Donahue1c2ed7a2019-01-17 16:35:29 -0500516 Value get(const String& key, const Value& defaultValue) const;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000517#ifdef JSON_USE_CPPTL
518 /// Return the member named key if it exist, defaultValue otherwise.
Christopher Dunn0fd28752015-03-05 16:38:43 -0600519 /// \note deep copy
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000520 Value get(const CppTL::ConstString& key, const Value& defaultValue) const;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000521#endif
Christopher Dunn25342ba2015-03-02 18:05:26 -0600522 /// Most general and efficient version of isMember()const, get()const,
523 /// and operator[]const
Christopher Dunn89704032015-07-11 12:09:59 -0500524 /// \note As stated elsewhere, behavior is undefined if (end-begin) >= 2^30
525 Value const* find(char const* begin, char const* end) const;
Christopher Dunnc28610f2015-02-21 11:44:16 -0600526 /// Most general and efficient version of object-mutators.
Christopher Dunn89704032015-07-11 12:09:59 -0500527 /// \note As stated elsewhere, behavior is undefined if (end-begin) >= 2^30
Christopher Dunnc28610f2015-02-21 11:44:16 -0600528 /// \return non-zero, but JSON_ASSERT if this is neither object nor nullValue.
Frank Richterd76fe562019-03-23 14:31:06 +0100529 Value* demand(char const* begin, char const* end);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000530 /// \brief Remove and return the named member.
531 ///
532 /// Do nothing if it did not exist.
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000533 /// \pre type() is objectValue or nullValue
534 /// \post type() is unchanged
Wolfram Röslera06b3902017-10-18 07:19:27 +0200535 void removeMember(const char* key);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000536 /// Same as removeMember(const char*)
Christopher Dunn25342ba2015-03-02 18:05:26 -0600537 /// \param key may contain embedded nulls.
Billy Donahue1c2ed7a2019-01-17 16:35:29 -0500538 void removeMember(const String& key);
Christopher Dunn89704032015-07-11 12:09:59 -0500539 /// Same as removeMember(const char* begin, const char* end, Value* removed),
Christopher Dunn25342ba2015-03-02 18:05:26 -0600540 /// but 'key' is null-terminated.
541 bool removeMember(const char* key, Value* removed);
Christopher Dunn76746b02015-01-21 16:01:30 -0600542 /** \brief Remove the named map member.
543
544 Update 'removed' iff removed.
Christopher Dunn25342ba2015-03-02 18:05:26 -0600545 \param key may contain embedded nulls.
Christopher Dunn76746b02015-01-21 16:01:30 -0600546 \return true iff removed (no exceptions)
547 */
Billy Donahue1c2ed7a2019-01-17 16:35:29 -0500548 bool removeMember(String const& key, Value* removed);
549 /// Same as removeMember(String const& key, Value* removed)
Christopher Dunn89704032015-07-11 12:09:59 -0500550 bool removeMember(const char* begin, const char* end, Value* removed);
Christopher Dunn9de2c2d2015-01-20 16:15:40 -0600551 /** \brief Remove the indexed array element.
552
553 O(n) expensive operations.
YantaoZhaoe32ee472018-07-03 21:29:18 +0800554 Update 'removed' iff removed.
Marian Klymovfc201342018-06-02 20:15:26 +0300555 \return true if removed (no exceptions)
Christopher Dunn9de2c2d2015-01-20 16:15:40 -0600556 */
Marian Klymovfc201342018-06-02 20:15:26 +0300557 bool removeIndex(ArrayIndex index, Value* removed);
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000558
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000559 /// Return true if the object has a member named key.
Christopher Dunnc28610f2015-02-21 11:44:16 -0600560 /// \note 'key' must be null-terminated.
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000561 bool isMember(const char* key) const;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000562 /// Return true if the object has a member named key.
Christopher Dunn25342ba2015-03-02 18:05:26 -0600563 /// \param key may contain embedded nulls.
Billy Donahue1c2ed7a2019-01-17 16:35:29 -0500564 bool isMember(const String& key) const;
565 /// Same as isMember(String const& key)const
Christopher Dunn89704032015-07-11 12:09:59 -0500566 bool isMember(const char* begin, const char* end) const;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000567#ifdef JSON_USE_CPPTL
568 /// Return true if the object has a member named key.
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000569 bool isMember(const CppTL::ConstString& key) const;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000570#endif
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000571
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000572 /// \brief Return a list of the member names.
573 ///
574 /// If null, return an empty list.
575 /// \pre type() is objectValue or nullValue
576 /// \post if type() was nullValue, it remains nullValue
577 Members getMemberNames() const;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000578
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000579 //# ifdef JSON_USE_CPPTL
580 // EnumMemberNames enumMemberNames() const;
581 // EnumValues enumValues() const;
582 //# endif
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000583
Christopher Dunn1e3149a2015-01-25 14:16:13 -0600584 /// \deprecated Always pass len.
Billy Donahue1c2ed7a2019-01-17 16:35:29 -0500585 JSONCPP_DEPRECATED("Use setComment(String const&) instead.")
Billy Donahue433107f2019-01-20 21:53:01 -0500586 void setComment(const char* comment, CommentPlacement placement) {
587 setComment(String(comment, strlen(comment)), placement);
588 }
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000589 /// Comments must be //... or /* ... */
Billy Donahue433107f2019-01-20 21:53:01 -0500590 void setComment(const char* comment, size_t len, CommentPlacement placement) {
591 setComment(String(comment, len), placement);
592 }
Christopher Dunn1e3149a2015-01-25 14:16:13 -0600593 /// Comments must be //... or /* ... */
Billy Donahue433107f2019-01-20 21:53:01 -0500594 void setComment(String comment, CommentPlacement placement);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000595 bool hasComment(CommentPlacement placement) const;
596 /// Include delimiters and embedded newlines.
Billy Donahue1c2ed7a2019-01-17 16:35:29 -0500597 String getComment(CommentPlacement placement) const;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000598
Billy Donahue1c2ed7a2019-01-17 16:35:29 -0500599 String toStyledString() const;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000600
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000601 const_iterator begin() const;
602 const_iterator end() const;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000603
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000604 iterator begin();
605 iterator end();
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000606
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000607 // Accessors for the [start, limit) range of bytes within the JSON text from
608 // which this value was parsed, if any.
Christopher Dunnd4513fc2016-02-06 09:25:20 -0600609 void setOffsetStart(ptrdiff_t start);
610 void setOffsetLimit(ptrdiff_t limit);
611 ptrdiff_t getOffsetStart() const;
612 ptrdiff_t getOffsetLimit() const;
Aaron Jacobs68db6552014-04-23 23:41:12 +0000613
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000614private:
Jordan Baylesd5bd1a72019-06-24 14:06:45 -0700615 void setType(ValueType v) {
616 bits_.value_type_ = static_cast<unsigned char>(v);
617 }
Billy Donahue0c1cc6e2019-01-20 23:59:16 -0500618 bool isAllocated() const { return bits_.allocated_; }
619 void setIsAllocated(bool v) { bits_.allocated_ = v; }
620
Billy Donahue8eb5d892014-11-10 01:35:42 -0500621 void initBasic(ValueType type, bool allocated = false);
Andrey Okoshkin9b569c82018-01-12 15:59:20 +0300622 void dupPayload(const Value& other);
Andrey Okoshkinc69148c2018-01-12 11:26:34 +0300623 void releasePayload();
Andrey Okoshkin9b569c82018-01-12 15:59:20 +0300624 void dupMeta(const Value& other);
Billy Donahue8eb5d892014-11-10 01:35:42 -0500625
Christopher Dunnc28610f2015-02-21 11:44:16 -0600626 Value& resolveReference(const char* key);
627 Value& resolveReference(const char* key, const char* end);
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000628
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000629 // struct MemberNamesTransform
630 //{
631 // typedef const char *result_type;
632 // const char *operator()( const CZString &name ) const
633 // {
634 // return name.c_str();
635 // }
636 //};
637
638 union ValueHolder {
639 LargestInt int_;
640 LargestUInt uint_;
641 double real_;
642 bool bool_;
Billy Donahue0c1cc6e2019-01-20 23:59:16 -0500643 char* string_; // if allocated_, ptr to { unsigned, char[] }.
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000644 ObjectValues* map_;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000645 } value_;
Billy Donahue0c1cc6e2019-01-20 23:59:16 -0500646
647 struct {
648 // Really a ValueType, but types should agree for bitfield packing.
649 unsigned int value_type_ : 8;
650 // Unless allocated_, string_ must be null-terminated.
651 unsigned int allocated_ : 1;
652 } bits_;
653
Billy Donahue433107f2019-01-20 21:53:01 -0500654 class Comments {
655 public:
656 Comments() = default;
657 Comments(const Comments& that);
Billy Donahue00558b32019-01-21 16:42:25 -0500658 Comments(Comments&& that);
Billy Donahue433107f2019-01-20 21:53:01 -0500659 Comments& operator=(const Comments& that);
Billy Donahue00558b32019-01-21 16:42:25 -0500660 Comments& operator=(Comments&& that);
Billy Donahue433107f2019-01-20 21:53:01 -0500661 bool has(CommentPlacement slot) const;
662 String get(CommentPlacement slot) const;
663 void set(CommentPlacement slot, String s);
664
665 private:
666 using Array = std::array<String, numberOfCommentPlacement>;
667 std::unique_ptr<Array> ptr_;
668 };
669 Comments comments_;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000670
671 // [start, limit) byte offsets in the source JSON text from which this Value
672 // was extracted.
Christopher Dunnd4513fc2016-02-06 09:25:20 -0600673 ptrdiff_t start_;
674 ptrdiff_t limit_;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000675};
676
677/** \brief Experimental and untested: represents an element of the "path" to
678 * access a node.
679 */
680class JSON_API PathArgument {
681public:
682 friend class Path;
683
684 PathArgument();
685 PathArgument(ArrayIndex index);
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000686 PathArgument(const char* key);
Billy Donahue1c2ed7a2019-01-17 16:35:29 -0500687 PathArgument(const String& key);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000688
689private:
Billy Donahueb5e1fe82018-05-20 16:55:27 -0400690 enum Kind { kindNone = 0, kindIndex, kindKey };
Billy Donahue1c2ed7a2019-01-17 16:35:29 -0500691 String key_;
Hans Johnsone817e4f2019-01-14 17:09:22 -0600692 ArrayIndex index_{};
Billy Donahue2b593a92019-01-18 03:46:57 -0500693 Kind kind_{kindNone};
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000694};
695
696/** \brief Experimental and untested: represents a "path" to access a node.
697 *
698 * Syntax:
699 * - "." => root node
700 * - ".[n]" => elements at index 'n' of root node (an array value)
701 * - ".name" => member named 'name' of root node (an object value)
702 * - ".name1.name2.name3"
703 * - ".[0][1][2].name1[3]"
704 * - ".%" => member name is provided as parameter
705 * - ".[%]" => index is provied as parameter
706 */
707class JSON_API Path {
708public:
Billy Donahue1c2ed7a2019-01-17 16:35:29 -0500709 Path(const String& path,
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000710 const PathArgument& a1 = PathArgument(),
711 const PathArgument& a2 = PathArgument(),
712 const PathArgument& a3 = PathArgument(),
713 const PathArgument& a4 = PathArgument(),
714 const PathArgument& a5 = PathArgument());
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000715
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000716 const Value& resolve(const Value& root) const;
717 Value resolve(const Value& root, const Value& defaultValue) const;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000718 /// Creates the "path" to access the specified node and returns a reference on
719 /// the node.
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000720 Value& make(Value& root) const;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000721
722private:
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000723 typedef std::vector<const PathArgument*> InArgs;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000724 typedef std::vector<PathArgument> Args;
725
Billy Donahue1c2ed7a2019-01-17 16:35:29 -0500726 void makePath(const String& path, const InArgs& in);
727 void addPathInArg(const String& path,
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000728 const InArgs& in,
729 InArgs::const_iterator& itInArg,
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000730 PathArgument::Kind kind);
Billy Donahue1c2ed7a2019-01-17 16:35:29 -0500731 static void invalidPath(const String& path, int location);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000732
733 Args args_;
734};
735
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000736/** \brief base class for Value iterators.
737 *
738 */
739class JSON_API ValueIteratorBase {
740public:
741 typedef std::bidirectional_iterator_tag iterator_category;
742 typedef unsigned int size_t;
743 typedef int difference_type;
744 typedef ValueIteratorBase SelfType;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000745
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000746 bool operator==(const SelfType& other) const { return isEqual(other); }
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000747
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000748 bool operator!=(const SelfType& other) const { return !isEqual(other); }
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000749
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000750 difference_type operator-(const SelfType& other) const {
Kevin Grant4c5832a2015-02-14 20:53:35 -0800751 return other.computeDistance(*this);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000752 }
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000753
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000754 /// Return either the index or the member name of the referenced value as a
755 /// Value.
756 Value key() const;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000757
Billy Donahueb5e1fe82018-05-20 16:55:27 -0400758 /// Return the index of the referenced Value, or -1 if it is not an
759 /// arrayValue.
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000760 UInt index() const;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000761
Christopher Dunned495ed2015-03-08 14:01:28 -0500762 /// Return the member name of the referenced Value, or "" if it is not an
763 /// objectValue.
764 /// \note Avoid `c_str()` on result, as embedded zeroes are possible.
Billy Donahue1c2ed7a2019-01-17 16:35:29 -0500765 String name() const;
Christopher Dunned495ed2015-03-08 14:01:28 -0500766
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000767 /// Return the member name of the referenced Value. "" if it is not an
768 /// objectValue.
Billy Donahueb5e1fe82018-05-20 16:55:27 -0400769 /// \deprecated This cannot be used for UTF-8 strings, since there can be
770 /// embedded nulls.
Christopher Dunned495ed2015-03-08 14:01:28 -0500771 JSONCPP_DEPRECATED("Use `key = name();` instead.")
Christopher Dunnc28610f2015-02-21 11:44:16 -0600772 char const* memberName() const;
773 /// Return the member name of the referenced Value, or NULL if it is not an
774 /// objectValue.
Christopher Dunned495ed2015-03-08 14:01:28 -0500775 /// \note Better version than memberName(). Allows embedded nulls.
Christopher Dunnc28610f2015-02-21 11:44:16 -0600776 char const* memberName(char const** end) const;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000777
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000778protected:
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000779 Value& deref() const;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000780
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000781 void increment();
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000782
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000783 void decrement();
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000784
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000785 difference_type computeDistance(const SelfType& other) const;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000786
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000787 bool isEqual(const SelfType& other) const;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000788
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000789 void copy(const SelfType& other);
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000790
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000791private:
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000792 Value::ObjectValues::iterator current_;
793 // Indicates that iterator is for a null value.
Billy Donahue2b593a92019-01-18 03:46:57 -0500794 bool isNull_{true};
Christopher Dunn2a10f4a2015-04-28 04:55:12 +0100795
796public:
797 // For some reason, BORLAND needs these at the end, rather
798 // than earlier. No idea why.
799 ValueIteratorBase();
800 explicit ValueIteratorBase(const Value::ObjectValues::iterator& current);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000801};
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000802
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000803/** \brief const iterator for object and array value.
804 *
805 */
806class JSON_API ValueConstIterator : public ValueIteratorBase {
807 friend class Value;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000808
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000809public:
810 typedef const Value value_type;
Billy Donahueb5e1fe82018-05-20 16:55:27 -0400811 // typedef unsigned int size_t;
812 // typedef int difference_type;
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000813 typedef const Value& reference;
814 typedef const Value* pointer;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000815 typedef ValueConstIterator SelfType;
816
817 ValueConstIterator();
ycqiuc8a8cfc2015-10-06 16:46:19 +0800818 ValueConstIterator(ValueIterator const& other);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000819
820private:
Billy Donahueb5e1fe82018-05-20 16:55:27 -0400821 /*! \internal Use by Value to create an iterator.
822 */
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000823 explicit ValueConstIterator(const Value::ObjectValues::iterator& current);
Billy Donahueb5e1fe82018-05-20 16:55:27 -0400824
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000825public:
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000826 SelfType& operator=(const ValueIteratorBase& other);
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000827
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000828 SelfType operator++(int) {
829 SelfType temp(*this);
830 ++*this;
831 return temp;
832 }
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000833
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000834 SelfType operator--(int) {
835 SelfType temp(*this);
836 --*this;
837 return temp;
838 }
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000839
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000840 SelfType& operator--() {
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000841 decrement();
842 return *this;
843 }
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000844
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000845 SelfType& operator++() {
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000846 increment();
847 return *this;
848 }
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000849
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000850 reference operator*() const { return deref(); }
Braden McDorman540db3b2014-09-14 02:31:23 -0500851
852 pointer operator->() const { return &deref(); }
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000853};
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000854
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000855/** \brief Iterator for object and array value.
856 */
857class JSON_API ValueIterator : public ValueIteratorBase {
858 friend class Value;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000859
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000860public:
861 typedef Value value_type;
862 typedef unsigned int size_t;
863 typedef int difference_type;
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000864 typedef Value& reference;
865 typedef Value* pointer;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000866 typedef ValueIterator SelfType;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000867
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000868 ValueIterator();
ycqiuc8a8cfc2015-10-06 16:46:19 +0800869 explicit ValueIterator(const ValueConstIterator& other);
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000870 ValueIterator(const ValueIterator& other);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000871
872private:
Billy Donahueb5e1fe82018-05-20 16:55:27 -0400873 /*! \internal Use by Value to create an iterator.
874 */
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000875 explicit ValueIterator(const Value::ObjectValues::iterator& current);
Billy Donahueb5e1fe82018-05-20 16:55:27 -0400876
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000877public:
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000878 SelfType& operator=(const SelfType& other);
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000879
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000880 SelfType operator++(int) {
881 SelfType temp(*this);
882 ++*this;
883 return temp;
884 }
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000885
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000886 SelfType operator--(int) {
887 SelfType temp(*this);
888 --*this;
889 return temp;
890 }
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000891
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000892 SelfType& operator--() {
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000893 decrement();
894 return *this;
895 }
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000896
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000897 SelfType& operator++() {
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000898 increment();
899 return *this;
900 }
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000901
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000902 reference operator*() const { return deref(); }
Braden McDorman540db3b2014-09-14 02:31:23 -0500903
904 pointer operator->() const { return &deref(); }
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000905};
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000906
Billy Donahue1d956282018-03-06 12:51:58 -0500907inline void swap(Value& a, Value& b) { a.swap(b); }
908
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000909} // namespace Json
910
Sergiy80d6e666f2016-12-03 22:29:14 +0200911#pragma pack(pop)
datadiode9454e682015-01-20 15:25:04 -0600912
Baptiste Lepilleureafd7022013-05-08 20:21:11 +0000913#if defined(JSONCPP_DISABLE_DLL_INTERFACE_WARNING)
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000914#pragma warning(pop)
Baptiste Lepilleureafd7022013-05-08 20:21:11 +0000915#endif // if defined(JSONCPP_DISABLE_DLL_INTERFACE_WARNING)
916
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000917#endif // CPPTL_JSON_H_INCLUDED