blob: 229aa9e3dbb034a0bf755b2241d136f292ab0b71 [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.
Christopher Dunn25342ba2015-03-02 18:05:26 -0600115 *
116 * \note Value string-length fit in size_t, but keys must fit in unsigned.
117 * (The reason is an implementation detail.) The readers will raise an
118 * exception if a bound is exceeded to avoid security holes in your app,
119 * but the Value API does *not* check bounds. That is the responsibility
120 * of the caller.
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000121 */
122class JSON_API Value {
123 friend class ValueIteratorBase;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000124public:
125 typedef std::vector<std::string> Members;
126 typedef ValueIterator iterator;
127 typedef ValueConstIterator const_iterator;
128 typedef Json::UInt UInt;
129 typedef Json::Int Int;
130#if defined(JSON_HAS_INT64)
131 typedef Json::UInt64 UInt64;
132 typedef Json::Int64 Int64;
Baptiste Lepilleur842e9ac2010-12-27 17:45:23 +0000133#endif // defined(JSON_HAS_INT64)
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000134 typedef Json::LargestInt LargestInt;
135 typedef Json::LargestUInt LargestUInt;
136 typedef Json::ArrayIndex ArrayIndex;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000137
Christopher Dunn07f0e932015-02-10 20:45:42 -0600138 static const Value& null; ///! We regret this reference to a global instance; prefer the simpler Value().
139 static const Value& nullRef; ///! just a kludge for binary-compatibility; same as null
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000140 /// Minimum signed integer value that can be stored in a Json::Value.
141 static const LargestInt minLargestInt;
142 /// Maximum signed integer value that can be stored in a Json::Value.
143 static const LargestInt maxLargestInt;
144 /// Maximum unsigned integer value that can be stored in a Json::Value.
145 static const LargestUInt maxLargestUInt;
Baptiste Lepilleur842e9ac2010-12-27 17:45:23 +0000146
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000147 /// Minimum signed int value that can be stored in a Json::Value.
148 static const Int minInt;
149 /// Maximum signed int value that can be stored in a Json::Value.
150 static const Int maxInt;
151 /// Maximum unsigned int value that can be stored in a Json::Value.
152 static const UInt maxUInt;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000153
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000154#if defined(JSON_HAS_INT64)
155 /// Minimum signed 64 bits int value that can be stored in a Json::Value.
156 static const Int64 minInt64;
157 /// Maximum signed 64 bits int value that can be stored in a Json::Value.
158 static const Int64 maxInt64;
159 /// Maximum unsigned 64 bits int value that can be stored in a Json::Value.
160 static const UInt64 maxUInt64;
Aaron Jacobsf1053e72011-05-24 03:18:02 +0000161#endif // defined(JSON_HAS_INT64)
Baptiste Lepilleur842e9ac2010-12-27 17:45:23 +0000162
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000163private:
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000164#ifndef JSONCPP_DOC_EXCLUDE_IMPLEMENTATION
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000165 class CZString {
166 public:
167 enum DuplicationPolicy {
168 noDuplication = 0,
169 duplicate,
170 duplicateOnCopy
171 };
172 CZString(ArrayIndex index);
Christopher Dunn8a770372015-03-02 12:20:22 -0600173 CZString(char const* cstr, unsigned length, DuplicationPolicy allocate);
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000174 CZString(const CZString& other);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000175 ~CZString();
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000176 CZString& operator=(CZString other);
177 bool operator<(const CZString& other) const;
178 bool operator==(const CZString& other) const;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000179 ArrayIndex index() const;
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000180 const char* c_str() const;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000181 bool isStaticString() const;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000182
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000183 private:
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000184 void swap(CZString& other);
Christopher Dunn57ad0512015-03-02 12:10:35 -0600185 struct StringStorage {
186 DuplicationPolicy policy_: 2;
187 unsigned length_: 30; // 1GB max
188 };
189
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000190 const char* cstr_;
Christopher Dunn57ad0512015-03-02 12:10:35 -0600191 union {
192 ArrayIndex index_;
193 StringStorage storage_;
194 };
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000195 };
196
197public:
198#ifndef JSON_USE_CPPTL_SMALLMAP
199 typedef std::map<CZString, Value> ObjectValues;
200#else
201 typedef CppTL::SmallMap<CZString, Value> ObjectValues;
202#endif // ifndef JSON_USE_CPPTL_SMALLMAP
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000203#endif // ifndef JSONCPP_DOC_EXCLUDE_IMPLEMENTATION
204
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000205public:
206 /** \brief Create a default Value of the given type.
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000207
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000208 This is a very useful constructor.
209 To create an empty array, pass arrayValue.
210 To create an empty object, pass objectValue.
211 Another Value can then be set to this one by assignment.
212This is useful since clear() and resize() will not alter types.
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000213
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000214 Examples:
215\code
216Json::Value null_value; // null
217Json::Value arr_value(Json::arrayValue); // []
218Json::Value obj_value(Json::objectValue); // {}
219\endcode
220 */
221 Value(ValueType type = nullValue);
222 Value(Int value);
223 Value(UInt value);
Baptiste Lepilleur842e9ac2010-12-27 17:45:23 +0000224#if defined(JSON_HAS_INT64)
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000225 Value(Int64 value);
226 Value(UInt64 value);
Baptiste Lepilleur842e9ac2010-12-27 17:45:23 +0000227#endif // if defined(JSON_HAS_INT64)
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000228 Value(double value);
Christopher Dunnef21fbc2015-02-20 22:47:27 -0600229 Value(const char* value); ///! Copy til first 0. (NULL causes to seg-fault.)
230 Value(const char* beginValue, const char* endValue); ///! Copy all, incl zeroes.
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000231 /** \brief Constructs a value from a static string.
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000232
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000233 * Like other value string constructor but do not duplicate the string for
234 * internal storage. The given string must remain alive after the call to this
235 * constructor.
236 * Example of usage:
237 * \code
238 * Json::Value aValue( StaticString("some text") );
239 * \endcode
240 */
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000241 Value(const StaticString& value);
Christopher Dunnef21fbc2015-02-20 22:47:27 -0600242 Value(const std::string& value); ///! Copy data() til size(). Embedded zeroes too.
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000243#ifdef JSON_USE_CPPTL
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000244 Value(const CppTL::ConstString& value);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000245#endif
246 Value(bool value);
Christopher Dunn66eb72f2015-01-20 11:02:22 -0600247 /// Deep copy.
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000248 Value(const Value& other);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000249 ~Value();
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000250
Christopher Dunn66eb72f2015-01-20 11:02:22 -0600251 // Deep copy, then swap(other).
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000252 Value& operator=(Value other);
Christopher Dunn66eb72f2015-01-20 11:02:22 -0600253 /// Swap everything.
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000254 void swap(Value& other);
Christopher Dunn66eb72f2015-01-20 11:02:22 -0600255 /// Swap values but leave comments and source offsets in place.
256 void swapPayload(Value& other);
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000257
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000258 ValueType type() const;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000259
Christopher Dunn66eb72f2015-01-20 11:02:22 -0600260 /// Compare payload only, not comments etc.
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000261 bool operator<(const Value& other) const;
262 bool operator<=(const Value& other) const;
263 bool operator>=(const Value& other) const;
264 bool operator>(const Value& other) const;
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000265 bool operator==(const Value& other) const;
266 bool operator!=(const Value& other) const;
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000267 int compare(const Value& other) const;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000268
Christopher Dunnef21fbc2015-02-20 22:47:27 -0600269 const char* asCString() const; ///! Embedded zeroes could cause you trouble!
270 std::string asString() const; ///! Embedded zeroes are possible.
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000271#ifdef JSON_USE_CPPTL
272 CppTL::ConstString asConstString() const;
273#endif
274 Int asInt() const;
275 UInt asUInt() const;
Aaron Jacobsf1053e72011-05-24 03:18:02 +0000276#if defined(JSON_HAS_INT64)
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000277 Int64 asInt64() const;
278 UInt64 asUInt64() const;
Aaron Jacobsf1053e72011-05-24 03:18:02 +0000279#endif // if defined(JSON_HAS_INT64)
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000280 LargestInt asLargestInt() const;
281 LargestUInt asLargestUInt() const;
282 float asFloat() const;
283 double asDouble() const;
284 bool asBool() const;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000285
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000286 bool isNull() const;
287 bool isBool() const;
288 bool isInt() const;
289 bool isInt64() const;
290 bool isUInt() const;
291 bool isUInt64() const;
292 bool isIntegral() const;
293 bool isDouble() const;
294 bool isNumeric() const;
295 bool isString() const;
296 bool isArray() const;
297 bool isObject() const;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000298
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000299 bool isConvertibleTo(ValueType other) const;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000300
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000301 /// Number of values in array or object
302 ArrayIndex size() const;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000303
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000304 /// \brief Return true if empty array, empty object, or null;
305 /// otherwise, false.
306 bool empty() const;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000307
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000308 /// Return isNull()
309 bool operator!() const;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000310
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000311 /// Remove all object members and array elements.
312 /// \pre type() is arrayValue, objectValue, or nullValue
313 /// \post type() is unchanged
314 void clear();
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000315
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000316 /// Resize the array to size elements.
317 /// New elements are initialized to null.
318 /// May only be called on nullValue or arrayValue.
319 /// \pre type() is arrayValue or nullValue
320 /// \post type() is arrayValue
321 void resize(ArrayIndex size);
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000322
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000323 /// Access an array element (zero based index ).
324 /// If the array contains less than index element, then null value are
325 /// inserted
326 /// in the array so that its size is index+1.
327 /// (You may need to say 'value[0u]' to get your compiler to distinguish
328 /// this from the operator[] which takes a string.)
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000329 Value& operator[](ArrayIndex index);
Baptiste Lepilleurfa130ef2010-12-24 12:47:14 +0000330
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000331 /// Access an array element (zero based index ).
332 /// If the array contains less than index element, then null value are
333 /// inserted
334 /// in the array so that its size is index+1.
335 /// (You may need to say 'value[0u]' to get your compiler to distinguish
336 /// this from the operator[] which takes a string.)
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000337 Value& operator[](int index);
Baptiste Lepilleurfa130ef2010-12-24 12:47:14 +0000338
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000339 /// Access an array element (zero based index )
340 /// (You may need to say 'value[0u]' to get your compiler to distinguish
341 /// this from the operator[] which takes a string.)
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000342 const Value& operator[](ArrayIndex index) const;
Baptiste Lepilleurfa130ef2010-12-24 12:47:14 +0000343
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000344 /// Access an array element (zero based index )
345 /// (You may need to say 'value[0u]' to get your compiler to distinguish
346 /// this from the operator[] which takes a string.)
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000347 const Value& operator[](int index) const;
Baptiste Lepilleurfa130ef2010-12-24 12:47:14 +0000348
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000349 /// If the array contains at least index+1 elements, returns the element
350 /// value,
351 /// otherwise returns defaultValue.
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000352 Value get(ArrayIndex index, const Value& defaultValue) const;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000353 /// Return true if index < size().
354 bool isValidIndex(ArrayIndex index) const;
355 /// \brief Append value to array at the end.
356 ///
357 /// Equivalent to jsonvalue[jsonvalue.size()] = value;
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000358 Value& append(const Value& value);
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000359
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000360 /// Access an object value by name, create a null member if it does not exist.
Christopher Dunn25342ba2015-03-02 18:05:26 -0600361 /// \note Because of our implementation, keys are limited to 2^30 -1 chars.
362 /// Exceeding that will cause an exception.
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000363 Value& operator[](const char* key);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000364 /// Access an object value by name, returns null if there is no member with
365 /// that name.
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000366 const Value& operator[](const char* key) const;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000367 /// Access an object value by name, create a null member if it does not exist.
Christopher Dunn25342ba2015-03-02 18:05:26 -0600368 /// \param key may contain embedded nulls.
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000369 Value& operator[](const std::string& key);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000370 /// Access an object value by name, returns null if there is no member with
371 /// that name.
Christopher Dunn25342ba2015-03-02 18:05:26 -0600372 /// \param key may contain embedded nulls.
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000373 const Value& operator[](const std::string& key) const;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000374 /** \brief Access an object value by name, create a null member if it does not
375 exist.
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000376
Christopher Dunn25342ba2015-03-02 18:05:26 -0600377 * \param key may contain embedded nulls.
378 *
379 * If the object has no entry for that name, then the member name used to store
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000380 * the new entry is not duplicated.
381 * Example of use:
382 * \code
383 * Json::Value object;
384 * static const StaticString code("code");
385 * object[code] = 1234;
386 * \endcode
387 */
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000388 Value& operator[](const StaticString& key);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000389#ifdef JSON_USE_CPPTL
390 /// Access an object value by name, create a null member if it does not exist.
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000391 Value& operator[](const CppTL::ConstString& key);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000392 /// Access an object value by name, returns null if there is no member with
393 /// that name.
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000394 const Value& operator[](const CppTL::ConstString& key) const;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000395#endif
396 /// Return the member named key if it exist, defaultValue otherwise.
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000397 Value get(const char* key, const Value& defaultValue) const;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000398 /// Return the member named key if it exist, defaultValue otherwise.
Christopher Dunn25342ba2015-03-02 18:05:26 -0600399 /// \param key may contain embedded nulls.
400 Value get(const char* key, const char* end, const Value& defaultValue) const;
401 /// Return the member named key if it exist, defaultValue otherwise.
402 /// \param key may contain embedded nulls.
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000403 Value get(const std::string& key, const Value& defaultValue) const;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000404#ifdef JSON_USE_CPPTL
405 /// Return the member named key if it exist, defaultValue otherwise.
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000406 Value get(const CppTL::ConstString& key, const Value& defaultValue) const;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000407#endif
Christopher Dunn25342ba2015-03-02 18:05:26 -0600408 /// Most general and efficient version of isMember()const, get()const,
409 /// and operator[]const
410 /// \note As stated elsewhere, behavior is undefined if (end-key) >= 2^30
411 Value const* find(char const* key, char const* end) const;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000412 /// \brief Remove and return the named member.
413 ///
414 /// Do nothing if it did not exist.
415 /// \return the removed Value, or null.
416 /// \pre type() is objectValue or nullValue
417 /// \post type() is unchanged
Christopher Dunn76746b02015-01-21 16:01:30 -0600418 /// \deprecated
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000419 Value removeMember(const char* key);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000420 /// Same as removeMember(const char*)
Christopher Dunn25342ba2015-03-02 18:05:26 -0600421 /// \param key may contain embedded nulls.
Christopher Dunn76746b02015-01-21 16:01:30 -0600422 /// \deprecated
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000423 Value removeMember(const std::string& key);
Christopher Dunn25342ba2015-03-02 18:05:26 -0600424 /// Same as removeMember(const char* key, const char* end, Value* removed),
425 /// but 'key' is null-terminated.
426 bool removeMember(const char* key, Value* removed);
Christopher Dunn76746b02015-01-21 16:01:30 -0600427 /** \brief Remove the named map member.
428
429 Update 'removed' iff removed.
Christopher Dunn25342ba2015-03-02 18:05:26 -0600430 \param key may contain embedded nulls.
Christopher Dunn76746b02015-01-21 16:01:30 -0600431 \return true iff removed (no exceptions)
432 */
Christopher Dunn25342ba2015-03-02 18:05:26 -0600433 bool removeMember(std::string const& key, Value* removed);
434 /// Same as removeMember(std::string const& key, Value* removed)
435 bool removeMember(const char* key, const char* end, Value* removed);
Christopher Dunn9de2c2d2015-01-20 16:15:40 -0600436 /** \brief Remove the indexed array element.
437
438 O(n) expensive operations.
439 Update 'removed' iff removed.
Christopher Dunne87e41c2015-01-20 16:24:11 -0600440 \return true iff removed (no exceptions)
Christopher Dunn9de2c2d2015-01-20 16:15:40 -0600441 */
442 bool removeIndex(ArrayIndex i, Value* removed);
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000443
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000444 /// Return true if the object has a member named key.
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000445 bool isMember(const char* key) const;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000446 /// Return true if the object has a member named key.
Christopher Dunn25342ba2015-03-02 18:05:26 -0600447 /// \param key may contain embedded nulls.
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000448 bool isMember(const std::string& key) const;
Christopher Dunn25342ba2015-03-02 18:05:26 -0600449 /// Same as isMember(std::string const& key)const
450 bool isMember(const char* key, const char* end) const;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000451#ifdef JSON_USE_CPPTL
452 /// Return true if the object has a member named key.
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000453 bool isMember(const CppTL::ConstString& key) const;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000454#endif
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000455
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000456 /// \brief Return a list of the member names.
457 ///
458 /// If null, return an empty list.
459 /// \pre type() is objectValue or nullValue
460 /// \post if type() was nullValue, it remains nullValue
461 Members getMemberNames() const;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000462
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000463 //# ifdef JSON_USE_CPPTL
464 // EnumMemberNames enumMemberNames() const;
465 // EnumValues enumValues() const;
466 //# endif
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000467
Christopher Dunn1e3149a2015-01-25 14:16:13 -0600468 /// \deprecated Always pass len.
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000469 void setComment(const char* comment, CommentPlacement placement);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000470 /// Comments must be //... or /* ... */
Christopher Dunn1e3149a2015-01-25 14:16:13 -0600471 void setComment(const char* comment, size_t len, CommentPlacement placement);
472 /// Comments must be //... or /* ... */
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000473 void setComment(const std::string& comment, CommentPlacement placement);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000474 bool hasComment(CommentPlacement placement) const;
475 /// Include delimiters and embedded newlines.
476 std::string getComment(CommentPlacement placement) const;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000477
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000478 std::string toStyledString() const;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000479
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000480 const_iterator begin() const;
481 const_iterator end() const;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000482
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000483 iterator begin();
484 iterator end();
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000485
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000486 // Accessors for the [start, limit) range of bytes within the JSON text from
487 // which this value was parsed, if any.
488 void setOffsetStart(size_t start);
489 void setOffsetLimit(size_t limit);
490 size_t getOffsetStart() const;
491 size_t getOffsetLimit() const;
Aaron Jacobs68db6552014-04-23 23:41:12 +0000492
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000493private:
Billy Donahue8eb5d892014-11-10 01:35:42 -0500494 void initBasic(ValueType type, bool allocated = false);
495
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000496 Value& resolveReference(const char* key, bool isStatic);
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000497
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000498 struct CommentInfo {
499 CommentInfo();
500 ~CommentInfo();
501
Christopher Dunn1e3149a2015-01-25 14:16:13 -0600502 void setComment(const char* text, size_t len);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000503
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000504 char* comment_;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000505 };
506
507 // struct MemberNamesTransform
508 //{
509 // typedef const char *result_type;
510 // const char *operator()( const CZString &name ) const
511 // {
512 // return name.c_str();
513 // }
514 //};
515
516 union ValueHolder {
517 LargestInt int_;
518 LargestUInt uint_;
519 double real_;
520 bool bool_;
Christopher Dunna5328352015-02-21 10:54:38 -0600521 char* string_; // actually ptr to unsigned, followed by str
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000522 ObjectValues* map_;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000523 } value_;
524 ValueType type_ : 8;
Christopher Dunn2bc61372015-01-24 13:42:37 -0600525 unsigned int allocated_ : 1; // Notes: if declared as bool, bitfield is useless.
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000526 CommentInfo* comments_;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000527
528 // [start, limit) byte offsets in the source JSON text from which this Value
529 // was extracted.
530 size_t start_;
531 size_t limit_;
532};
533
534/** \brief Experimental and untested: represents an element of the "path" to
535 * access a node.
536 */
537class JSON_API PathArgument {
538public:
539 friend class Path;
540
541 PathArgument();
542 PathArgument(ArrayIndex index);
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000543 PathArgument(const char* key);
544 PathArgument(const std::string& key);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000545
546private:
547 enum Kind {
548 kindNone = 0,
549 kindIndex,
550 kindKey
551 };
552 std::string key_;
553 ArrayIndex index_;
554 Kind kind_;
555};
556
557/** \brief Experimental and untested: represents a "path" to access a node.
558 *
559 * Syntax:
560 * - "." => root node
561 * - ".[n]" => elements at index 'n' of root node (an array value)
562 * - ".name" => member named 'name' of root node (an object value)
563 * - ".name1.name2.name3"
564 * - ".[0][1][2].name1[3]"
565 * - ".%" => member name is provided as parameter
566 * - ".[%]" => index is provied as parameter
567 */
568class JSON_API Path {
569public:
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000570 Path(const std::string& path,
571 const PathArgument& a1 = PathArgument(),
572 const PathArgument& a2 = PathArgument(),
573 const PathArgument& a3 = PathArgument(),
574 const PathArgument& a4 = PathArgument(),
575 const PathArgument& a5 = PathArgument());
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000576
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000577 const Value& resolve(const Value& root) const;
578 Value resolve(const Value& root, const Value& defaultValue) const;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000579 /// Creates the "path" to access the specified node and returns a reference on
580 /// the node.
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000581 Value& make(Value& root) const;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000582
583private:
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000584 typedef std::vector<const PathArgument*> InArgs;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000585 typedef std::vector<PathArgument> Args;
586
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000587 void makePath(const std::string& path, const InArgs& in);
588 void addPathInArg(const std::string& path,
589 const InArgs& in,
590 InArgs::const_iterator& itInArg,
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000591 PathArgument::Kind kind);
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000592 void invalidPath(const std::string& path, int location);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000593
594 Args args_;
595};
596
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000597/** \brief base class for Value iterators.
598 *
599 */
600class JSON_API ValueIteratorBase {
601public:
602 typedef std::bidirectional_iterator_tag iterator_category;
603 typedef unsigned int size_t;
604 typedef int difference_type;
605 typedef ValueIteratorBase SelfType;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000606
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000607 ValueIteratorBase();
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000608 explicit ValueIteratorBase(const Value::ObjectValues::iterator& current);
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000609
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000610 bool operator==(const SelfType& other) const { return isEqual(other); }
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000611
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000612 bool operator!=(const SelfType& other) const { return !isEqual(other); }
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000613
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000614 difference_type operator-(const SelfType& other) const {
Kevin Grant4c5832a2015-02-14 20:53:35 -0800615 return other.computeDistance(*this);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000616 }
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000617
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000618 /// Return either the index or the member name of the referenced value as a
619 /// Value.
620 Value key() const;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000621
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000622 /// Return the index of the referenced Value. -1 if it is not an arrayValue.
623 UInt index() const;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000624
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000625 /// Return the member name of the referenced Value. "" if it is not an
626 /// objectValue.
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000627 const char* memberName() const;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000628
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000629protected:
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000630 Value& deref() const;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000631
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000632 void increment();
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000633
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000634 void decrement();
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000635
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000636 difference_type computeDistance(const SelfType& other) const;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000637
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000638 bool isEqual(const SelfType& other) const;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000639
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000640 void copy(const SelfType& other);
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000641
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000642private:
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000643 Value::ObjectValues::iterator current_;
644 // Indicates that iterator is for a null value.
645 bool isNull_;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000646};
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000647
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000648/** \brief const iterator for object and array value.
649 *
650 */
651class JSON_API ValueConstIterator : public ValueIteratorBase {
652 friend class Value;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000653
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000654public:
655 typedef const Value value_type;
656 typedef unsigned int size_t;
657 typedef int difference_type;
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000658 typedef const Value& reference;
659 typedef const Value* pointer;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000660 typedef ValueConstIterator SelfType;
661
662 ValueConstIterator();
663
664private:
665/*! \internal Use by Value to create an iterator.
666 */
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000667 explicit ValueConstIterator(const Value::ObjectValues::iterator& current);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000668public:
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000669 SelfType& operator=(const ValueIteratorBase& other);
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000670
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000671 SelfType operator++(int) {
672 SelfType temp(*this);
673 ++*this;
674 return temp;
675 }
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000676
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000677 SelfType operator--(int) {
678 SelfType temp(*this);
679 --*this;
680 return temp;
681 }
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000682
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000683 SelfType& operator--() {
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000684 decrement();
685 return *this;
686 }
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000687
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000688 SelfType& operator++() {
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000689 increment();
690 return *this;
691 }
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000692
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000693 reference operator*() const { return deref(); }
Braden McDorman540db3b2014-09-14 02:31:23 -0500694
695 pointer operator->() const { return &deref(); }
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000696};
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000697
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000698/** \brief Iterator for object and array value.
699 */
700class JSON_API ValueIterator : public ValueIteratorBase {
701 friend class Value;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000702
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000703public:
704 typedef Value value_type;
705 typedef unsigned int size_t;
706 typedef int difference_type;
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000707 typedef Value& reference;
708 typedef Value* pointer;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000709 typedef ValueIterator SelfType;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000710
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000711 ValueIterator();
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000712 ValueIterator(const ValueConstIterator& other);
713 ValueIterator(const ValueIterator& other);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000714
715private:
716/*! \internal Use by Value to create an iterator.
717 */
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000718 explicit ValueIterator(const Value::ObjectValues::iterator& current);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000719public:
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000720 SelfType& operator=(const SelfType& other);
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000721
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000722 SelfType operator++(int) {
723 SelfType temp(*this);
724 ++*this;
725 return temp;
726 }
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000727
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000728 SelfType operator--(int) {
729 SelfType temp(*this);
730 --*this;
731 return temp;
732 }
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000733
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000734 SelfType& operator--() {
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000735 decrement();
736 return *this;
737 }
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000738
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000739 SelfType& operator++() {
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000740 increment();
741 return *this;
742 }
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000743
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000744 reference operator*() const { return deref(); }
Braden McDorman540db3b2014-09-14 02:31:23 -0500745
746 pointer operator->() const { return &deref(); }
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000747};
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000748
749} // namespace Json
750
datadiode9454e682015-01-20 15:25:04 -0600751
752namespace std {
753/// Specialize std::swap() for Json::Value.
754template<>
755inline void swap(Json::Value& a, Json::Value& b) { a.swap(b); }
756}
757
758
Baptiste Lepilleureafd7022013-05-08 20:21:11 +0000759#if defined(JSONCPP_DISABLE_DLL_INTERFACE_WARNING)
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000760#pragma warning(pop)
Baptiste Lepilleureafd7022013-05-08 20:21:11 +0000761#endif // if defined(JSONCPP_DISABLE_DLL_INTERFACE_WARNING)
762
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000763#endif // CPPTL_JSON_H_INCLUDED