blob: a9d9ea11542cc78a956303d82d1afa1a5cdf8674 [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
Aaron Jacobs9fa4e842014-07-01 08:48:54 +100027// Disable warning C4251: <data member>: <type> needs to have dll-interface to
28// be used by...
Baptiste Lepilleureafd7022013-05-08 20:21:11 +000029#if defined(JSONCPP_DISABLE_DLL_INTERFACE_WARNING)
Aaron Jacobs9fa4e842014-07-01 08:48:54 +100030#pragma warning(push)
31#pragma warning(disable : 4251)
Baptiste Lepilleureafd7022013-05-08 20:21:11 +000032#endif // if defined(JSONCPP_DISABLE_DLL_INTERFACE_WARNING)
33
Sergiy80d6e666f2016-12-03 22:29:14 +020034#pragma pack(push, 8)
35
Christopher Dunn6d135cb2007-06-13 15:51:04 +000036/** \brief JSON (JavaScript Object Notation).
37 */
38namespace Json {
39
Jordan Bayles83cc9212019-06-06 13:41:47 -070040#if JSON_USE_EXCEPTION
Christopher Dunn75279cc2015-03-08 12:20:06 -050041/** Base class for all exceptions we throw.
Christopher Dunn4e30c4f2015-03-08 12:56:32 -050042 *
43 * We use nothing but these internally. Of course, STL can throw others.
Christopher Dunn75279cc2015-03-08 12:20:06 -050044 */
Christopher Dunn949babd2015-07-23 00:19:12 -050045class JSON_API Exception : public std::exception {
46public:
Billy Donahue1c2ed7a2019-01-17 16:35:29 -050047 Exception(String msg);
Hans Johnson2853b1c2019-01-11 13:58:53 -060048 ~Exception() JSONCPP_NOEXCEPT override;
49 char const* what() const JSONCPP_NOEXCEPT override;
Billy Donahueb5e1fe82018-05-20 16:55:27 -040050
Christopher Dunn949babd2015-07-23 00:19:12 -050051protected:
Billy Donahue1c2ed7a2019-01-17 16:35:29 -050052 String msg_;
Christopher Dunn949babd2015-07-23 00:19:12 -050053};
54
Christopher Dunn53837942015-03-08 12:31:00 -050055/** Exceptions which the user cannot easily avoid.
56 *
Christopher Dunn4e30c4f2015-03-08 12:56:32 -050057 * E.g. out-of-memory (when we use malloc), stack-overflow, malicious input
Dhruv Paranjape8996c372017-07-08 17:27:07 +053058 *
Christopher Dunn4e30c4f2015-03-08 12:56:32 -050059 * \remark derived from Json::Exception
Christopher Dunn53837942015-03-08 12:31:00 -050060 */
Christopher Dunn949babd2015-07-23 00:19:12 -050061class JSON_API RuntimeError : public Exception {
62public:
Billy Donahue1c2ed7a2019-01-17 16:35:29 -050063 RuntimeError(String const& msg);
Christopher Dunn949babd2015-07-23 00:19:12 -050064};
65
Christopher Dunn4e30c4f2015-03-08 12:56:32 -050066/** Exceptions thrown by JSON_ASSERT/JSON_FAIL macros.
Christopher Dunn53837942015-03-08 12:31:00 -050067 *
68 * These are precondition-violations (user bugs) and internal errors (our bugs).
Dhruv Paranjape8996c372017-07-08 17:27:07 +053069 *
Christopher Dunn4e30c4f2015-03-08 12:56:32 -050070 * \remark derived from Json::Exception
Christopher Dunn53837942015-03-08 12:31:00 -050071 */
Christopher Dunn949babd2015-07-23 00:19:12 -050072class JSON_API LogicError : public Exception {
73public:
Billy Donahue1c2ed7a2019-01-17 16:35:29 -050074 LogicError(String const& msg);
Christopher Dunn949babd2015-07-23 00:19:12 -050075};
Jordan Bayles83cc9212019-06-06 13:41:47 -070076#endif
Christopher Dunn53837942015-03-08 12:31:00 -050077
Christopher Dunn4e30c4f2015-03-08 12:56:32 -050078/// used internally
Jordan Bayles9ef812a2019-07-09 15:03:39 -070079[[noreturn]] void throwRuntimeError(String const& msg);
Christopher Dunn4e30c4f2015-03-08 12:56:32 -050080/// used internally
Jordan Bayles9ef812a2019-07-09 15:03:39 -070081[[noreturn]] void throwLogicError(String const& msg);
Christopher Dunn75279cc2015-03-08 12:20:06 -050082
Aaron Jacobs9fa4e842014-07-01 08:48:54 +100083/** \brief Type of the value held by a Value object.
84 */
85enum ValueType {
86 nullValue = 0, ///< 'null' value
87 intValue, ///< signed integer value
88 uintValue, ///< unsigned integer value
89 realValue, ///< double value
90 stringValue, ///< UTF-8 string value
91 booleanValue, ///< bool value
92 arrayValue, ///< array value (ordered list)
93 objectValue ///< object value (collection of name/value pairs).
94};
Christopher Dunn6d135cb2007-06-13 15:51:04 +000095
Aaron Jacobs9fa4e842014-07-01 08:48:54 +100096enum CommentPlacement {
97 commentBefore = 0, ///< a comment placed on the line before a value
98 commentAfterOnSameLine, ///< a comment just after a value on the same line
99 commentAfter, ///< a comment on the line after a value (only make sense for
Aaron Jacobs3a0c4fc2014-07-01 09:20:48 +1000100 /// root value)
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000101 numberOfCommentPlacement
102};
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000103
Mike Ra07fc532018-03-13 23:35:31 +0300104/** \brief Type of precision for formatting of real values.
105 */
106enum PrecisionType {
107 significantDigits = 0, ///< we set max number of significant digits in string
108 decimalPlaces ///< we set max number of digits after "." in string
109};
110
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000111//# ifdef JSON_USE_CPPTL
112// typedef CppTL::AnyEnumerator<const char *> EnumMemberNames;
113// typedef CppTL::AnyEnumerator<const Value &> EnumValues;
114//# endif
115
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000116/** \brief Lightweight wrapper to tag static string.
117 *
Josh Sorefe6a588a2017-12-03 11:54:29 -0500118 * Value constructor and objectValue member assignment takes advantage of the
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000119 * StaticString and avoid the cost of string duplication when storing the
120 * string or the member name.
121 *
122 * Example of usage:
123 * \code
124 * Json::Value aValue( StaticString("some text") );
125 * Json::Value object;
126 * static const StaticString code("code");
127 * object[code] = 1234;
128 * \endcode
129 */
130class JSON_API StaticString {
131public:
Christopher Dunnff617522015-03-06 10:31:46 -0600132 explicit StaticString(const char* czstring) : c_str_(czstring) {}
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000133
Christopher Dunnff617522015-03-06 10:31:46 -0600134 operator const char*() const { return c_str_; }
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000135
Christopher Dunnff617522015-03-06 10:31:46 -0600136 const char* c_str() const { return c_str_; }
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000137
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000138private:
Christopher Dunnff617522015-03-06 10:31:46 -0600139 const char* c_str_;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000140};
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000141
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000142/** \brief Represents a <a HREF="http://www.json.org">JSON</a> value.
143 *
144 * This class is a discriminated union wrapper that can represents a:
145 * - signed integer [range: Value::minInt - Value::maxInt]
146 * - unsigned integer (range: 0 - Value::maxUInt)
147 * - double
148 * - UTF-8 string
149 * - boolean
150 * - 'null'
151 * - an ordered list of Value
152 * - collection of name/value pairs (javascript object)
153 *
154 * The type of the held value is represented by a #ValueType and
155 * can be obtained using type().
156 *
Christopher Dunnc28610f2015-02-21 11:44:16 -0600157 * Values of an #objectValue or #arrayValue can be accessed using operator[]()
158 * methods.
159 * Non-const methods will automatically create the a #nullValue element
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000160 * if it does not exist.
Christopher Dunnc28610f2015-02-21 11:44:16 -0600161 * The sequence of an #arrayValue will be automatically resized and initialized
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000162 * with #nullValue. resize() can be used to enlarge or truncate an #arrayValue.
163 *
Christopher Dunnc28610f2015-02-21 11:44:16 -0600164 * The get() methods can be used to obtain default value in the case the
165 * required element does not exist.
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000166 *
Mathias L. Baumann08ddeed2018-12-12 17:59:43 +0100167 * It is possible to iterate over the list of member keys of an object using
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000168 * the getMemberNames() method.
Christopher Dunn25342ba2015-03-02 18:05:26 -0600169 *
Christopher Dunnc28610f2015-02-21 11:44:16 -0600170 * \note #Value string-length fit in size_t, but keys must be < 2^30.
171 * (The reason is an implementation detail.) A #CharReader will raise an
Christopher Dunn25342ba2015-03-02 18:05:26 -0600172 * exception if a bound is exceeded to avoid security holes in your app,
173 * but the Value API does *not* check bounds. That is the responsibility
174 * of the caller.
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000175 */
176class JSON_API Value {
177 friend class ValueIteratorBase;
Billy Donahueb5e1fe82018-05-20 16:55:27 -0400178
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000179public:
Billy Donahue1c2ed7a2019-01-17 16:35:29 -0500180 typedef std::vector<String> Members;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000181 typedef ValueIterator iterator;
182 typedef ValueConstIterator const_iterator;
183 typedef Json::UInt UInt;
184 typedef Json::Int Int;
185#if defined(JSON_HAS_INT64)
186 typedef Json::UInt64 UInt64;
187 typedef Json::Int64 Int64;
Baptiste Lepilleur842e9ac2010-12-27 17:45:23 +0000188#endif // defined(JSON_HAS_INT64)
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000189 typedef Json::LargestInt LargestInt;
190 typedef Json::LargestUInt LargestUInt;
191 typedef Json::ArrayIndex ArrayIndex;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000192
Wolfram Röslerff6b4492017-09-14 09:31:36 +0200193 // Required for boost integration, e. g. BOOST_TEST
194 typedef std::string value_type;
195
Billy Donahueb5e1fe82018-05-20 16:55:27 -0400196 static const Value& null; ///< We regret this reference to a global instance;
197 ///< prefer the simpler Value().
198 static const Value& nullRef; ///< just a kludge for binary-compatibility; same
199 ///< as null
Christopher Dunn0f288ae2016-06-26 18:47:43 -0500200 static Value const& nullSingleton(); ///< Prefer this to null or nullRef.
201
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000202 /// Minimum signed integer value that can be stored in a Json::Value.
203 static const LargestInt minLargestInt;
204 /// Maximum signed integer value that can be stored in a Json::Value.
205 static const LargestInt maxLargestInt;
206 /// Maximum unsigned integer value that can be stored in a Json::Value.
207 static const LargestUInt maxLargestUInt;
Baptiste Lepilleur842e9ac2010-12-27 17:45:23 +0000208
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000209 /// Minimum signed int value that can be stored in a Json::Value.
210 static const Int minInt;
211 /// Maximum signed int value that can be stored in a Json::Value.
212 static const Int maxInt;
213 /// Maximum unsigned int value that can be stored in a Json::Value.
214 static const UInt maxUInt;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000215
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000216#if defined(JSON_HAS_INT64)
217 /// Minimum signed 64 bits int value that can be stored in a Json::Value.
218 static const Int64 minInt64;
219 /// Maximum signed 64 bits int value that can be stored in a Json::Value.
220 static const Int64 maxInt64;
221 /// Maximum unsigned 64 bits int value that can be stored in a Json::Value.
222 static const UInt64 maxUInt64;
Aaron Jacobsf1053e72011-05-24 03:18:02 +0000223#endif // defined(JSON_HAS_INT64)
Baptiste Lepilleur842e9ac2010-12-27 17:45:23 +0000224
Mike Ra07fc532018-03-13 23:35:31 +0300225 /// Default precision for real value for string representation.
226 static const UInt defaultRealPrecision;
227
Darcy Beurle798f6ba2017-12-22 22:48:20 +0100228// Workaround for bug in the NVIDIAs CUDA 9.1 nvcc compiler
229// when using gcc and clang backend compilers. CZString
230// cannot be defined as private. See issue #486
231#ifdef __NVCC__
232public:
233#else
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000234private:
Darcy Beurle798f6ba2017-12-22 22:48:20 +0100235#endif
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000236#ifndef JSONCPP_DOC_EXCLUDE_IMPLEMENTATION
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000237 class CZString {
238 public:
Billy Donahueb5e1fe82018-05-20 16:55:27 -0400239 enum DuplicationPolicy { noDuplication = 0, duplicate, duplicateOnCopy };
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000240 CZString(ArrayIndex index);
Christopher Dunnc28610f2015-02-21 11:44:16 -0600241 CZString(char const* str, unsigned length, DuplicationPolicy allocate);
242 CZString(CZString const& other);
Motti2b008912015-04-20 17:44:47 +0300243 CZString(CZString&& other);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000244 ~CZString();
Dhruv Paranjape0ba8bd72017-07-08 17:47:13 +0530245 CZString& operator=(const CZString& other);
Dhruv Paranjape0ba8bd72017-07-08 17:47:13 +0530246 CZString& operator=(CZString&& other);
Dhruv Paranjape0ba8bd72017-07-08 17:47:13 +0530247
Christopher Dunnc28610f2015-02-21 11:44:16 -0600248 bool operator<(CZString const& other) const;
249 bool operator==(CZString const& other) const;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000250 ArrayIndex index() const;
Billy Donahueb5e1fe82018-05-20 16:55:27 -0400251 // const char* c_str() const; ///< \deprecated
Christopher Dunnc28610f2015-02-21 11:44:16 -0600252 char const* data() const;
253 unsigned length() const;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000254 bool isStaticString() const;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000255
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000256 private:
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000257 void swap(CZString& other);
Christopher Dunnc28610f2015-02-21 11:44:16 -0600258
Christopher Dunn57ad0512015-03-02 12:10:35 -0600259 struct StringStorage {
Billy Donahueb5e1fe82018-05-20 16:55:27 -0400260 unsigned policy_ : 2;
261 unsigned length_ : 30; // 1GB max
Christopher Dunn57ad0512015-03-02 12:10:35 -0600262 };
263
Billy Donahueb5e1fe82018-05-20 16:55:27 -0400264 char const* cstr_; // actually, a prefixed string, unless policy is noDup
Christopher Dunn57ad0512015-03-02 12:10:35 -0600265 union {
266 ArrayIndex index_;
267 StringStorage storage_;
268 };
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000269 };
270
271public:
272#ifndef JSON_USE_CPPTL_SMALLMAP
273 typedef std::map<CZString, Value> ObjectValues;
274#else
275 typedef CppTL::SmallMap<CZString, Value> ObjectValues;
276#endif // ifndef JSON_USE_CPPTL_SMALLMAP
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000277#endif // ifndef JSONCPP_DOC_EXCLUDE_IMPLEMENTATION
278
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000279public:
280 /** \brief Create a default Value of the given type.
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000281
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000282 This is a very useful constructor.
283 To create an empty array, pass arrayValue.
284 To create an empty object, pass objectValue.
285 Another Value can then be set to this one by assignment.
286This is useful since clear() and resize() will not alter types.
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000287
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000288 Examples:
289\code
290Json::Value null_value; // null
291Json::Value arr_value(Json::arrayValue); // []
292Json::Value obj_value(Json::objectValue); // {}
293\endcode
294 */
295 Value(ValueType type = nullValue);
296 Value(Int value);
297 Value(UInt value);
Baptiste Lepilleur842e9ac2010-12-27 17:45:23 +0000298#if defined(JSON_HAS_INT64)
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000299 Value(Int64 value);
300 Value(UInt64 value);
Baptiste Lepilleur842e9ac2010-12-27 17:45:23 +0000301#endif // if defined(JSON_HAS_INT64)
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000302 Value(double value);
Christopher Dunn8a702972015-03-03 10:38:27 -0600303 Value(const char* value); ///< Copy til first 0. (NULL causes to seg-fault.)
Christopher Dunn89704032015-07-11 12:09:59 -0500304 Value(const char* begin, const char* end); ///< Copy all, incl zeroes.
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000305 /** \brief Constructs a value from a static string.
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000306
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000307 * Like other value string constructor but do not duplicate the string for
308 * internal storage. The given string must remain alive after the call to this
309 * constructor.
Christopher Dunnc28610f2015-02-21 11:44:16 -0600310 * \note This works only for null-terminated strings. (We cannot change the
311 * size of this class, so we have nowhere to store the length,
312 * which might be computed later for various operations.)
313 *
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000314 * Example of usage:
315 * \code
Christopher Dunnc28610f2015-02-21 11:44:16 -0600316 * static StaticString foo("some text");
317 * Json::Value aValue(foo);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000318 * \endcode
319 */
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000320 Value(const StaticString& value);
Billy Donahue1c2ed7a2019-01-17 16:35:29 -0500321 Value(const String& value); ///< Copy data() til size(). Embedded
322 ///< zeroes too.
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000323#ifdef JSON_USE_CPPTL
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000324 Value(const CppTL::ConstString& value);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000325#endif
326 Value(bool value);
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000327 Value(const Value& other);
Motti2b008912015-04-20 17:44:47 +0300328 Value(Value&& other);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000329 ~Value();
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000330
Billy Donahue0c1cc6e2019-01-20 23:59:16 -0500331 /// \note Overwrite existing comments. To preserve comments, use
Billy Donahueb5e1fe82018-05-20 16:55:27 -0400332 /// #swapPayload().
Billy Donahue0c1cc6e2019-01-20 23:59:16 -0500333 Value& operator=(const Value& other);
334 Value& operator=(Value&& other);
Dhruv Paranjape8996c372017-07-08 17:27:07 +0530335
Christopher Dunn66eb72f2015-01-20 11:02:22 -0600336 /// Swap everything.
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000337 void swap(Value& other);
Christopher Dunn66eb72f2015-01-20 11:02:22 -0600338 /// Swap values but leave comments and source offsets in place.
339 void swapPayload(Value& other);
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000340
Dhruv Paranjape8996c372017-07-08 17:27:07 +0530341 /// copy everything.
342 void copy(const Value& other);
343 /// copy values but leave comments and source offsets in place.
344 void copyPayload(const Value& other);
345
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000346 ValueType type() const;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000347
Christopher Dunn66eb72f2015-01-20 11:02:22 -0600348 /// Compare payload only, not comments etc.
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000349 bool operator<(const Value& other) const;
350 bool operator<=(const Value& other) const;
351 bool operator>=(const Value& other) const;
352 bool operator>(const Value& other) const;
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000353 bool operator==(const Value& other) const;
354 bool operator!=(const Value& other) const;
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000355 int compare(const Value& other) const;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000356
Christopher Dunn8a702972015-03-03 10:38:27 -0600357 const char* asCString() const; ///< Embedded zeroes could cause you trouble!
dawescae564652016-03-14 19:11:02 -0500358#if JSONCPP_USING_SECURE_MEMORY
Billy Donahueb5e1fe82018-05-20 16:55:27 -0400359 unsigned getCStringLength() const; // Allows you to understand the length of
360 // the CString
dawescae564652016-03-14 19:11:02 -0500361#endif
Billy Donahue1c2ed7a2019-01-17 16:35:29 -0500362 String asString() const; ///< Embedded zeroes are possible.
Christopher Dunnc28610f2015-02-21 11:44:16 -0600363 /** Get raw char* of string-value.
364 * \return false if !string. (Seg-fault if str or end are NULL.)
365 */
Billy Donahueb5e1fe82018-05-20 16:55:27 -0400366 bool getString(char const** begin, char const** end) const;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000367#ifdef JSON_USE_CPPTL
368 CppTL::ConstString asConstString() const;
369#endif
370 Int asInt() const;
371 UInt asUInt() const;
Aaron Jacobsf1053e72011-05-24 03:18:02 +0000372#if defined(JSON_HAS_INT64)
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000373 Int64 asInt64() const;
374 UInt64 asUInt64() const;
Aaron Jacobsf1053e72011-05-24 03:18:02 +0000375#endif // if defined(JSON_HAS_INT64)
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000376 LargestInt asLargestInt() const;
377 LargestUInt asLargestUInt() const;
378 float asFloat() const;
379 double asDouble() const;
380 bool asBool() const;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000381
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000382 bool isNull() const;
383 bool isBool() const;
384 bool isInt() const;
385 bool isInt64() const;
386 bool isUInt() const;
387 bool isUInt64() const;
388 bool isIntegral() const;
389 bool isDouble() const;
390 bool isNumeric() const;
391 bool isString() const;
392 bool isArray() const;
393 bool isObject() const;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000394
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000395 bool isConvertibleTo(ValueType other) const;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000396
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000397 /// Number of values in array or object
398 ArrayIndex size() const;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000399
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000400 /// \brief Return true if empty array, empty object, or null;
401 /// otherwise, false.
402 bool empty() const;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000403
Wolfram Rösler90794222017-12-05 18:18:55 +0100404 /// Return !isNull()
drgler04abe382018-01-13 15:28:19 +0100405 JSONCPP_OP_EXPLICIT operator bool() const;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000406
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000407 /// Remove all object members and array elements.
408 /// \pre type() is arrayValue, objectValue, or nullValue
409 /// \post type() is unchanged
410 void clear();
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000411
Marian Klymovfc201342018-06-02 20:15:26 +0300412 /// Resize the array to newSize elements.
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000413 /// New elements are initialized to null.
414 /// May only be called on nullValue or arrayValue.
415 /// \pre type() is arrayValue or nullValue
416 /// \post type() is arrayValue
Marian Klymovfc201342018-06-02 20:15:26 +0300417 void resize(ArrayIndex newSize);
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000418
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000419 /// Access an array element (zero based index ).
420 /// If the array contains less than index element, then null value are
421 /// inserted
422 /// in the array so that its size is index+1.
423 /// (You may need to say 'value[0u]' to get your compiler to distinguish
424 /// this from the operator[] which takes a string.)
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000425 Value& operator[](ArrayIndex index);
Baptiste Lepilleurfa130ef2010-12-24 12:47:14 +0000426
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000427 /// Access an array element (zero based index ).
428 /// If the array contains less than index element, then null value are
429 /// inserted
430 /// in the array so that its size is index+1.
431 /// (You may need to say 'value[0u]' to get your compiler to distinguish
432 /// this from the operator[] which takes a string.)
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000433 Value& operator[](int index);
Baptiste Lepilleurfa130ef2010-12-24 12:47:14 +0000434
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000435 /// Access an array element (zero based index )
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 const Value& operator[](ArrayIndex index) const;
Baptiste Lepilleurfa130ef2010-12-24 12:47:14 +0000439
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000440 /// Access an array element (zero based index )
441 /// (You may need to say 'value[0u]' to get your compiler to distinguish
442 /// this from the operator[] which takes a string.)
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000443 const Value& operator[](int index) const;
Baptiste Lepilleurfa130ef2010-12-24 12:47:14 +0000444
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000445 /// If the array contains at least index+1 elements, returns the element
446 /// value,
447 /// otherwise returns defaultValue.
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000448 Value get(ArrayIndex index, const Value& defaultValue) const;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000449 /// Return true if index < size().
450 bool isValidIndex(ArrayIndex index) const;
451 /// \brief Append value to array at the end.
452 ///
453 /// Equivalent to jsonvalue[jsonvalue.size()] = value;
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000454 Value& append(const Value& value);
Dhruv Paranjape23c44d92017-07-08 17:30:47 +0530455 Value& append(Value&& value);
Dhruv Paranjape23c44d92017-07-08 17:30:47 +0530456
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000457 /// Access an object value by name, create a null member if it does not exist.
Christopher Dunn25342ba2015-03-02 18:05:26 -0600458 /// \note Because of our implementation, keys are limited to 2^30 -1 chars.
459 /// Exceeding that will cause an exception.
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000460 Value& operator[](const char* key);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000461 /// Access an object value by name, returns null if there is no member with
462 /// that name.
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000463 const Value& operator[](const char* key) const;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000464 /// Access an object value by name, create a null member if it does not exist.
Christopher Dunn25342ba2015-03-02 18:05:26 -0600465 /// \param key may contain embedded nulls.
Billy Donahue1c2ed7a2019-01-17 16:35:29 -0500466 Value& operator[](const String& key);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000467 /// Access an object value by name, returns null if there is no member with
468 /// that name.
Christopher Dunn25342ba2015-03-02 18:05:26 -0600469 /// \param key may contain embedded nulls.
Billy Donahue1c2ed7a2019-01-17 16:35:29 -0500470 const Value& operator[](const String& key) const;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000471 /** \brief Access an object value by name, create a null member if it does not
472 exist.
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000473
Billy Donahueb5e1fe82018-05-20 16:55:27 -0400474 * If the object has no entry for that name, then the member name used to
475 store
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000476 * the new entry is not duplicated.
477 * Example of use:
478 * \code
479 * Json::Value object;
480 * static const StaticString code("code");
481 * object[code] = 1234;
482 * \endcode
483 */
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000484 Value& operator[](const StaticString& key);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000485#ifdef JSON_USE_CPPTL
486 /// Access an object value by name, create a null member if it does not exist.
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000487 Value& operator[](const CppTL::ConstString& key);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000488 /// Access an object value by name, returns null if there is no member with
489 /// that name.
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000490 const Value& operator[](const CppTL::ConstString& key) const;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000491#endif
492 /// Return the member named key if it exist, defaultValue otherwise.
Christopher Dunn0fd28752015-03-05 16:38:43 -0600493 /// \note deep copy
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000494 Value get(const char* key, const Value& defaultValue) const;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000495 /// Return the member named key if it exist, defaultValue otherwise.
Christopher Dunn0fd28752015-03-05 16:38:43 -0600496 /// \note deep copy
Christopher Dunn89704032015-07-11 12:09:59 -0500497 /// \note key may contain embedded nulls.
Billy Donahueb5e1fe82018-05-20 16:55:27 -0400498 Value
499 get(const char* begin, const char* end, const Value& defaultValue) const;
Christopher Dunn25342ba2015-03-02 18:05:26 -0600500 /// Return the member named key if it exist, defaultValue otherwise.
Christopher Dunn0fd28752015-03-05 16:38:43 -0600501 /// \note deep copy
Christopher Dunn25342ba2015-03-02 18:05:26 -0600502 /// \param key may contain embedded nulls.
Billy Donahue1c2ed7a2019-01-17 16:35:29 -0500503 Value get(const String& key, const Value& defaultValue) const;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000504#ifdef JSON_USE_CPPTL
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 CppTL::ConstString& key, const Value& defaultValue) const;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000508#endif
Christopher Dunn25342ba2015-03-02 18:05:26 -0600509 /// Most general and efficient version of isMember()const, get()const,
510 /// and operator[]const
Christopher Dunn89704032015-07-11 12:09:59 -0500511 /// \note As stated elsewhere, behavior is undefined if (end-begin) >= 2^30
512 Value const* find(char const* begin, char const* end) const;
Christopher Dunnc28610f2015-02-21 11:44:16 -0600513 /// Most general and efficient version of object-mutators.
Christopher Dunn89704032015-07-11 12:09:59 -0500514 /// \note As stated elsewhere, behavior is undefined if (end-begin) >= 2^30
Christopher Dunnc28610f2015-02-21 11:44:16 -0600515 /// \return non-zero, but JSON_ASSERT if this is neither object nor nullValue.
Frank Richterd76fe562019-03-23 14:31:06 +0100516 Value* demand(char const* begin, char const* end);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000517 /// \brief Remove and return the named member.
518 ///
519 /// Do nothing if it did not exist.
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000520 /// \pre type() is objectValue or nullValue
521 /// \post type() is unchanged
Wolfram Röslera06b3902017-10-18 07:19:27 +0200522 void removeMember(const char* key);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000523 /// Same as removeMember(const char*)
Christopher Dunn25342ba2015-03-02 18:05:26 -0600524 /// \param key may contain embedded nulls.
Billy Donahue1c2ed7a2019-01-17 16:35:29 -0500525 void removeMember(const String& key);
Christopher Dunn89704032015-07-11 12:09:59 -0500526 /// Same as removeMember(const char* begin, const char* end, Value* removed),
Christopher Dunn25342ba2015-03-02 18:05:26 -0600527 /// but 'key' is null-terminated.
528 bool removeMember(const char* key, Value* removed);
Christopher Dunn76746b02015-01-21 16:01:30 -0600529 /** \brief Remove the named map member.
530
531 Update 'removed' iff removed.
Christopher Dunn25342ba2015-03-02 18:05:26 -0600532 \param key may contain embedded nulls.
Christopher Dunn76746b02015-01-21 16:01:30 -0600533 \return true iff removed (no exceptions)
534 */
Billy Donahue1c2ed7a2019-01-17 16:35:29 -0500535 bool removeMember(String const& key, Value* removed);
536 /// Same as removeMember(String const& key, Value* removed)
Christopher Dunn89704032015-07-11 12:09:59 -0500537 bool removeMember(const char* begin, const char* end, Value* removed);
Christopher Dunn9de2c2d2015-01-20 16:15:40 -0600538 /** \brief Remove the indexed array element.
539
540 O(n) expensive operations.
YantaoZhaoe32ee472018-07-03 21:29:18 +0800541 Update 'removed' iff removed.
Marian Klymovfc201342018-06-02 20:15:26 +0300542 \return true if removed (no exceptions)
Christopher Dunn9de2c2d2015-01-20 16:15:40 -0600543 */
Marian Klymovfc201342018-06-02 20:15:26 +0300544 bool removeIndex(ArrayIndex index, Value* removed);
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000545
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000546 /// Return true if the object has a member named key.
Christopher Dunnc28610f2015-02-21 11:44:16 -0600547 /// \note 'key' must be null-terminated.
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000548 bool isMember(const char* key) const;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000549 /// Return true if the object has a member named key.
Christopher Dunn25342ba2015-03-02 18:05:26 -0600550 /// \param key may contain embedded nulls.
Billy Donahue1c2ed7a2019-01-17 16:35:29 -0500551 bool isMember(const String& key) const;
552 /// Same as isMember(String const& key)const
Christopher Dunn89704032015-07-11 12:09:59 -0500553 bool isMember(const char* begin, const char* end) const;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000554#ifdef JSON_USE_CPPTL
555 /// Return true if the object has a member named key.
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000556 bool isMember(const CppTL::ConstString& key) const;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000557#endif
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000558
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000559 /// \brief Return a list of the member names.
560 ///
561 /// If null, return an empty list.
562 /// \pre type() is objectValue or nullValue
563 /// \post if type() was nullValue, it remains nullValue
564 Members getMemberNames() const;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000565
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000566 //# ifdef JSON_USE_CPPTL
567 // EnumMemberNames enumMemberNames() const;
568 // EnumValues enumValues() const;
569 //# endif
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000570
Christopher Dunn1e3149a2015-01-25 14:16:13 -0600571 /// \deprecated Always pass len.
Billy Donahue1c2ed7a2019-01-17 16:35:29 -0500572 JSONCPP_DEPRECATED("Use setComment(String const&) instead.")
Billy Donahue433107f2019-01-20 21:53:01 -0500573 void setComment(const char* comment, CommentPlacement placement) {
574 setComment(String(comment, strlen(comment)), placement);
575 }
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000576 /// Comments must be //... or /* ... */
Billy Donahue433107f2019-01-20 21:53:01 -0500577 void setComment(const char* comment, size_t len, CommentPlacement placement) {
578 setComment(String(comment, len), placement);
579 }
Christopher Dunn1e3149a2015-01-25 14:16:13 -0600580 /// Comments must be //... or /* ... */
Billy Donahue433107f2019-01-20 21:53:01 -0500581 void setComment(String comment, CommentPlacement placement);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000582 bool hasComment(CommentPlacement placement) const;
583 /// Include delimiters and embedded newlines.
Billy Donahue1c2ed7a2019-01-17 16:35:29 -0500584 String getComment(CommentPlacement placement) const;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000585
Billy Donahue1c2ed7a2019-01-17 16:35:29 -0500586 String toStyledString() const;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000587
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000588 const_iterator begin() const;
589 const_iterator end() const;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000590
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000591 iterator begin();
592 iterator end();
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000593
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000594 // Accessors for the [start, limit) range of bytes within the JSON text from
595 // which this value was parsed, if any.
Christopher Dunnd4513fc2016-02-06 09:25:20 -0600596 void setOffsetStart(ptrdiff_t start);
597 void setOffsetLimit(ptrdiff_t limit);
598 ptrdiff_t getOffsetStart() const;
599 ptrdiff_t getOffsetLimit() const;
Aaron Jacobs68db6552014-04-23 23:41:12 +0000600
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000601private:
Jordan Baylesd5bd1a72019-06-24 14:06:45 -0700602 void setType(ValueType v) {
603 bits_.value_type_ = static_cast<unsigned char>(v);
604 }
Billy Donahue0c1cc6e2019-01-20 23:59:16 -0500605 bool isAllocated() const { return bits_.allocated_; }
606 void setIsAllocated(bool v) { bits_.allocated_ = v; }
607
Billy Donahue8eb5d892014-11-10 01:35:42 -0500608 void initBasic(ValueType type, bool allocated = false);
Andrey Okoshkin9b569c82018-01-12 15:59:20 +0300609 void dupPayload(const Value& other);
Andrey Okoshkinc69148c2018-01-12 11:26:34 +0300610 void releasePayload();
Andrey Okoshkin9b569c82018-01-12 15:59:20 +0300611 void dupMeta(const Value& other);
Billy Donahue8eb5d892014-11-10 01:35:42 -0500612
Christopher Dunnc28610f2015-02-21 11:44:16 -0600613 Value& resolveReference(const char* key);
614 Value& resolveReference(const char* key, const char* end);
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000615
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000616 // struct MemberNamesTransform
617 //{
618 // typedef const char *result_type;
619 // const char *operator()( const CZString &name ) const
620 // {
621 // return name.c_str();
622 // }
623 //};
624
625 union ValueHolder {
626 LargestInt int_;
627 LargestUInt uint_;
628 double real_;
629 bool bool_;
Billy Donahue0c1cc6e2019-01-20 23:59:16 -0500630 char* string_; // if allocated_, ptr to { unsigned, char[] }.
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000631 ObjectValues* map_;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000632 } value_;
Billy Donahue0c1cc6e2019-01-20 23:59:16 -0500633
634 struct {
635 // Really a ValueType, but types should agree for bitfield packing.
636 unsigned int value_type_ : 8;
637 // Unless allocated_, string_ must be null-terminated.
638 unsigned int allocated_ : 1;
639 } bits_;
640
Billy Donahue433107f2019-01-20 21:53:01 -0500641 class Comments {
642 public:
643 Comments() = default;
644 Comments(const Comments& that);
Billy Donahue00558b32019-01-21 16:42:25 -0500645 Comments(Comments&& that);
Billy Donahue433107f2019-01-20 21:53:01 -0500646 Comments& operator=(const Comments& that);
Billy Donahue00558b32019-01-21 16:42:25 -0500647 Comments& operator=(Comments&& that);
Billy Donahue433107f2019-01-20 21:53:01 -0500648 bool has(CommentPlacement slot) const;
649 String get(CommentPlacement slot) const;
650 void set(CommentPlacement slot, String s);
651
652 private:
653 using Array = std::array<String, numberOfCommentPlacement>;
654 std::unique_ptr<Array> ptr_;
655 };
656 Comments comments_;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000657
658 // [start, limit) byte offsets in the source JSON text from which this Value
659 // was extracted.
Christopher Dunnd4513fc2016-02-06 09:25:20 -0600660 ptrdiff_t start_;
661 ptrdiff_t limit_;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000662};
663
664/** \brief Experimental and untested: represents an element of the "path" to
665 * access a node.
666 */
667class JSON_API PathArgument {
668public:
669 friend class Path;
670
671 PathArgument();
672 PathArgument(ArrayIndex index);
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000673 PathArgument(const char* key);
Billy Donahue1c2ed7a2019-01-17 16:35:29 -0500674 PathArgument(const String& key);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000675
676private:
Billy Donahueb5e1fe82018-05-20 16:55:27 -0400677 enum Kind { kindNone = 0, kindIndex, kindKey };
Billy Donahue1c2ed7a2019-01-17 16:35:29 -0500678 String key_;
Hans Johnsone817e4f2019-01-14 17:09:22 -0600679 ArrayIndex index_{};
Billy Donahue2b593a92019-01-18 03:46:57 -0500680 Kind kind_{kindNone};
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000681};
682
683/** \brief Experimental and untested: represents a "path" to access a node.
684 *
685 * Syntax:
686 * - "." => root node
687 * - ".[n]" => elements at index 'n' of root node (an array value)
688 * - ".name" => member named 'name' of root node (an object value)
689 * - ".name1.name2.name3"
690 * - ".[0][1][2].name1[3]"
691 * - ".%" => member name is provided as parameter
692 * - ".[%]" => index is provied as parameter
693 */
694class JSON_API Path {
695public:
Billy Donahue1c2ed7a2019-01-17 16:35:29 -0500696 Path(const String& path,
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000697 const PathArgument& a1 = PathArgument(),
698 const PathArgument& a2 = PathArgument(),
699 const PathArgument& a3 = PathArgument(),
700 const PathArgument& a4 = PathArgument(),
701 const PathArgument& a5 = PathArgument());
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000702
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000703 const Value& resolve(const Value& root) const;
704 Value resolve(const Value& root, const Value& defaultValue) const;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000705 /// Creates the "path" to access the specified node and returns a reference on
706 /// the node.
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000707 Value& make(Value& root) const;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000708
709private:
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000710 typedef std::vector<const PathArgument*> InArgs;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000711 typedef std::vector<PathArgument> Args;
712
Billy Donahue1c2ed7a2019-01-17 16:35:29 -0500713 void makePath(const String& path, const InArgs& in);
714 void addPathInArg(const String& path,
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000715 const InArgs& in,
716 InArgs::const_iterator& itInArg,
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000717 PathArgument::Kind kind);
Billy Donahue1c2ed7a2019-01-17 16:35:29 -0500718 static void invalidPath(const String& path, int location);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000719
720 Args args_;
721};
722
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000723/** \brief base class for Value iterators.
724 *
725 */
726class JSON_API ValueIteratorBase {
727public:
728 typedef std::bidirectional_iterator_tag iterator_category;
729 typedef unsigned int size_t;
730 typedef int difference_type;
731 typedef ValueIteratorBase SelfType;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000732
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000733 bool operator==(const SelfType& other) const { return isEqual(other); }
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000734
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000735 bool operator!=(const SelfType& other) const { return !isEqual(other); }
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000736
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000737 difference_type operator-(const SelfType& other) const {
Kevin Grant4c5832a2015-02-14 20:53:35 -0800738 return other.computeDistance(*this);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000739 }
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000740
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000741 /// Return either the index or the member name of the referenced value as a
742 /// Value.
743 Value key() const;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000744
Billy Donahueb5e1fe82018-05-20 16:55:27 -0400745 /// Return the index of the referenced Value, or -1 if it is not an
746 /// arrayValue.
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000747 UInt index() const;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000748
Christopher Dunned495ed2015-03-08 14:01:28 -0500749 /// Return the member name of the referenced Value, or "" if it is not an
750 /// objectValue.
751 /// \note Avoid `c_str()` on result, as embedded zeroes are possible.
Billy Donahue1c2ed7a2019-01-17 16:35:29 -0500752 String name() const;
Christopher Dunned495ed2015-03-08 14:01:28 -0500753
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000754 /// Return the member name of the referenced Value. "" if it is not an
755 /// objectValue.
Billy Donahueb5e1fe82018-05-20 16:55:27 -0400756 /// \deprecated This cannot be used for UTF-8 strings, since there can be
757 /// embedded nulls.
Christopher Dunned495ed2015-03-08 14:01:28 -0500758 JSONCPP_DEPRECATED("Use `key = name();` instead.")
Christopher Dunnc28610f2015-02-21 11:44:16 -0600759 char const* memberName() const;
760 /// Return the member name of the referenced Value, or NULL if it is not an
761 /// objectValue.
Christopher Dunned495ed2015-03-08 14:01:28 -0500762 /// \note Better version than memberName(). Allows embedded nulls.
Christopher Dunnc28610f2015-02-21 11:44:16 -0600763 char const* memberName(char const** end) const;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000764
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000765protected:
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000766 Value& deref() const;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000767
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000768 void increment();
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000769
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000770 void decrement();
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000771
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000772 difference_type computeDistance(const SelfType& other) const;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000773
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000774 bool isEqual(const SelfType& other) const;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000775
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000776 void copy(const SelfType& other);
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000777
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000778private:
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000779 Value::ObjectValues::iterator current_;
780 // Indicates that iterator is for a null value.
Billy Donahue2b593a92019-01-18 03:46:57 -0500781 bool isNull_{true};
Christopher Dunn2a10f4a2015-04-28 04:55:12 +0100782
783public:
784 // For some reason, BORLAND needs these at the end, rather
785 // than earlier. No idea why.
786 ValueIteratorBase();
787 explicit ValueIteratorBase(const Value::ObjectValues::iterator& current);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000788};
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000789
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000790/** \brief const iterator for object and array value.
791 *
792 */
793class JSON_API ValueConstIterator : public ValueIteratorBase {
794 friend class Value;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000795
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000796public:
797 typedef const Value value_type;
Billy Donahueb5e1fe82018-05-20 16:55:27 -0400798 // typedef unsigned int size_t;
799 // typedef int difference_type;
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000800 typedef const Value& reference;
801 typedef const Value* pointer;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000802 typedef ValueConstIterator SelfType;
803
804 ValueConstIterator();
ycqiuc8a8cfc2015-10-06 16:46:19 +0800805 ValueConstIterator(ValueIterator const& other);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000806
807private:
Billy Donahueb5e1fe82018-05-20 16:55:27 -0400808 /*! \internal Use by Value to create an iterator.
809 */
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000810 explicit ValueConstIterator(const Value::ObjectValues::iterator& current);
Billy Donahueb5e1fe82018-05-20 16:55:27 -0400811
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000812public:
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000813 SelfType& operator=(const ValueIteratorBase& other);
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000814
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000815 SelfType operator++(int) {
816 SelfType temp(*this);
817 ++*this;
818 return temp;
819 }
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000820
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000821 SelfType operator--(int) {
822 SelfType temp(*this);
823 --*this;
824 return temp;
825 }
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000826
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000827 SelfType& operator--() {
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000828 decrement();
829 return *this;
830 }
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000831
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000832 SelfType& operator++() {
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000833 increment();
834 return *this;
835 }
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000836
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000837 reference operator*() const { return deref(); }
Braden McDorman540db3b2014-09-14 02:31:23 -0500838
839 pointer operator->() const { return &deref(); }
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000840};
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000841
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000842/** \brief Iterator for object and array value.
843 */
844class JSON_API ValueIterator : public ValueIteratorBase {
845 friend class Value;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000846
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000847public:
848 typedef Value value_type;
849 typedef unsigned int size_t;
850 typedef int difference_type;
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000851 typedef Value& reference;
852 typedef Value* pointer;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000853 typedef ValueIterator SelfType;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000854
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000855 ValueIterator();
ycqiuc8a8cfc2015-10-06 16:46:19 +0800856 explicit ValueIterator(const ValueConstIterator& other);
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000857 ValueIterator(const ValueIterator& other);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000858
859private:
Billy Donahueb5e1fe82018-05-20 16:55:27 -0400860 /*! \internal Use by Value to create an iterator.
861 */
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000862 explicit ValueIterator(const Value::ObjectValues::iterator& current);
Billy Donahueb5e1fe82018-05-20 16:55:27 -0400863
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000864public:
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000865 SelfType& operator=(const SelfType& other);
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000866
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000867 SelfType operator++(int) {
868 SelfType temp(*this);
869 ++*this;
870 return temp;
871 }
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000872
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000873 SelfType operator--(int) {
874 SelfType temp(*this);
875 --*this;
876 return temp;
877 }
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000878
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000879 SelfType& operator--() {
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000880 decrement();
881 return *this;
882 }
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000883
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000884 SelfType& operator++() {
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000885 increment();
886 return *this;
887 }
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000888
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000889 reference operator*() const { return deref(); }
Braden McDorman540db3b2014-09-14 02:31:23 -0500890
891 pointer operator->() const { return &deref(); }
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000892};
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000893
Billy Donahue1d956282018-03-06 12:51:58 -0500894inline void swap(Value& a, Value& b) { a.swap(b); }
895
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000896} // namespace Json
897
Sergiy80d6e666f2016-12-03 22:29:14 +0200898#pragma pack(pop)
datadiode9454e682015-01-20 15:25:04 -0600899
Baptiste Lepilleureafd7022013-05-08 20:21:11 +0000900#if defined(JSONCPP_DISABLE_DLL_INTERFACE_WARNING)
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000901#pragma warning(pop)
Baptiste Lepilleureafd7022013-05-08 20:21:11 +0000902#endif // if defined(JSONCPP_DISABLE_DLL_INTERFACE_WARNING)
903
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000904#endif // CPPTL_JSON_H_INCLUDED