blob: f8fe824995e1f2b6323cb20bd03f6d39077853d5 [file] [log] [blame]
Baptiste Lepilleur7469f1d2010-04-20 21:35:19 +00001// Copyright 2007-2010 Baptiste Lepilleur
2// 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)
Aaron Jacobs9fa4e842014-07-01 08:48:54 +100012#include <string>
13#include <vector>
Christopher Dunn6d135cb2007-06-13 15:51:04 +000014
Aaron Jacobs9fa4e842014-07-01 08:48:54 +100015#ifndef JSON_USE_CPPTL_SMALLMAP
16#include <map>
17#else
18#include <cpptl/smallmap.h>
19#endif
20#ifdef JSON_USE_CPPTL
21#include <cpptl/forwards.h>
22#endif
Christopher Dunn6d135cb2007-06-13 15:51:04 +000023
Aaron Jacobs9fa4e842014-07-01 08:48:54 +100024// Disable warning C4251: <data member>: <type> needs to have dll-interface to
25// be used by...
Baptiste Lepilleureafd7022013-05-08 20:21:11 +000026#if defined(JSONCPP_DISABLE_DLL_INTERFACE_WARNING)
Aaron Jacobs9fa4e842014-07-01 08:48:54 +100027#pragma warning(push)
28#pragma warning(disable : 4251)
Baptiste Lepilleureafd7022013-05-08 20:21:11 +000029#endif // if defined(JSONCPP_DISABLE_DLL_INTERFACE_WARNING)
30
Christopher Dunn6d135cb2007-06-13 15:51:04 +000031/** \brief JSON (JavaScript Object Notation).
32 */
33namespace Json {
34
Aaron Jacobs9fa4e842014-07-01 08:48:54 +100035/** \brief Type of the value held by a Value object.
36 */
37enum ValueType {
38 nullValue = 0, ///< 'null' value
39 intValue, ///< signed integer value
40 uintValue, ///< unsigned integer value
41 realValue, ///< double value
42 stringValue, ///< UTF-8 string value
43 booleanValue, ///< bool value
44 arrayValue, ///< array value (ordered list)
45 objectValue ///< object value (collection of name/value pairs).
46};
Christopher Dunn6d135cb2007-06-13 15:51:04 +000047
Aaron Jacobs9fa4e842014-07-01 08:48:54 +100048enum CommentPlacement {
49 commentBefore = 0, ///< a comment placed on the line before a value
50 commentAfterOnSameLine, ///< a comment just after a value on the same line
51 commentAfter, ///< a comment on the line after a value (only make sense for
Aaron Jacobs3a0c4fc2014-07-01 09:20:48 +100052 /// root value)
Aaron Jacobs9fa4e842014-07-01 08:48:54 +100053 numberOfCommentPlacement
54};
Christopher Dunn6d135cb2007-06-13 15:51:04 +000055
56//# ifdef JSON_USE_CPPTL
57// typedef CppTL::AnyEnumerator<const char *> EnumMemberNames;
58// typedef CppTL::AnyEnumerator<const Value &> EnumValues;
59//# endif
60
Aaron Jacobs9fa4e842014-07-01 08:48:54 +100061/** \brief Lightweight wrapper to tag static string.
62 *
63 * Value constructor and objectValue member assignement takes advantage of the
64 * StaticString and avoid the cost of string duplication when storing the
65 * string or the member name.
66 *
67 * Example of usage:
68 * \code
69 * Json::Value aValue( StaticString("some text") );
70 * Json::Value object;
71 * static const StaticString code("code");
72 * object[code] = 1234;
73 * \endcode
74 */
75class JSON_API StaticString {
76public:
Aaron Jacobs11086dd2014-09-15 10:15:29 +100077 explicit StaticString(const char* czstring) : str_(czstring) {}
Christopher Dunn6d135cb2007-06-13 15:51:04 +000078
Aaron Jacobs11086dd2014-09-15 10:15:29 +100079 operator const char*() const { return str_; }
Christopher Dunn6d135cb2007-06-13 15:51:04 +000080
Aaron Jacobs11086dd2014-09-15 10:15:29 +100081 const char* c_str() const { return str_; }
Christopher Dunn6d135cb2007-06-13 15:51:04 +000082
Aaron Jacobs9fa4e842014-07-01 08:48:54 +100083private:
Aaron Jacobs11086dd2014-09-15 10:15:29 +100084 const char* str_;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +100085};
Christopher Dunn6d135cb2007-06-13 15:51:04 +000086
Aaron Jacobs9fa4e842014-07-01 08:48:54 +100087/** \brief Represents a <a HREF="http://www.json.org">JSON</a> value.
88 *
89 * This class is a discriminated union wrapper that can represents a:
90 * - signed integer [range: Value::minInt - Value::maxInt]
91 * - unsigned integer (range: 0 - Value::maxUInt)
92 * - double
93 * - UTF-8 string
94 * - boolean
95 * - 'null'
96 * - an ordered list of Value
97 * - collection of name/value pairs (javascript object)
98 *
99 * The type of the held value is represented by a #ValueType and
100 * can be obtained using type().
101 *
102 * values of an #objectValue or #arrayValue can be accessed using operator[]()
103 *methods.
104 * Non const methods will automatically create the a #nullValue element
105 * if it does not exist.
106 * The sequence of an #arrayValue will be automatically resize and initialized
107 * with #nullValue. resize() can be used to enlarge or truncate an #arrayValue.
108 *
109 * The get() methods can be used to obtanis default value in the case the
110 *required element
111 * does not exist.
112 *
113 * It is possible to iterate over the list of a #objectValue values using
114 * the getMemberNames() method.
115 */
116class JSON_API Value {
117 friend class ValueIteratorBase;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000118public:
119 typedef std::vector<std::string> Members;
120 typedef ValueIterator iterator;
121 typedef ValueConstIterator const_iterator;
122 typedef Json::UInt UInt;
123 typedef Json::Int Int;
124#if defined(JSON_HAS_INT64)
125 typedef Json::UInt64 UInt64;
126 typedef Json::Int64 Int64;
Baptiste Lepilleur842e9ac2010-12-27 17:45:23 +0000127#endif // defined(JSON_HAS_INT64)
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000128 typedef Json::LargestInt LargestInt;
129 typedef Json::LargestUInt LargestUInt;
130 typedef Json::ArrayIndex ArrayIndex;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000131
Christopher Dunn07f0e932015-02-10 20:45:42 -0600132 static const Value& null; ///! We regret this reference to a global instance; prefer the simpler Value().
133 static const Value& nullRef; ///! just a kludge for binary-compatibility; same as null
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000134 /// Minimum signed integer value that can be stored in a Json::Value.
135 static const LargestInt minLargestInt;
136 /// Maximum signed integer value that can be stored in a Json::Value.
137 static const LargestInt maxLargestInt;
138 /// Maximum unsigned integer value that can be stored in a Json::Value.
139 static const LargestUInt maxLargestUInt;
Baptiste Lepilleur842e9ac2010-12-27 17:45:23 +0000140
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000141 /// Minimum signed int value that can be stored in a Json::Value.
142 static const Int minInt;
143 /// Maximum signed int value that can be stored in a Json::Value.
144 static const Int maxInt;
145 /// Maximum unsigned int value that can be stored in a Json::Value.
146 static const UInt maxUInt;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000147
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000148#if defined(JSON_HAS_INT64)
149 /// Minimum signed 64 bits int value that can be stored in a Json::Value.
150 static const Int64 minInt64;
151 /// Maximum signed 64 bits int value that can be stored in a Json::Value.
152 static const Int64 maxInt64;
153 /// Maximum unsigned 64 bits int value that can be stored in a Json::Value.
154 static const UInt64 maxUInt64;
Aaron Jacobsf1053e72011-05-24 03:18:02 +0000155#endif // defined(JSON_HAS_INT64)
Baptiste Lepilleur842e9ac2010-12-27 17:45:23 +0000156
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000157private:
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000158#ifndef JSONCPP_DOC_EXCLUDE_IMPLEMENTATION
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000159 class CZString {
160 public:
161 enum DuplicationPolicy {
162 noDuplication = 0,
163 duplicate,
164 duplicateOnCopy
165 };
166 CZString(ArrayIndex index);
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000167 CZString(const char* cstr, DuplicationPolicy allocate);
168 CZString(const CZString& other);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000169 ~CZString();
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000170 CZString& operator=(CZString other);
171 bool operator<(const CZString& other) const;
172 bool operator==(const CZString& other) const;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000173 ArrayIndex index() const;
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000174 const char* c_str() const;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000175 bool isStaticString() const;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000176
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000177 private:
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000178 void swap(CZString& other);
179 const char* cstr_;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000180 ArrayIndex index_;
181 };
182
183public:
184#ifndef JSON_USE_CPPTL_SMALLMAP
185 typedef std::map<CZString, Value> ObjectValues;
186#else
187 typedef CppTL::SmallMap<CZString, Value> ObjectValues;
188#endif // ifndef JSON_USE_CPPTL_SMALLMAP
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000189#endif // ifndef JSONCPP_DOC_EXCLUDE_IMPLEMENTATION
190
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000191public:
192 /** \brief Create a default Value of the given type.
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000193
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000194 This is a very useful constructor.
195 To create an empty array, pass arrayValue.
196 To create an empty object, pass objectValue.
197 Another Value can then be set to this one by assignment.
198This is useful since clear() and resize() will not alter types.
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000199
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000200 Examples:
201\code
202Json::Value null_value; // null
203Json::Value arr_value(Json::arrayValue); // []
204Json::Value obj_value(Json::objectValue); // {}
205\endcode
206 */
207 Value(ValueType type = nullValue);
208 Value(Int value);
209 Value(UInt value);
Baptiste Lepilleur842e9ac2010-12-27 17:45:23 +0000210#if defined(JSON_HAS_INT64)
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000211 Value(Int64 value);
212 Value(UInt64 value);
Baptiste Lepilleur842e9ac2010-12-27 17:45:23 +0000213#endif // if defined(JSON_HAS_INT64)
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000214 Value(double value);
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000215 Value(const char* value);
216 Value(const char* beginValue, const char* endValue);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000217 /** \brief Constructs a value from a static string.
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000218
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000219 * Like other value string constructor but do not duplicate the string for
220 * internal storage. The given string must remain alive after the call to this
221 * constructor.
222 * Example of usage:
223 * \code
224 * Json::Value aValue( StaticString("some text") );
225 * \endcode
226 */
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000227 Value(const StaticString& value);
228 Value(const std::string& value);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000229#ifdef JSON_USE_CPPTL
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000230 Value(const CppTL::ConstString& value);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000231#endif
232 Value(bool value);
Christopher Dunn66eb72f2015-01-20 11:02:22 -0600233 /// Deep copy.
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000234 Value(const Value& other);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000235 ~Value();
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000236
Christopher Dunn66eb72f2015-01-20 11:02:22 -0600237 // Deep copy, then swap(other).
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000238 Value& operator=(Value other);
Christopher Dunn66eb72f2015-01-20 11:02:22 -0600239 /// Swap everything.
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000240 void swap(Value& other);
Christopher Dunn66eb72f2015-01-20 11:02:22 -0600241 /// Swap values but leave comments and source offsets in place.
242 void swapPayload(Value& other);
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000243
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000244 ValueType type() const;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000245
Christopher Dunn66eb72f2015-01-20 11:02:22 -0600246 /// Compare payload only, not comments etc.
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000247 bool operator<(const Value& other) const;
248 bool operator<=(const Value& other) const;
249 bool operator>=(const Value& other) const;
250 bool operator>(const Value& other) const;
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000251 bool operator==(const Value& other) const;
252 bool operator!=(const Value& other) const;
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000253 int compare(const Value& other) const;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000254
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000255 const char* asCString() const;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000256 std::string asString() const;
257#ifdef JSON_USE_CPPTL
258 CppTL::ConstString asConstString() const;
259#endif
260 Int asInt() const;
261 UInt asUInt() const;
Aaron Jacobsf1053e72011-05-24 03:18:02 +0000262#if defined(JSON_HAS_INT64)
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000263 Int64 asInt64() const;
264 UInt64 asUInt64() const;
Aaron Jacobsf1053e72011-05-24 03:18:02 +0000265#endif // if defined(JSON_HAS_INT64)
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000266 LargestInt asLargestInt() const;
267 LargestUInt asLargestUInt() const;
268 float asFloat() const;
269 double asDouble() const;
270 bool asBool() const;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000271
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000272 bool isNull() const;
273 bool isBool() const;
274 bool isInt() const;
275 bool isInt64() const;
276 bool isUInt() const;
277 bool isUInt64() const;
278 bool isIntegral() const;
279 bool isDouble() const;
280 bool isNumeric() const;
281 bool isString() const;
282 bool isArray() const;
283 bool isObject() const;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000284
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000285 bool isConvertibleTo(ValueType other) const;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000286
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000287 /// Number of values in array or object
288 ArrayIndex size() const;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000289
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000290 /// \brief Return true if empty array, empty object, or null;
291 /// otherwise, false.
292 bool empty() const;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000293
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000294 /// Return isNull()
295 bool operator!() const;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000296
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000297 /// Remove all object members and array elements.
298 /// \pre type() is arrayValue, objectValue, or nullValue
299 /// \post type() is unchanged
300 void clear();
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000301
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000302 /// Resize the array to size elements.
303 /// New elements are initialized to null.
304 /// May only be called on nullValue or arrayValue.
305 /// \pre type() is arrayValue or nullValue
306 /// \post type() is arrayValue
307 void resize(ArrayIndex size);
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000308
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000309 /// Access an array element (zero based index ).
310 /// If the array contains less than index element, then null value are
311 /// inserted
312 /// in the array so that its size is index+1.
313 /// (You may need to say 'value[0u]' to get your compiler to distinguish
314 /// this from the operator[] which takes a string.)
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000315 Value& operator[](ArrayIndex index);
Baptiste Lepilleurfa130ef2010-12-24 12:47:14 +0000316
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000317 /// Access an array element (zero based index ).
318 /// If the array contains less than index element, then null value are
319 /// inserted
320 /// in the array so that its size is index+1.
321 /// (You may need to say 'value[0u]' to get your compiler to distinguish
322 /// this from the operator[] which takes a string.)
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000323 Value& operator[](int index);
Baptiste Lepilleurfa130ef2010-12-24 12:47:14 +0000324
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000325 /// Access an array element (zero based index )
326 /// (You may need to say 'value[0u]' to get your compiler to distinguish
327 /// this from the operator[] which takes a string.)
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000328 const Value& operator[](ArrayIndex index) const;
Baptiste Lepilleurfa130ef2010-12-24 12:47:14 +0000329
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000330 /// Access an array element (zero based index )
331 /// (You may need to say 'value[0u]' to get your compiler to distinguish
332 /// this from the operator[] which takes a string.)
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000333 const Value& operator[](int index) const;
Baptiste Lepilleurfa130ef2010-12-24 12:47:14 +0000334
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000335 /// If the array contains at least index+1 elements, returns the element
336 /// value,
337 /// otherwise returns defaultValue.
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000338 Value get(ArrayIndex index, const Value& defaultValue) const;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000339 /// Return true if index < size().
340 bool isValidIndex(ArrayIndex index) const;
341 /// \brief Append value to array at the end.
342 ///
343 /// Equivalent to jsonvalue[jsonvalue.size()] = value;
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000344 Value& append(const Value& value);
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000345
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000346 /// Access an object value by name, create a null member if it does not exist.
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000347 Value& operator[](const char* key);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000348 /// Access an object value by name, returns null if there is no member with
349 /// that name.
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000350 const Value& operator[](const char* key) const;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000351 /// Access an object value by name, create a null member if it does not exist.
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000352 Value& operator[](const std::string& key);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000353 /// Access an object value by name, returns null if there is no member with
354 /// that name.
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000355 const Value& operator[](const std::string& key) const;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000356 /** \brief Access an object value by name, create a null member if it does not
357 exist.
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000358
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000359 * If the object as no entry for that name, then the member name used to store
360 * the new entry is not duplicated.
361 * Example of use:
362 * \code
363 * Json::Value object;
364 * static const StaticString code("code");
365 * object[code] = 1234;
366 * \endcode
367 */
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000368 Value& operator[](const StaticString& key);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000369#ifdef JSON_USE_CPPTL
370 /// Access an object value by name, create a null member if it does not exist.
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000371 Value& operator[](const CppTL::ConstString& key);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000372 /// Access an object value by name, returns null if there is no member with
373 /// that name.
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000374 const Value& operator[](const CppTL::ConstString& key) const;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000375#endif
376 /// Return the member named key if it exist, defaultValue otherwise.
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000377 Value get(const char* key, const Value& defaultValue) const;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000378 /// Return the member named key if it exist, defaultValue otherwise.
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000379 Value get(const std::string& key, const Value& defaultValue) const;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000380#ifdef JSON_USE_CPPTL
381 /// Return the member named key if it exist, defaultValue otherwise.
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000382 Value get(const CppTL::ConstString& key, const Value& defaultValue) const;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000383#endif
384 /// \brief Remove and return the named member.
385 ///
386 /// Do nothing if it did not exist.
387 /// \return the removed Value, or null.
388 /// \pre type() is objectValue or nullValue
389 /// \post type() is unchanged
Christopher Dunn76746b02015-01-21 16:01:30 -0600390 /// \deprecated
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000391 Value removeMember(const char* key);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000392 /// Same as removeMember(const char*)
Christopher Dunn76746b02015-01-21 16:01:30 -0600393 /// \deprecated
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000394 Value removeMember(const std::string& key);
Christopher Dunn76746b02015-01-21 16:01:30 -0600395 /** \brief Remove the named map member.
396
397 Update 'removed' iff removed.
398 \return true iff removed (no exceptions)
399 */
400 bool removeMember(const char* key, Value* removed);
Christopher Dunn9de2c2d2015-01-20 16:15:40 -0600401 /** \brief Remove the indexed array element.
402
403 O(n) expensive operations.
404 Update 'removed' iff removed.
Christopher Dunne87e41c2015-01-20 16:24:11 -0600405 \return true iff removed (no exceptions)
Christopher Dunn9de2c2d2015-01-20 16:15:40 -0600406 */
407 bool removeIndex(ArrayIndex i, Value* removed);
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000408
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000409 /// Return true if the object has a member named key.
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000410 bool isMember(const char* key) const;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000411 /// Return true if the object has a member named key.
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000412 bool isMember(const std::string& key) const;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000413#ifdef JSON_USE_CPPTL
414 /// Return true if the object has a member named key.
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000415 bool isMember(const CppTL::ConstString& key) const;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000416#endif
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000417
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000418 /// \brief Return a list of the member names.
419 ///
420 /// If null, return an empty list.
421 /// \pre type() is objectValue or nullValue
422 /// \post if type() was nullValue, it remains nullValue
423 Members getMemberNames() const;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000424
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000425 //# ifdef JSON_USE_CPPTL
426 // EnumMemberNames enumMemberNames() const;
427 // EnumValues enumValues() const;
428 //# endif
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000429
Christopher Dunn1e3149a2015-01-25 14:16:13 -0600430 /// \deprecated Always pass len.
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000431 void setComment(const char* comment, CommentPlacement placement);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000432 /// Comments must be //... or /* ... */
Christopher Dunn1e3149a2015-01-25 14:16:13 -0600433 void setComment(const char* comment, size_t len, CommentPlacement placement);
434 /// Comments must be //... or /* ... */
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000435 void setComment(const std::string& comment, CommentPlacement placement);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000436 bool hasComment(CommentPlacement placement) const;
437 /// Include delimiters and embedded newlines.
438 std::string getComment(CommentPlacement placement) const;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000439
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000440 std::string toStyledString() const;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000441
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000442 const_iterator begin() const;
443 const_iterator end() const;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000444
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000445 iterator begin();
446 iterator end();
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000447
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000448 // Accessors for the [start, limit) range of bytes within the JSON text from
449 // which this value was parsed, if any.
450 void setOffsetStart(size_t start);
451 void setOffsetLimit(size_t limit);
452 size_t getOffsetStart() const;
453 size_t getOffsetLimit() const;
Aaron Jacobs68db6552014-04-23 23:41:12 +0000454
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000455private:
Billy Donahue8eb5d892014-11-10 01:35:42 -0500456 void initBasic(ValueType type, bool allocated = false);
457
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000458 Value& resolveReference(const char* key, bool isStatic);
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000459
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000460 struct CommentInfo {
461 CommentInfo();
462 ~CommentInfo();
463
Christopher Dunn1e3149a2015-01-25 14:16:13 -0600464 void setComment(const char* text, size_t len);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000465
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000466 char* comment_;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000467 };
468
469 // struct MemberNamesTransform
470 //{
471 // typedef const char *result_type;
472 // const char *operator()( const CZString &name ) const
473 // {
474 // return name.c_str();
475 // }
476 //};
477
478 union ValueHolder {
479 LargestInt int_;
480 LargestUInt uint_;
481 double real_;
482 bool bool_;
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000483 char* string_;
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000484 ObjectValues* map_;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000485 } value_;
486 ValueType type_ : 8;
Christopher Dunn2bc61372015-01-24 13:42:37 -0600487 unsigned int allocated_ : 1; // Notes: if declared as bool, bitfield is useless.
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000488 CommentInfo* comments_;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000489
490 // [start, limit) byte offsets in the source JSON text from which this Value
491 // was extracted.
492 size_t start_;
493 size_t limit_;
494};
495
496/** \brief Experimental and untested: represents an element of the "path" to
497 * access a node.
498 */
499class JSON_API PathArgument {
500public:
501 friend class Path;
502
503 PathArgument();
504 PathArgument(ArrayIndex index);
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000505 PathArgument(const char* key);
506 PathArgument(const std::string& key);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000507
508private:
509 enum Kind {
510 kindNone = 0,
511 kindIndex,
512 kindKey
513 };
514 std::string key_;
515 ArrayIndex index_;
516 Kind kind_;
517};
518
519/** \brief Experimental and untested: represents a "path" to access a node.
520 *
521 * Syntax:
522 * - "." => root node
523 * - ".[n]" => elements at index 'n' of root node (an array value)
524 * - ".name" => member named 'name' of root node (an object value)
525 * - ".name1.name2.name3"
526 * - ".[0][1][2].name1[3]"
527 * - ".%" => member name is provided as parameter
528 * - ".[%]" => index is provied as parameter
529 */
530class JSON_API Path {
531public:
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000532 Path(const std::string& path,
533 const PathArgument& a1 = PathArgument(),
534 const PathArgument& a2 = PathArgument(),
535 const PathArgument& a3 = PathArgument(),
536 const PathArgument& a4 = PathArgument(),
537 const PathArgument& a5 = PathArgument());
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000538
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000539 const Value& resolve(const Value& root) const;
540 Value resolve(const Value& root, const Value& defaultValue) const;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000541 /// Creates the "path" to access the specified node and returns a reference on
542 /// the node.
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000543 Value& make(Value& root) const;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000544
545private:
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000546 typedef std::vector<const PathArgument*> InArgs;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000547 typedef std::vector<PathArgument> Args;
548
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000549 void makePath(const std::string& path, const InArgs& in);
550 void addPathInArg(const std::string& path,
551 const InArgs& in,
552 InArgs::const_iterator& itInArg,
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000553 PathArgument::Kind kind);
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000554 void invalidPath(const std::string& path, int location);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000555
556 Args args_;
557};
558
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000559/** \brief base class for Value iterators.
560 *
561 */
562class JSON_API ValueIteratorBase {
563public:
564 typedef std::bidirectional_iterator_tag iterator_category;
565 typedef unsigned int size_t;
566 typedef int difference_type;
567 typedef ValueIteratorBase SelfType;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000568
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000569 ValueIteratorBase();
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000570 explicit ValueIteratorBase(const Value::ObjectValues::iterator& current);
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000571
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000572 bool operator==(const SelfType& other) const { return isEqual(other); }
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000573
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000574 bool operator!=(const SelfType& other) const { return !isEqual(other); }
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000575
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000576 difference_type operator-(const SelfType& other) const {
Kevin Grant4c5832a2015-02-14 20:53:35 -0800577 return other.computeDistance(*this);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000578 }
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000579
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000580 /// Return either the index or the member name of the referenced value as a
581 /// Value.
582 Value key() const;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000583
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000584 /// Return the index of the referenced Value. -1 if it is not an arrayValue.
585 UInt index() const;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000586
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000587 /// Return the member name of the referenced Value. "" if it is not an
588 /// objectValue.
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000589 const char* memberName() const;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000590
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000591protected:
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000592 Value& deref() const;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000593
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000594 void increment();
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000595
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000596 void decrement();
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000597
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000598 difference_type computeDistance(const SelfType& other) const;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000599
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000600 bool isEqual(const SelfType& other) const;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000601
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000602 void copy(const SelfType& other);
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000603
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000604private:
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000605 Value::ObjectValues::iterator current_;
606 // Indicates that iterator is for a null value.
607 bool isNull_;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000608};
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000609
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000610/** \brief const iterator for object and array value.
611 *
612 */
613class JSON_API ValueConstIterator : public ValueIteratorBase {
614 friend class Value;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000615
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000616public:
617 typedef const Value value_type;
618 typedef unsigned int size_t;
619 typedef int difference_type;
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000620 typedef const Value& reference;
621 typedef const Value* pointer;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000622 typedef ValueConstIterator SelfType;
623
624 ValueConstIterator();
625
626private:
627/*! \internal Use by Value to create an iterator.
628 */
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000629 explicit ValueConstIterator(const Value::ObjectValues::iterator& current);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000630public:
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000631 SelfType& operator=(const ValueIteratorBase& other);
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000632
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000633 SelfType operator++(int) {
634 SelfType temp(*this);
635 ++*this;
636 return temp;
637 }
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000638
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000639 SelfType operator--(int) {
640 SelfType temp(*this);
641 --*this;
642 return temp;
643 }
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000644
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000645 SelfType& operator--() {
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000646 decrement();
647 return *this;
648 }
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000649
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000650 SelfType& operator++() {
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000651 increment();
652 return *this;
653 }
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000654
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000655 reference operator*() const { return deref(); }
Braden McDorman540db3b2014-09-14 02:31:23 -0500656
657 pointer operator->() const { return &deref(); }
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000658};
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000659
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000660/** \brief Iterator for object and array value.
661 */
662class JSON_API ValueIterator : public ValueIteratorBase {
663 friend class Value;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000664
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000665public:
666 typedef Value value_type;
667 typedef unsigned int size_t;
668 typedef int difference_type;
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000669 typedef Value& reference;
670 typedef Value* pointer;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000671 typedef ValueIterator SelfType;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000672
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000673 ValueIterator();
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000674 ValueIterator(const ValueConstIterator& other);
675 ValueIterator(const ValueIterator& other);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000676
677private:
678/*! \internal Use by Value to create an iterator.
679 */
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000680 explicit ValueIterator(const Value::ObjectValues::iterator& current);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000681public:
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000682 SelfType& operator=(const SelfType& other);
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000683
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000684 SelfType operator++(int) {
685 SelfType temp(*this);
686 ++*this;
687 return temp;
688 }
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000689
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000690 SelfType operator--(int) {
691 SelfType temp(*this);
692 --*this;
693 return temp;
694 }
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000695
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000696 SelfType& operator--() {
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000697 decrement();
698 return *this;
699 }
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000700
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000701 SelfType& operator++() {
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000702 increment();
703 return *this;
704 }
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000705
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000706 reference operator*() const { return deref(); }
Braden McDorman540db3b2014-09-14 02:31:23 -0500707
708 pointer operator->() const { return &deref(); }
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000709};
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000710
711} // namespace Json
712
datadiode9454e682015-01-20 15:25:04 -0600713
714namespace std {
715/// Specialize std::swap() for Json::Value.
716template<>
717inline void swap(Json::Value& a, Json::Value& b) { a.swap(b); }
718}
719
720
Baptiste Lepilleureafd7022013-05-08 20:21:11 +0000721#if defined(JSONCPP_DISABLE_DLL_INTERFACE_WARNING)
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000722#pragma warning(pop)
Baptiste Lepilleureafd7022013-05-08 20:21:11 +0000723#endif // if defined(JSONCPP_DISABLE_DLL_INTERFACE_WARNING)
724
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000725#endif // CPPTL_JSON_H_INCLUDED