blob: df1eba6acb2fe92b838c128a22d904dd17d63316 [file] [log] [blame]
Haibo Huangb0bee822021-02-24 15:40:15 -08001// Copyright 2007-2010 Baptiste Lepilleur and The JsonCpp Authors
Leon Scroggins IIIf59fb0e2014-05-28 15:19:42 -04002// 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
Haibo Huangb0bee822021-02-24 15:40:15 -08006#ifndef JSON_H_INCLUDED
7#define JSON_H_INCLUDED
Leon Scroggins IIIf59fb0e2014-05-28 15:19:42 -04008
9#if !defined(JSON_IS_AMALGAMATION)
Derek Sollenberger2eb3b4d2016-01-11 14:41:40 -050010#include "forwards.h"
Leon Scroggins IIIf59fb0e2014-05-28 15:19:42 -040011#endif // if !defined(JSON_IS_AMALGAMATION)
Haibo Huangb0bee822021-02-24 15:40:15 -080012
13// Conditional NORETURN attribute on the throw functions would:
14// a) suppress false positives from static code analysis
15// b) possibly improve optimization opportunities.
16#if !defined(JSONCPP_NORETURN)
17#if defined(_MSC_VER) && _MSC_VER == 1800
18#define JSONCPP_NORETURN __declspec(noreturn)
19#else
20#define JSONCPP_NORETURN [[noreturn]]
21#endif
22#endif
23
24// Support for '= delete' with template declarations was a late addition
25// to the c++11 standard and is rejected by clang 3.8 and Apple clang 8.2
26// even though these declare themselves to be c++11 compilers.
27#if !defined(JSONCPP_TEMPLATE_DELETE)
28#if defined(__clang__) && defined(__apple_build_version__)
29#if __apple_build_version__ <= 8000042
30#define JSONCPP_TEMPLATE_DELETE
31#endif
32#elif defined(__clang__)
33#if __clang_major__ == 3 && __clang_minor__ <= 8
34#define JSONCPP_TEMPLATE_DELETE
35#endif
36#endif
37#if !defined(JSONCPP_TEMPLATE_DELETE)
38#define JSONCPP_TEMPLATE_DELETE = delete
39#endif
40#endif
41
42#include <array>
43#include <exception>
44#include <map>
45#include <memory>
Derek Sollenberger2eb3b4d2016-01-11 14:41:40 -050046#include <string>
47#include <vector>
Leon Scroggins IIIf59fb0e2014-05-28 15:19:42 -040048
Derek Sollenberger2eb3b4d2016-01-11 14:41:40 -050049// Disable warning C4251: <data member>: <type> needs to have dll-interface to
50// be used by...
51#if defined(JSONCPP_DISABLE_DLL_INTERFACE_WARNING)
52#pragma warning(push)
53#pragma warning(disable : 4251)
54#endif // if defined(JSONCPP_DISABLE_DLL_INTERFACE_WARNING)
Leon Scroggins IIIf59fb0e2014-05-28 15:19:42 -040055
Haibo Huangb0bee822021-02-24 15:40:15 -080056#pragma pack(push, 8)
57
Leon Scroggins IIIf59fb0e2014-05-28 15:19:42 -040058/** \brief JSON (JavaScript Object Notation).
59 */
60namespace Json {
61
Haibo Huangb0bee822021-02-24 15:40:15 -080062#if JSON_USE_EXCEPTION
63/** Base class for all exceptions we throw.
64 *
65 * We use nothing but these internally. Of course, STL can throw others.
66 */
67class JSON_API Exception : public std::exception {
68public:
69 Exception(String msg);
70 ~Exception() noexcept override;
71 char const* what() const noexcept override;
72
73protected:
74 String msg_;
75};
76
77/** Exceptions which the user cannot easily avoid.
78 *
79 * E.g. out-of-memory (when we use malloc), stack-overflow, malicious input
80 *
81 * \remark derived from Json::Exception
82 */
83class JSON_API RuntimeError : public Exception {
84public:
85 RuntimeError(String const& msg);
86};
87
88/** Exceptions thrown by JSON_ASSERT/JSON_FAIL macros.
89 *
90 * These are precondition-violations (user bugs) and internal errors (our bugs).
91 *
92 * \remark derived from Json::Exception
93 */
94class JSON_API LogicError : public Exception {
95public:
96 LogicError(String const& msg);
97};
98#endif
99
100/// used internally
101JSONCPP_NORETURN void throwRuntimeError(String const& msg);
102/// used internally
103JSONCPP_NORETURN void throwLogicError(String const& msg);
104
Derek Sollenberger2eb3b4d2016-01-11 14:41:40 -0500105/** \brief Type of the value held by a Value object.
106 */
107enum ValueType {
108 nullValue = 0, ///< 'null' value
109 intValue, ///< signed integer value
110 uintValue, ///< unsigned integer value
111 realValue, ///< double value
112 stringValue, ///< UTF-8 string value
113 booleanValue, ///< bool value
114 arrayValue, ///< array value (ordered list)
115 objectValue ///< object value (collection of name/value pairs).
116};
Leon Scroggins IIIf59fb0e2014-05-28 15:19:42 -0400117
Derek Sollenberger2eb3b4d2016-01-11 14:41:40 -0500118enum CommentPlacement {
119 commentBefore = 0, ///< a comment placed on the line before a value
120 commentAfterOnSameLine, ///< a comment just after a value on the same line
121 commentAfter, ///< a comment on the line after a value (only make sense for
122 /// root value)
123 numberOfCommentPlacement
124};
Leon Scroggins IIIf59fb0e2014-05-28 15:19:42 -0400125
Haibo Huangb0bee822021-02-24 15:40:15 -0800126/** \brief Type of precision for formatting of real values.
127 */
128enum PrecisionType {
129 significantDigits = 0, ///< we set max number of significant digits in string
130 decimalPlaces ///< we set max number of digits after "." in string
131};
Leon Scroggins IIIf59fb0e2014-05-28 15:19:42 -0400132
Derek Sollenberger2eb3b4d2016-01-11 14:41:40 -0500133/** \brief Lightweight wrapper to tag static string.
134 *
Haibo Huangb0bee822021-02-24 15:40:15 -0800135 * Value constructor and objectValue member assignment takes advantage of the
Derek Sollenberger2eb3b4d2016-01-11 14:41:40 -0500136 * StaticString and avoid the cost of string duplication when storing the
137 * string or the member name.
138 *
139 * Example of usage:
140 * \code
141 * Json::Value aValue( StaticString("some text") );
142 * Json::Value object;
143 * static const StaticString code("code");
144 * object[code] = 1234;
145 * \endcode
146 */
147class JSON_API StaticString {
148public:
Haibo Huangb0bee822021-02-24 15:40:15 -0800149 explicit StaticString(const char* czstring) : c_str_(czstring) {}
Leon Scroggins IIIf59fb0e2014-05-28 15:19:42 -0400150
Haibo Huangb0bee822021-02-24 15:40:15 -0800151 operator const char*() const { return c_str_; }
Leon Scroggins IIIf59fb0e2014-05-28 15:19:42 -0400152
Haibo Huangb0bee822021-02-24 15:40:15 -0800153 const char* c_str() const { return c_str_; }
Leon Scroggins IIIf59fb0e2014-05-28 15:19:42 -0400154
Derek Sollenberger2eb3b4d2016-01-11 14:41:40 -0500155private:
Haibo Huangb0bee822021-02-24 15:40:15 -0800156 const char* c_str_;
Derek Sollenberger2eb3b4d2016-01-11 14:41:40 -0500157};
Leon Scroggins IIIf59fb0e2014-05-28 15:19:42 -0400158
Derek Sollenberger2eb3b4d2016-01-11 14:41:40 -0500159/** \brief Represents a <a HREF="http://www.json.org">JSON</a> value.
160 *
161 * This class is a discriminated union wrapper that can represents a:
162 * - signed integer [range: Value::minInt - Value::maxInt]
163 * - unsigned integer (range: 0 - Value::maxUInt)
164 * - double
165 * - UTF-8 string
166 * - boolean
167 * - 'null'
168 * - an ordered list of Value
169 * - collection of name/value pairs (javascript object)
170 *
171 * The type of the held value is represented by a #ValueType and
172 * can be obtained using type().
173 *
Haibo Huangb0bee822021-02-24 15:40:15 -0800174 * Values of an #objectValue or #arrayValue can be accessed using operator[]()
175 * methods.
176 * Non-const methods will automatically create the a #nullValue element
Derek Sollenberger2eb3b4d2016-01-11 14:41:40 -0500177 * if it does not exist.
Haibo Huangb0bee822021-02-24 15:40:15 -0800178 * The sequence of an #arrayValue will be automatically resized and initialized
Derek Sollenberger2eb3b4d2016-01-11 14:41:40 -0500179 * with #nullValue. resize() can be used to enlarge or truncate an #arrayValue.
180 *
Haibo Huangb0bee822021-02-24 15:40:15 -0800181 * The get() methods can be used to obtain default value in the case the
182 * required element does not exist.
Derek Sollenberger2eb3b4d2016-01-11 14:41:40 -0500183 *
Haibo Huangb0bee822021-02-24 15:40:15 -0800184 * It is possible to iterate over the list of member keys of an object using
Derek Sollenberger2eb3b4d2016-01-11 14:41:40 -0500185 * the getMemberNames() method.
Haibo Huangb0bee822021-02-24 15:40:15 -0800186 *
187 * \note #Value string-length fit in size_t, but keys must be < 2^30.
188 * (The reason is an implementation detail.) A #CharReader will raise an
189 * exception if a bound is exceeded to avoid security holes in your app,
190 * but the Value API does *not* check bounds. That is the responsibility
191 * of the caller.
Derek Sollenberger2eb3b4d2016-01-11 14:41:40 -0500192 */
193class JSON_API Value {
194 friend class ValueIteratorBase;
Leon Scroggins IIIf59fb0e2014-05-28 15:19:42 -0400195
Haibo Huangb0bee822021-02-24 15:40:15 -0800196public:
197 using Members = std::vector<String>;
198 using iterator = ValueIterator;
199 using const_iterator = ValueConstIterator;
200 using UInt = Json::UInt;
201 using Int = Json::Int;
202#if defined(JSON_HAS_INT64)
203 using UInt64 = Json::UInt64;
204 using Int64 = Json::Int64;
205#endif // defined(JSON_HAS_INT64)
206 using LargestInt = Json::LargestInt;
207 using LargestUInt = Json::LargestUInt;
208 using ArrayIndex = Json::ArrayIndex;
209
210 // Required for boost integration, e. g. BOOST_TEST
211 using value_type = std::string;
212
213#if JSON_USE_NULLREF
214 // Binary compatibility kludges, do not use.
Derek Sollenberger2eb3b4d2016-01-11 14:41:40 -0500215 static const Value& null;
Haibo Huangb0bee822021-02-24 15:40:15 -0800216 static const Value& nullRef;
217#endif
218
219 // null and nullRef are deprecated, use this instead.
220 static Value const& nullSingleton();
221
Derek Sollenberger2eb3b4d2016-01-11 14:41:40 -0500222 /// Minimum signed integer value that can be stored in a Json::Value.
Haibo Huangb0bee822021-02-24 15:40:15 -0800223 static constexpr LargestInt minLargestInt =
224 LargestInt(~(LargestUInt(-1) / 2));
Derek Sollenberger2eb3b4d2016-01-11 14:41:40 -0500225 /// Maximum signed integer value that can be stored in a Json::Value.
Haibo Huangb0bee822021-02-24 15:40:15 -0800226 static constexpr LargestInt maxLargestInt = LargestInt(LargestUInt(-1) / 2);
Derek Sollenberger2eb3b4d2016-01-11 14:41:40 -0500227 /// Maximum unsigned integer value that can be stored in a Json::Value.
Haibo Huangb0bee822021-02-24 15:40:15 -0800228 static constexpr LargestUInt maxLargestUInt = LargestUInt(-1);
Leon Scroggins IIIf59fb0e2014-05-28 15:19:42 -0400229
Derek Sollenberger2eb3b4d2016-01-11 14:41:40 -0500230 /// Minimum signed int value that can be stored in a Json::Value.
Haibo Huangb0bee822021-02-24 15:40:15 -0800231 static constexpr Int minInt = Int(~(UInt(-1) / 2));
Derek Sollenberger2eb3b4d2016-01-11 14:41:40 -0500232 /// Maximum signed int value that can be stored in a Json::Value.
Haibo Huangb0bee822021-02-24 15:40:15 -0800233 static constexpr Int maxInt = Int(UInt(-1) / 2);
Derek Sollenberger2eb3b4d2016-01-11 14:41:40 -0500234 /// Maximum unsigned int value that can be stored in a Json::Value.
Haibo Huangb0bee822021-02-24 15:40:15 -0800235 static constexpr UInt maxUInt = UInt(-1);
Leon Scroggins IIIf59fb0e2014-05-28 15:19:42 -0400236
Derek Sollenberger2eb3b4d2016-01-11 14:41:40 -0500237#if defined(JSON_HAS_INT64)
238 /// Minimum signed 64 bits int value that can be stored in a Json::Value.
Haibo Huangb0bee822021-02-24 15:40:15 -0800239 static constexpr Int64 minInt64 = Int64(~(UInt64(-1) / 2));
Derek Sollenberger2eb3b4d2016-01-11 14:41:40 -0500240 /// Maximum signed 64 bits int value that can be stored in a Json::Value.
Haibo Huangb0bee822021-02-24 15:40:15 -0800241 static constexpr Int64 maxInt64 = Int64(UInt64(-1) / 2);
Derek Sollenberger2eb3b4d2016-01-11 14:41:40 -0500242 /// Maximum unsigned 64 bits int value that can be stored in a Json::Value.
Haibo Huangb0bee822021-02-24 15:40:15 -0800243 static constexpr UInt64 maxUInt64 = UInt64(-1);
Leon Scroggins IIIf59fb0e2014-05-28 15:19:42 -0400244#endif // defined(JSON_HAS_INT64)
Haibo Huangb0bee822021-02-24 15:40:15 -0800245 /// Default precision for real value for string representation.
246 static constexpr UInt defaultRealPrecision = 17;
247 // The constant is hard-coded because some compiler have trouble
248 // converting Value::maxUInt64 to a double correctly (AIX/xlC).
249 // Assumes that UInt64 is a 64 bits integer.
250 static constexpr double maxUInt64AsDouble = 18446744073709551615.0;
251// Workaround for bug in the NVIDIAs CUDA 9.1 nvcc compiler
252// when using gcc and clang backend compilers. CZString
253// cannot be defined as private. See issue #486
254#ifdef __NVCC__
255public:
256#else
Derek Sollenberger2eb3b4d2016-01-11 14:41:40 -0500257private:
Haibo Huangb0bee822021-02-24 15:40:15 -0800258#endif
Leon Scroggins IIIf59fb0e2014-05-28 15:19:42 -0400259#ifndef JSONCPP_DOC_EXCLUDE_IMPLEMENTATION
Derek Sollenberger2eb3b4d2016-01-11 14:41:40 -0500260 class CZString {
261 public:
Haibo Huangb0bee822021-02-24 15:40:15 -0800262 enum DuplicationPolicy { noDuplication = 0, duplicate, duplicateOnCopy };
Derek Sollenberger2eb3b4d2016-01-11 14:41:40 -0500263 CZString(ArrayIndex index);
Haibo Huangb0bee822021-02-24 15:40:15 -0800264 CZString(char const* str, unsigned length, DuplicationPolicy allocate);
265 CZString(CZString const& other);
266 CZString(CZString&& other);
Derek Sollenberger2eb3b4d2016-01-11 14:41:40 -0500267 ~CZString();
Haibo Huangb0bee822021-02-24 15:40:15 -0800268 CZString& operator=(const CZString& other);
269 CZString& operator=(CZString&& other);
270
271 bool operator<(CZString const& other) const;
272 bool operator==(CZString const& other) const;
Derek Sollenberger2eb3b4d2016-01-11 14:41:40 -0500273 ArrayIndex index() const;
Haibo Huangb0bee822021-02-24 15:40:15 -0800274 // const char* c_str() const; ///< \deprecated
275 char const* data() const;
276 unsigned length() const;
Derek Sollenberger2eb3b4d2016-01-11 14:41:40 -0500277 bool isStaticString() const;
Leon Scroggins IIIf59fb0e2014-05-28 15:19:42 -0400278
Derek Sollenberger2eb3b4d2016-01-11 14:41:40 -0500279 private:
280 void swap(CZString& other);
Haibo Huangb0bee822021-02-24 15:40:15 -0800281
282 struct StringStorage {
283 unsigned policy_ : 2;
284 unsigned length_ : 30; // 1GB max
285 };
286
287 char const* cstr_; // actually, a prefixed string, unless policy is noDup
288 union {
289 ArrayIndex index_;
290 StringStorage storage_;
291 };
Derek Sollenberger2eb3b4d2016-01-11 14:41:40 -0500292 };
293
294public:
Derek Sollenberger2eb3b4d2016-01-11 14:41:40 -0500295 typedef std::map<CZString, Value> ObjectValues;
Leon Scroggins IIIf59fb0e2014-05-28 15:19:42 -0400296#endif // ifndef JSONCPP_DOC_EXCLUDE_IMPLEMENTATION
297
Derek Sollenberger2eb3b4d2016-01-11 14:41:40 -0500298public:
Haibo Huangb0bee822021-02-24 15:40:15 -0800299 /**
300 * \brief Create a default Value of the given type.
301 *
302 * This is a very useful constructor.
303 * To create an empty array, pass arrayValue.
304 * To create an empty object, pass objectValue.
305 * Another Value can then be set to this one by assignment.
306 * This is useful since clear() and resize() will not alter types.
307 *
308 * Examples:
309 * \code
310 * Json::Value null_value; // null
311 * Json::Value arr_value(Json::arrayValue); // []
312 * Json::Value obj_value(Json::objectValue); // {}
313 * \endcode
314 */
Derek Sollenberger2eb3b4d2016-01-11 14:41:40 -0500315 Value(ValueType type = nullValue);
316 Value(Int value);
317 Value(UInt value);
Leon Scroggins IIIf59fb0e2014-05-28 15:19:42 -0400318#if defined(JSON_HAS_INT64)
Derek Sollenberger2eb3b4d2016-01-11 14:41:40 -0500319 Value(Int64 value);
320 Value(UInt64 value);
Leon Scroggins IIIf59fb0e2014-05-28 15:19:42 -0400321#endif // if defined(JSON_HAS_INT64)
Derek Sollenberger2eb3b4d2016-01-11 14:41:40 -0500322 Value(double value);
Haibo Huangb0bee822021-02-24 15:40:15 -0800323 Value(const char* value); ///< Copy til first 0. (NULL causes to seg-fault.)
324 Value(const char* begin, const char* end); ///< Copy all, incl zeroes.
325 /**
326 * \brief Constructs a value from a static string.
327 *
Derek Sollenberger2eb3b4d2016-01-11 14:41:40 -0500328 * Like other value string constructor but do not duplicate the string for
Haibo Huangb0bee822021-02-24 15:40:15 -0800329 * internal storage. The given string must remain alive after the call to
330 * this constructor.
331 *
332 * \note This works only for null-terminated strings. (We cannot change the
333 * size of this class, so we have nowhere to store the length, which might be
334 * computed later for various operations.)
335 *
Derek Sollenberger2eb3b4d2016-01-11 14:41:40 -0500336 * Example of usage:
Haibo Huangb0bee822021-02-24 15:40:15 -0800337 * \code
338 * static StaticString foo("some text");
339 * Json::Value aValue(foo);
340 * \endcode
Derek Sollenberger2eb3b4d2016-01-11 14:41:40 -0500341 */
342 Value(const StaticString& value);
Haibo Huangb0bee822021-02-24 15:40:15 -0800343 Value(const String& value);
Derek Sollenberger2eb3b4d2016-01-11 14:41:40 -0500344 Value(bool value);
Haibo Huangb0bee822021-02-24 15:40:15 -0800345 Value(std::nullptr_t ptr) = delete;
Derek Sollenberger2eb3b4d2016-01-11 14:41:40 -0500346 Value(const Value& other);
Haibo Huangb0bee822021-02-24 15:40:15 -0800347 Value(Value&& other);
Derek Sollenberger2eb3b4d2016-01-11 14:41:40 -0500348 ~Value();
Leon Scroggins IIIf59fb0e2014-05-28 15:19:42 -0400349
Haibo Huangb0bee822021-02-24 15:40:15 -0800350 /// \note Overwrite existing comments. To preserve comments, use
351 /// #swapPayload().
352 Value& operator=(const Value& other);
353 Value& operator=(Value&& other);
354
355 /// Swap everything.
Derek Sollenberger2eb3b4d2016-01-11 14:41:40 -0500356 void swap(Value& other);
Haibo Huangb0bee822021-02-24 15:40:15 -0800357 /// Swap values but leave comments and source offsets in place.
358 void swapPayload(Value& other);
359
360 /// copy everything.
361 void copy(const Value& other);
362 /// copy values but leave comments and source offsets in place.
363 void copyPayload(const Value& other);
Leon Scroggins IIIf59fb0e2014-05-28 15:19:42 -0400364
Derek Sollenberger2eb3b4d2016-01-11 14:41:40 -0500365 ValueType type() const;
Leon Scroggins IIIf59fb0e2014-05-28 15:19:42 -0400366
Haibo Huangb0bee822021-02-24 15:40:15 -0800367 /// Compare payload only, not comments etc.
Derek Sollenberger2eb3b4d2016-01-11 14:41:40 -0500368 bool operator<(const Value& other) const;
369 bool operator<=(const Value& other) const;
370 bool operator>=(const Value& other) const;
371 bool operator>(const Value& other) const;
Derek Sollenberger2eb3b4d2016-01-11 14:41:40 -0500372 bool operator==(const Value& other) const;
373 bool operator!=(const Value& other) const;
Derek Sollenberger2eb3b4d2016-01-11 14:41:40 -0500374 int compare(const Value& other) const;
Leon Scroggins IIIf59fb0e2014-05-28 15:19:42 -0400375
Haibo Huangb0bee822021-02-24 15:40:15 -0800376 const char* asCString() const; ///< Embedded zeroes could cause you trouble!
377#if JSONCPP_USING_SECURE_MEMORY
378 unsigned getCStringLength() const; // Allows you to understand the length of
379 // the CString
Derek Sollenberger2eb3b4d2016-01-11 14:41:40 -0500380#endif
Haibo Huangb0bee822021-02-24 15:40:15 -0800381 String asString() const; ///< Embedded zeroes are possible.
382 /** Get raw char* of string-value.
383 * \return false if !string. (Seg-fault if str or end are NULL.)
384 */
385 bool getString(char const** begin, char const** end) const;
Derek Sollenberger2eb3b4d2016-01-11 14:41:40 -0500386 Int asInt() const;
387 UInt asUInt() const;
Leon Scroggins IIIf59fb0e2014-05-28 15:19:42 -0400388#if defined(JSON_HAS_INT64)
Derek Sollenberger2eb3b4d2016-01-11 14:41:40 -0500389 Int64 asInt64() const;
390 UInt64 asUInt64() const;
Leon Scroggins IIIf59fb0e2014-05-28 15:19:42 -0400391#endif // if defined(JSON_HAS_INT64)
Derek Sollenberger2eb3b4d2016-01-11 14:41:40 -0500392 LargestInt asLargestInt() const;
393 LargestUInt asLargestUInt() const;
394 float asFloat() const;
395 double asDouble() const;
396 bool asBool() const;
Leon Scroggins IIIf59fb0e2014-05-28 15:19:42 -0400397
Derek Sollenberger2eb3b4d2016-01-11 14:41:40 -0500398 bool isNull() const;
399 bool isBool() const;
400 bool isInt() const;
401 bool isInt64() const;
402 bool isUInt() const;
403 bool isUInt64() const;
404 bool isIntegral() const;
405 bool isDouble() const;
406 bool isNumeric() const;
407 bool isString() const;
408 bool isArray() const;
409 bool isObject() const;
Leon Scroggins IIIf59fb0e2014-05-28 15:19:42 -0400410
Haibo Huangb0bee822021-02-24 15:40:15 -0800411 /// The `as<T>` and `is<T>` member function templates and specializations.
412 template <typename T> T as() const JSONCPP_TEMPLATE_DELETE;
413 template <typename T> bool is() const JSONCPP_TEMPLATE_DELETE;
414
Derek Sollenberger2eb3b4d2016-01-11 14:41:40 -0500415 bool isConvertibleTo(ValueType other) const;
Leon Scroggins IIIf59fb0e2014-05-28 15:19:42 -0400416
Derek Sollenberger2eb3b4d2016-01-11 14:41:40 -0500417 /// Number of values in array or object
418 ArrayIndex size() const;
Leon Scroggins IIIf59fb0e2014-05-28 15:19:42 -0400419
Derek Sollenberger2eb3b4d2016-01-11 14:41:40 -0500420 /// \brief Return true if empty array, empty object, or null;
421 /// otherwise, false.
422 bool empty() const;
Leon Scroggins IIIf59fb0e2014-05-28 15:19:42 -0400423
Haibo Huangb0bee822021-02-24 15:40:15 -0800424 /// Return !isNull()
425 explicit operator bool() const;
Leon Scroggins IIIf59fb0e2014-05-28 15:19:42 -0400426
Derek Sollenberger2eb3b4d2016-01-11 14:41:40 -0500427 /// Remove all object members and array elements.
428 /// \pre type() is arrayValue, objectValue, or nullValue
429 /// \post type() is unchanged
430 void clear();
Leon Scroggins IIIf59fb0e2014-05-28 15:19:42 -0400431
Haibo Huangb0bee822021-02-24 15:40:15 -0800432 /// Resize the array to newSize elements.
Derek Sollenberger2eb3b4d2016-01-11 14:41:40 -0500433 /// New elements are initialized to null.
434 /// May only be called on nullValue or arrayValue.
435 /// \pre type() is arrayValue or nullValue
436 /// \post type() is arrayValue
Haibo Huangb0bee822021-02-24 15:40:15 -0800437 void resize(ArrayIndex newSize);
Leon Scroggins IIIf59fb0e2014-05-28 15:19:42 -0400438
Haibo Huangb0bee822021-02-24 15:40:15 -0800439 //@{
440 /// Access an array element (zero based index). If the array contains less
441 /// than index element, then null value are inserted in the array so that
442 /// its size is index+1.
Derek Sollenberger2eb3b4d2016-01-11 14:41:40 -0500443 /// (You may need to say 'value[0u]' to get your compiler to distinguish
Haibo Huangb0bee822021-02-24 15:40:15 -0800444 /// this from the operator[] which takes a string.)
Derek Sollenberger2eb3b4d2016-01-11 14:41:40 -0500445 Value& operator[](ArrayIndex index);
Derek Sollenberger2eb3b4d2016-01-11 14:41:40 -0500446 Value& operator[](int index);
Haibo Huangb0bee822021-02-24 15:40:15 -0800447 //@}
Leon Scroggins IIIf59fb0e2014-05-28 15:19:42 -0400448
Haibo Huangb0bee822021-02-24 15:40:15 -0800449 //@{
450 /// Access an array element (zero based index).
Derek Sollenberger2eb3b4d2016-01-11 14:41:40 -0500451 /// (You may need to say 'value[0u]' to get your compiler to distinguish
Haibo Huangb0bee822021-02-24 15:40:15 -0800452 /// this from the operator[] which takes a string.)
Derek Sollenberger2eb3b4d2016-01-11 14:41:40 -0500453 const Value& operator[](ArrayIndex index) const;
Derek Sollenberger2eb3b4d2016-01-11 14:41:40 -0500454 const Value& operator[](int index) const;
Haibo Huangb0bee822021-02-24 15:40:15 -0800455 //@}
Leon Scroggins IIIf59fb0e2014-05-28 15:19:42 -0400456
Derek Sollenberger2eb3b4d2016-01-11 14:41:40 -0500457 /// If the array contains at least index+1 elements, returns the element
Haibo Huangb0bee822021-02-24 15:40:15 -0800458 /// value, otherwise returns defaultValue.
Derek Sollenberger2eb3b4d2016-01-11 14:41:40 -0500459 Value get(ArrayIndex index, const Value& defaultValue) const;
460 /// Return true if index < size().
461 bool isValidIndex(ArrayIndex index) const;
462 /// \brief Append value to array at the end.
463 ///
464 /// Equivalent to jsonvalue[jsonvalue.size()] = value;
465 Value& append(const Value& value);
Haibo Huangb0bee822021-02-24 15:40:15 -0800466 Value& append(Value&& value);
467
468 /// \brief Insert value in array at specific index
469 bool insert(ArrayIndex index, const Value& newValue);
470 bool insert(ArrayIndex index, Value&& newValue);
Leon Scroggins IIIf59fb0e2014-05-28 15:19:42 -0400471
Derek Sollenberger2eb3b4d2016-01-11 14:41:40 -0500472 /// Access an object value by name, create a null member if it does not exist.
Haibo Huangb0bee822021-02-24 15:40:15 -0800473 /// \note Because of our implementation, keys are limited to 2^30 -1 chars.
474 /// Exceeding that will cause an exception.
Derek Sollenberger2eb3b4d2016-01-11 14:41:40 -0500475 Value& operator[](const char* key);
476 /// Access an object value by name, returns null if there is no member with
477 /// that name.
478 const Value& operator[](const char* key) const;
479 /// Access an object value by name, create a null member if it does not exist.
Haibo Huangb0bee822021-02-24 15:40:15 -0800480 /// \param key may contain embedded nulls.
481 Value& operator[](const String& key);
Derek Sollenberger2eb3b4d2016-01-11 14:41:40 -0500482 /// Access an object value by name, returns null if there is no member with
483 /// that name.
Haibo Huangb0bee822021-02-24 15:40:15 -0800484 /// \param key may contain embedded nulls.
485 const Value& operator[](const String& key) const;
Derek Sollenberger2eb3b4d2016-01-11 14:41:40 -0500486 /** \brief Access an object value by name, create a null member if it does not
Haibo Huangb0bee822021-02-24 15:40:15 -0800487 * exist.
488 *
489 * If the object has no entry for that name, then the member name used to
490 * store the new entry is not duplicated.
Derek Sollenberger2eb3b4d2016-01-11 14:41:40 -0500491 * Example of use:
Haibo Huangb0bee822021-02-24 15:40:15 -0800492 * \code
493 * Json::Value object;
494 * static const StaticString code("code");
495 * object[code] = 1234;
496 * \endcode
Derek Sollenberger2eb3b4d2016-01-11 14:41:40 -0500497 */
498 Value& operator[](const StaticString& key);
Derek Sollenberger2eb3b4d2016-01-11 14:41:40 -0500499 /// Return the member named key if it exist, defaultValue otherwise.
Haibo Huangb0bee822021-02-24 15:40:15 -0800500 /// \note deep copy
Derek Sollenberger2eb3b4d2016-01-11 14:41:40 -0500501 Value get(const char* key, const Value& defaultValue) const;
502 /// Return the member named key if it exist, defaultValue otherwise.
Haibo Huangb0bee822021-02-24 15:40:15 -0800503 /// \note deep copy
504 /// \note key may contain embedded nulls.
505 Value get(const char* begin, const char* end,
506 const Value& defaultValue) const;
Derek Sollenberger2eb3b4d2016-01-11 14:41:40 -0500507 /// Return the member named key if it exist, defaultValue otherwise.
Haibo Huangb0bee822021-02-24 15:40:15 -0800508 /// \note deep copy
509 /// \param key may contain embedded nulls.
510 Value get(const String& key, const Value& defaultValue) const;
511 /// Most general and efficient version of isMember()const, get()const,
512 /// and operator[]const
513 /// \note As stated elsewhere, behavior is undefined if (end-begin) >= 2^30
514 Value const* find(char const* begin, char const* end) const;
515 /// Most general and efficient version of object-mutators.
516 /// \note As stated elsewhere, behavior is undefined if (end-begin) >= 2^30
517 /// \return non-zero, but JSON_ASSERT if this is neither object nor nullValue.
518 Value* demand(char const* begin, char const* end);
Derek Sollenberger2eb3b4d2016-01-11 14:41:40 -0500519 /// \brief Remove and return the named member.
520 ///
521 /// Do nothing if it did not exist.
Derek Sollenberger2eb3b4d2016-01-11 14:41:40 -0500522 /// \pre type() is objectValue or nullValue
523 /// \post type() is unchanged
Haibo Huangb0bee822021-02-24 15:40:15 -0800524 void removeMember(const char* key);
Derek Sollenberger2eb3b4d2016-01-11 14:41:40 -0500525 /// Same as removeMember(const char*)
Haibo Huangb0bee822021-02-24 15:40:15 -0800526 /// \param key may contain embedded nulls.
527 void removeMember(const String& key);
528 /// Same as removeMember(const char* begin, const char* end, Value* removed),
529 /// but 'key' is null-terminated.
530 bool removeMember(const char* key, Value* removed);
531 /** \brief Remove the named map member.
532 *
533 * Update 'removed' iff removed.
534 * \param key may contain embedded nulls.
535 * \return true iff removed (no exceptions)
536 */
537 bool removeMember(String const& key, Value* removed);
538 /// Same as removeMember(String const& key, Value* removed)
539 bool removeMember(const char* begin, const char* end, Value* removed);
540 /** \brief Remove the indexed array element.
541 *
542 * O(n) expensive operations.
543 * Update 'removed' iff removed.
544 * \return true if removed (no exceptions)
545 */
546 bool removeIndex(ArrayIndex index, Value* removed);
Leon Scroggins IIIf59fb0e2014-05-28 15:19:42 -0400547
Derek Sollenberger2eb3b4d2016-01-11 14:41:40 -0500548 /// Return true if the object has a member named key.
Haibo Huangb0bee822021-02-24 15:40:15 -0800549 /// \note 'key' must be null-terminated.
Derek Sollenberger2eb3b4d2016-01-11 14:41:40 -0500550 bool isMember(const char* key) const;
551 /// Return true if the object has a member named key.
Haibo Huangb0bee822021-02-24 15:40:15 -0800552 /// \param key may contain embedded nulls.
553 bool isMember(const String& key) const;
554 /// Same as isMember(String const& key)const
555 bool isMember(const char* begin, const char* end) const;
Leon Scroggins IIIf59fb0e2014-05-28 15:19:42 -0400556
Derek Sollenberger2eb3b4d2016-01-11 14:41:40 -0500557 /// \brief Return a list of the member names.
558 ///
559 /// If null, return an empty list.
560 /// \pre type() is objectValue or nullValue
561 /// \post if type() was nullValue, it remains nullValue
562 Members getMemberNames() const;
Leon Scroggins IIIf59fb0e2014-05-28 15:19:42 -0400563
Haibo Huangb0bee822021-02-24 15:40:15 -0800564 /// \deprecated Always pass len.
565 JSONCPP_DEPRECATED("Use setComment(String const&) instead.")
566 void setComment(const char* comment, CommentPlacement placement) {
567 setComment(String(comment, strlen(comment)), placement);
568 }
Derek Sollenberger2eb3b4d2016-01-11 14:41:40 -0500569 /// Comments must be //... or /* ... */
Haibo Huangb0bee822021-02-24 15:40:15 -0800570 void setComment(const char* comment, size_t len, CommentPlacement placement) {
571 setComment(String(comment, len), placement);
572 }
Derek Sollenberger2eb3b4d2016-01-11 14:41:40 -0500573 /// Comments must be //... or /* ... */
Haibo Huangb0bee822021-02-24 15:40:15 -0800574 void setComment(String comment, CommentPlacement placement);
Derek Sollenberger2eb3b4d2016-01-11 14:41:40 -0500575 bool hasComment(CommentPlacement placement) const;
576 /// Include delimiters and embedded newlines.
Haibo Huangb0bee822021-02-24 15:40:15 -0800577 String getComment(CommentPlacement placement) const;
Leon Scroggins IIIf59fb0e2014-05-28 15:19:42 -0400578
Haibo Huangb0bee822021-02-24 15:40:15 -0800579 String toStyledString() const;
Leon Scroggins IIIf59fb0e2014-05-28 15:19:42 -0400580
Derek Sollenberger2eb3b4d2016-01-11 14:41:40 -0500581 const_iterator begin() const;
582 const_iterator end() const;
Leon Scroggins IIIf59fb0e2014-05-28 15:19:42 -0400583
Derek Sollenberger2eb3b4d2016-01-11 14:41:40 -0500584 iterator begin();
585 iterator end();
Leon Scroggins IIIf59fb0e2014-05-28 15:19:42 -0400586
Derek Sollenberger2eb3b4d2016-01-11 14:41:40 -0500587 // Accessors for the [start, limit) range of bytes within the JSON text from
588 // which this value was parsed, if any.
Haibo Huangb0bee822021-02-24 15:40:15 -0800589 void setOffsetStart(ptrdiff_t start);
590 void setOffsetLimit(ptrdiff_t limit);
591 ptrdiff_t getOffsetStart() const;
592 ptrdiff_t getOffsetLimit() const;
Leon Scroggins IIIf59fb0e2014-05-28 15:19:42 -0400593
Derek Sollenberger2eb3b4d2016-01-11 14:41:40 -0500594private:
Haibo Huangb0bee822021-02-24 15:40:15 -0800595 void setType(ValueType v) {
596 bits_.value_type_ = static_cast<unsigned char>(v);
Derek Sollenberger2eb3b4d2016-01-11 14:41:40 -0500597 }
Haibo Huangb0bee822021-02-24 15:40:15 -0800598 bool isAllocated() const { return bits_.allocated_; }
599 void setIsAllocated(bool v) { bits_.allocated_ = v; }
Derek Sollenberger2eb3b4d2016-01-11 14:41:40 -0500600
Haibo Huangb0bee822021-02-24 15:40:15 -0800601 void initBasic(ValueType type, bool allocated = false);
602 void dupPayload(const Value& other);
603 void releasePayload();
604 void dupMeta(const Value& other);
Derek Sollenberger2eb3b4d2016-01-11 14:41:40 -0500605
Haibo Huangb0bee822021-02-24 15:40:15 -0800606 Value& resolveReference(const char* key);
607 Value& resolveReference(const char* key, const char* end);
Derek Sollenberger2eb3b4d2016-01-11 14:41:40 -0500608
609 // struct MemberNamesTransform
610 //{
611 // typedef const char *result_type;
612 // const char *operator()( const CZString &name ) const
613 // {
614 // return name.c_str();
615 // }
616 //};
617
618 union ValueHolder {
619 LargestInt int_;
620 LargestUInt uint_;
621 double real_;
622 bool bool_;
Haibo Huangb0bee822021-02-24 15:40:15 -0800623 char* string_; // if allocated_, ptr to { unsigned, char[] }.
Derek Sollenberger2eb3b4d2016-01-11 14:41:40 -0500624 ObjectValues* map_;
Derek Sollenberger2eb3b4d2016-01-11 14:41:40 -0500625 } value_;
Haibo Huangb0bee822021-02-24 15:40:15 -0800626
627 struct {
628 // Really a ValueType, but types should agree for bitfield packing.
629 unsigned int value_type_ : 8;
630 // Unless allocated_, string_ must be null-terminated.
631 unsigned int allocated_ : 1;
632 } bits_;
633
634 class Comments {
635 public:
636 Comments() = default;
637 Comments(const Comments& that);
638 Comments(Comments&& that);
639 Comments& operator=(const Comments& that);
640 Comments& operator=(Comments&& that);
641 bool has(CommentPlacement slot) const;
642 String get(CommentPlacement slot) const;
643 void set(CommentPlacement slot, String comment);
644
645 private:
646 using Array = std::array<String, numberOfCommentPlacement>;
647 std::unique_ptr<Array> ptr_;
648 };
649 Comments comments_;
Derek Sollenberger2eb3b4d2016-01-11 14:41:40 -0500650
651 // [start, limit) byte offsets in the source JSON text from which this Value
652 // was extracted.
Haibo Huangb0bee822021-02-24 15:40:15 -0800653 ptrdiff_t start_;
654 ptrdiff_t limit_;
Derek Sollenberger2eb3b4d2016-01-11 14:41:40 -0500655};
656
Haibo Huangb0bee822021-02-24 15:40:15 -0800657template <> inline bool Value::as<bool>() const { return asBool(); }
658template <> inline bool Value::is<bool>() const { return isBool(); }
659
660template <> inline Int Value::as<Int>() const { return asInt(); }
661template <> inline bool Value::is<Int>() const { return isInt(); }
662
663template <> inline UInt Value::as<UInt>() const { return asUInt(); }
664template <> inline bool Value::is<UInt>() const { return isUInt(); }
665
666#if defined(JSON_HAS_INT64)
667template <> inline Int64 Value::as<Int64>() const { return asInt64(); }
668template <> inline bool Value::is<Int64>() const { return isInt64(); }
669
670template <> inline UInt64 Value::as<UInt64>() const { return asUInt64(); }
671template <> inline bool Value::is<UInt64>() const { return isUInt64(); }
672#endif
673
674template <> inline double Value::as<double>() const { return asDouble(); }
675template <> inline bool Value::is<double>() const { return isDouble(); }
676
677template <> inline String Value::as<String>() const { return asString(); }
678template <> inline bool Value::is<String>() const { return isString(); }
679
680/// These `as` specializations are type conversions, and do not have a
681/// corresponding `is`.
682template <> inline float Value::as<float>() const { return asFloat(); }
683template <> inline const char* Value::as<const char*>() const {
684 return asCString();
685}
686
Derek Sollenberger2eb3b4d2016-01-11 14:41:40 -0500687/** \brief Experimental and untested: represents an element of the "path" to
688 * access a node.
689 */
690class JSON_API PathArgument {
691public:
692 friend class Path;
693
694 PathArgument();
695 PathArgument(ArrayIndex index);
696 PathArgument(const char* key);
Haibo Huangb0bee822021-02-24 15:40:15 -0800697 PathArgument(String key);
Derek Sollenberger2eb3b4d2016-01-11 14:41:40 -0500698
699private:
Haibo Huangb0bee822021-02-24 15:40:15 -0800700 enum Kind { kindNone = 0, kindIndex, kindKey };
701 String key_;
702 ArrayIndex index_{};
703 Kind kind_{kindNone};
Derek Sollenberger2eb3b4d2016-01-11 14:41:40 -0500704};
705
706/** \brief Experimental and untested: represents a "path" to access a node.
707 *
708 * Syntax:
709 * - "." => root node
710 * - ".[n]" => elements at index 'n' of root node (an array value)
711 * - ".name" => member named 'name' of root node (an object value)
712 * - ".name1.name2.name3"
713 * - ".[0][1][2].name1[3]"
714 * - ".%" => member name is provided as parameter
Haibo Huangb0bee822021-02-24 15:40:15 -0800715 * - ".[%]" => index is provided as parameter
Derek Sollenberger2eb3b4d2016-01-11 14:41:40 -0500716 */
717class JSON_API Path {
718public:
Haibo Huangb0bee822021-02-24 15:40:15 -0800719 Path(const String& path, const PathArgument& a1 = PathArgument(),
Derek Sollenberger2eb3b4d2016-01-11 14:41:40 -0500720 const PathArgument& a2 = PathArgument(),
721 const PathArgument& a3 = PathArgument(),
722 const PathArgument& a4 = PathArgument(),
723 const PathArgument& a5 = PathArgument());
724
725 const Value& resolve(const Value& root) const;
726 Value resolve(const Value& root, const Value& defaultValue) const;
727 /// Creates the "path" to access the specified node and returns a reference on
728 /// the node.
729 Value& make(Value& root) const;
730
731private:
Haibo Huangb0bee822021-02-24 15:40:15 -0800732 using InArgs = std::vector<const PathArgument*>;
733 using Args = std::vector<PathArgument>;
Derek Sollenberger2eb3b4d2016-01-11 14:41:40 -0500734
Haibo Huangb0bee822021-02-24 15:40:15 -0800735 void makePath(const String& path, const InArgs& in);
736 void addPathInArg(const String& path, const InArgs& in,
737 InArgs::const_iterator& itInArg, PathArgument::Kind kind);
738 static void invalidPath(const String& path, int location);
Derek Sollenberger2eb3b4d2016-01-11 14:41:40 -0500739
740 Args args_;
741};
742
Derek Sollenberger2eb3b4d2016-01-11 14:41:40 -0500743/** \brief base class for Value iterators.
744 *
745 */
746class JSON_API ValueIteratorBase {
747public:
Haibo Huangb0bee822021-02-24 15:40:15 -0800748 using iterator_category = std::bidirectional_iterator_tag;
749 using size_t = unsigned int;
750 using difference_type = int;
751 using SelfType = ValueIteratorBase;
Leon Scroggins IIIf59fb0e2014-05-28 15:19:42 -0400752
Derek Sollenberger2eb3b4d2016-01-11 14:41:40 -0500753 bool operator==(const SelfType& other) const { return isEqual(other); }
Leon Scroggins IIIf59fb0e2014-05-28 15:19:42 -0400754
Derek Sollenberger2eb3b4d2016-01-11 14:41:40 -0500755 bool operator!=(const SelfType& other) const { return !isEqual(other); }
Leon Scroggins IIIf59fb0e2014-05-28 15:19:42 -0400756
Derek Sollenberger2eb3b4d2016-01-11 14:41:40 -0500757 difference_type operator-(const SelfType& other) const {
Haibo Huangb0bee822021-02-24 15:40:15 -0800758 return other.computeDistance(*this);
Derek Sollenberger2eb3b4d2016-01-11 14:41:40 -0500759 }
Leon Scroggins IIIf59fb0e2014-05-28 15:19:42 -0400760
Derek Sollenberger2eb3b4d2016-01-11 14:41:40 -0500761 /// Return either the index or the member name of the referenced value as a
762 /// Value.
763 Value key() const;
Leon Scroggins IIIf59fb0e2014-05-28 15:19:42 -0400764
Haibo Huangb0bee822021-02-24 15:40:15 -0800765 /// Return the index of the referenced Value, or -1 if it is not an
766 /// arrayValue.
Derek Sollenberger2eb3b4d2016-01-11 14:41:40 -0500767 UInt index() const;
Leon Scroggins IIIf59fb0e2014-05-28 15:19:42 -0400768
Haibo Huangb0bee822021-02-24 15:40:15 -0800769 /// Return the member name of the referenced Value, or "" if it is not an
770 /// objectValue.
771 /// \note Avoid `c_str()` on result, as embedded zeroes are possible.
772 String name() const;
773
Derek Sollenberger2eb3b4d2016-01-11 14:41:40 -0500774 /// Return the member name of the referenced Value. "" if it is not an
775 /// objectValue.
Haibo Huangb0bee822021-02-24 15:40:15 -0800776 /// \deprecated This cannot be used for UTF-8 strings, since there can be
777 /// embedded nulls.
778 JSONCPP_DEPRECATED("Use `key = name();` instead.")
779 char const* memberName() const;
780 /// Return the member name of the referenced Value, or NULL if it is not an
781 /// objectValue.
782 /// \note Better version than memberName(). Allows embedded nulls.
783 char const* memberName(char const** end) const;
Leon Scroggins IIIf59fb0e2014-05-28 15:19:42 -0400784
Derek Sollenberger2eb3b4d2016-01-11 14:41:40 -0500785protected:
Haibo Huangb0bee822021-02-24 15:40:15 -0800786 /*! Internal utility functions to assist with implementing
787 * other iterator functions. The const and non-const versions
788 * of the "deref" protected methods expose the protected
789 * current_ member variable in a way that can often be
790 * optimized away by the compiler.
791 */
792 const Value& deref() const;
793 Value& deref();
Leon Scroggins IIIf59fb0e2014-05-28 15:19:42 -0400794
Derek Sollenberger2eb3b4d2016-01-11 14:41:40 -0500795 void increment();
Leon Scroggins IIIf59fb0e2014-05-28 15:19:42 -0400796
Derek Sollenberger2eb3b4d2016-01-11 14:41:40 -0500797 void decrement();
Leon Scroggins IIIf59fb0e2014-05-28 15:19:42 -0400798
Derek Sollenberger2eb3b4d2016-01-11 14:41:40 -0500799 difference_type computeDistance(const SelfType& other) const;
Leon Scroggins IIIf59fb0e2014-05-28 15:19:42 -0400800
Derek Sollenberger2eb3b4d2016-01-11 14:41:40 -0500801 bool isEqual(const SelfType& other) const;
Leon Scroggins IIIf59fb0e2014-05-28 15:19:42 -0400802
Derek Sollenberger2eb3b4d2016-01-11 14:41:40 -0500803 void copy(const SelfType& other);
Leon Scroggins IIIf59fb0e2014-05-28 15:19:42 -0400804
Derek Sollenberger2eb3b4d2016-01-11 14:41:40 -0500805private:
Derek Sollenberger2eb3b4d2016-01-11 14:41:40 -0500806 Value::ObjectValues::iterator current_;
807 // Indicates that iterator is for a null value.
Haibo Huangb0bee822021-02-24 15:40:15 -0800808 bool isNull_{true};
809
810public:
811 // For some reason, BORLAND needs these at the end, rather
812 // than earlier. No idea why.
813 ValueIteratorBase();
814 explicit ValueIteratorBase(const Value::ObjectValues::iterator& current);
Derek Sollenberger2eb3b4d2016-01-11 14:41:40 -0500815};
Leon Scroggins IIIf59fb0e2014-05-28 15:19:42 -0400816
Derek Sollenberger2eb3b4d2016-01-11 14:41:40 -0500817/** \brief const iterator for object and array value.
818 *
819 */
820class JSON_API ValueConstIterator : public ValueIteratorBase {
821 friend class Value;
Leon Scroggins IIIf59fb0e2014-05-28 15:19:42 -0400822
Derek Sollenberger2eb3b4d2016-01-11 14:41:40 -0500823public:
Haibo Huangb0bee822021-02-24 15:40:15 -0800824 using value_type = const Value;
825 // typedef unsigned int size_t;
826 // typedef int difference_type;
827 using reference = const Value&;
828 using pointer = const Value*;
829 using SelfType = ValueConstIterator;
Derek Sollenberger2eb3b4d2016-01-11 14:41:40 -0500830
831 ValueConstIterator();
Haibo Huangb0bee822021-02-24 15:40:15 -0800832 ValueConstIterator(ValueIterator const& other);
Derek Sollenberger2eb3b4d2016-01-11 14:41:40 -0500833
834private:
Haibo Huangb0bee822021-02-24 15:40:15 -0800835 /*! \internal Use by Value to create an iterator.
836 */
Derek Sollenberger2eb3b4d2016-01-11 14:41:40 -0500837 explicit ValueConstIterator(const Value::ObjectValues::iterator& current);
Haibo Huangb0bee822021-02-24 15:40:15 -0800838
Derek Sollenberger2eb3b4d2016-01-11 14:41:40 -0500839public:
840 SelfType& operator=(const ValueIteratorBase& other);
Leon Scroggins IIIf59fb0e2014-05-28 15:19:42 -0400841
Derek Sollenberger2eb3b4d2016-01-11 14:41:40 -0500842 SelfType operator++(int) {
843 SelfType temp(*this);
844 ++*this;
845 return temp;
846 }
Leon Scroggins IIIf59fb0e2014-05-28 15:19:42 -0400847
Derek Sollenberger2eb3b4d2016-01-11 14:41:40 -0500848 SelfType operator--(int) {
849 SelfType temp(*this);
850 --*this;
851 return temp;
852 }
Leon Scroggins IIIf59fb0e2014-05-28 15:19:42 -0400853
Derek Sollenberger2eb3b4d2016-01-11 14:41:40 -0500854 SelfType& operator--() {
855 decrement();
856 return *this;
857 }
Leon Scroggins IIIf59fb0e2014-05-28 15:19:42 -0400858
Derek Sollenberger2eb3b4d2016-01-11 14:41:40 -0500859 SelfType& operator++() {
860 increment();
861 return *this;
862 }
Leon Scroggins IIIf59fb0e2014-05-28 15:19:42 -0400863
Derek Sollenberger2eb3b4d2016-01-11 14:41:40 -0500864 reference operator*() const { return deref(); }
Leon Scroggins IIIf59fb0e2014-05-28 15:19:42 -0400865
Derek Sollenberger2eb3b4d2016-01-11 14:41:40 -0500866 pointer operator->() const { return &deref(); }
867};
Leon Scroggins IIIf59fb0e2014-05-28 15:19:42 -0400868
Derek Sollenberger2eb3b4d2016-01-11 14:41:40 -0500869/** \brief Iterator for object and array value.
870 */
871class JSON_API ValueIterator : public ValueIteratorBase {
872 friend class Value;
Leon Scroggins IIIf59fb0e2014-05-28 15:19:42 -0400873
Derek Sollenberger2eb3b4d2016-01-11 14:41:40 -0500874public:
Haibo Huangb0bee822021-02-24 15:40:15 -0800875 using value_type = Value;
876 using size_t = unsigned int;
877 using difference_type = int;
878 using reference = Value&;
879 using pointer = Value*;
880 using SelfType = ValueIterator;
Derek Sollenberger2eb3b4d2016-01-11 14:41:40 -0500881
882 ValueIterator();
Haibo Huangb0bee822021-02-24 15:40:15 -0800883 explicit ValueIterator(const ValueConstIterator& other);
Derek Sollenberger2eb3b4d2016-01-11 14:41:40 -0500884 ValueIterator(const ValueIterator& other);
885
886private:
Haibo Huangb0bee822021-02-24 15:40:15 -0800887 /*! \internal Use by Value to create an iterator.
888 */
Derek Sollenberger2eb3b4d2016-01-11 14:41:40 -0500889 explicit ValueIterator(const Value::ObjectValues::iterator& current);
Haibo Huangb0bee822021-02-24 15:40:15 -0800890
Derek Sollenberger2eb3b4d2016-01-11 14:41:40 -0500891public:
892 SelfType& operator=(const SelfType& other);
Leon Scroggins IIIf59fb0e2014-05-28 15:19:42 -0400893
Derek Sollenberger2eb3b4d2016-01-11 14:41:40 -0500894 SelfType operator++(int) {
895 SelfType temp(*this);
896 ++*this;
897 return temp;
898 }
Leon Scroggins IIIf59fb0e2014-05-28 15:19:42 -0400899
Derek Sollenberger2eb3b4d2016-01-11 14:41:40 -0500900 SelfType operator--(int) {
901 SelfType temp(*this);
902 --*this;
903 return temp;
904 }
Leon Scroggins IIIf59fb0e2014-05-28 15:19:42 -0400905
Derek Sollenberger2eb3b4d2016-01-11 14:41:40 -0500906 SelfType& operator--() {
907 decrement();
908 return *this;
909 }
Leon Scroggins IIIf59fb0e2014-05-28 15:19:42 -0400910
Derek Sollenberger2eb3b4d2016-01-11 14:41:40 -0500911 SelfType& operator++() {
912 increment();
913 return *this;
914 }
Leon Scroggins IIIf59fb0e2014-05-28 15:19:42 -0400915
Haibo Huangb0bee822021-02-24 15:40:15 -0800916 /*! The return value of non-const iterators can be
917 * changed, so the these functions are not const
918 * because the returned references/pointers can be used
919 * to change state of the base class.
920 */
921 reference operator*() { return deref(); }
922 pointer operator->() { return &deref(); }
Derek Sollenberger2eb3b4d2016-01-11 14:41:40 -0500923};
Leon Scroggins IIIf59fb0e2014-05-28 15:19:42 -0400924
Haibo Huangb0bee822021-02-24 15:40:15 -0800925inline void swap(Value& a, Value& b) { a.swap(b); }
926
Leon Scroggins IIIf59fb0e2014-05-28 15:19:42 -0400927} // namespace Json
928
Haibo Huangb0bee822021-02-24 15:40:15 -0800929#pragma pack(pop)
930
Derek Sollenberger2eb3b4d2016-01-11 14:41:40 -0500931#if defined(JSONCPP_DISABLE_DLL_INTERFACE_WARNING)
932#pragma warning(pop)
933#endif // if defined(JSONCPP_DISABLE_DLL_INTERFACE_WARNING)
Leon Scroggins IIIf59fb0e2014-05-28 15:19:42 -0400934
Haibo Huangb0bee822021-02-24 15:40:15 -0800935#endif // JSON_H_INCLUDED