blob: 439f05d0d0ae11cd185685ed8fb846a6ee6dd497 [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);
Christopher Dunn57ad0512015-03-02 12:10:35 -0600179 struct StringStorage {
180 DuplicationPolicy policy_: 2;
181 unsigned length_: 30; // 1GB max
182 };
183
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000184 const char* cstr_;
Christopher Dunn57ad0512015-03-02 12:10:35 -0600185 union {
186 ArrayIndex index_;
187 StringStorage storage_;
188 };
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000189 };
190
191public:
192#ifndef JSON_USE_CPPTL_SMALLMAP
193 typedef std::map<CZString, Value> ObjectValues;
194#else
195 typedef CppTL::SmallMap<CZString, Value> ObjectValues;
196#endif // ifndef JSON_USE_CPPTL_SMALLMAP
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000197#endif // ifndef JSONCPP_DOC_EXCLUDE_IMPLEMENTATION
198
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000199public:
200 /** \brief Create a default Value of the given type.
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000201
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000202 This is a very useful constructor.
203 To create an empty array, pass arrayValue.
204 To create an empty object, pass objectValue.
205 Another Value can then be set to this one by assignment.
206This is useful since clear() and resize() will not alter types.
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000207
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000208 Examples:
209\code
210Json::Value null_value; // null
211Json::Value arr_value(Json::arrayValue); // []
212Json::Value obj_value(Json::objectValue); // {}
213\endcode
214 */
215 Value(ValueType type = nullValue);
216 Value(Int value);
217 Value(UInt value);
Baptiste Lepilleur842e9ac2010-12-27 17:45:23 +0000218#if defined(JSON_HAS_INT64)
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000219 Value(Int64 value);
220 Value(UInt64 value);
Baptiste Lepilleur842e9ac2010-12-27 17:45:23 +0000221#endif // if defined(JSON_HAS_INT64)
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000222 Value(double value);
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000223 Value(const char* value);
224 Value(const char* beginValue, const char* endValue);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000225 /** \brief Constructs a value from a static string.
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000226
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000227 * Like other value string constructor but do not duplicate the string for
228 * internal storage. The given string must remain alive after the call to this
229 * constructor.
230 * Example of usage:
231 * \code
232 * Json::Value aValue( StaticString("some text") );
233 * \endcode
234 */
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000235 Value(const StaticString& value);
236 Value(const std::string& value);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000237#ifdef JSON_USE_CPPTL
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000238 Value(const CppTL::ConstString& value);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000239#endif
240 Value(bool value);
Christopher Dunn66eb72f2015-01-20 11:02:22 -0600241 /// Deep copy.
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000242 Value(const Value& other);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000243 ~Value();
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000244
Christopher Dunn66eb72f2015-01-20 11:02:22 -0600245 // Deep copy, then swap(other).
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000246 Value& operator=(Value other);
Christopher Dunn66eb72f2015-01-20 11:02:22 -0600247 /// Swap everything.
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000248 void swap(Value& other);
Christopher Dunn66eb72f2015-01-20 11:02:22 -0600249 /// Swap values but leave comments and source offsets in place.
250 void swapPayload(Value& other);
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000251
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000252 ValueType type() const;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000253
Christopher Dunn66eb72f2015-01-20 11:02:22 -0600254 /// Compare payload only, not comments etc.
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000255 bool operator<(const Value& other) const;
256 bool operator<=(const Value& other) const;
257 bool operator>=(const Value& other) const;
258 bool operator>(const Value& other) const;
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000259 bool operator==(const Value& other) const;
260 bool operator!=(const Value& other) const;
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000261 int compare(const Value& other) const;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000262
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000263 const char* asCString() const;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000264 std::string asString() const;
265#ifdef JSON_USE_CPPTL
266 CppTL::ConstString asConstString() const;
267#endif
268 Int asInt() const;
269 UInt asUInt() const;
Aaron Jacobsf1053e72011-05-24 03:18:02 +0000270#if defined(JSON_HAS_INT64)
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000271 Int64 asInt64() const;
272 UInt64 asUInt64() const;
Aaron Jacobsf1053e72011-05-24 03:18:02 +0000273#endif // if defined(JSON_HAS_INT64)
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000274 LargestInt asLargestInt() const;
275 LargestUInt asLargestUInt() const;
276 float asFloat() const;
277 double asDouble() const;
278 bool asBool() const;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000279
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000280 bool isNull() const;
281 bool isBool() const;
282 bool isInt() const;
283 bool isInt64() const;
284 bool isUInt() const;
285 bool isUInt64() const;
286 bool isIntegral() const;
287 bool isDouble() const;
288 bool isNumeric() const;
289 bool isString() const;
290 bool isArray() const;
291 bool isObject() const;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000292
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000293 bool isConvertibleTo(ValueType other) const;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000294
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000295 /// Number of values in array or object
296 ArrayIndex size() const;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000297
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000298 /// \brief Return true if empty array, empty object, or null;
299 /// otherwise, false.
300 bool empty() const;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000301
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000302 /// Return isNull()
303 bool operator!() const;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000304
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000305 /// Remove all object members and array elements.
306 /// \pre type() is arrayValue, objectValue, or nullValue
307 /// \post type() is unchanged
308 void clear();
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000309
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000310 /// Resize the array to size elements.
311 /// New elements are initialized to null.
312 /// May only be called on nullValue or arrayValue.
313 /// \pre type() is arrayValue or nullValue
314 /// \post type() is arrayValue
315 void resize(ArrayIndex size);
Christopher Dunn6d135cb2007-06-13 15:51:04 +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[](ArrayIndex 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 /// If the array contains less than index element, then null value are
327 /// inserted
328 /// in the array so that its size is index+1.
329 /// (You may need to say 'value[0u]' to get your compiler to distinguish
330 /// this from the operator[] which takes a string.)
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000331 Value& operator[](int index);
Baptiste Lepilleurfa130ef2010-12-24 12:47:14 +0000332
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000333 /// Access an array element (zero based index )
334 /// (You may need to say 'value[0u]' to get your compiler to distinguish
335 /// this from the operator[] which takes a string.)
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000336 const Value& operator[](ArrayIndex index) const;
Baptiste Lepilleurfa130ef2010-12-24 12:47:14 +0000337
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000338 /// Access an array element (zero based index )
339 /// (You may need to say 'value[0u]' to get your compiler to distinguish
340 /// this from the operator[] which takes a string.)
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000341 const Value& operator[](int index) const;
Baptiste Lepilleurfa130ef2010-12-24 12:47:14 +0000342
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000343 /// If the array contains at least index+1 elements, returns the element
344 /// value,
345 /// otherwise returns defaultValue.
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000346 Value get(ArrayIndex index, const Value& defaultValue) const;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000347 /// Return true if index < size().
348 bool isValidIndex(ArrayIndex index) const;
349 /// \brief Append value to array at the end.
350 ///
351 /// Equivalent to jsonvalue[jsonvalue.size()] = value;
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000352 Value& append(const Value& value);
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000353
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000354 /// Access an object value by name, create a null member if it does not exist.
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000355 Value& operator[](const char* key);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000356 /// Access an object value by name, returns null if there is no member with
357 /// that name.
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000358 const Value& operator[](const char* key) const;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000359 /// Access an object value by name, create a null member if it does not exist.
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000360 Value& operator[](const std::string& key);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000361 /// Access an object value by name, returns null if there is no member with
362 /// that name.
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000363 const Value& operator[](const std::string& key) const;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000364 /** \brief Access an object value by name, create a null member if it does not
365 exist.
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000366
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000367 * If the object as no entry for that name, then the member name used to store
368 * the new entry is not duplicated.
369 * Example of use:
370 * \code
371 * Json::Value object;
372 * static const StaticString code("code");
373 * object[code] = 1234;
374 * \endcode
375 */
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000376 Value& operator[](const StaticString& key);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000377#ifdef JSON_USE_CPPTL
378 /// Access an object value by name, create a null member if it does not exist.
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000379 Value& operator[](const CppTL::ConstString& key);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000380 /// Access an object value by name, returns null if there is no member with
381 /// that name.
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000382 const Value& operator[](const CppTL::ConstString& key) const;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000383#endif
384 /// Return the member named key if it exist, defaultValue otherwise.
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000385 Value get(const char* key, const Value& defaultValue) const;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000386 /// Return the member named key if it exist, defaultValue otherwise.
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000387 Value get(const std::string& key, const Value& defaultValue) const;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000388#ifdef JSON_USE_CPPTL
389 /// Return the member named key if it exist, defaultValue otherwise.
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000390 Value get(const CppTL::ConstString& key, const Value& defaultValue) const;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000391#endif
392 /// \brief Remove and return the named member.
393 ///
394 /// Do nothing if it did not exist.
395 /// \return the removed Value, or null.
396 /// \pre type() is objectValue or nullValue
397 /// \post type() is unchanged
Christopher Dunn76746b02015-01-21 16:01:30 -0600398 /// \deprecated
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000399 Value removeMember(const char* key);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000400 /// Same as removeMember(const char*)
Christopher Dunn76746b02015-01-21 16:01:30 -0600401 /// \deprecated
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000402 Value removeMember(const std::string& key);
Christopher Dunn76746b02015-01-21 16:01:30 -0600403 /** \brief Remove the named map member.
404
405 Update 'removed' iff removed.
406 \return true iff removed (no exceptions)
407 */
408 bool removeMember(const char* key, Value* removed);
Christopher Dunn9de2c2d2015-01-20 16:15:40 -0600409 /** \brief Remove the indexed array element.
410
411 O(n) expensive operations.
412 Update 'removed' iff removed.
Christopher Dunne87e41c2015-01-20 16:24:11 -0600413 \return true iff removed (no exceptions)
Christopher Dunn9de2c2d2015-01-20 16:15:40 -0600414 */
415 bool removeIndex(ArrayIndex i, Value* removed);
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000416
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000417 /// Return true if the object has a member named key.
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000418 bool isMember(const char* key) const;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000419 /// Return true if the object has a member named key.
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000420 bool isMember(const std::string& key) const;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000421#ifdef JSON_USE_CPPTL
422 /// Return true if the object has a member named key.
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000423 bool isMember(const CppTL::ConstString& key) const;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000424#endif
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000425
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000426 /// \brief Return a list of the member names.
427 ///
428 /// If null, return an empty list.
429 /// \pre type() is objectValue or nullValue
430 /// \post if type() was nullValue, it remains nullValue
431 Members getMemberNames() const;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000432
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000433 //# ifdef JSON_USE_CPPTL
434 // EnumMemberNames enumMemberNames() const;
435 // EnumValues enumValues() const;
436 //# endif
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000437
Christopher Dunn1e3149a2015-01-25 14:16:13 -0600438 /// \deprecated Always pass len.
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000439 void setComment(const char* comment, CommentPlacement placement);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000440 /// Comments must be //... or /* ... */
Christopher Dunn1e3149a2015-01-25 14:16:13 -0600441 void setComment(const char* comment, size_t len, CommentPlacement placement);
442 /// Comments must be //... or /* ... */
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000443 void setComment(const std::string& comment, CommentPlacement placement);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000444 bool hasComment(CommentPlacement placement) const;
445 /// Include delimiters and embedded newlines.
446 std::string getComment(CommentPlacement placement) const;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000447
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000448 std::string toStyledString() const;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000449
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000450 const_iterator begin() const;
451 const_iterator end() const;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000452
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000453 iterator begin();
454 iterator end();
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000455
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000456 // Accessors for the [start, limit) range of bytes within the JSON text from
457 // which this value was parsed, if any.
458 void setOffsetStart(size_t start);
459 void setOffsetLimit(size_t limit);
460 size_t getOffsetStart() const;
461 size_t getOffsetLimit() const;
Aaron Jacobs68db6552014-04-23 23:41:12 +0000462
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000463private:
Billy Donahue8eb5d892014-11-10 01:35:42 -0500464 void initBasic(ValueType type, bool allocated = false);
465
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000466 Value& resolveReference(const char* key, bool isStatic);
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000467
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000468 struct CommentInfo {
469 CommentInfo();
470 ~CommentInfo();
471
Christopher Dunn1e3149a2015-01-25 14:16:13 -0600472 void setComment(const char* text, size_t len);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000473
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000474 char* comment_;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000475 };
476
477 // struct MemberNamesTransform
478 //{
479 // typedef const char *result_type;
480 // const char *operator()( const CZString &name ) const
481 // {
482 // return name.c_str();
483 // }
484 //};
485
486 union ValueHolder {
487 LargestInt int_;
488 LargestUInt uint_;
489 double real_;
490 bool bool_;
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000491 char* string_;
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000492 ObjectValues* map_;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000493 } value_;
494 ValueType type_ : 8;
Christopher Dunn2bc61372015-01-24 13:42:37 -0600495 unsigned int allocated_ : 1; // Notes: if declared as bool, bitfield is useless.
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000496 CommentInfo* comments_;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000497
498 // [start, limit) byte offsets in the source JSON text from which this Value
499 // was extracted.
500 size_t start_;
501 size_t limit_;
502};
503
504/** \brief Experimental and untested: represents an element of the "path" to
505 * access a node.
506 */
507class JSON_API PathArgument {
508public:
509 friend class Path;
510
511 PathArgument();
512 PathArgument(ArrayIndex index);
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000513 PathArgument(const char* key);
514 PathArgument(const std::string& key);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000515
516private:
517 enum Kind {
518 kindNone = 0,
519 kindIndex,
520 kindKey
521 };
522 std::string key_;
523 ArrayIndex index_;
524 Kind kind_;
525};
526
527/** \brief Experimental and untested: represents a "path" to access a node.
528 *
529 * Syntax:
530 * - "." => root node
531 * - ".[n]" => elements at index 'n' of root node (an array value)
532 * - ".name" => member named 'name' of root node (an object value)
533 * - ".name1.name2.name3"
534 * - ".[0][1][2].name1[3]"
535 * - ".%" => member name is provided as parameter
536 * - ".[%]" => index is provied as parameter
537 */
538class JSON_API Path {
539public:
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000540 Path(const std::string& path,
541 const PathArgument& a1 = PathArgument(),
542 const PathArgument& a2 = PathArgument(),
543 const PathArgument& a3 = PathArgument(),
544 const PathArgument& a4 = PathArgument(),
545 const PathArgument& a5 = PathArgument());
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000546
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000547 const Value& resolve(const Value& root) const;
548 Value resolve(const Value& root, const Value& defaultValue) const;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000549 /// Creates the "path" to access the specified node and returns a reference on
550 /// the node.
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000551 Value& make(Value& root) const;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000552
553private:
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000554 typedef std::vector<const PathArgument*> InArgs;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000555 typedef std::vector<PathArgument> Args;
556
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000557 void makePath(const std::string& path, const InArgs& in);
558 void addPathInArg(const std::string& path,
559 const InArgs& in,
560 InArgs::const_iterator& itInArg,
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000561 PathArgument::Kind kind);
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000562 void invalidPath(const std::string& path, int location);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000563
564 Args args_;
565};
566
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000567/** \brief base class for Value iterators.
568 *
569 */
570class JSON_API ValueIteratorBase {
571public:
572 typedef std::bidirectional_iterator_tag iterator_category;
573 typedef unsigned int size_t;
574 typedef int difference_type;
575 typedef ValueIteratorBase SelfType;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000576
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000577 ValueIteratorBase();
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000578 explicit ValueIteratorBase(const Value::ObjectValues::iterator& current);
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000579
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000580 bool operator==(const SelfType& other) const { return isEqual(other); }
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000581
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000582 bool operator!=(const SelfType& other) const { return !isEqual(other); }
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000583
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000584 difference_type operator-(const SelfType& other) const {
Kevin Grant4c5832a2015-02-14 20:53:35 -0800585 return other.computeDistance(*this);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000586 }
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000587
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000588 /// Return either the index or the member name of the referenced value as a
589 /// Value.
590 Value key() const;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000591
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000592 /// Return the index of the referenced Value. -1 if it is not an arrayValue.
593 UInt index() const;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000594
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000595 /// Return the member name of the referenced Value. "" if it is not an
596 /// objectValue.
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000597 const char* memberName() const;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000598
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000599protected:
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000600 Value& deref() const;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000601
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000602 void increment();
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000603
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000604 void decrement();
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000605
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000606 difference_type computeDistance(const SelfType& other) const;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000607
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000608 bool isEqual(const SelfType& other) const;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000609
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000610 void copy(const SelfType& other);
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000611
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000612private:
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000613 Value::ObjectValues::iterator current_;
614 // Indicates that iterator is for a null value.
615 bool isNull_;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000616};
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000617
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000618/** \brief const iterator for object and array value.
619 *
620 */
621class JSON_API ValueConstIterator : public ValueIteratorBase {
622 friend class Value;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000623
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000624public:
625 typedef const Value value_type;
626 typedef unsigned int size_t;
627 typedef int difference_type;
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000628 typedef const Value& reference;
629 typedef const Value* pointer;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000630 typedef ValueConstIterator SelfType;
631
632 ValueConstIterator();
633
634private:
635/*! \internal Use by Value to create an iterator.
636 */
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000637 explicit ValueConstIterator(const Value::ObjectValues::iterator& current);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000638public:
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000639 SelfType& operator=(const ValueIteratorBase& other);
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000640
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000641 SelfType operator++(int) {
642 SelfType temp(*this);
643 ++*this;
644 return temp;
645 }
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000646
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000647 SelfType operator--(int) {
648 SelfType temp(*this);
649 --*this;
650 return temp;
651 }
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000652
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000653 SelfType& operator--() {
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000654 decrement();
655 return *this;
656 }
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000657
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000658 SelfType& operator++() {
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000659 increment();
660 return *this;
661 }
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000662
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000663 reference operator*() const { return deref(); }
Braden McDorman540db3b2014-09-14 02:31:23 -0500664
665 pointer operator->() const { return &deref(); }
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000666};
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000667
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000668/** \brief Iterator for object and array value.
669 */
670class JSON_API ValueIterator : public ValueIteratorBase {
671 friend class Value;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000672
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000673public:
674 typedef Value value_type;
675 typedef unsigned int size_t;
676 typedef int difference_type;
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000677 typedef Value& reference;
678 typedef Value* pointer;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000679 typedef ValueIterator SelfType;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000680
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000681 ValueIterator();
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000682 ValueIterator(const ValueConstIterator& other);
683 ValueIterator(const ValueIterator& other);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000684
685private:
686/*! \internal Use by Value to create an iterator.
687 */
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000688 explicit ValueIterator(const Value::ObjectValues::iterator& current);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000689public:
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000690 SelfType& operator=(const SelfType& other);
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000691
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000692 SelfType operator++(int) {
693 SelfType temp(*this);
694 ++*this;
695 return temp;
696 }
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000697
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000698 SelfType operator--(int) {
699 SelfType temp(*this);
700 --*this;
701 return temp;
702 }
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000703
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000704 SelfType& operator--() {
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000705 decrement();
706 return *this;
707 }
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000708
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000709 SelfType& operator++() {
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000710 increment();
711 return *this;
712 }
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000713
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000714 reference operator*() const { return deref(); }
Braden McDorman540db3b2014-09-14 02:31:23 -0500715
716 pointer operator->() const { return &deref(); }
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000717};
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000718
719} // namespace Json
720
datadiode9454e682015-01-20 15:25:04 -0600721
722namespace std {
723/// Specialize std::swap() for Json::Value.
724template<>
725inline void swap(Json::Value& a, Json::Value& b) { a.swap(b); }
726}
727
728
Baptiste Lepilleureafd7022013-05-08 20:21:11 +0000729#if defined(JSONCPP_DISABLE_DLL_INTERFACE_WARNING)
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000730#pragma warning(pop)
Baptiste Lepilleureafd7022013-05-08 20:21:11 +0000731#endif // if defined(JSONCPP_DISABLE_DLL_INTERFACE_WARNING)
732
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000733#endif // CPPTL_JSON_H_INCLUDED