blob: c4169b44092d3e3cea6da4570d662fb704f7c346 [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;
118#ifdef JSON_VALUE_USE_INTERNAL_MAP
119 friend class ValueInternalLink;
120 friend class ValueInternalMap;
121#endif
122public:
123 typedef std::vector<std::string> Members;
124 typedef ValueIterator iterator;
125 typedef ValueConstIterator const_iterator;
126 typedef Json::UInt UInt;
127 typedef Json::Int Int;
128#if defined(JSON_HAS_INT64)
129 typedef Json::UInt64 UInt64;
130 typedef Json::Int64 Int64;
Baptiste Lepilleur842e9ac2010-12-27 17:45:23 +0000131#endif // defined(JSON_HAS_INT64)
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000132 typedef Json::LargestInt LargestInt;
133 typedef Json::LargestUInt LargestUInt;
134 typedef Json::ArrayIndex ArrayIndex;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000135
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000136 static const Value& null;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000137 /// Minimum signed integer value that can be stored in a Json::Value.
138 static const LargestInt minLargestInt;
139 /// Maximum signed integer value that can be stored in a Json::Value.
140 static const LargestInt maxLargestInt;
141 /// Maximum unsigned integer value that can be stored in a Json::Value.
142 static const LargestUInt maxLargestUInt;
Baptiste Lepilleur842e9ac2010-12-27 17:45:23 +0000143
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000144 /// Minimum signed int value that can be stored in a Json::Value.
145 static const Int minInt;
146 /// Maximum signed int value that can be stored in a Json::Value.
147 static const Int maxInt;
148 /// Maximum unsigned int value that can be stored in a Json::Value.
149 static const UInt maxUInt;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000150
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000151#if defined(JSON_HAS_INT64)
152 /// Minimum signed 64 bits int value that can be stored in a Json::Value.
153 static const Int64 minInt64;
154 /// Maximum signed 64 bits int value that can be stored in a Json::Value.
155 static const Int64 maxInt64;
156 /// Maximum unsigned 64 bits int value that can be stored in a Json::Value.
157 static const UInt64 maxUInt64;
Aaron Jacobsf1053e72011-05-24 03:18:02 +0000158#endif // defined(JSON_HAS_INT64)
Baptiste Lepilleur842e9ac2010-12-27 17:45:23 +0000159
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000160private:
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000161#ifndef JSONCPP_DOC_EXCLUDE_IMPLEMENTATION
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000162#ifndef JSON_VALUE_USE_INTERNAL_MAP
163 class CZString {
164 public:
165 enum DuplicationPolicy {
166 noDuplication = 0,
167 duplicate,
168 duplicateOnCopy
169 };
170 CZString(ArrayIndex index);
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000171 CZString(const char* cstr, DuplicationPolicy allocate);
172 CZString(const CZString& other);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000173 ~CZString();
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000174 CZString& operator=(CZString other);
175 bool operator<(const CZString& other) const;
176 bool operator==(const CZString& other) const;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000177 ArrayIndex index() const;
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000178 const char* c_str() const;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000179 bool isStaticString() const;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000180
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000181 private:
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000182 void swap(CZString& other);
183 const char* cstr_;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000184 ArrayIndex index_;
185 };
186
187public:
188#ifndef JSON_USE_CPPTL_SMALLMAP
189 typedef std::map<CZString, Value> ObjectValues;
190#else
191 typedef CppTL::SmallMap<CZString, Value> ObjectValues;
192#endif // ifndef JSON_USE_CPPTL_SMALLMAP
193#endif // ifndef JSON_VALUE_USE_INTERNAL_MAP
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000194#endif // ifndef JSONCPP_DOC_EXCLUDE_IMPLEMENTATION
195
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000196public:
197 /** \brief Create a default Value of the given type.
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000198
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000199 This is a very useful constructor.
200 To create an empty array, pass arrayValue.
201 To create an empty object, pass objectValue.
202 Another Value can then be set to this one by assignment.
203This is useful since clear() and resize() will not alter types.
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000204
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000205 Examples:
206\code
207Json::Value null_value; // null
208Json::Value arr_value(Json::arrayValue); // []
209Json::Value obj_value(Json::objectValue); // {}
210\endcode
211 */
212 Value(ValueType type = nullValue);
213 Value(Int value);
214 Value(UInt value);
Baptiste Lepilleur842e9ac2010-12-27 17:45:23 +0000215#if defined(JSON_HAS_INT64)
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000216 Value(Int64 value);
217 Value(UInt64 value);
Baptiste Lepilleur842e9ac2010-12-27 17:45:23 +0000218#endif // if defined(JSON_HAS_INT64)
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000219 Value(double value);
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000220 Value(const char* value);
221 Value(const char* beginValue, const char* endValue);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000222 /** \brief Constructs a value from a static string.
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000223
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000224 * Like other value string constructor but do not duplicate the string for
225 * internal storage. The given string must remain alive after the call to this
226 * constructor.
227 * Example of usage:
228 * \code
229 * Json::Value aValue( StaticString("some text") );
230 * \endcode
231 */
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000232 Value(const StaticString& value);
233 Value(const std::string& value);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000234#ifdef JSON_USE_CPPTL
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000235 Value(const CppTL::ConstString& value);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000236#endif
237 Value(bool value);
Christopher Dunn66eb72f2015-01-20 11:02:22 -0600238 /// Deep copy.
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000239 Value(const Value& other);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000240 ~Value();
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000241
Christopher Dunn66eb72f2015-01-20 11:02:22 -0600242 // Deep copy, then swap(other).
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000243 Value& operator=(Value other);
Christopher Dunn66eb72f2015-01-20 11:02:22 -0600244 /// Swap everything.
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000245 void swap(Value& other);
Christopher Dunn66eb72f2015-01-20 11:02:22 -0600246 /// Swap values but leave comments and source offsets in place.
247 void swapPayload(Value& other);
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000248
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000249 ValueType type() const;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000250
Christopher Dunn66eb72f2015-01-20 11:02:22 -0600251 /// Compare payload only, not comments etc.
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000252 bool operator<(const Value& other) const;
253 bool operator<=(const Value& other) const;
254 bool operator>=(const Value& other) const;
255 bool operator>(const Value& other) const;
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000256 bool operator==(const Value& other) const;
257 bool operator!=(const Value& other) const;
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000258 int compare(const Value& other) const;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000259
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000260 const char* asCString() const;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000261 std::string asString() const;
262#ifdef JSON_USE_CPPTL
263 CppTL::ConstString asConstString() const;
264#endif
265 Int asInt() const;
266 UInt asUInt() const;
Aaron Jacobsf1053e72011-05-24 03:18:02 +0000267#if defined(JSON_HAS_INT64)
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000268 Int64 asInt64() const;
269 UInt64 asUInt64() const;
Aaron Jacobsf1053e72011-05-24 03:18:02 +0000270#endif // if defined(JSON_HAS_INT64)
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000271 LargestInt asLargestInt() const;
272 LargestUInt asLargestUInt() const;
273 float asFloat() const;
274 double asDouble() const;
275 bool asBool() const;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000276
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000277 bool isNull() const;
278 bool isBool() const;
279 bool isInt() const;
280 bool isInt64() const;
281 bool isUInt() const;
282 bool isUInt64() const;
283 bool isIntegral() const;
284 bool isDouble() const;
285 bool isNumeric() const;
286 bool isString() const;
287 bool isArray() const;
288 bool isObject() const;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000289
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000290 bool isConvertibleTo(ValueType other) const;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000291
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000292 /// Number of values in array or object
293 ArrayIndex size() const;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000294
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000295 /// \brief Return true if empty array, empty object, or null;
296 /// otherwise, false.
297 bool empty() const;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000298
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000299 /// Return isNull()
300 bool operator!() const;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000301
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000302 /// Remove all object members and array elements.
303 /// \pre type() is arrayValue, objectValue, or nullValue
304 /// \post type() is unchanged
305 void clear();
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000306
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000307 /// Resize the array to size elements.
308 /// New elements are initialized to null.
309 /// May only be called on nullValue or arrayValue.
310 /// \pre type() is arrayValue or nullValue
311 /// \post type() is arrayValue
312 void resize(ArrayIndex size);
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000313
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000314 /// Access an array element (zero based index ).
315 /// If the array contains less than index element, then null value are
316 /// inserted
317 /// in the array so that its size is index+1.
318 /// (You may need to say 'value[0u]' to get your compiler to distinguish
319 /// this from the operator[] which takes a string.)
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000320 Value& operator[](ArrayIndex index);
Baptiste Lepilleurfa130ef2010-12-24 12:47:14 +0000321
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000322 /// Access an array element (zero based index ).
323 /// If the array contains less than index element, then null value are
324 /// inserted
325 /// in the array so that its size is index+1.
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 Value& operator[](int index);
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[](ArrayIndex index) const;
Baptiste Lepilleurfa130ef2010-12-24 12:47:14 +0000334
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000335 /// Access an array element (zero based index )
336 /// (You may need to say 'value[0u]' to get your compiler to distinguish
337 /// this from the operator[] which takes a string.)
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000338 const Value& operator[](int index) const;
Baptiste Lepilleurfa130ef2010-12-24 12:47:14 +0000339
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000340 /// If the array contains at least index+1 elements, returns the element
341 /// value,
342 /// otherwise returns defaultValue.
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000343 Value get(ArrayIndex index, const Value& defaultValue) const;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000344 /// Return true if index < size().
345 bool isValidIndex(ArrayIndex index) const;
346 /// \brief Append value to array at the end.
347 ///
348 /// Equivalent to jsonvalue[jsonvalue.size()] = value;
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000349 Value& append(const Value& value);
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000350
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 char* 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 char* key) const;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000356 /// Access an object value by name, create a null member if it does not exist.
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000357 Value& operator[](const std::string& key);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000358 /// Access an object value by name, returns null if there is no member with
359 /// that name.
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000360 const Value& operator[](const std::string& key) const;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000361 /** \brief Access an object value by name, create a null member if it does not
362 exist.
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000363
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000364 * If the object as no entry for that name, then the member name used to store
365 * the new entry is not duplicated.
366 * Example of use:
367 * \code
368 * Json::Value object;
369 * static const StaticString code("code");
370 * object[code] = 1234;
371 * \endcode
372 */
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000373 Value& operator[](const StaticString& key);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000374#ifdef JSON_USE_CPPTL
375 /// Access an object value by name, create a null member if it does not exist.
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000376 Value& operator[](const CppTL::ConstString& key);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000377 /// Access an object value by name, returns null if there is no member with
378 /// that name.
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000379 const Value& operator[](const CppTL::ConstString& key) const;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000380#endif
381 /// Return the member named key if it exist, defaultValue otherwise.
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000382 Value get(const char* key, const Value& defaultValue) const;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000383 /// Return the member named key if it exist, defaultValue otherwise.
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000384 Value get(const std::string& key, const Value& defaultValue) const;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000385#ifdef JSON_USE_CPPTL
386 /// Return the member named key if it exist, defaultValue otherwise.
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000387 Value get(const CppTL::ConstString& key, const Value& defaultValue) const;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000388#endif
389 /// \brief Remove and return the named member.
390 ///
391 /// Do nothing if it did not exist.
392 /// \return the removed Value, or null.
393 /// \pre type() is objectValue or nullValue
394 /// \post type() is unchanged
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000395 Value removeMember(const char* key);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000396 /// Same as removeMember(const char*)
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000397 Value removeMember(const std::string& key);
Christopher Dunn9de2c2d2015-01-20 16:15:40 -0600398 /** \brief Remove the indexed array element.
399
400 O(n) expensive operations.
401 Update 'removed' iff removed.
402 (This is a better pattern than removeMember().)
Christopher Dunne87e41c2015-01-20 16:24:11 -0600403 \return true iff removed (no exceptions)
Christopher Dunn9de2c2d2015-01-20 16:15:40 -0600404 */
405 bool removeIndex(ArrayIndex i, Value* removed);
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000406
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000407 /// Return true if the object has a member named key.
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000408 bool isMember(const char* key) const;
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 std::string& key) const;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000411#ifdef JSON_USE_CPPTL
412 /// Return true if the object has a member named key.
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000413 bool isMember(const CppTL::ConstString& key) const;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000414#endif
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000415
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000416 /// \brief Return a list of the member names.
417 ///
418 /// If null, return an empty list.
419 /// \pre type() is objectValue or nullValue
420 /// \post if type() was nullValue, it remains nullValue
421 Members getMemberNames() const;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000422
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000423 //# ifdef JSON_USE_CPPTL
424 // EnumMemberNames enumMemberNames() const;
425 // EnumValues enumValues() const;
426 //# endif
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000427
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000428 /// Comments must be //... or /* ... */
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000429 void setComment(const char* comment, CommentPlacement placement);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000430 /// Comments must be //... or /* ... */
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000431 void setComment(const std::string& comment, CommentPlacement placement);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000432 bool hasComment(CommentPlacement placement) const;
433 /// Include delimiters and embedded newlines.
434 std::string getComment(CommentPlacement placement) const;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000435
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000436 std::string toStyledString() const;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000437
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000438 const_iterator begin() const;
439 const_iterator end() const;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000440
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000441 iterator begin();
442 iterator end();
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000443
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000444 // Accessors for the [start, limit) range of bytes within the JSON text from
445 // which this value was parsed, if any.
446 void setOffsetStart(size_t start);
447 void setOffsetLimit(size_t limit);
448 size_t getOffsetStart() const;
449 size_t getOffsetLimit() const;
Aaron Jacobs68db6552014-04-23 23:41:12 +0000450
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000451private:
Billy Donahue8eb5d892014-11-10 01:35:42 -0500452 void initBasic(ValueType type, bool allocated = false);
453
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000454 Value& resolveReference(const char* key, bool isStatic);
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000455
456#ifdef JSON_VALUE_USE_INTERNAL_MAP
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000457 inline bool isItemAvailable() const { return itemIsUsed_ == 0; }
458
459 inline void setItemUsed(bool isUsed = true) { itemIsUsed_ = isUsed ? 1 : 0; }
460
461 inline bool isMemberNameStatic() const { return memberNameIsStatic_ == 0; }
462
463 inline void setMemberNameIsStatic(bool isStatic) {
464 memberNameIsStatic_ = isStatic ? 1 : 0;
465 }
466#endif // # ifdef JSON_VALUE_USE_INTERNAL_MAP
467
468private:
469 struct CommentInfo {
470 CommentInfo();
471 ~CommentInfo();
472
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000473 void setComment(const char* text);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000474
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000475 char* comment_;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000476 };
477
478 // struct MemberNamesTransform
479 //{
480 // typedef const char *result_type;
481 // const char *operator()( const CZString &name ) const
482 // {
483 // return name.c_str();
484 // }
485 //};
486
487 union ValueHolder {
488 LargestInt int_;
489 LargestUInt uint_;
490 double real_;
491 bool bool_;
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000492 char* string_;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000493#ifdef JSON_VALUE_USE_INTERNAL_MAP
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000494 ValueInternalArray* array_;
495 ValueInternalMap* map_;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000496#else
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000497 ObjectValues* map_;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000498#endif
499 } value_;
500 ValueType type_ : 8;
501 int allocated_ : 1; // Notes: if declared as bool, bitfield is useless.
502#ifdef JSON_VALUE_USE_INTERNAL_MAP
503 unsigned int itemIsUsed_ : 1; // used by the ValueInternalMap container.
504 int memberNameIsStatic_ : 1; // used by the ValueInternalMap container.
505#endif
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000506 CommentInfo* comments_;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000507
508 // [start, limit) byte offsets in the source JSON text from which this Value
509 // was extracted.
510 size_t start_;
511 size_t limit_;
512};
513
514/** \brief Experimental and untested: represents an element of the "path" to
515 * access a node.
516 */
517class JSON_API PathArgument {
518public:
519 friend class Path;
520
521 PathArgument();
522 PathArgument(ArrayIndex index);
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000523 PathArgument(const char* key);
524 PathArgument(const std::string& key);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000525
526private:
527 enum Kind {
528 kindNone = 0,
529 kindIndex,
530 kindKey
531 };
532 std::string key_;
533 ArrayIndex index_;
534 Kind kind_;
535};
536
537/** \brief Experimental and untested: represents a "path" to access a node.
538 *
539 * Syntax:
540 * - "." => root node
541 * - ".[n]" => elements at index 'n' of root node (an array value)
542 * - ".name" => member named 'name' of root node (an object value)
543 * - ".name1.name2.name3"
544 * - ".[0][1][2].name1[3]"
545 * - ".%" => member name is provided as parameter
546 * - ".[%]" => index is provied as parameter
547 */
548class JSON_API Path {
549public:
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000550 Path(const std::string& path,
551 const PathArgument& a1 = PathArgument(),
552 const PathArgument& a2 = PathArgument(),
553 const PathArgument& a3 = PathArgument(),
554 const PathArgument& a4 = PathArgument(),
555 const PathArgument& a5 = PathArgument());
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000556
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000557 const Value& resolve(const Value& root) const;
558 Value resolve(const Value& root, const Value& defaultValue) const;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000559 /// Creates the "path" to access the specified node and returns a reference on
560 /// the node.
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000561 Value& make(Value& root) const;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000562
563private:
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000564 typedef std::vector<const PathArgument*> InArgs;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000565 typedef std::vector<PathArgument> Args;
566
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000567 void makePath(const std::string& path, const InArgs& in);
568 void addPathInArg(const std::string& path,
569 const InArgs& in,
570 InArgs::const_iterator& itInArg,
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000571 PathArgument::Kind kind);
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000572 void invalidPath(const std::string& path, int location);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000573
574 Args args_;
575};
576
577#ifdef JSON_VALUE_USE_INTERNAL_MAP
578/** \brief Allocator to customize Value internal map.
579 * Below is an example of a simple implementation (default implementation
580 actually
581 * use memory pool for speed).
582 * \code
583 class DefaultValueMapAllocator : public ValueMapAllocator
584 {
585 public: // overridden from ValueMapAllocator
586 virtual ValueInternalMap *newMap()
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000587 {
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000588 return new ValueInternalMap();
589 }
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000590
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000591 virtual ValueInternalMap *newMapCopy( const ValueInternalMap &other )
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000592 {
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000593 return new ValueInternalMap( other );
594 }
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000595
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000596 virtual void destructMap( ValueInternalMap *map )
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000597 {
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000598 delete map;
599 }
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000600
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000601 virtual ValueInternalLink *allocateMapBuckets( unsigned int size )
602 {
603 return new ValueInternalLink[size];
604 }
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000605
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000606 virtual void releaseMapBuckets( ValueInternalLink *links )
607 {
608 delete [] links;
609 }
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000610
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000611 virtual ValueInternalLink *allocateMapLink()
612 {
613 return new ValueInternalLink();
614 }
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000615
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000616 virtual void releaseMapLink( ValueInternalLink *link )
617 {
618 delete link;
619 }
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000620 };
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000621 * \endcode
622 */
623class JSON_API ValueMapAllocator {
624public:
625 virtual ~ValueMapAllocator();
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000626 virtual ValueInternalMap* newMap() = 0;
627 virtual ValueInternalMap* newMapCopy(const ValueInternalMap& other) = 0;
628 virtual void destructMap(ValueInternalMap* map) = 0;
629 virtual ValueInternalLink* allocateMapBuckets(unsigned int size) = 0;
630 virtual void releaseMapBuckets(ValueInternalLink* links) = 0;
631 virtual ValueInternalLink* allocateMapLink() = 0;
632 virtual void releaseMapLink(ValueInternalLink* link) = 0;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000633};
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000634
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000635/** \brief ValueInternalMap hash-map bucket chain link (for internal use only).
636 * \internal previous_ & next_ allows for bidirectional traversal.
637 */
638class JSON_API ValueInternalLink {
639public:
640 enum {
641 itemPerLink = 6
642 }; // sizeof(ValueInternalLink) = 128 on 32 bits architecture.
643 enum InternalFlags {
644 flagAvailable = 0,
645 flagUsed = 1
646 };
647
648 ValueInternalLink();
649
650 ~ValueInternalLink();
651
652 Value items_[itemPerLink];
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000653 char* keys_[itemPerLink];
654 ValueInternalLink* previous_;
655 ValueInternalLink* next_;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000656};
657
658/** \brief A linked page based hash-table implementation used internally by
659 *Value.
660 * \internal ValueInternalMap is a tradional bucket based hash-table, with a
661 *linked
662 * list in each bucket to handle collision. There is an addional twist in that
663 * each node of the collision linked list is a page containing a fixed amount of
664 * value. This provides a better compromise between memory usage and speed.
665 *
666 * Each bucket is made up of a chained list of ValueInternalLink. The last
667 * link of a given bucket can be found in the 'previous_' field of the following
668 *bucket.
669 * The last link of the last bucket is stored in tailLink_ as it has no
670 *following bucket.
671 * Only the last link of a bucket may contains 'available' item. The last link
672 *always
673 * contains at least one element unless is it the bucket one very first link.
674 */
675class JSON_API ValueInternalMap {
676 friend class ValueIteratorBase;
677 friend class Value;
678
679public:
680 typedef unsigned int HashKey;
681 typedef unsigned int BucketIndex;
682
683#ifndef JSONCPP_DOC_EXCLUDE_IMPLEMENTATION
684 struct IteratorState {
685 IteratorState() : map_(0), link_(0), itemIndex_(0), bucketIndex_(0) {}
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000686 ValueInternalMap* map_;
687 ValueInternalLink* link_;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000688 BucketIndex itemIndex_;
689 BucketIndex bucketIndex_;
690 };
691#endif // ifndef JSONCPP_DOC_EXCLUDE_IMPLEMENTATION
692
693 ValueInternalMap();
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000694 ValueInternalMap(const ValueInternalMap& other);
695 ValueInternalMap& operator=(ValueInternalMap other);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000696 ~ValueInternalMap();
697
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000698 void swap(ValueInternalMap& other);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000699
700 BucketIndex size() const;
701
702 void clear();
703
704 bool reserveDelta(BucketIndex growth);
705
706 bool reserve(BucketIndex newItemCount);
707
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000708 const Value* find(const char* key) const;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000709
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000710 Value* find(const char* key);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000711
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000712 Value& resolveReference(const char* key, bool isStatic);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000713
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000714 void remove(const char* key);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000715
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000716 void doActualRemove(ValueInternalLink* link,
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000717 BucketIndex index,
718 BucketIndex bucketIndex);
719
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000720 ValueInternalLink*& getLastLinkInBucket(BucketIndex bucketIndex);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000721
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000722 Value& setNewItem(const char* key,
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000723 bool isStatic,
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000724 ValueInternalLink* link,
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000725 BucketIndex index);
726
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000727 Value& unsafeAdd(const char* key, bool isStatic, HashKey hashedKey);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000728
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000729 HashKey hash(const char* key) const;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000730
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000731 int compare(const ValueInternalMap& other) const;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000732
733private:
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000734 void makeBeginIterator(IteratorState& it) const;
735 void makeEndIterator(IteratorState& it) const;
736 static bool equals(const IteratorState& x, const IteratorState& other);
737 static void increment(IteratorState& iterator);
738 static void incrementBucket(IteratorState& iterator);
739 static void decrement(IteratorState& iterator);
740 static const char* key(const IteratorState& iterator);
741 static const char* key(const IteratorState& iterator, bool& isStatic);
742 static Value& value(const IteratorState& iterator);
743 static int distance(const IteratorState& x, const IteratorState& y);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000744
745private:
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000746 ValueInternalLink* buckets_;
747 ValueInternalLink* tailLink_;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000748 BucketIndex bucketsSize_;
749 BucketIndex itemCount_;
750};
751
752/** \brief A simplified deque implementation used internally by Value.
753* \internal
754* It is based on a list of fixed "page", each page contains a fixed number of
755*items.
756* Instead of using a linked-list, a array of pointer is used for fast item
757*look-up.
758* Look-up for an element is as follow:
759* - compute page index: pageIndex = itemIndex / itemsPerPage
760* - look-up item in page: pages_[pageIndex][itemIndex % itemsPerPage]
761*
762* Insertion is amortized constant time (only the array containing the index of
763*pointers
764* need to be reallocated when items are appended).
765*/
766class JSON_API ValueInternalArray {
767 friend class Value;
768 friend class ValueIteratorBase;
769
770public:
771 enum {
772 itemsPerPage = 8
773 }; // should be a power of 2 for fast divide and modulo.
774 typedef Value::ArrayIndex ArrayIndex;
775 typedef unsigned int PageIndex;
776
777#ifndef JSONCPP_DOC_EXCLUDE_IMPLEMENTATION
778 struct IteratorState // Must be a POD
779 {
780 IteratorState() : array_(0), currentPageIndex_(0), currentItemIndex_(0) {}
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000781 ValueInternalArray* array_;
782 Value** currentPageIndex_;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000783 unsigned int currentItemIndex_;
784 };
785#endif // ifndef JSONCPP_DOC_EXCLUDE_IMPLEMENTATION
786
787 ValueInternalArray();
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000788 ValueInternalArray(const ValueInternalArray& other);
789 ValueInternalArray& operator=(ValueInternalArray other);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000790 ~ValueInternalArray();
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000791 void swap(ValueInternalArray& other);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000792
793 void clear();
794 void resize(ArrayIndex newSize);
795
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000796 Value& resolveReference(ArrayIndex index);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000797
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000798 Value* find(ArrayIndex index) const;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000799
800 ArrayIndex size() const;
801
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000802 int compare(const ValueInternalArray& other) const;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000803
804private:
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000805 static bool equals(const IteratorState& x, const IteratorState& other);
806 static void increment(IteratorState& iterator);
807 static void decrement(IteratorState& iterator);
808 static Value& dereference(const IteratorState& iterator);
809 static Value& unsafeDereference(const IteratorState& iterator);
810 static int distance(const IteratorState& x, const IteratorState& y);
811 static ArrayIndex indexOf(const IteratorState& iterator);
812 void makeBeginIterator(IteratorState& it) const;
813 void makeEndIterator(IteratorState& it) const;
814 void makeIterator(IteratorState& it, ArrayIndex index) const;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000815
816 void makeIndexValid(ArrayIndex index);
817
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000818 Value** pages_;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000819 ArrayIndex size_;
820 PageIndex pageCount_;
821};
822
823/** \brief Experimental: do not use. Allocator to customize Value internal
824array.
825 * Below is an example of a simple implementation (actual implementation use
826 * memory pool).
827 \code
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000828class DefaultValueArrayAllocator : public ValueArrayAllocator
829{
830public: // overridden from ValueArrayAllocator
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000831virtual ~DefaultValueArrayAllocator()
832{
833}
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000834
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000835virtual ValueInternalArray *newArray()
836{
837 return new ValueInternalArray();
838}
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000839
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000840virtual ValueInternalArray *newArrayCopy( const ValueInternalArray &other )
841{
842 return new ValueInternalArray( other );
843}
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000844
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000845virtual void destruct( ValueInternalArray *array )
846{
847 delete array;
848}
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000849
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000850virtual void reallocateArrayPageIndex( Value **&indexes,
851 ValueInternalArray::PageIndex
852&indexCount,
853 ValueInternalArray::PageIndex
854minNewIndexCount )
855{
856 ValueInternalArray::PageIndex newIndexCount = (indexCount*3)/2 + 1;
857 if ( minNewIndexCount > newIndexCount )
858 newIndexCount = minNewIndexCount;
859 void *newIndexes = realloc( indexes, sizeof(Value*) * newIndexCount );
860 if ( !newIndexes )
861 throw std::bad_alloc();
862 indexCount = newIndexCount;
863 indexes = static_cast<Value **>( newIndexes );
864}
865virtual void releaseArrayPageIndex( Value **indexes,
866 ValueInternalArray::PageIndex indexCount )
867{
868 if ( indexes )
869 free( indexes );
870}
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000871
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000872virtual Value *allocateArrayPage()
873{
874 return static_cast<Value *>( malloc( sizeof(Value) *
875ValueInternalArray::itemsPerPage ) );
876}
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000877
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000878virtual void releaseArrayPage( Value *value )
879{
880 if ( value )
881 free( value );
882}
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000883};
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000884 \endcode
885 */
886class JSON_API ValueArrayAllocator {
887public:
888 virtual ~ValueArrayAllocator();
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000889 virtual ValueInternalArray* newArray() = 0;
890 virtual ValueInternalArray* newArrayCopy(const ValueInternalArray& other) = 0;
891 virtual void destructArray(ValueInternalArray* array) = 0;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000892 /** \brief Reallocate array page index.
893 * Reallocates an array of pointer on each page.
894 * \param indexes [input] pointer on the current index. May be \c NULL.
895 * [output] pointer on the new index of at least
896 * \a minNewIndexCount pages.
897 * \param indexCount [input] current number of pages in the index.
898 * [output] number of page the reallocated index can handle.
899 * \b MUST be >= \a minNewIndexCount.
900 * \param minNewIndexCount Minimum number of page the new index must be able
901 * to
902 * handle.
903 */
904 virtual void
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000905 reallocateArrayPageIndex(Value**& indexes,
906 ValueInternalArray::PageIndex& indexCount,
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000907 ValueInternalArray::PageIndex minNewIndexCount) = 0;
908 virtual void
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000909 releaseArrayPageIndex(Value** indexes,
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000910 ValueInternalArray::PageIndex indexCount) = 0;
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000911 virtual Value* allocateArrayPage() = 0;
912 virtual void releaseArrayPage(Value* value) = 0;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000913};
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000914#endif // #ifdef JSON_VALUE_USE_INTERNAL_MAP
915
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000916/** \brief base class for Value iterators.
917 *
918 */
919class JSON_API ValueIteratorBase {
920public:
921 typedef std::bidirectional_iterator_tag iterator_category;
922 typedef unsigned int size_t;
923 typedef int difference_type;
924 typedef ValueIteratorBase SelfType;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000925
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000926 ValueIteratorBase();
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000927#ifndef JSON_VALUE_USE_INTERNAL_MAP
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000928 explicit ValueIteratorBase(const Value::ObjectValues::iterator& current);
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000929#else
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000930 ValueIteratorBase(const ValueInternalArray::IteratorState& state);
931 ValueIteratorBase(const ValueInternalMap::IteratorState& state);
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000932#endif
933
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000934 bool operator==(const SelfType& other) const { return isEqual(other); }
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000935
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000936 bool operator!=(const SelfType& other) const { return !isEqual(other); }
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000937
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000938 difference_type operator-(const SelfType& other) const {
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000939 return computeDistance(other);
940 }
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000941
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000942 /// Return either the index or the member name of the referenced value as a
943 /// Value.
944 Value key() const;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000945
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000946 /// Return the index of the referenced Value. -1 if it is not an arrayValue.
947 UInt index() const;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000948
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000949 /// Return the member name of the referenced Value. "" if it is not an
950 /// objectValue.
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000951 const char* memberName() const;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000952
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000953protected:
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000954 Value& deref() const;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000955
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000956 void increment();
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000957
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000958 void decrement();
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000959
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000960 difference_type computeDistance(const SelfType& other) const;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000961
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000962 bool isEqual(const SelfType& other) const;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000963
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000964 void copy(const SelfType& other);
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000965
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000966private:
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000967#ifndef JSON_VALUE_USE_INTERNAL_MAP
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000968 Value::ObjectValues::iterator current_;
969 // Indicates that iterator is for a null value.
970 bool isNull_;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000971#else
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000972 union {
973 ValueInternalArray::IteratorState array_;
974 ValueInternalMap::IteratorState map_;
975 } iterator_;
976 bool isArray_;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000977#endif
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000978};
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000979
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000980/** \brief const iterator for object and array value.
981 *
982 */
983class JSON_API ValueConstIterator : public ValueIteratorBase {
984 friend class Value;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000985
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000986public:
987 typedef const Value value_type;
988 typedef unsigned int size_t;
989 typedef int difference_type;
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000990 typedef const Value& reference;
991 typedef const Value* pointer;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000992 typedef ValueConstIterator SelfType;
993
994 ValueConstIterator();
995
996private:
997/*! \internal Use by Value to create an iterator.
998 */
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000999#ifndef JSON_VALUE_USE_INTERNAL_MAP
Aaron Jacobs11086dd2014-09-15 10:15:29 +10001000 explicit ValueConstIterator(const Value::ObjectValues::iterator& current);
Christopher Dunn6d135cb2007-06-13 15:51:04 +00001001#else
Aaron Jacobs11086dd2014-09-15 10:15:29 +10001002 ValueConstIterator(const ValueInternalArray::IteratorState& state);
1003 ValueConstIterator(const ValueInternalMap::IteratorState& state);
Christopher Dunn6d135cb2007-06-13 15:51:04 +00001004#endif
Aaron Jacobs9fa4e842014-07-01 08:48:54 +10001005public:
Aaron Jacobs11086dd2014-09-15 10:15:29 +10001006 SelfType& operator=(const ValueIteratorBase& other);
Christopher Dunn6d135cb2007-06-13 15:51:04 +00001007
Aaron Jacobs9fa4e842014-07-01 08:48:54 +10001008 SelfType operator++(int) {
1009 SelfType temp(*this);
1010 ++*this;
1011 return temp;
1012 }
Christopher Dunn6d135cb2007-06-13 15:51:04 +00001013
Aaron Jacobs9fa4e842014-07-01 08:48:54 +10001014 SelfType operator--(int) {
1015 SelfType temp(*this);
1016 --*this;
1017 return temp;
1018 }
Christopher Dunn6d135cb2007-06-13 15:51:04 +00001019
Aaron Jacobs11086dd2014-09-15 10:15:29 +10001020 SelfType& operator--() {
Aaron Jacobs9fa4e842014-07-01 08:48:54 +10001021 decrement();
1022 return *this;
1023 }
Christopher Dunn6d135cb2007-06-13 15:51:04 +00001024
Aaron Jacobs11086dd2014-09-15 10:15:29 +10001025 SelfType& operator++() {
Aaron Jacobs9fa4e842014-07-01 08:48:54 +10001026 increment();
1027 return *this;
1028 }
Christopher Dunn6d135cb2007-06-13 15:51:04 +00001029
Aaron Jacobs9fa4e842014-07-01 08:48:54 +10001030 reference operator*() const { return deref(); }
Braden McDorman540db3b2014-09-14 02:31:23 -05001031
1032 pointer operator->() const { return &deref(); }
Aaron Jacobs9fa4e842014-07-01 08:48:54 +10001033};
Christopher Dunn6d135cb2007-06-13 15:51:04 +00001034
Aaron Jacobs9fa4e842014-07-01 08:48:54 +10001035/** \brief Iterator for object and array value.
1036 */
1037class JSON_API ValueIterator : public ValueIteratorBase {
1038 friend class Value;
Christopher Dunn6d135cb2007-06-13 15:51:04 +00001039
Aaron Jacobs9fa4e842014-07-01 08:48:54 +10001040public:
1041 typedef Value value_type;
1042 typedef unsigned int size_t;
1043 typedef int difference_type;
Aaron Jacobs11086dd2014-09-15 10:15:29 +10001044 typedef Value& reference;
1045 typedef Value* pointer;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +10001046 typedef ValueIterator SelfType;
Christopher Dunn6d135cb2007-06-13 15:51:04 +00001047
Aaron Jacobs9fa4e842014-07-01 08:48:54 +10001048 ValueIterator();
Aaron Jacobs11086dd2014-09-15 10:15:29 +10001049 ValueIterator(const ValueConstIterator& other);
1050 ValueIterator(const ValueIterator& other);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +10001051
1052private:
1053/*! \internal Use by Value to create an iterator.
1054 */
Christopher Dunn6d135cb2007-06-13 15:51:04 +00001055#ifndef JSON_VALUE_USE_INTERNAL_MAP
Aaron Jacobs11086dd2014-09-15 10:15:29 +10001056 explicit ValueIterator(const Value::ObjectValues::iterator& current);
Christopher Dunn6d135cb2007-06-13 15:51:04 +00001057#else
Aaron Jacobs11086dd2014-09-15 10:15:29 +10001058 ValueIterator(const ValueInternalArray::IteratorState& state);
1059 ValueIterator(const ValueInternalMap::IteratorState& state);
Christopher Dunn6d135cb2007-06-13 15:51:04 +00001060#endif
Aaron Jacobs9fa4e842014-07-01 08:48:54 +10001061public:
Aaron Jacobs11086dd2014-09-15 10:15:29 +10001062 SelfType& operator=(const SelfType& other);
Christopher Dunn6d135cb2007-06-13 15:51:04 +00001063
Aaron Jacobs9fa4e842014-07-01 08:48:54 +10001064 SelfType operator++(int) {
1065 SelfType temp(*this);
1066 ++*this;
1067 return temp;
1068 }
Christopher Dunn6d135cb2007-06-13 15:51:04 +00001069
Aaron Jacobs9fa4e842014-07-01 08:48:54 +10001070 SelfType operator--(int) {
1071 SelfType temp(*this);
1072 --*this;
1073 return temp;
1074 }
Christopher Dunn6d135cb2007-06-13 15:51:04 +00001075
Aaron Jacobs11086dd2014-09-15 10:15:29 +10001076 SelfType& operator--() {
Aaron Jacobs9fa4e842014-07-01 08:48:54 +10001077 decrement();
1078 return *this;
1079 }
Christopher Dunn6d135cb2007-06-13 15:51:04 +00001080
Aaron Jacobs11086dd2014-09-15 10:15:29 +10001081 SelfType& operator++() {
Aaron Jacobs9fa4e842014-07-01 08:48:54 +10001082 increment();
1083 return *this;
1084 }
Christopher Dunn6d135cb2007-06-13 15:51:04 +00001085
Aaron Jacobs9fa4e842014-07-01 08:48:54 +10001086 reference operator*() const { return deref(); }
Braden McDorman540db3b2014-09-14 02:31:23 -05001087
1088 pointer operator->() const { return &deref(); }
Aaron Jacobs9fa4e842014-07-01 08:48:54 +10001089};
Christopher Dunn6d135cb2007-06-13 15:51:04 +00001090
1091} // namespace Json
1092
datadiode9454e682015-01-20 15:25:04 -06001093
1094namespace std {
1095/// Specialize std::swap() for Json::Value.
1096template<>
1097inline void swap(Json::Value& a, Json::Value& b) { a.swap(b); }
1098}
1099
1100
Baptiste Lepilleureafd7022013-05-08 20:21:11 +00001101#if defined(JSONCPP_DISABLE_DLL_INTERFACE_WARNING)
Aaron Jacobs9fa4e842014-07-01 08:48:54 +10001102#pragma warning(pop)
Baptiste Lepilleureafd7022013-05-08 20:21:11 +00001103#endif // if defined(JSONCPP_DISABLE_DLL_INTERFACE_WARNING)
1104
Christopher Dunn6d135cb2007-06-13 15:51:04 +00001105#endif // CPPTL_JSON_H_INCLUDED