blob: 06a6c0c01be70cdbd49b1e09168af2e590a431ac [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 Donahueb5e1fe82018-05-20 16:55:27 -040012#include <exception>
Aaron Jacobs9fa4e842014-07-01 08:48:54 +100013#include <string>
14#include <vector>
Christopher Dunn6d135cb2007-06-13 15:51:04 +000015
Aaron Jacobs9fa4e842014-07-01 08:48:54 +100016#ifndef JSON_USE_CPPTL_SMALLMAP
17#include <map>
18#else
19#include <cpptl/smallmap.h>
20#endif
21#ifdef JSON_USE_CPPTL
22#include <cpptl/forwards.h>
23#endif
Christopher Dunn6d135cb2007-06-13 15:51:04 +000024
Billy Donahueb5e1fe82018-05-20 16:55:27 -040025// Conditional NORETURN attribute on the throw functions would:
Dhruv Paranjape8996c372017-07-08 17:27:07 +053026// a) suppress false positives from static code analysis
Gauravd97ea5b2016-03-16 11:15:09 +053027// b) possibly improve optimization opportunities.
28#if !defined(JSONCPP_NORETURN)
Billy Donahueb5e1fe82018-05-20 16:55:27 -040029#if defined(_MSC_VER)
30#define JSONCPP_NORETURN __declspec(noreturn)
31#elif defined(__GNUC__)
32#define JSONCPP_NORETURN __attribute__((__noreturn__))
33#else
34#define JSONCPP_NORETURN
35#endif
Gauravd97ea5b2016-03-16 11:15:09 +053036#endif
37
Aaron Jacobs9fa4e842014-07-01 08:48:54 +100038// Disable warning C4251: <data member>: <type> needs to have dll-interface to
39// be used by...
Baptiste Lepilleureafd7022013-05-08 20:21:11 +000040#if defined(JSONCPP_DISABLE_DLL_INTERFACE_WARNING)
Aaron Jacobs9fa4e842014-07-01 08:48:54 +100041#pragma warning(push)
42#pragma warning(disable : 4251)
Baptiste Lepilleureafd7022013-05-08 20:21:11 +000043#endif // if defined(JSONCPP_DISABLE_DLL_INTERFACE_WARNING)
44
Sergiy80d6e666f2016-12-03 22:29:14 +020045#pragma pack(push, 8)
46
Christopher Dunn6d135cb2007-06-13 15:51:04 +000047/** \brief JSON (JavaScript Object Notation).
48 */
49namespace Json {
50
Christopher Dunn75279cc2015-03-08 12:20:06 -050051/** Base class for all exceptions we throw.
Christopher Dunn4e30c4f2015-03-08 12:56:32 -050052 *
53 * We use nothing but these internally. Of course, STL can throw others.
Christopher Dunn75279cc2015-03-08 12:20:06 -050054 */
Christopher Dunn949babd2015-07-23 00:19:12 -050055class JSON_API Exception : public std::exception {
56public:
Christopher Dunnde5b7922016-03-06 11:19:46 -060057 Exception(JSONCPP_STRING const& msg);
Omkar Wagh91c1d232016-11-07 13:57:00 -050058 ~Exception() JSONCPP_NOEXCEPT JSONCPP_OVERRIDE;
59 char const* what() const JSONCPP_NOEXCEPT JSONCPP_OVERRIDE;
Billy Donahueb5e1fe82018-05-20 16:55:27 -040060
Christopher Dunn949babd2015-07-23 00:19:12 -050061protected:
Christopher Dunnde5b7922016-03-06 11:19:46 -060062 JSONCPP_STRING msg_;
Christopher Dunn949babd2015-07-23 00:19:12 -050063};
64
Christopher Dunn53837942015-03-08 12:31:00 -050065/** Exceptions which the user cannot easily avoid.
66 *
Christopher Dunn4e30c4f2015-03-08 12:56:32 -050067 * E.g. out-of-memory (when we use malloc), stack-overflow, malicious input
Dhruv Paranjape8996c372017-07-08 17:27:07 +053068 *
Christopher Dunn4e30c4f2015-03-08 12:56:32 -050069 * \remark derived from Json::Exception
Christopher Dunn53837942015-03-08 12:31:00 -050070 */
Christopher Dunn949babd2015-07-23 00:19:12 -050071class JSON_API RuntimeError : public Exception {
72public:
Christopher Dunnde5b7922016-03-06 11:19:46 -060073 RuntimeError(JSONCPP_STRING const& msg);
Christopher Dunn949babd2015-07-23 00:19:12 -050074};
75
Christopher Dunn4e30c4f2015-03-08 12:56:32 -050076/** Exceptions thrown by JSON_ASSERT/JSON_FAIL macros.
Christopher Dunn53837942015-03-08 12:31:00 -050077 *
78 * These are precondition-violations (user bugs) and internal errors (our bugs).
Dhruv Paranjape8996c372017-07-08 17:27:07 +053079 *
Christopher Dunn4e30c4f2015-03-08 12:56:32 -050080 * \remark derived from Json::Exception
Christopher Dunn53837942015-03-08 12:31:00 -050081 */
Christopher Dunn949babd2015-07-23 00:19:12 -050082class JSON_API LogicError : public Exception {
83public:
Christopher Dunnde5b7922016-03-06 11:19:46 -060084 LogicError(JSONCPP_STRING const& msg);
Christopher Dunn949babd2015-07-23 00:19:12 -050085};
Christopher Dunn53837942015-03-08 12:31:00 -050086
Christopher Dunn4e30c4f2015-03-08 12:56:32 -050087/// used internally
Gauravd97ea5b2016-03-16 11:15:09 +053088JSONCPP_NORETURN void throwRuntimeError(JSONCPP_STRING const& msg);
Christopher Dunn4e30c4f2015-03-08 12:56:32 -050089/// used internally
Gauravd97ea5b2016-03-16 11:15:09 +053090JSONCPP_NORETURN void throwLogicError(JSONCPP_STRING const& msg);
Christopher Dunn75279cc2015-03-08 12:20:06 -050091
Aaron Jacobs9fa4e842014-07-01 08:48:54 +100092/** \brief Type of the value held by a Value object.
93 */
94enum ValueType {
95 nullValue = 0, ///< 'null' value
96 intValue, ///< signed integer value
97 uintValue, ///< unsigned integer value
98 realValue, ///< double value
99 stringValue, ///< UTF-8 string value
100 booleanValue, ///< bool value
101 arrayValue, ///< array value (ordered list)
102 objectValue ///< object value (collection of name/value pairs).
103};
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000104
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000105enum CommentPlacement {
106 commentBefore = 0, ///< a comment placed on the line before a value
107 commentAfterOnSameLine, ///< a comment just after a value on the same line
108 commentAfter, ///< a comment on the line after a value (only make sense for
Aaron Jacobs3a0c4fc2014-07-01 09:20:48 +1000109 /// root value)
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000110 numberOfCommentPlacement
111};
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000112
Mike Ra07fc532018-03-13 23:35:31 +0300113/** \brief Type of precision for formatting of real values.
114 */
115enum PrecisionType {
116 significantDigits = 0, ///< we set max number of significant digits in string
117 decimalPlaces ///< we set max number of digits after "." in string
118};
119
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000120//# ifdef JSON_USE_CPPTL
121// typedef CppTL::AnyEnumerator<const char *> EnumMemberNames;
122// typedef CppTL::AnyEnumerator<const Value &> EnumValues;
123//# endif
124
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000125/** \brief Lightweight wrapper to tag static string.
126 *
Josh Sorefe6a588a2017-12-03 11:54:29 -0500127 * Value constructor and objectValue member assignment takes advantage of the
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000128 * StaticString and avoid the cost of string duplication when storing the
129 * string or the member name.
130 *
131 * Example of usage:
132 * \code
133 * Json::Value aValue( StaticString("some text") );
134 * Json::Value object;
135 * static const StaticString code("code");
136 * object[code] = 1234;
137 * \endcode
138 */
139class JSON_API StaticString {
140public:
Christopher Dunnff617522015-03-06 10:31:46 -0600141 explicit StaticString(const char* czstring) : c_str_(czstring) {}
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000142
Christopher Dunnff617522015-03-06 10:31:46 -0600143 operator const char*() const { return c_str_; }
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000144
Christopher Dunnff617522015-03-06 10:31:46 -0600145 const char* c_str() const { return c_str_; }
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000146
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000147private:
Christopher Dunnff617522015-03-06 10:31:46 -0600148 const char* c_str_;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000149};
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000150
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000151/** \brief Represents a <a HREF="http://www.json.org">JSON</a> value.
152 *
153 * This class is a discriminated union wrapper that can represents a:
154 * - signed integer [range: Value::minInt - Value::maxInt]
155 * - unsigned integer (range: 0 - Value::maxUInt)
156 * - double
157 * - UTF-8 string
158 * - boolean
159 * - 'null'
160 * - an ordered list of Value
161 * - collection of name/value pairs (javascript object)
162 *
163 * The type of the held value is represented by a #ValueType and
164 * can be obtained using type().
165 *
Christopher Dunnc28610f2015-02-21 11:44:16 -0600166 * Values of an #objectValue or #arrayValue can be accessed using operator[]()
167 * methods.
168 * Non-const methods will automatically create the a #nullValue element
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000169 * if it does not exist.
Christopher Dunnc28610f2015-02-21 11:44:16 -0600170 * The sequence of an #arrayValue will be automatically resized and initialized
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000171 * with #nullValue. resize() can be used to enlarge or truncate an #arrayValue.
172 *
Christopher Dunnc28610f2015-02-21 11:44:16 -0600173 * The get() methods can be used to obtain default value in the case the
174 * required element does not exist.
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000175 *
176 * It is possible to iterate over the list of a #objectValue values using
177 * the getMemberNames() method.
Christopher Dunn25342ba2015-03-02 18:05:26 -0600178 *
Christopher Dunnc28610f2015-02-21 11:44:16 -0600179 * \note #Value string-length fit in size_t, but keys must be < 2^30.
180 * (The reason is an implementation detail.) A #CharReader will raise an
Christopher Dunn25342ba2015-03-02 18:05:26 -0600181 * exception if a bound is exceeded to avoid security holes in your app,
182 * but the Value API does *not* check bounds. That is the responsibility
183 * of the caller.
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000184 */
185class JSON_API Value {
186 friend class ValueIteratorBase;
Billy Donahueb5e1fe82018-05-20 16:55:27 -0400187
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000188public:
Christopher Dunnde5b7922016-03-06 11:19:46 -0600189 typedef std::vector<JSONCPP_STRING> Members;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000190 typedef ValueIterator iterator;
191 typedef ValueConstIterator const_iterator;
192 typedef Json::UInt UInt;
193 typedef Json::Int Int;
194#if defined(JSON_HAS_INT64)
195 typedef Json::UInt64 UInt64;
196 typedef Json::Int64 Int64;
Baptiste Lepilleur842e9ac2010-12-27 17:45:23 +0000197#endif // defined(JSON_HAS_INT64)
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000198 typedef Json::LargestInt LargestInt;
199 typedef Json::LargestUInt LargestUInt;
200 typedef Json::ArrayIndex ArrayIndex;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000201
Wolfram Röslerff6b4492017-09-14 09:31:36 +0200202 // Required for boost integration, e. g. BOOST_TEST
203 typedef std::string value_type;
204
Billy Donahueb5e1fe82018-05-20 16:55:27 -0400205 static const Value& null; ///< We regret this reference to a global instance;
206 ///< prefer the simpler Value().
207 static const Value& nullRef; ///< just a kludge for binary-compatibility; same
208 ///< as null
Christopher Dunn0f288ae2016-06-26 18:47:43 -0500209 static Value const& nullSingleton(); ///< Prefer this to null or nullRef.
210
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000211 /// Minimum signed integer value that can be stored in a Json::Value.
212 static const LargestInt minLargestInt;
213 /// Maximum signed integer value that can be stored in a Json::Value.
214 static const LargestInt maxLargestInt;
215 /// Maximum unsigned integer value that can be stored in a Json::Value.
216 static const LargestUInt maxLargestUInt;
Baptiste Lepilleur842e9ac2010-12-27 17:45:23 +0000217
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000218 /// Minimum signed int value that can be stored in a Json::Value.
219 static const Int minInt;
220 /// Maximum signed int value that can be stored in a Json::Value.
221 static const Int maxInt;
222 /// Maximum unsigned int value that can be stored in a Json::Value.
223 static const UInt maxUInt;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000224
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000225#if defined(JSON_HAS_INT64)
226 /// Minimum signed 64 bits int value that can be stored in a Json::Value.
227 static const Int64 minInt64;
228 /// Maximum signed 64 bits int value that can be stored in a Json::Value.
229 static const Int64 maxInt64;
230 /// Maximum unsigned 64 bits int value that can be stored in a Json::Value.
231 static const UInt64 maxUInt64;
Aaron Jacobsf1053e72011-05-24 03:18:02 +0000232#endif // defined(JSON_HAS_INT64)
Baptiste Lepilleur842e9ac2010-12-27 17:45:23 +0000233
Mike Ra07fc532018-03-13 23:35:31 +0300234 /// Default precision for real value for string representation.
235 static const UInt defaultRealPrecision;
236
Darcy Beurle798f6ba2017-12-22 22:48:20 +0100237// Workaround for bug in the NVIDIAs CUDA 9.1 nvcc compiler
238// when using gcc and clang backend compilers. CZString
239// cannot be defined as private. See issue #486
240#ifdef __NVCC__
241public:
242#else
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000243private:
Darcy Beurle798f6ba2017-12-22 22:48:20 +0100244#endif
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000245#ifndef JSONCPP_DOC_EXCLUDE_IMPLEMENTATION
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000246 class CZString {
247 public:
Billy Donahueb5e1fe82018-05-20 16:55:27 -0400248 enum DuplicationPolicy { noDuplication = 0, duplicate, duplicateOnCopy };
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000249 CZString(ArrayIndex index);
Christopher Dunnc28610f2015-02-21 11:44:16 -0600250 CZString(char const* str, unsigned length, DuplicationPolicy allocate);
251 CZString(CZString const& other);
Motti2b008912015-04-20 17:44:47 +0300252#if JSON_HAS_RVALUE_REFERENCES
253 CZString(CZString&& other);
254#endif
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000255 ~CZString();
Dhruv Paranjape0ba8bd72017-07-08 17:47:13 +0530256 CZString& operator=(const CZString& other);
257
258#if JSON_HAS_RVALUE_REFERENCES
259 CZString& operator=(CZString&& other);
260#endif
261
Christopher Dunnc28610f2015-02-21 11:44:16 -0600262 bool operator<(CZString const& other) const;
263 bool operator==(CZString const& other) const;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000264 ArrayIndex index() const;
Billy Donahueb5e1fe82018-05-20 16:55:27 -0400265 // const char* c_str() const; ///< \deprecated
Christopher Dunnc28610f2015-02-21 11:44:16 -0600266 char const* data() const;
267 unsigned length() const;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000268 bool isStaticString() const;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000269
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000270 private:
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000271 void swap(CZString& other);
Christopher Dunnc28610f2015-02-21 11:44:16 -0600272
Christopher Dunn57ad0512015-03-02 12:10:35 -0600273 struct StringStorage {
Billy Donahueb5e1fe82018-05-20 16:55:27 -0400274 unsigned policy_ : 2;
275 unsigned length_ : 30; // 1GB max
Christopher Dunn57ad0512015-03-02 12:10:35 -0600276 };
277
Billy Donahueb5e1fe82018-05-20 16:55:27 -0400278 char const* cstr_; // actually, a prefixed string, unless policy is noDup
Christopher Dunn57ad0512015-03-02 12:10:35 -0600279 union {
280 ArrayIndex index_;
281 StringStorage storage_;
282 };
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000283 };
284
285public:
286#ifndef JSON_USE_CPPTL_SMALLMAP
287 typedef std::map<CZString, Value> ObjectValues;
288#else
289 typedef CppTL::SmallMap<CZString, Value> ObjectValues;
290#endif // ifndef JSON_USE_CPPTL_SMALLMAP
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000291#endif // ifndef JSONCPP_DOC_EXCLUDE_IMPLEMENTATION
292
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000293public:
294 /** \brief Create a default Value of the given type.
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000295
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000296 This is a very useful constructor.
297 To create an empty array, pass arrayValue.
298 To create an empty object, pass objectValue.
299 Another Value can then be set to this one by assignment.
300This is useful since clear() and resize() will not alter types.
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000301
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000302 Examples:
303\code
304Json::Value null_value; // null
305Json::Value arr_value(Json::arrayValue); // []
306Json::Value obj_value(Json::objectValue); // {}
307\endcode
308 */
309 Value(ValueType type = nullValue);
310 Value(Int value);
311 Value(UInt value);
Baptiste Lepilleur842e9ac2010-12-27 17:45:23 +0000312#if defined(JSON_HAS_INT64)
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000313 Value(Int64 value);
314 Value(UInt64 value);
Baptiste Lepilleur842e9ac2010-12-27 17:45:23 +0000315#endif // if defined(JSON_HAS_INT64)
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000316 Value(double value);
Christopher Dunn8a702972015-03-03 10:38:27 -0600317 Value(const char* value); ///< Copy til first 0. (NULL causes to seg-fault.)
Christopher Dunn89704032015-07-11 12:09:59 -0500318 Value(const char* begin, const char* end); ///< Copy all, incl zeroes.
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000319 /** \brief Constructs a value from a static string.
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000320
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000321 * Like other value string constructor but do not duplicate the string for
322 * internal storage. The given string must remain alive after the call to this
323 * constructor.
Christopher Dunnc28610f2015-02-21 11:44:16 -0600324 * \note This works only for null-terminated strings. (We cannot change the
325 * size of this class, so we have nowhere to store the length,
326 * which might be computed later for various operations.)
327 *
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000328 * Example of usage:
329 * \code
Christopher Dunnc28610f2015-02-21 11:44:16 -0600330 * static StaticString foo("some text");
331 * Json::Value aValue(foo);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000332 * \endcode
333 */
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000334 Value(const StaticString& value);
Billy Donahueb5e1fe82018-05-20 16:55:27 -0400335 Value(const JSONCPP_STRING& value); ///< Copy data() til size(). Embedded
336 ///< zeroes too.
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000337#ifdef JSON_USE_CPPTL
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000338 Value(const CppTL::ConstString& value);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000339#endif
340 Value(bool value);
Christopher Dunn66eb72f2015-01-20 11:02:22 -0600341 /// Deep copy.
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000342 Value(const Value& other);
Motti2b008912015-04-20 17:44:47 +0300343#if JSON_HAS_RVALUE_REFERENCES
344 /// Move constructor
345 Value(Value&& other);
346#endif
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000347 ~Value();
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000348
Christopher Dunn7f439f42015-03-06 09:22:57 -0600349 /// Deep copy, then swap(other).
Billy Donahueb5e1fe82018-05-20 16:55:27 -0400350 /// \note Over-write existing comments. To preserve comments, use
351 /// #swapPayload().
Александр Малинин6a15ca62017-07-31 15:29:02 +0300352 Value& operator=(Value other);
Dhruv Paranjape8996c372017-07-08 17:27:07 +0530353
Christopher Dunn66eb72f2015-01-20 11:02:22 -0600354 /// Swap everything.
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000355 void swap(Value& other);
Christopher Dunn66eb72f2015-01-20 11:02:22 -0600356 /// Swap values but leave comments and source offsets in place.
357 void swapPayload(Value& other);
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000358
Dhruv Paranjape8996c372017-07-08 17:27:07 +0530359 /// copy everything.
360 void copy(const Value& other);
361 /// copy values but leave comments and source offsets in place.
362 void copyPayload(const Value& other);
363
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000364 ValueType type() const;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000365
Christopher Dunn66eb72f2015-01-20 11:02:22 -0600366 /// Compare payload only, not comments etc.
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000367 bool operator<(const Value& other) const;
368 bool operator<=(const Value& other) const;
369 bool operator>=(const Value& other) const;
370 bool operator>(const Value& other) const;
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000371 bool operator==(const Value& other) const;
372 bool operator!=(const Value& other) const;
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000373 int compare(const Value& other) const;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000374
Christopher Dunn8a702972015-03-03 10:38:27 -0600375 const char* asCString() const; ///< Embedded zeroes could cause you trouble!
dawescae564652016-03-14 19:11:02 -0500376#if JSONCPP_USING_SECURE_MEMORY
Billy Donahueb5e1fe82018-05-20 16:55:27 -0400377 unsigned getCStringLength() const; // Allows you to understand the length of
378 // the CString
dawescae564652016-03-14 19:11:02 -0500379#endif
Christopher Dunnde5b7922016-03-06 11:19:46 -0600380 JSONCPP_STRING asString() const; ///< Embedded zeroes are possible.
Christopher Dunnc28610f2015-02-21 11:44:16 -0600381 /** Get raw char* of string-value.
382 * \return false if !string. (Seg-fault if str or end are NULL.)
383 */
Billy Donahueb5e1fe82018-05-20 16:55:27 -0400384 bool getString(char const** begin, char const** end) const;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000385#ifdef JSON_USE_CPPTL
386 CppTL::ConstString asConstString() const;
387#endif
388 Int asInt() const;
389 UInt asUInt() const;
Aaron Jacobsf1053e72011-05-24 03:18:02 +0000390#if defined(JSON_HAS_INT64)
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000391 Int64 asInt64() const;
392 UInt64 asUInt64() const;
Aaron Jacobsf1053e72011-05-24 03:18:02 +0000393#endif // if defined(JSON_HAS_INT64)
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000394 LargestInt asLargestInt() const;
395 LargestUInt asLargestUInt() const;
396 float asFloat() const;
397 double asDouble() const;
398 bool asBool() const;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000399
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000400 bool isNull() const;
401 bool isBool() const;
402 bool isInt() const;
403 bool isInt64() const;
404 bool isUInt() const;
405 bool isUInt64() const;
406 bool isIntegral() const;
407 bool isDouble() const;
408 bool isNumeric() const;
409 bool isString() const;
410 bool isArray() const;
411 bool isObject() const;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000412
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000413 bool isConvertibleTo(ValueType other) const;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000414
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000415 /// Number of values in array or object
416 ArrayIndex size() const;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000417
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000418 /// \brief Return true if empty array, empty object, or null;
419 /// otherwise, false.
420 bool empty() const;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000421
Wolfram Rösler90794222017-12-05 18:18:55 +0100422 /// Return !isNull()
drgler04abe382018-01-13 15:28:19 +0100423 JSONCPP_OP_EXPLICIT operator bool() const;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000424
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000425 /// Remove all object members and array elements.
426 /// \pre type() is arrayValue, objectValue, or nullValue
427 /// \post type() is unchanged
428 void clear();
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000429
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000430 /// Resize the array to size elements.
431 /// New elements are initialized to null.
432 /// May only be called on nullValue or arrayValue.
433 /// \pre type() is arrayValue or nullValue
434 /// \post type() is arrayValue
435 void resize(ArrayIndex size);
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000436
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000437 /// Access an array element (zero based index ).
438 /// If the array contains less than index element, then null value are
439 /// inserted
440 /// in the array so that its size is index+1.
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 Value& operator[](ArrayIndex index);
Baptiste Lepilleurfa130ef2010-12-24 12:47:14 +0000444
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000445 /// Access an array element (zero based index ).
446 /// If the array contains less than index element, then null value are
447 /// inserted
448 /// in the array so that its size is index+1.
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 Value& operator[](int index);
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[](ArrayIndex index) const;
Baptiste Lepilleurfa130ef2010-12-24 12:47:14 +0000457
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000458 /// Access an array element (zero based index )
459 /// (You may need to say 'value[0u]' to get your compiler to distinguish
460 /// this from the operator[] which takes a string.)
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000461 const Value& operator[](int index) const;
Baptiste Lepilleurfa130ef2010-12-24 12:47:14 +0000462
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000463 /// If the array contains at least index+1 elements, returns the element
464 /// value,
465 /// otherwise returns defaultValue.
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000466 Value get(ArrayIndex index, const Value& defaultValue) const;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000467 /// Return true if index < size().
468 bool isValidIndex(ArrayIndex index) const;
469 /// \brief Append value to array at the end.
470 ///
471 /// Equivalent to jsonvalue[jsonvalue.size()] = value;
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000472 Value& append(const Value& value);
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000473
Dhruv Paranjape0ba8bd72017-07-08 17:47:13 +0530474#if JSON_HAS_RVALUE_REFERENCES
Dhruv Paranjape23c44d92017-07-08 17:30:47 +0530475 Value& append(Value&& value);
476#endif
477
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000478 /// Access an object value by name, create a null member if it does not exist.
Christopher Dunn25342ba2015-03-02 18:05:26 -0600479 /// \note Because of our implementation, keys are limited to 2^30 -1 chars.
480 /// Exceeding that will cause an exception.
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000481 Value& operator[](const char* key);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000482 /// Access an object value by name, returns null if there is no member with
483 /// that name.
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000484 const Value& operator[](const char* key) const;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000485 /// Access an object value by name, create a null member if it does not exist.
Christopher Dunn25342ba2015-03-02 18:05:26 -0600486 /// \param key may contain embedded nulls.
Christopher Dunnde5b7922016-03-06 11:19:46 -0600487 Value& operator[](const JSONCPP_STRING& 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.
Christopher Dunn25342ba2015-03-02 18:05:26 -0600490 /// \param key may contain embedded nulls.
Christopher Dunnde5b7922016-03-06 11:19:46 -0600491 const Value& operator[](const JSONCPP_STRING& key) const;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000492 /** \brief Access an object value by name, create a null member if it does not
493 exist.
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000494
Billy Donahueb5e1fe82018-05-20 16:55:27 -0400495 * If the object has no entry for that name, then the member name used to
496 store
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000497 * the new entry is not duplicated.
498 * Example of use:
499 * \code
500 * Json::Value object;
501 * static const StaticString code("code");
502 * object[code] = 1234;
503 * \endcode
504 */
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000505 Value& operator[](const StaticString& key);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000506#ifdef JSON_USE_CPPTL
507 /// Access an object value by name, create a null member if it does not exist.
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000508 Value& operator[](const CppTL::ConstString& key);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000509 /// Access an object value by name, returns null if there is no member with
510 /// that name.
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000511 const Value& operator[](const CppTL::ConstString& key) const;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000512#endif
513 /// Return the member named key if it exist, defaultValue otherwise.
Christopher Dunn0fd28752015-03-05 16:38:43 -0600514 /// \note deep copy
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000515 Value get(const char* key, const Value& defaultValue) const;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000516 /// Return the member named key if it exist, defaultValue otherwise.
Christopher Dunn0fd28752015-03-05 16:38:43 -0600517 /// \note deep copy
Christopher Dunn89704032015-07-11 12:09:59 -0500518 /// \note key may contain embedded nulls.
Billy Donahueb5e1fe82018-05-20 16:55:27 -0400519 Value
520 get(const char* begin, const char* end, const Value& defaultValue) const;
Christopher Dunn25342ba2015-03-02 18:05:26 -0600521 /// Return the member named key if it exist, defaultValue otherwise.
Christopher Dunn0fd28752015-03-05 16:38:43 -0600522 /// \note deep copy
Christopher Dunn25342ba2015-03-02 18:05:26 -0600523 /// \param key may contain embedded nulls.
Christopher Dunnde5b7922016-03-06 11:19:46 -0600524 Value get(const JSONCPP_STRING& key, const Value& defaultValue) const;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000525#ifdef JSON_USE_CPPTL
526 /// Return the member named key if it exist, defaultValue otherwise.
Christopher Dunn0fd28752015-03-05 16:38:43 -0600527 /// \note deep copy
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000528 Value get(const CppTL::ConstString& key, const Value& defaultValue) const;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000529#endif
Christopher Dunn25342ba2015-03-02 18:05:26 -0600530 /// Most general and efficient version of isMember()const, get()const,
531 /// and operator[]const
Christopher Dunn89704032015-07-11 12:09:59 -0500532 /// \note As stated elsewhere, behavior is undefined if (end-begin) >= 2^30
533 Value const* find(char const* begin, char const* end) const;
Christopher Dunnc28610f2015-02-21 11:44:16 -0600534 /// Most general and efficient version of object-mutators.
Christopher Dunn89704032015-07-11 12:09:59 -0500535 /// \note As stated elsewhere, behavior is undefined if (end-begin) >= 2^30
Christopher Dunnc28610f2015-02-21 11:44:16 -0600536 /// \return non-zero, but JSON_ASSERT if this is neither object nor nullValue.
Christopher Dunn89704032015-07-11 12:09:59 -0500537 Value const* demand(char const* begin, char const* end);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000538 /// \brief Remove and return the named member.
539 ///
540 /// Do nothing if it did not exist.
541 /// \return the removed Value, or null.
542 /// \pre type() is objectValue or nullValue
543 /// \post type() is unchanged
Christopher Dunn76746b02015-01-21 16:01:30 -0600544 /// \deprecated
Wolfram Röslera06b3902017-10-18 07:19:27 +0200545 void removeMember(const char* key);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000546 /// Same as removeMember(const char*)
Christopher Dunn25342ba2015-03-02 18:05:26 -0600547 /// \param key may contain embedded nulls.
Christopher Dunn76746b02015-01-21 16:01:30 -0600548 /// \deprecated
Wolfram Röslera06b3902017-10-18 07:19:27 +0200549 void removeMember(const JSONCPP_STRING& key);
Christopher Dunn89704032015-07-11 12:09:59 -0500550 /// Same as removeMember(const char* begin, const char* end, Value* removed),
Christopher Dunn25342ba2015-03-02 18:05:26 -0600551 /// but 'key' is null-terminated.
552 bool removeMember(const char* key, Value* removed);
Christopher Dunn76746b02015-01-21 16:01:30 -0600553 /** \brief Remove the named map member.
554
555 Update 'removed' iff removed.
Christopher Dunn25342ba2015-03-02 18:05:26 -0600556 \param key may contain embedded nulls.
Christopher Dunn76746b02015-01-21 16:01:30 -0600557 \return true iff removed (no exceptions)
558 */
Christopher Dunnde5b7922016-03-06 11:19:46 -0600559 bool removeMember(JSONCPP_STRING const& key, Value* removed);
560 /// Same as removeMember(JSONCPP_STRING const& key, Value* removed)
Christopher Dunn89704032015-07-11 12:09:59 -0500561 bool removeMember(const char* begin, const char* end, Value* removed);
Christopher Dunn9de2c2d2015-01-20 16:15:40 -0600562 /** \brief Remove the indexed array element.
563
564 O(n) expensive operations.
565 Update 'removed' iff removed.
Christopher Dunne87e41c2015-01-20 16:24:11 -0600566 \return true iff removed (no exceptions)
Christopher Dunn9de2c2d2015-01-20 16:15:40 -0600567 */
568 bool removeIndex(ArrayIndex i, Value* removed);
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000569
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000570 /// Return true if the object has a member named key.
Christopher Dunnc28610f2015-02-21 11:44:16 -0600571 /// \note 'key' must be null-terminated.
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000572 bool isMember(const char* key) const;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000573 /// Return true if the object has a member named key.
Christopher Dunn25342ba2015-03-02 18:05:26 -0600574 /// \param key may contain embedded nulls.
Christopher Dunnde5b7922016-03-06 11:19:46 -0600575 bool isMember(const JSONCPP_STRING& key) const;
576 /// Same as isMember(JSONCPP_STRING const& key)const
Christopher Dunn89704032015-07-11 12:09:59 -0500577 bool isMember(const char* begin, const char* end) const;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000578#ifdef JSON_USE_CPPTL
579 /// Return true if the object has a member named key.
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000580 bool isMember(const CppTL::ConstString& key) const;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000581#endif
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000582
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000583 /// \brief Return a list of the member names.
584 ///
585 /// If null, return an empty list.
586 /// \pre type() is objectValue or nullValue
587 /// \post if type() was nullValue, it remains nullValue
588 Members getMemberNames() const;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000589
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000590 //# ifdef JSON_USE_CPPTL
591 // EnumMemberNames enumMemberNames() const;
592 // EnumValues enumValues() const;
593 //# endif
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000594
Christopher Dunn1e3149a2015-01-25 14:16:13 -0600595 /// \deprecated Always pass len.
Christopher Dunnde5b7922016-03-06 11:19:46 -0600596 JSONCPP_DEPRECATED("Use setComment(JSONCPP_STRING const&) instead.")
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000597 void setComment(const char* comment, CommentPlacement placement);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000598 /// Comments must be //... or /* ... */
Christopher Dunn1e3149a2015-01-25 14:16:13 -0600599 void setComment(const char* comment, size_t len, CommentPlacement placement);
600 /// Comments must be //... or /* ... */
Christopher Dunnde5b7922016-03-06 11:19:46 -0600601 void setComment(const JSONCPP_STRING& comment, CommentPlacement placement);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000602 bool hasComment(CommentPlacement placement) const;
603 /// Include delimiters and embedded newlines.
Christopher Dunnde5b7922016-03-06 11:19:46 -0600604 JSONCPP_STRING getComment(CommentPlacement placement) const;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000605
Christopher Dunnde5b7922016-03-06 11:19:46 -0600606 JSONCPP_STRING toStyledString() const;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000607
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000608 const_iterator begin() const;
609 const_iterator end() const;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000610
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000611 iterator begin();
612 iterator end();
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000613
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000614 // Accessors for the [start, limit) range of bytes within the JSON text from
615 // which this value was parsed, if any.
Christopher Dunnd4513fc2016-02-06 09:25:20 -0600616 void setOffsetStart(ptrdiff_t start);
617 void setOffsetLimit(ptrdiff_t limit);
618 ptrdiff_t getOffsetStart() const;
619 ptrdiff_t getOffsetLimit() const;
Aaron Jacobs68db6552014-04-23 23:41:12 +0000620
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000621private:
Billy Donahue8eb5d892014-11-10 01:35:42 -0500622 void initBasic(ValueType type, bool allocated = false);
Andrey Okoshkin9b569c82018-01-12 15:59:20 +0300623 void dupPayload(const Value& other);
Andrey Okoshkinc69148c2018-01-12 11:26:34 +0300624 void releasePayload();
Andrey Okoshkin9b569c82018-01-12 15:59:20 +0300625 void dupMeta(const Value& other);
Billy Donahue8eb5d892014-11-10 01:35:42 -0500626
Christopher Dunnc28610f2015-02-21 11:44:16 -0600627 Value& resolveReference(const char* key);
628 Value& resolveReference(const char* key, const char* end);
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000629
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000630 struct CommentInfo {
631 CommentInfo();
632 ~CommentInfo();
633
Christopher Dunn1e3149a2015-01-25 14:16:13 -0600634 void setComment(const char* text, size_t len);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000635
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000636 char* comment_;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000637 };
638
639 // struct MemberNamesTransform
640 //{
641 // typedef const char *result_type;
642 // const char *operator()( const CZString &name ) const
643 // {
644 // return name.c_str();
645 // }
646 //};
647
648 union ValueHolder {
649 LargestInt int_;
650 LargestUInt uint_;
651 double real_;
652 bool bool_;
Billy Donahueb5e1fe82018-05-20 16:55:27 -0400653 char* string_; // actually ptr to unsigned, followed by str, unless
654 // !allocated_
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000655 ObjectValues* map_;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000656 } value_;
657 ValueType type_ : 8;
Billy Donahueb5e1fe82018-05-20 16:55:27 -0400658 unsigned int allocated_ : 1; // Notes: if declared as bool, bitfield is
659 // useless. If not allocated_, string_ must be
660 // null-terminated.
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000661 CommentInfo* comments_;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000662
663 // [start, limit) byte offsets in the source JSON text from which this Value
664 // was extracted.
Christopher Dunnd4513fc2016-02-06 09:25:20 -0600665 ptrdiff_t start_;
666 ptrdiff_t limit_;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000667};
668
669/** \brief Experimental and untested: represents an element of the "path" to
670 * access a node.
671 */
672class JSON_API PathArgument {
673public:
674 friend class Path;
675
676 PathArgument();
677 PathArgument(ArrayIndex index);
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000678 PathArgument(const char* key);
Christopher Dunnde5b7922016-03-06 11:19:46 -0600679 PathArgument(const JSONCPP_STRING& key);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000680
681private:
Billy Donahueb5e1fe82018-05-20 16:55:27 -0400682 enum Kind { kindNone = 0, kindIndex, kindKey };
Christopher Dunnde5b7922016-03-06 11:19:46 -0600683 JSONCPP_STRING key_;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000684 ArrayIndex index_;
685 Kind kind_;
686};
687
688/** \brief Experimental and untested: represents a "path" to access a node.
689 *
690 * Syntax:
691 * - "." => root node
692 * - ".[n]" => elements at index 'n' of root node (an array value)
693 * - ".name" => member named 'name' of root node (an object value)
694 * - ".name1.name2.name3"
695 * - ".[0][1][2].name1[3]"
696 * - ".%" => member name is provided as parameter
697 * - ".[%]" => index is provied as parameter
698 */
699class JSON_API Path {
700public:
Christopher Dunnde5b7922016-03-06 11:19:46 -0600701 Path(const JSONCPP_STRING& path,
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000702 const PathArgument& a1 = PathArgument(),
703 const PathArgument& a2 = PathArgument(),
704 const PathArgument& a3 = PathArgument(),
705 const PathArgument& a4 = PathArgument(),
706 const PathArgument& a5 = PathArgument());
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000707
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000708 const Value& resolve(const Value& root) const;
709 Value resolve(const Value& root, const Value& defaultValue) const;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000710 /// Creates the "path" to access the specified node and returns a reference on
711 /// the node.
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000712 Value& make(Value& root) const;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000713
714private:
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000715 typedef std::vector<const PathArgument*> InArgs;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000716 typedef std::vector<PathArgument> Args;
717
Christopher Dunnde5b7922016-03-06 11:19:46 -0600718 void makePath(const JSONCPP_STRING& path, const InArgs& in);
719 void addPathInArg(const JSONCPP_STRING& path,
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000720 const InArgs& in,
721 InArgs::const_iterator& itInArg,
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000722 PathArgument::Kind kind);
Christopher Dunnde5b7922016-03-06 11:19:46 -0600723 void invalidPath(const JSONCPP_STRING& path, int location);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000724
725 Args args_;
726};
727
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000728/** \brief base class for Value iterators.
729 *
730 */
731class JSON_API ValueIteratorBase {
732public:
733 typedef std::bidirectional_iterator_tag iterator_category;
734 typedef unsigned int size_t;
735 typedef int difference_type;
736 typedef ValueIteratorBase SelfType;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000737
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000738 bool operator==(const SelfType& other) const { return isEqual(other); }
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000739
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000740 bool operator!=(const SelfType& other) const { return !isEqual(other); }
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000741
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000742 difference_type operator-(const SelfType& other) const {
Kevin Grant4c5832a2015-02-14 20:53:35 -0800743 return other.computeDistance(*this);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000744 }
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000745
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000746 /// Return either the index or the member name of the referenced value as a
747 /// Value.
748 Value key() const;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000749
Billy Donahueb5e1fe82018-05-20 16:55:27 -0400750 /// Return the index of the referenced Value, or -1 if it is not an
751 /// arrayValue.
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000752 UInt index() const;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000753
Christopher Dunned495ed2015-03-08 14:01:28 -0500754 /// Return the member name of the referenced Value, or "" if it is not an
755 /// objectValue.
756 /// \note Avoid `c_str()` on result, as embedded zeroes are possible.
Christopher Dunnde5b7922016-03-06 11:19:46 -0600757 JSONCPP_STRING name() const;
Christopher Dunned495ed2015-03-08 14:01:28 -0500758
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000759 /// Return the member name of the referenced Value. "" if it is not an
760 /// objectValue.
Billy Donahueb5e1fe82018-05-20 16:55:27 -0400761 /// \deprecated This cannot be used for UTF-8 strings, since there can be
762 /// embedded nulls.
Christopher Dunned495ed2015-03-08 14:01:28 -0500763 JSONCPP_DEPRECATED("Use `key = name();` instead.")
Christopher Dunnc28610f2015-02-21 11:44:16 -0600764 char const* memberName() const;
765 /// Return the member name of the referenced Value, or NULL if it is not an
766 /// objectValue.
Christopher Dunned495ed2015-03-08 14:01:28 -0500767 /// \note Better version than memberName(). Allows embedded nulls.
Christopher Dunnc28610f2015-02-21 11:44:16 -0600768 char const* memberName(char const** end) const;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000769
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000770protected:
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000771 Value& deref() const;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000772
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000773 void increment();
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000774
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000775 void decrement();
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000776
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000777 difference_type computeDistance(const SelfType& other) const;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000778
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000779 bool isEqual(const SelfType& other) const;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000780
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000781 void copy(const SelfType& other);
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000782
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000783private:
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000784 Value::ObjectValues::iterator current_;
785 // Indicates that iterator is for a null value.
786 bool isNull_;
Christopher Dunn2a10f4a2015-04-28 04:55:12 +0100787
788public:
789 // For some reason, BORLAND needs these at the end, rather
790 // than earlier. No idea why.
791 ValueIteratorBase();
792 explicit ValueIteratorBase(const Value::ObjectValues::iterator& current);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000793};
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000794
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000795/** \brief const iterator for object and array value.
796 *
797 */
798class JSON_API ValueConstIterator : public ValueIteratorBase {
799 friend class Value;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000800
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000801public:
802 typedef const Value value_type;
Billy Donahueb5e1fe82018-05-20 16:55:27 -0400803 // typedef unsigned int size_t;
804 // typedef int difference_type;
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000805 typedef const Value& reference;
806 typedef const Value* pointer;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000807 typedef ValueConstIterator SelfType;
808
809 ValueConstIterator();
ycqiuc8a8cfc2015-10-06 16:46:19 +0800810 ValueConstIterator(ValueIterator const& other);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000811
812private:
Billy Donahueb5e1fe82018-05-20 16:55:27 -0400813 /*! \internal Use by Value to create an iterator.
814 */
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000815 explicit ValueConstIterator(const Value::ObjectValues::iterator& current);
Billy Donahueb5e1fe82018-05-20 16:55:27 -0400816
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000817public:
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000818 SelfType& operator=(const ValueIteratorBase& other);
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000819
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000820 SelfType operator++(int) {
821 SelfType temp(*this);
822 ++*this;
823 return temp;
824 }
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000825
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000826 SelfType operator--(int) {
827 SelfType temp(*this);
828 --*this;
829 return temp;
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 decrement();
834 return *this;
835 }
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000836
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000837 SelfType& operator++() {
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000838 increment();
839 return *this;
840 }
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000841
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000842 reference operator*() const { return deref(); }
Braden McDorman540db3b2014-09-14 02:31:23 -0500843
844 pointer operator->() const { return &deref(); }
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000845};
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000846
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000847/** \brief Iterator for object and array value.
848 */
849class JSON_API ValueIterator : public ValueIteratorBase {
850 friend class Value;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000851
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000852public:
853 typedef Value value_type;
854 typedef unsigned int size_t;
855 typedef int difference_type;
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000856 typedef Value& reference;
857 typedef Value* pointer;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000858 typedef ValueIterator SelfType;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000859
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000860 ValueIterator();
ycqiuc8a8cfc2015-10-06 16:46:19 +0800861 explicit ValueIterator(const ValueConstIterator& other);
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000862 ValueIterator(const ValueIterator& other);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000863
864private:
Billy Donahueb5e1fe82018-05-20 16:55:27 -0400865 /*! \internal Use by Value to create an iterator.
866 */
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000867 explicit ValueIterator(const Value::ObjectValues::iterator& current);
Billy Donahueb5e1fe82018-05-20 16:55:27 -0400868
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000869public:
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000870 SelfType& operator=(const SelfType& other);
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000871
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000872 SelfType operator++(int) {
873 SelfType temp(*this);
874 ++*this;
875 return temp;
876 }
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000877
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000878 SelfType operator--(int) {
879 SelfType temp(*this);
880 --*this;
881 return temp;
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 decrement();
886 return *this;
887 }
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000888
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000889 SelfType& operator++() {
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000890 increment();
891 return *this;
892 }
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000893
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000894 reference operator*() const { return deref(); }
Braden McDorman540db3b2014-09-14 02:31:23 -0500895
896 pointer operator->() const { return &deref(); }
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000897};
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000898
Billy Donahue1d956282018-03-06 12:51:58 -0500899inline void swap(Value& a, Value& b) { a.swap(b); }
900
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000901} // namespace Json
902
Sergiy80d6e666f2016-12-03 22:29:14 +0200903#pragma pack(pop)
datadiode9454e682015-01-20 15:25:04 -0600904
Baptiste Lepilleureafd7022013-05-08 20:21:11 +0000905#if defined(JSONCPP_DISABLE_DLL_INTERFACE_WARNING)
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000906#pragma warning(pop)
Baptiste Lepilleureafd7022013-05-08 20:21:11 +0000907#endif // if defined(JSONCPP_DISABLE_DLL_INTERFACE_WARNING)
908
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000909#endif // CPPTL_JSON_H_INCLUDED