json test

Committer:
tgw
Date:
Fri Jan 26 06:05:31 2018 +0000
Revision:
0:2ee762ea11b3
json

Who changed what in which revision?

UserRevisionLine numberNew contents of line
tgw 0:2ee762ea11b3 1 // Copyright 2007-2010 Baptiste Lepilleur and The JsonCpp Authors
tgw 0:2ee762ea11b3 2 // Distributed under MIT license, or public domain if desired and
tgw 0:2ee762ea11b3 3 // recognized in your jurisdiction.
tgw 0:2ee762ea11b3 4 // See file LICENSE for detail or copy at http://jsoncpp.sourceforge.net/LICENSE
tgw 0:2ee762ea11b3 5
tgw 0:2ee762ea11b3 6 #ifndef CPPTL_JSON_H_INCLUDED
tgw 0:2ee762ea11b3 7 #define CPPTL_JSON_H_INCLUDED
tgw 0:2ee762ea11b3 8
tgw 0:2ee762ea11b3 9 #if !defined(JSON_IS_AMALGAMATION)
tgw 0:2ee762ea11b3 10 #include "forwards.h"
tgw 0:2ee762ea11b3 11 #endif // if !defined(JSON_IS_AMALGAMATION)
tgw 0:2ee762ea11b3 12 #include <string>
tgw 0:2ee762ea11b3 13 #include <vector>
tgw 0:2ee762ea11b3 14 #include <exception>
tgw 0:2ee762ea11b3 15
tgw 0:2ee762ea11b3 16 #ifndef JSON_USE_CPPTL_SMALLMAP
tgw 0:2ee762ea11b3 17 #include <map>
tgw 0:2ee762ea11b3 18 #else
tgw 0:2ee762ea11b3 19 #include <cpptl/smallmap.h>
tgw 0:2ee762ea11b3 20 #endif
tgw 0:2ee762ea11b3 21 #ifdef JSON_USE_CPPTL
tgw 0:2ee762ea11b3 22 #include <cpptl/forwards.h>
tgw 0:2ee762ea11b3 23 #endif
tgw 0:2ee762ea11b3 24
tgw 0:2ee762ea11b3 25 //Conditional NORETURN attribute on the throw functions would:
tgw 0:2ee762ea11b3 26 // a) suppress false positives from static code analysis
tgw 0:2ee762ea11b3 27 // b) possibly improve optimization opportunities.
tgw 0:2ee762ea11b3 28 #if !defined(JSONCPP_NORETURN)
tgw 0:2ee762ea11b3 29 # if defined(_MSC_VER)
tgw 0:2ee762ea11b3 30 # define JSONCPP_NORETURN __declspec(noreturn)
tgw 0:2ee762ea11b3 31 # elif defined(__GNUC__)
tgw 0:2ee762ea11b3 32 # define JSONCPP_NORETURN __attribute__ ((__noreturn__))
tgw 0:2ee762ea11b3 33 # else
tgw 0:2ee762ea11b3 34 # define JSONCPP_NORETURN
tgw 0:2ee762ea11b3 35 # endif
tgw 0:2ee762ea11b3 36 #endif
tgw 0:2ee762ea11b3 37
tgw 0:2ee762ea11b3 38 // Disable warning C4251: <data member>: <type> needs to have dll-interface to
tgw 0:2ee762ea11b3 39 // be used by...
tgw 0:2ee762ea11b3 40 #if defined(JSONCPP_DISABLE_DLL_INTERFACE_WARNING)
tgw 0:2ee762ea11b3 41 #pragma warning(push)
tgw 0:2ee762ea11b3 42 #pragma warning(disable : 4251)
tgw 0:2ee762ea11b3 43 #endif // if defined(JSONCPP_DISABLE_DLL_INTERFACE_WARNING)
tgw 0:2ee762ea11b3 44
tgw 0:2ee762ea11b3 45 #pragma pack(push, 8)
tgw 0:2ee762ea11b3 46
tgw 0:2ee762ea11b3 47 /** \brief JSON (JavaScript Object Notation).
tgw 0:2ee762ea11b3 48 */
tgw 0:2ee762ea11b3 49 namespace Json {
tgw 0:2ee762ea11b3 50
tgw 0:2ee762ea11b3 51 /** Base class for all exceptions we throw.
tgw 0:2ee762ea11b3 52 *
tgw 0:2ee762ea11b3 53 * We use nothing but these internally. Of course, STL can throw others.
tgw 0:2ee762ea11b3 54 */
tgw 0:2ee762ea11b3 55 class JSON_API Exception : public std::exception {
tgw 0:2ee762ea11b3 56 public:
tgw 0:2ee762ea11b3 57 Exception(JSONCPP_STRING const& msg);
tgw 0:2ee762ea11b3 58 ~Exception() JSONCPP_NOEXCEPT JSONCPP_OVERRIDE;
tgw 0:2ee762ea11b3 59 char const* what() const JSONCPP_NOEXCEPT JSONCPP_OVERRIDE;
tgw 0:2ee762ea11b3 60 protected:
tgw 0:2ee762ea11b3 61 JSONCPP_STRING msg_;
tgw 0:2ee762ea11b3 62 };
tgw 0:2ee762ea11b3 63
tgw 0:2ee762ea11b3 64 /** Exceptions which the user cannot easily avoid.
tgw 0:2ee762ea11b3 65 *
tgw 0:2ee762ea11b3 66 * E.g. out-of-memory (when we use malloc), stack-overflow, malicious input
tgw 0:2ee762ea11b3 67 *
tgw 0:2ee762ea11b3 68 * \remark derived from Json::Exception
tgw 0:2ee762ea11b3 69 */
tgw 0:2ee762ea11b3 70 class JSON_API RuntimeError : public Exception {
tgw 0:2ee762ea11b3 71 public:
tgw 0:2ee762ea11b3 72 RuntimeError(JSONCPP_STRING const& msg);
tgw 0:2ee762ea11b3 73 };
tgw 0:2ee762ea11b3 74
tgw 0:2ee762ea11b3 75 /** Exceptions thrown by JSON_ASSERT/JSON_FAIL macros.
tgw 0:2ee762ea11b3 76 *
tgw 0:2ee762ea11b3 77 * These are precondition-violations (user bugs) and internal errors (our bugs).
tgw 0:2ee762ea11b3 78 *
tgw 0:2ee762ea11b3 79 * \remark derived from Json::Exception
tgw 0:2ee762ea11b3 80 */
tgw 0:2ee762ea11b3 81 class JSON_API LogicError : public Exception {
tgw 0:2ee762ea11b3 82 public:
tgw 0:2ee762ea11b3 83 LogicError(JSONCPP_STRING const& msg);
tgw 0:2ee762ea11b3 84 };
tgw 0:2ee762ea11b3 85
tgw 0:2ee762ea11b3 86 /// used internally
tgw 0:2ee762ea11b3 87 JSONCPP_NORETURN void throwRuntimeError(JSONCPP_STRING const& msg);
tgw 0:2ee762ea11b3 88 /// used internally
tgw 0:2ee762ea11b3 89 JSONCPP_NORETURN void throwLogicError(JSONCPP_STRING const& msg);
tgw 0:2ee762ea11b3 90
tgw 0:2ee762ea11b3 91 /** \brief Type of the value held by a Value object.
tgw 0:2ee762ea11b3 92 */
tgw 0:2ee762ea11b3 93 enum ValueType {
tgw 0:2ee762ea11b3 94 nullValue = 0, ///< 'null' value
tgw 0:2ee762ea11b3 95 intValue, ///< signed integer value
tgw 0:2ee762ea11b3 96 uintValue, ///< unsigned integer value
tgw 0:2ee762ea11b3 97 realValue, ///< double value
tgw 0:2ee762ea11b3 98 stringValue, ///< UTF-8 string value
tgw 0:2ee762ea11b3 99 booleanValue, ///< bool value
tgw 0:2ee762ea11b3 100 arrayValue, ///< array value (ordered list)
tgw 0:2ee762ea11b3 101 objectValue ///< object value (collection of name/value pairs).
tgw 0:2ee762ea11b3 102 };
tgw 0:2ee762ea11b3 103
tgw 0:2ee762ea11b3 104 enum CommentPlacement {
tgw 0:2ee762ea11b3 105 commentBefore = 0, ///< a comment placed on the line before a value
tgw 0:2ee762ea11b3 106 commentAfterOnSameLine, ///< a comment just after a value on the same line
tgw 0:2ee762ea11b3 107 commentAfter, ///< a comment on the line after a value (only make sense for
tgw 0:2ee762ea11b3 108 /// root value)
tgw 0:2ee762ea11b3 109 numberOfCommentPlacement
tgw 0:2ee762ea11b3 110 };
tgw 0:2ee762ea11b3 111
tgw 0:2ee762ea11b3 112 //# ifdef JSON_USE_CPPTL
tgw 0:2ee762ea11b3 113 // typedef CppTL::AnyEnumerator<const char *> EnumMemberNames;
tgw 0:2ee762ea11b3 114 // typedef CppTL::AnyEnumerator<const Value &> EnumValues;
tgw 0:2ee762ea11b3 115 //# endif
tgw 0:2ee762ea11b3 116
tgw 0:2ee762ea11b3 117 /** \brief Lightweight wrapper to tag static string.
tgw 0:2ee762ea11b3 118 *
tgw 0:2ee762ea11b3 119 * Value constructor and objectValue member assignment takes advantage of the
tgw 0:2ee762ea11b3 120 * StaticString and avoid the cost of string duplication when storing the
tgw 0:2ee762ea11b3 121 * string or the member name.
tgw 0:2ee762ea11b3 122 *
tgw 0:2ee762ea11b3 123 * Example of usage:
tgw 0:2ee762ea11b3 124 * \code
tgw 0:2ee762ea11b3 125 * Json::Value aValue( StaticString("some text") );
tgw 0:2ee762ea11b3 126 * Json::Value object;
tgw 0:2ee762ea11b3 127 * static const StaticString code("code");
tgw 0:2ee762ea11b3 128 * object[code] = 1234;
tgw 0:2ee762ea11b3 129 * \endcode
tgw 0:2ee762ea11b3 130 */
tgw 0:2ee762ea11b3 131 class JSON_API StaticString {
tgw 0:2ee762ea11b3 132 public:
tgw 0:2ee762ea11b3 133 explicit StaticString(const char* czstring) : c_str_(czstring) {}
tgw 0:2ee762ea11b3 134
tgw 0:2ee762ea11b3 135 operator const char*() const { return c_str_; }
tgw 0:2ee762ea11b3 136
tgw 0:2ee762ea11b3 137 const char* c_str() const { return c_str_; }
tgw 0:2ee762ea11b3 138
tgw 0:2ee762ea11b3 139 private:
tgw 0:2ee762ea11b3 140 const char* c_str_;
tgw 0:2ee762ea11b3 141 };
tgw 0:2ee762ea11b3 142
tgw 0:2ee762ea11b3 143 /** \brief Represents a <a HREF="http://www.json.org">JSON</a> value.
tgw 0:2ee762ea11b3 144 *
tgw 0:2ee762ea11b3 145 * This class is a discriminated union wrapper that can represents a:
tgw 0:2ee762ea11b3 146 * - signed integer [range: Value::minInt - Value::maxInt]
tgw 0:2ee762ea11b3 147 * - unsigned integer (range: 0 - Value::maxUInt)
tgw 0:2ee762ea11b3 148 * - double
tgw 0:2ee762ea11b3 149 * - UTF-8 string
tgw 0:2ee762ea11b3 150 * - boolean
tgw 0:2ee762ea11b3 151 * - 'null'
tgw 0:2ee762ea11b3 152 * - an ordered list of Value
tgw 0:2ee762ea11b3 153 * - collection of name/value pairs (javascript object)
tgw 0:2ee762ea11b3 154 *
tgw 0:2ee762ea11b3 155 * The type of the held value is represented by a #ValueType and
tgw 0:2ee762ea11b3 156 * can be obtained using type().
tgw 0:2ee762ea11b3 157 *
tgw 0:2ee762ea11b3 158 * Values of an #objectValue or #arrayValue can be accessed using operator[]()
tgw 0:2ee762ea11b3 159 * methods.
tgw 0:2ee762ea11b3 160 * Non-const methods will automatically create the a #nullValue element
tgw 0:2ee762ea11b3 161 * if it does not exist.
tgw 0:2ee762ea11b3 162 * The sequence of an #arrayValue will be automatically resized and initialized
tgw 0:2ee762ea11b3 163 * with #nullValue. resize() can be used to enlarge or truncate an #arrayValue.
tgw 0:2ee762ea11b3 164 *
tgw 0:2ee762ea11b3 165 * The get() methods can be used to obtain default value in the case the
tgw 0:2ee762ea11b3 166 * required element does not exist.
tgw 0:2ee762ea11b3 167 *
tgw 0:2ee762ea11b3 168 * It is possible to iterate over the list of a #objectValue values using
tgw 0:2ee762ea11b3 169 * the getMemberNames() method.
tgw 0:2ee762ea11b3 170 *
tgw 0:2ee762ea11b3 171 * \note #Value string-length fit in size_t, but keys must be < 2^30.
tgw 0:2ee762ea11b3 172 * (The reason is an implementation detail.) A #CharReader will raise an
tgw 0:2ee762ea11b3 173 * exception if a bound is exceeded to avoid security holes in your app,
tgw 0:2ee762ea11b3 174 * but the Value API does *not* check bounds. That is the responsibility
tgw 0:2ee762ea11b3 175 * of the caller.
tgw 0:2ee762ea11b3 176 */
tgw 0:2ee762ea11b3 177 class JSON_API Value {
tgw 0:2ee762ea11b3 178 friend class ValueIteratorBase;
tgw 0:2ee762ea11b3 179 public:
tgw 0:2ee762ea11b3 180 typedef std::vector<JSONCPP_STRING> Members;
tgw 0:2ee762ea11b3 181 typedef ValueIterator iterator;
tgw 0:2ee762ea11b3 182 typedef ValueConstIterator const_iterator;
tgw 0:2ee762ea11b3 183 typedef Json::UInt UInt;
tgw 0:2ee762ea11b3 184 typedef Json::Int Int;
tgw 0:2ee762ea11b3 185 #if defined(JSON_HAS_INT64)
tgw 0:2ee762ea11b3 186 typedef Json::UInt64 UInt64;
tgw 0:2ee762ea11b3 187 typedef Json::Int64 Int64;
tgw 0:2ee762ea11b3 188 #endif // defined(JSON_HAS_INT64)
tgw 0:2ee762ea11b3 189 typedef Json::LargestInt LargestInt;
tgw 0:2ee762ea11b3 190 typedef Json::LargestUInt LargestUInt;
tgw 0:2ee762ea11b3 191 typedef Json::ArrayIndex ArrayIndex;
tgw 0:2ee762ea11b3 192
tgw 0:2ee762ea11b3 193 // Required for boost integration, e. g. BOOST_TEST
tgw 0:2ee762ea11b3 194 typedef std::string value_type;
tgw 0:2ee762ea11b3 195
tgw 0:2ee762ea11b3 196 static const Value& null; ///< We regret this reference to a global instance; prefer the simpler Value().
tgw 0:2ee762ea11b3 197 static const Value& nullRef; ///< just a kludge for binary-compatibility; same as null
tgw 0:2ee762ea11b3 198 static Value const& nullSingleton(); ///< Prefer this to null or nullRef.
tgw 0:2ee762ea11b3 199
tgw 0:2ee762ea11b3 200 /// Minimum signed integer value that can be stored in a Json::Value.
tgw 0:2ee762ea11b3 201 static const LargestInt minLargestInt;
tgw 0:2ee762ea11b3 202 /// Maximum signed integer value that can be stored in a Json::Value.
tgw 0:2ee762ea11b3 203 static const LargestInt maxLargestInt;
tgw 0:2ee762ea11b3 204 /// Maximum unsigned integer value that can be stored in a Json::Value.
tgw 0:2ee762ea11b3 205 static const LargestUInt maxLargestUInt;
tgw 0:2ee762ea11b3 206
tgw 0:2ee762ea11b3 207 /// Minimum signed int value that can be stored in a Json::Value.
tgw 0:2ee762ea11b3 208 static const Int minInt;
tgw 0:2ee762ea11b3 209 /// Maximum signed int value that can be stored in a Json::Value.
tgw 0:2ee762ea11b3 210 static const Int maxInt;
tgw 0:2ee762ea11b3 211 /// Maximum unsigned int value that can be stored in a Json::Value.
tgw 0:2ee762ea11b3 212 static const UInt maxUInt;
tgw 0:2ee762ea11b3 213
tgw 0:2ee762ea11b3 214 #if defined(JSON_HAS_INT64)
tgw 0:2ee762ea11b3 215 /// Minimum signed 64 bits int value that can be stored in a Json::Value.
tgw 0:2ee762ea11b3 216 static const Int64 minInt64;
tgw 0:2ee762ea11b3 217 /// Maximum signed 64 bits int value that can be stored in a Json::Value.
tgw 0:2ee762ea11b3 218 static const Int64 maxInt64;
tgw 0:2ee762ea11b3 219 /// Maximum unsigned 64 bits int value that can be stored in a Json::Value.
tgw 0:2ee762ea11b3 220 static const UInt64 maxUInt64;
tgw 0:2ee762ea11b3 221 #endif // defined(JSON_HAS_INT64)
tgw 0:2ee762ea11b3 222
tgw 0:2ee762ea11b3 223 private:
tgw 0:2ee762ea11b3 224 #ifndef JSONCPP_DOC_EXCLUDE_IMPLEMENTATION
tgw 0:2ee762ea11b3 225 class CZString {
tgw 0:2ee762ea11b3 226 public:
tgw 0:2ee762ea11b3 227 enum DuplicationPolicy {
tgw 0:2ee762ea11b3 228 noDuplication = 0,
tgw 0:2ee762ea11b3 229 duplicate,
tgw 0:2ee762ea11b3 230 duplicateOnCopy
tgw 0:2ee762ea11b3 231 };
tgw 0:2ee762ea11b3 232 CZString(ArrayIndex index);
tgw 0:2ee762ea11b3 233 CZString(char const* str, unsigned length, DuplicationPolicy allocate);
tgw 0:2ee762ea11b3 234 CZString(CZString const& other);
tgw 0:2ee762ea11b3 235 #if JSON_HAS_RVALUE_REFERENCES
tgw 0:2ee762ea11b3 236 CZString(CZString&& other);
tgw 0:2ee762ea11b3 237 #endif
tgw 0:2ee762ea11b3 238 ~CZString();
tgw 0:2ee762ea11b3 239 CZString& operator=(const CZString& other);
tgw 0:2ee762ea11b3 240
tgw 0:2ee762ea11b3 241 #if JSON_HAS_RVALUE_REFERENCES
tgw 0:2ee762ea11b3 242 CZString& operator=(CZString&& other);
tgw 0:2ee762ea11b3 243 #endif
tgw 0:2ee762ea11b3 244
tgw 0:2ee762ea11b3 245 bool operator<(CZString const& other) const;
tgw 0:2ee762ea11b3 246 bool operator==(CZString const& other) const;
tgw 0:2ee762ea11b3 247 ArrayIndex index() const;
tgw 0:2ee762ea11b3 248 //const char* c_str() const; ///< \deprecated
tgw 0:2ee762ea11b3 249 char const* data() const;
tgw 0:2ee762ea11b3 250 unsigned length() const;
tgw 0:2ee762ea11b3 251 bool isStaticString() const;
tgw 0:2ee762ea11b3 252
tgw 0:2ee762ea11b3 253 private:
tgw 0:2ee762ea11b3 254 void swap(CZString& other);
tgw 0:2ee762ea11b3 255
tgw 0:2ee762ea11b3 256 struct StringStorage {
tgw 0:2ee762ea11b3 257 unsigned policy_: 2;
tgw 0:2ee762ea11b3 258 unsigned length_: 30; // 1GB max
tgw 0:2ee762ea11b3 259 };
tgw 0:2ee762ea11b3 260
tgw 0:2ee762ea11b3 261 char const* cstr_; // actually, a prefixed string, unless policy is noDup
tgw 0:2ee762ea11b3 262 union {
tgw 0:2ee762ea11b3 263 ArrayIndex index_;
tgw 0:2ee762ea11b3 264 StringStorage storage_;
tgw 0:2ee762ea11b3 265 };
tgw 0:2ee762ea11b3 266 };
tgw 0:2ee762ea11b3 267
tgw 0:2ee762ea11b3 268 public:
tgw 0:2ee762ea11b3 269 #ifndef JSON_USE_CPPTL_SMALLMAP
tgw 0:2ee762ea11b3 270 typedef std::map<CZString, Value> ObjectValues;
tgw 0:2ee762ea11b3 271 #else
tgw 0:2ee762ea11b3 272 typedef CppTL::SmallMap<CZString, Value> ObjectValues;
tgw 0:2ee762ea11b3 273 #endif // ifndef JSON_USE_CPPTL_SMALLMAP
tgw 0:2ee762ea11b3 274 #endif // ifndef JSONCPP_DOC_EXCLUDE_IMPLEMENTATION
tgw 0:2ee762ea11b3 275
tgw 0:2ee762ea11b3 276 public:
tgw 0:2ee762ea11b3 277 /** \brief Create a default Value of the given type.
tgw 0:2ee762ea11b3 278
tgw 0:2ee762ea11b3 279 This is a very useful constructor.
tgw 0:2ee762ea11b3 280 To create an empty array, pass arrayValue.
tgw 0:2ee762ea11b3 281 To create an empty object, pass objectValue.
tgw 0:2ee762ea11b3 282 Another Value can then be set to this one by assignment.
tgw 0:2ee762ea11b3 283 This is useful since clear() and resize() will not alter types.
tgw 0:2ee762ea11b3 284
tgw 0:2ee762ea11b3 285 Examples:
tgw 0:2ee762ea11b3 286 \code
tgw 0:2ee762ea11b3 287 Json::Value null_value; // null
tgw 0:2ee762ea11b3 288 Json::Value arr_value(Json::arrayValue); // []
tgw 0:2ee762ea11b3 289 Json::Value obj_value(Json::objectValue); // {}
tgw 0:2ee762ea11b3 290 \endcode
tgw 0:2ee762ea11b3 291 */
tgw 0:2ee762ea11b3 292 Value(ValueType type = nullValue);
tgw 0:2ee762ea11b3 293 Value(Int value);
tgw 0:2ee762ea11b3 294 Value(UInt value);
tgw 0:2ee762ea11b3 295 #if defined(JSON_HAS_INT64)
tgw 0:2ee762ea11b3 296 Value(Int64 value);
tgw 0:2ee762ea11b3 297 Value(UInt64 value);
tgw 0:2ee762ea11b3 298 #endif // if defined(JSON_HAS_INT64)
tgw 0:2ee762ea11b3 299 Value(double value);
tgw 0:2ee762ea11b3 300 Value(const char* value); ///< Copy til first 0. (NULL causes to seg-fault.)
tgw 0:2ee762ea11b3 301 Value(const char* begin, const char* end); ///< Copy all, incl zeroes.
tgw 0:2ee762ea11b3 302 /** \brief Constructs a value from a static string.
tgw 0:2ee762ea11b3 303
tgw 0:2ee762ea11b3 304 * Like other value string constructor but do not duplicate the string for
tgw 0:2ee762ea11b3 305 * internal storage. The given string must remain alive after the call to this
tgw 0:2ee762ea11b3 306 * constructor.
tgw 0:2ee762ea11b3 307 * \note This works only for null-terminated strings. (We cannot change the
tgw 0:2ee762ea11b3 308 * size of this class, so we have nowhere to store the length,
tgw 0:2ee762ea11b3 309 * which might be computed later for various operations.)
tgw 0:2ee762ea11b3 310 *
tgw 0:2ee762ea11b3 311 * Example of usage:
tgw 0:2ee762ea11b3 312 * \code
tgw 0:2ee762ea11b3 313 * static StaticString foo("some text");
tgw 0:2ee762ea11b3 314 * Json::Value aValue(foo);
tgw 0:2ee762ea11b3 315 * \endcode
tgw 0:2ee762ea11b3 316 */
tgw 0:2ee762ea11b3 317 Value(const StaticString& value);
tgw 0:2ee762ea11b3 318 Value(const JSONCPP_STRING& value); ///< Copy data() til size(). Embedded zeroes too.
tgw 0:2ee762ea11b3 319 #ifdef JSON_USE_CPPTL
tgw 0:2ee762ea11b3 320 Value(const CppTL::ConstString& value);
tgw 0:2ee762ea11b3 321 #endif
tgw 0:2ee762ea11b3 322 Value(bool value);
tgw 0:2ee762ea11b3 323 /// Deep copy.
tgw 0:2ee762ea11b3 324 Value(const Value& other);
tgw 0:2ee762ea11b3 325 #if JSON_HAS_RVALUE_REFERENCES
tgw 0:2ee762ea11b3 326 /// Move constructor
tgw 0:2ee762ea11b3 327 Value(Value&& other);
tgw 0:2ee762ea11b3 328 #endif
tgw 0:2ee762ea11b3 329 ~Value();
tgw 0:2ee762ea11b3 330
tgw 0:2ee762ea11b3 331 /// Deep copy, then swap(other).
tgw 0:2ee762ea11b3 332 /// \note Over-write existing comments. To preserve comments, use #swapPayload().
tgw 0:2ee762ea11b3 333 Value& operator=(Value other);
tgw 0:2ee762ea11b3 334
tgw 0:2ee762ea11b3 335 /// Swap everything.
tgw 0:2ee762ea11b3 336 void swap(Value& other);
tgw 0:2ee762ea11b3 337 /// Swap values but leave comments and source offsets in place.
tgw 0:2ee762ea11b3 338 void swapPayload(Value& other);
tgw 0:2ee762ea11b3 339
tgw 0:2ee762ea11b3 340 /// copy everything.
tgw 0:2ee762ea11b3 341 void copy(const Value& other);
tgw 0:2ee762ea11b3 342 /// copy values but leave comments and source offsets in place.
tgw 0:2ee762ea11b3 343 void copyPayload(const Value& other);
tgw 0:2ee762ea11b3 344
tgw 0:2ee762ea11b3 345 ValueType type() const;
tgw 0:2ee762ea11b3 346
tgw 0:2ee762ea11b3 347 /// Compare payload only, not comments etc.
tgw 0:2ee762ea11b3 348 bool operator<(const Value& other) const;
tgw 0:2ee762ea11b3 349 bool operator<=(const Value& other) const;
tgw 0:2ee762ea11b3 350 bool operator>=(const Value& other) const;
tgw 0:2ee762ea11b3 351 bool operator>(const Value& other) const;
tgw 0:2ee762ea11b3 352 bool operator==(const Value& other) const;
tgw 0:2ee762ea11b3 353 bool operator!=(const Value& other) const;
tgw 0:2ee762ea11b3 354 int compare(const Value& other) const;
tgw 0:2ee762ea11b3 355
tgw 0:2ee762ea11b3 356 const char* asCString() const; ///< Embedded zeroes could cause you trouble!
tgw 0:2ee762ea11b3 357 #if JSONCPP_USING_SECURE_MEMORY
tgw 0:2ee762ea11b3 358 unsigned getCStringLength() const; //Allows you to understand the length of the CString
tgw 0:2ee762ea11b3 359 #endif
tgw 0:2ee762ea11b3 360 JSONCPP_STRING asString() const; ///< Embedded zeroes are possible.
tgw 0:2ee762ea11b3 361 /** Get raw char* of string-value.
tgw 0:2ee762ea11b3 362 * \return false if !string. (Seg-fault if str or end are NULL.)
tgw 0:2ee762ea11b3 363 */
tgw 0:2ee762ea11b3 364 bool getString(
tgw 0:2ee762ea11b3 365 char const** begin, char const** end) const;
tgw 0:2ee762ea11b3 366 #ifdef JSON_USE_CPPTL
tgw 0:2ee762ea11b3 367 CppTL::ConstString asConstString() const;
tgw 0:2ee762ea11b3 368 #endif
tgw 0:2ee762ea11b3 369 Int asInt() const;
tgw 0:2ee762ea11b3 370 UInt asUInt() const;
tgw 0:2ee762ea11b3 371 #if defined(JSON_HAS_INT64)
tgw 0:2ee762ea11b3 372 Int64 asInt64() const;
tgw 0:2ee762ea11b3 373 UInt64 asUInt64() const;
tgw 0:2ee762ea11b3 374 #endif // if defined(JSON_HAS_INT64)
tgw 0:2ee762ea11b3 375 LargestInt asLargestInt() const;
tgw 0:2ee762ea11b3 376 LargestUInt asLargestUInt() const;
tgw 0:2ee762ea11b3 377 float asFloat() const;
tgw 0:2ee762ea11b3 378 double asDouble() const;
tgw 0:2ee762ea11b3 379 bool asBool() const;
tgw 0:2ee762ea11b3 380
tgw 0:2ee762ea11b3 381 bool isNull() const;
tgw 0:2ee762ea11b3 382 bool isBool() const;
tgw 0:2ee762ea11b3 383 bool isInt() const;
tgw 0:2ee762ea11b3 384 bool isInt64() const;
tgw 0:2ee762ea11b3 385 bool isUInt() const;
tgw 0:2ee762ea11b3 386 bool isUInt64() const;
tgw 0:2ee762ea11b3 387 bool isIntegral() const;
tgw 0:2ee762ea11b3 388 bool isDouble() const;
tgw 0:2ee762ea11b3 389 bool isNumeric() const;
tgw 0:2ee762ea11b3 390 bool isString() const;
tgw 0:2ee762ea11b3 391 bool isArray() const;
tgw 0:2ee762ea11b3 392 bool isObject() const;
tgw 0:2ee762ea11b3 393
tgw 0:2ee762ea11b3 394 bool isConvertibleTo(ValueType other) const;
tgw 0:2ee762ea11b3 395
tgw 0:2ee762ea11b3 396 /// Number of values in array or object
tgw 0:2ee762ea11b3 397 ArrayIndex size() const;
tgw 0:2ee762ea11b3 398
tgw 0:2ee762ea11b3 399 /// \brief Return true if empty array, empty object, or null;
tgw 0:2ee762ea11b3 400 /// otherwise, false.
tgw 0:2ee762ea11b3 401 bool empty() const;
tgw 0:2ee762ea11b3 402
tgw 0:2ee762ea11b3 403 /// Return !isNull()
tgw 0:2ee762ea11b3 404 explicit operator bool() const;
tgw 0:2ee762ea11b3 405
tgw 0:2ee762ea11b3 406 /// Remove all object members and array elements.
tgw 0:2ee762ea11b3 407 /// \pre type() is arrayValue, objectValue, or nullValue
tgw 0:2ee762ea11b3 408 /// \post type() is unchanged
tgw 0:2ee762ea11b3 409 void clear();
tgw 0:2ee762ea11b3 410
tgw 0:2ee762ea11b3 411 /// Resize the array to size elements.
tgw 0:2ee762ea11b3 412 /// New elements are initialized to null.
tgw 0:2ee762ea11b3 413 /// May only be called on nullValue or arrayValue.
tgw 0:2ee762ea11b3 414 /// \pre type() is arrayValue or nullValue
tgw 0:2ee762ea11b3 415 /// \post type() is arrayValue
tgw 0:2ee762ea11b3 416 void resize(ArrayIndex size);
tgw 0:2ee762ea11b3 417
tgw 0:2ee762ea11b3 418 /// Access an array element (zero based index ).
tgw 0:2ee762ea11b3 419 /// If the array contains less than index element, then null value are
tgw 0:2ee762ea11b3 420 /// inserted
tgw 0:2ee762ea11b3 421 /// in the array so that its size is index+1.
tgw 0:2ee762ea11b3 422 /// (You may need to say 'value[0u]' to get your compiler to distinguish
tgw 0:2ee762ea11b3 423 /// this from the operator[] which takes a string.)
tgw 0:2ee762ea11b3 424 Value& operator[](ArrayIndex index);
tgw 0:2ee762ea11b3 425
tgw 0:2ee762ea11b3 426 /// Access an array element (zero based index ).
tgw 0:2ee762ea11b3 427 /// If the array contains less than index element, then null value are
tgw 0:2ee762ea11b3 428 /// inserted
tgw 0:2ee762ea11b3 429 /// in the array so that its size is index+1.
tgw 0:2ee762ea11b3 430 /// (You may need to say 'value[0u]' to get your compiler to distinguish
tgw 0:2ee762ea11b3 431 /// this from the operator[] which takes a string.)
tgw 0:2ee762ea11b3 432 Value& operator[](int index);
tgw 0:2ee762ea11b3 433
tgw 0:2ee762ea11b3 434 /// Access an array element (zero based index )
tgw 0:2ee762ea11b3 435 /// (You may need to say 'value[0u]' to get your compiler to distinguish
tgw 0:2ee762ea11b3 436 /// this from the operator[] which takes a string.)
tgw 0:2ee762ea11b3 437 const Value& operator[](ArrayIndex index) const;
tgw 0:2ee762ea11b3 438
tgw 0:2ee762ea11b3 439 /// Access an array element (zero based index )
tgw 0:2ee762ea11b3 440 /// (You may need to say 'value[0u]' to get your compiler to distinguish
tgw 0:2ee762ea11b3 441 /// this from the operator[] which takes a string.)
tgw 0:2ee762ea11b3 442 const Value& operator[](int index) const;
tgw 0:2ee762ea11b3 443
tgw 0:2ee762ea11b3 444 /// If the array contains at least index+1 elements, returns the element
tgw 0:2ee762ea11b3 445 /// value,
tgw 0:2ee762ea11b3 446 /// otherwise returns defaultValue.
tgw 0:2ee762ea11b3 447 Value get(ArrayIndex index, const Value& defaultValue) const;
tgw 0:2ee762ea11b3 448 /// Return true if index < size().
tgw 0:2ee762ea11b3 449 bool isValidIndex(ArrayIndex index) const;
tgw 0:2ee762ea11b3 450 /// \brief Append value to array at the end.
tgw 0:2ee762ea11b3 451 ///
tgw 0:2ee762ea11b3 452 /// Equivalent to jsonvalue[jsonvalue.size()] = value;
tgw 0:2ee762ea11b3 453 Value& append(const Value& value);
tgw 0:2ee762ea11b3 454
tgw 0:2ee762ea11b3 455 #if JSON_HAS_RVALUE_REFERENCES
tgw 0:2ee762ea11b3 456 Value& append(Value&& value);
tgw 0:2ee762ea11b3 457 #endif
tgw 0:2ee762ea11b3 458
tgw 0:2ee762ea11b3 459 /// Access an object value by name, create a null member if it does not exist.
tgw 0:2ee762ea11b3 460 /// \note Because of our implementation, keys are limited to 2^30 -1 chars.
tgw 0:2ee762ea11b3 461 /// Exceeding that will cause an exception.
tgw 0:2ee762ea11b3 462 Value& operator[](const char* key);
tgw 0:2ee762ea11b3 463 /// Access an object value by name, returns null if there is no member with
tgw 0:2ee762ea11b3 464 /// that name.
tgw 0:2ee762ea11b3 465 const Value& operator[](const char* key) const;
tgw 0:2ee762ea11b3 466 /// Access an object value by name, create a null member if it does not exist.
tgw 0:2ee762ea11b3 467 /// \param key may contain embedded nulls.
tgw 0:2ee762ea11b3 468 Value& operator[](const JSONCPP_STRING& key);
tgw 0:2ee762ea11b3 469 /// Access an object value by name, returns null if there is no member with
tgw 0:2ee762ea11b3 470 /// that name.
tgw 0:2ee762ea11b3 471 /// \param key may contain embedded nulls.
tgw 0:2ee762ea11b3 472 const Value& operator[](const JSONCPP_STRING& key) const;
tgw 0:2ee762ea11b3 473 /** \brief Access an object value by name, create a null member if it does not
tgw 0:2ee762ea11b3 474 exist.
tgw 0:2ee762ea11b3 475
tgw 0:2ee762ea11b3 476 * If the object has no entry for that name, then the member name used to store
tgw 0:2ee762ea11b3 477 * the new entry is not duplicated.
tgw 0:2ee762ea11b3 478 * Example of use:
tgw 0:2ee762ea11b3 479 * \code
tgw 0:2ee762ea11b3 480 * Json::Value object;
tgw 0:2ee762ea11b3 481 * static const StaticString code("code");
tgw 0:2ee762ea11b3 482 * object[code] = 1234;
tgw 0:2ee762ea11b3 483 * \endcode
tgw 0:2ee762ea11b3 484 */
tgw 0:2ee762ea11b3 485 Value& operator[](const StaticString& key);
tgw 0:2ee762ea11b3 486 #ifdef JSON_USE_CPPTL
tgw 0:2ee762ea11b3 487 /// Access an object value by name, create a null member if it does not exist.
tgw 0:2ee762ea11b3 488 Value& operator[](const CppTL::ConstString& key);
tgw 0:2ee762ea11b3 489 /// Access an object value by name, returns null if there is no member with
tgw 0:2ee762ea11b3 490 /// that name.
tgw 0:2ee762ea11b3 491 const Value& operator[](const CppTL::ConstString& key) const;
tgw 0:2ee762ea11b3 492 #endif
tgw 0:2ee762ea11b3 493 /// Return the member named key if it exist, defaultValue otherwise.
tgw 0:2ee762ea11b3 494 /// \note deep copy
tgw 0:2ee762ea11b3 495 Value get(const char* key, const Value& defaultValue) const;
tgw 0:2ee762ea11b3 496 /// Return the member named key if it exist, defaultValue otherwise.
tgw 0:2ee762ea11b3 497 /// \note deep copy
tgw 0:2ee762ea11b3 498 /// \note key may contain embedded nulls.
tgw 0:2ee762ea11b3 499 Value get(const char* begin, const char* end, const Value& defaultValue) const;
tgw 0:2ee762ea11b3 500 /// Return the member named key if it exist, defaultValue otherwise.
tgw 0:2ee762ea11b3 501 /// \note deep copy
tgw 0:2ee762ea11b3 502 /// \param key may contain embedded nulls.
tgw 0:2ee762ea11b3 503 Value get(const JSONCPP_STRING& key, const Value& defaultValue) const;
tgw 0:2ee762ea11b3 504 #ifdef JSON_USE_CPPTL
tgw 0:2ee762ea11b3 505 /// Return the member named key if it exist, defaultValue otherwise.
tgw 0:2ee762ea11b3 506 /// \note deep copy
tgw 0:2ee762ea11b3 507 Value get(const CppTL::ConstString& key, const Value& defaultValue) const;
tgw 0:2ee762ea11b3 508 #endif
tgw 0:2ee762ea11b3 509 /// Most general and efficient version of isMember()const, get()const,
tgw 0:2ee762ea11b3 510 /// and operator[]const
tgw 0:2ee762ea11b3 511 /// \note As stated elsewhere, behavior is undefined if (end-begin) >= 2^30
tgw 0:2ee762ea11b3 512 Value const* find(char const* begin, char const* end) const;
tgw 0:2ee762ea11b3 513 /// Most general and efficient version of object-mutators.
tgw 0:2ee762ea11b3 514 /// \note As stated elsewhere, behavior is undefined if (end-begin) >= 2^30
tgw 0:2ee762ea11b3 515 /// \return non-zero, but JSON_ASSERT if this is neither object nor nullValue.
tgw 0:2ee762ea11b3 516 Value const* demand(char const* begin, char const* end);
tgw 0:2ee762ea11b3 517 /// \brief Remove and return the named member.
tgw 0:2ee762ea11b3 518 ///
tgw 0:2ee762ea11b3 519 /// Do nothing if it did not exist.
tgw 0:2ee762ea11b3 520 /// \return the removed Value, or null.
tgw 0:2ee762ea11b3 521 /// \pre type() is objectValue or nullValue
tgw 0:2ee762ea11b3 522 /// \post type() is unchanged
tgw 0:2ee762ea11b3 523 /// \deprecated
tgw 0:2ee762ea11b3 524 void removeMember(const char* key);
tgw 0:2ee762ea11b3 525 /// Same as removeMember(const char*)
tgw 0:2ee762ea11b3 526 /// \param key may contain embedded nulls.
tgw 0:2ee762ea11b3 527 /// \deprecated
tgw 0:2ee762ea11b3 528 void removeMember(const JSONCPP_STRING& key);
tgw 0:2ee762ea11b3 529 /// Same as removeMember(const char* begin, const char* end, Value* removed),
tgw 0:2ee762ea11b3 530 /// but 'key' is null-terminated.
tgw 0:2ee762ea11b3 531 bool removeMember(const char* key, Value* removed);
tgw 0:2ee762ea11b3 532 /** \brief Remove the named map member.
tgw 0:2ee762ea11b3 533
tgw 0:2ee762ea11b3 534 Update 'removed' iff removed.
tgw 0:2ee762ea11b3 535 \param key may contain embedded nulls.
tgw 0:2ee762ea11b3 536 \return true iff removed (no exceptions)
tgw 0:2ee762ea11b3 537 */
tgw 0:2ee762ea11b3 538 bool removeMember(JSONCPP_STRING const& key, Value* removed);
tgw 0:2ee762ea11b3 539 /// Same as removeMember(JSONCPP_STRING const& key, Value* removed)
tgw 0:2ee762ea11b3 540 bool removeMember(const char* begin, const char* end, Value* removed);
tgw 0:2ee762ea11b3 541 /** \brief Remove the indexed array element.
tgw 0:2ee762ea11b3 542
tgw 0:2ee762ea11b3 543 O(n) expensive operations.
tgw 0:2ee762ea11b3 544 Update 'removed' iff removed.
tgw 0:2ee762ea11b3 545 \return true iff removed (no exceptions)
tgw 0:2ee762ea11b3 546 */
tgw 0:2ee762ea11b3 547 bool removeIndex(ArrayIndex i, Value* removed);
tgw 0:2ee762ea11b3 548
tgw 0:2ee762ea11b3 549 /// Return true if the object has a member named key.
tgw 0:2ee762ea11b3 550 /// \note 'key' must be null-terminated.
tgw 0:2ee762ea11b3 551 bool isMember(const char* key) const;
tgw 0:2ee762ea11b3 552 /// Return true if the object has a member named key.
tgw 0:2ee762ea11b3 553 /// \param key may contain embedded nulls.
tgw 0:2ee762ea11b3 554 bool isMember(const JSONCPP_STRING& key) const;
tgw 0:2ee762ea11b3 555 /// Same as isMember(JSONCPP_STRING const& key)const
tgw 0:2ee762ea11b3 556 bool isMember(const char* begin, const char* end) const;
tgw 0:2ee762ea11b3 557 #ifdef JSON_USE_CPPTL
tgw 0:2ee762ea11b3 558 /// Return true if the object has a member named key.
tgw 0:2ee762ea11b3 559 bool isMember(const CppTL::ConstString& key) const;
tgw 0:2ee762ea11b3 560 #endif
tgw 0:2ee762ea11b3 561
tgw 0:2ee762ea11b3 562 /// \brief Return a list of the member names.
tgw 0:2ee762ea11b3 563 ///
tgw 0:2ee762ea11b3 564 /// If null, return an empty list.
tgw 0:2ee762ea11b3 565 /// \pre type() is objectValue or nullValue
tgw 0:2ee762ea11b3 566 /// \post if type() was nullValue, it remains nullValue
tgw 0:2ee762ea11b3 567 Members getMemberNames() const;
tgw 0:2ee762ea11b3 568
tgw 0:2ee762ea11b3 569 //# ifdef JSON_USE_CPPTL
tgw 0:2ee762ea11b3 570 // EnumMemberNames enumMemberNames() const;
tgw 0:2ee762ea11b3 571 // EnumValues enumValues() const;
tgw 0:2ee762ea11b3 572 //# endif
tgw 0:2ee762ea11b3 573
tgw 0:2ee762ea11b3 574 /// \deprecated Always pass len.
tgw 0:2ee762ea11b3 575 JSONCPP_DEPRECATED("Use setComment(JSONCPP_STRING const&) instead.")
tgw 0:2ee762ea11b3 576 void setComment(const char* comment, CommentPlacement placement);
tgw 0:2ee762ea11b3 577 /// Comments must be //... or /* ... */
tgw 0:2ee762ea11b3 578 void setComment(const char* comment, size_t len, CommentPlacement placement);
tgw 0:2ee762ea11b3 579 /// Comments must be //... or /* ... */
tgw 0:2ee762ea11b3 580 void setComment(const JSONCPP_STRING& comment, CommentPlacement placement);
tgw 0:2ee762ea11b3 581 bool hasComment(CommentPlacement placement) const;
tgw 0:2ee762ea11b3 582 /// Include delimiters and embedded newlines.
tgw 0:2ee762ea11b3 583 JSONCPP_STRING getComment(CommentPlacement placement) const;
tgw 0:2ee762ea11b3 584
tgw 0:2ee762ea11b3 585 JSONCPP_STRING toStyledString() const;
tgw 0:2ee762ea11b3 586
tgw 0:2ee762ea11b3 587 const_iterator begin() const;
tgw 0:2ee762ea11b3 588 const_iterator end() const;
tgw 0:2ee762ea11b3 589
tgw 0:2ee762ea11b3 590 iterator begin();
tgw 0:2ee762ea11b3 591 iterator end();
tgw 0:2ee762ea11b3 592
tgw 0:2ee762ea11b3 593 // Accessors for the [start, limit) range of bytes within the JSON text from
tgw 0:2ee762ea11b3 594 // which this value was parsed, if any.
tgw 0:2ee762ea11b3 595 void setOffsetStart(ptrdiff_t start);
tgw 0:2ee762ea11b3 596 void setOffsetLimit(ptrdiff_t limit);
tgw 0:2ee762ea11b3 597 ptrdiff_t getOffsetStart() const;
tgw 0:2ee762ea11b3 598 ptrdiff_t getOffsetLimit() const;
tgw 0:2ee762ea11b3 599
tgw 0:2ee762ea11b3 600 private:
tgw 0:2ee762ea11b3 601 void initBasic(ValueType type, bool allocated = false);
tgw 0:2ee762ea11b3 602
tgw 0:2ee762ea11b3 603 Value& resolveReference(const char* key);
tgw 0:2ee762ea11b3 604 Value& resolveReference(const char* key, const char* end);
tgw 0:2ee762ea11b3 605
tgw 0:2ee762ea11b3 606 struct CommentInfo {
tgw 0:2ee762ea11b3 607 CommentInfo();
tgw 0:2ee762ea11b3 608 ~CommentInfo();
tgw 0:2ee762ea11b3 609
tgw 0:2ee762ea11b3 610 void setComment(const char* text, size_t len);
tgw 0:2ee762ea11b3 611
tgw 0:2ee762ea11b3 612 char* comment_;
tgw 0:2ee762ea11b3 613 };
tgw 0:2ee762ea11b3 614
tgw 0:2ee762ea11b3 615 // struct MemberNamesTransform
tgw 0:2ee762ea11b3 616 //{
tgw 0:2ee762ea11b3 617 // typedef const char *result_type;
tgw 0:2ee762ea11b3 618 // const char *operator()( const CZString &name ) const
tgw 0:2ee762ea11b3 619 // {
tgw 0:2ee762ea11b3 620 // return name.c_str();
tgw 0:2ee762ea11b3 621 // }
tgw 0:2ee762ea11b3 622 //};
tgw 0:2ee762ea11b3 623
tgw 0:2ee762ea11b3 624 union ValueHolder {
tgw 0:2ee762ea11b3 625 LargestInt int_;
tgw 0:2ee762ea11b3 626 LargestUInt uint_;
tgw 0:2ee762ea11b3 627 double real_;
tgw 0:2ee762ea11b3 628 bool bool_;
tgw 0:2ee762ea11b3 629 char* string_; // actually ptr to unsigned, followed by str, unless !allocated_
tgw 0:2ee762ea11b3 630 ObjectValues* map_;
tgw 0:2ee762ea11b3 631 } value_;
tgw 0:2ee762ea11b3 632 ValueType type_ : 8;
tgw 0:2ee762ea11b3 633 unsigned int allocated_ : 1; // Notes: if declared as bool, bitfield is useless.
tgw 0:2ee762ea11b3 634 // If not allocated_, string_ must be null-terminated.
tgw 0:2ee762ea11b3 635 CommentInfo* comments_;
tgw 0:2ee762ea11b3 636
tgw 0:2ee762ea11b3 637 // [start, limit) byte offsets in the source JSON text from which this Value
tgw 0:2ee762ea11b3 638 // was extracted.
tgw 0:2ee762ea11b3 639 ptrdiff_t start_;
tgw 0:2ee762ea11b3 640 ptrdiff_t limit_;
tgw 0:2ee762ea11b3 641 };
tgw 0:2ee762ea11b3 642
tgw 0:2ee762ea11b3 643 /** \brief Experimental and untested: represents an element of the "path" to
tgw 0:2ee762ea11b3 644 * access a node.
tgw 0:2ee762ea11b3 645 */
tgw 0:2ee762ea11b3 646 class JSON_API PathArgument {
tgw 0:2ee762ea11b3 647 public:
tgw 0:2ee762ea11b3 648 friend class Path;
tgw 0:2ee762ea11b3 649
tgw 0:2ee762ea11b3 650 PathArgument();
tgw 0:2ee762ea11b3 651 PathArgument(ArrayIndex index);
tgw 0:2ee762ea11b3 652 PathArgument(const char* key);
tgw 0:2ee762ea11b3 653 PathArgument(const JSONCPP_STRING& key);
tgw 0:2ee762ea11b3 654
tgw 0:2ee762ea11b3 655 private:
tgw 0:2ee762ea11b3 656 enum Kind {
tgw 0:2ee762ea11b3 657 kindNone = 0,
tgw 0:2ee762ea11b3 658 kindIndex,
tgw 0:2ee762ea11b3 659 kindKey
tgw 0:2ee762ea11b3 660 };
tgw 0:2ee762ea11b3 661 JSONCPP_STRING key_;
tgw 0:2ee762ea11b3 662 ArrayIndex index_;
tgw 0:2ee762ea11b3 663 Kind kind_;
tgw 0:2ee762ea11b3 664 };
tgw 0:2ee762ea11b3 665
tgw 0:2ee762ea11b3 666 /** \brief Experimental and untested: represents a "path" to access a node.
tgw 0:2ee762ea11b3 667 *
tgw 0:2ee762ea11b3 668 * Syntax:
tgw 0:2ee762ea11b3 669 * - "." => root node
tgw 0:2ee762ea11b3 670 * - ".[n]" => elements at index 'n' of root node (an array value)
tgw 0:2ee762ea11b3 671 * - ".name" => member named 'name' of root node (an object value)
tgw 0:2ee762ea11b3 672 * - ".name1.name2.name3"
tgw 0:2ee762ea11b3 673 * - ".[0][1][2].name1[3]"
tgw 0:2ee762ea11b3 674 * - ".%" => member name is provided as parameter
tgw 0:2ee762ea11b3 675 * - ".[%]" => index is provied as parameter
tgw 0:2ee762ea11b3 676 */
tgw 0:2ee762ea11b3 677 class JSON_API Path {
tgw 0:2ee762ea11b3 678 public:
tgw 0:2ee762ea11b3 679 Path(const JSONCPP_STRING& path,
tgw 0:2ee762ea11b3 680 const PathArgument& a1 = PathArgument(),
tgw 0:2ee762ea11b3 681 const PathArgument& a2 = PathArgument(),
tgw 0:2ee762ea11b3 682 const PathArgument& a3 = PathArgument(),
tgw 0:2ee762ea11b3 683 const PathArgument& a4 = PathArgument(),
tgw 0:2ee762ea11b3 684 const PathArgument& a5 = PathArgument());
tgw 0:2ee762ea11b3 685
tgw 0:2ee762ea11b3 686 const Value& resolve(const Value& root) const;
tgw 0:2ee762ea11b3 687 Value resolve(const Value& root, const Value& defaultValue) const;
tgw 0:2ee762ea11b3 688 /// Creates the "path" to access the specified node and returns a reference on
tgw 0:2ee762ea11b3 689 /// the node.
tgw 0:2ee762ea11b3 690 Value& make(Value& root) const;
tgw 0:2ee762ea11b3 691
tgw 0:2ee762ea11b3 692 private:
tgw 0:2ee762ea11b3 693 typedef std::vector<const PathArgument*> InArgs;
tgw 0:2ee762ea11b3 694 typedef std::vector<PathArgument> Args;
tgw 0:2ee762ea11b3 695
tgw 0:2ee762ea11b3 696 void makePath(const JSONCPP_STRING& path, const InArgs& in);
tgw 0:2ee762ea11b3 697 void addPathInArg(const JSONCPP_STRING& path,
tgw 0:2ee762ea11b3 698 const InArgs& in,
tgw 0:2ee762ea11b3 699 InArgs::const_iterator& itInArg,
tgw 0:2ee762ea11b3 700 PathArgument::Kind kind);
tgw 0:2ee762ea11b3 701 void invalidPath(const JSONCPP_STRING& path, int location);
tgw 0:2ee762ea11b3 702
tgw 0:2ee762ea11b3 703 Args args_;
tgw 0:2ee762ea11b3 704 };
tgw 0:2ee762ea11b3 705
tgw 0:2ee762ea11b3 706 /** \brief base class for Value iterators.
tgw 0:2ee762ea11b3 707 *
tgw 0:2ee762ea11b3 708 */
tgw 0:2ee762ea11b3 709 class JSON_API ValueIteratorBase {
tgw 0:2ee762ea11b3 710 public:
tgw 0:2ee762ea11b3 711 typedef std::bidirectional_iterator_tag iterator_category;
tgw 0:2ee762ea11b3 712 typedef unsigned int size_t;
tgw 0:2ee762ea11b3 713 typedef int difference_type;
tgw 0:2ee762ea11b3 714 typedef ValueIteratorBase SelfType;
tgw 0:2ee762ea11b3 715
tgw 0:2ee762ea11b3 716 bool operator==(const SelfType& other) const { return isEqual(other); }
tgw 0:2ee762ea11b3 717
tgw 0:2ee762ea11b3 718 bool operator!=(const SelfType& other) const { return !isEqual(other); }
tgw 0:2ee762ea11b3 719
tgw 0:2ee762ea11b3 720 difference_type operator-(const SelfType& other) const {
tgw 0:2ee762ea11b3 721 return other.computeDistance(*this);
tgw 0:2ee762ea11b3 722 }
tgw 0:2ee762ea11b3 723
tgw 0:2ee762ea11b3 724 /// Return either the index or the member name of the referenced value as a
tgw 0:2ee762ea11b3 725 /// Value.
tgw 0:2ee762ea11b3 726 Value key() const;
tgw 0:2ee762ea11b3 727
tgw 0:2ee762ea11b3 728 /// Return the index of the referenced Value, or -1 if it is not an arrayValue.
tgw 0:2ee762ea11b3 729 UInt index() const;
tgw 0:2ee762ea11b3 730
tgw 0:2ee762ea11b3 731 /// Return the member name of the referenced Value, or "" if it is not an
tgw 0:2ee762ea11b3 732 /// objectValue.
tgw 0:2ee762ea11b3 733 /// \note Avoid `c_str()` on result, as embedded zeroes are possible.
tgw 0:2ee762ea11b3 734 JSONCPP_STRING name() const;
tgw 0:2ee762ea11b3 735
tgw 0:2ee762ea11b3 736 /// Return the member name of the referenced Value. "" if it is not an
tgw 0:2ee762ea11b3 737 /// objectValue.
tgw 0:2ee762ea11b3 738 /// \deprecated This cannot be used for UTF-8 strings, since there can be embedded nulls.
tgw 0:2ee762ea11b3 739 JSONCPP_DEPRECATED("Use `key = name();` instead.")
tgw 0:2ee762ea11b3 740 char const* memberName() const;
tgw 0:2ee762ea11b3 741 /// Return the member name of the referenced Value, or NULL if it is not an
tgw 0:2ee762ea11b3 742 /// objectValue.
tgw 0:2ee762ea11b3 743 /// \note Better version than memberName(). Allows embedded nulls.
tgw 0:2ee762ea11b3 744 char const* memberName(char const** end) const;
tgw 0:2ee762ea11b3 745
tgw 0:2ee762ea11b3 746 protected:
tgw 0:2ee762ea11b3 747 Value& deref() const;
tgw 0:2ee762ea11b3 748
tgw 0:2ee762ea11b3 749 void increment();
tgw 0:2ee762ea11b3 750
tgw 0:2ee762ea11b3 751 void decrement();
tgw 0:2ee762ea11b3 752
tgw 0:2ee762ea11b3 753 difference_type computeDistance(const SelfType& other) const;
tgw 0:2ee762ea11b3 754
tgw 0:2ee762ea11b3 755 bool isEqual(const SelfType& other) const;
tgw 0:2ee762ea11b3 756
tgw 0:2ee762ea11b3 757 void copy(const SelfType& other);
tgw 0:2ee762ea11b3 758
tgw 0:2ee762ea11b3 759 private:
tgw 0:2ee762ea11b3 760 Value::ObjectValues::iterator current_;
tgw 0:2ee762ea11b3 761 // Indicates that iterator is for a null value.
tgw 0:2ee762ea11b3 762 bool isNull_;
tgw 0:2ee762ea11b3 763
tgw 0:2ee762ea11b3 764 public:
tgw 0:2ee762ea11b3 765 // For some reason, BORLAND needs these at the end, rather
tgw 0:2ee762ea11b3 766 // than earlier. No idea why.
tgw 0:2ee762ea11b3 767 ValueIteratorBase();
tgw 0:2ee762ea11b3 768 explicit ValueIteratorBase(const Value::ObjectValues::iterator& current);
tgw 0:2ee762ea11b3 769 };
tgw 0:2ee762ea11b3 770
tgw 0:2ee762ea11b3 771 /** \brief const iterator for object and array value.
tgw 0:2ee762ea11b3 772 *
tgw 0:2ee762ea11b3 773 */
tgw 0:2ee762ea11b3 774 class JSON_API ValueConstIterator : public ValueIteratorBase {
tgw 0:2ee762ea11b3 775 friend class Value;
tgw 0:2ee762ea11b3 776
tgw 0:2ee762ea11b3 777 public:
tgw 0:2ee762ea11b3 778 typedef const Value value_type;
tgw 0:2ee762ea11b3 779 //typedef unsigned int size_t;
tgw 0:2ee762ea11b3 780 //typedef int difference_type;
tgw 0:2ee762ea11b3 781 typedef const Value& reference;
tgw 0:2ee762ea11b3 782 typedef const Value* pointer;
tgw 0:2ee762ea11b3 783 typedef ValueConstIterator SelfType;
tgw 0:2ee762ea11b3 784
tgw 0:2ee762ea11b3 785 ValueConstIterator();
tgw 0:2ee762ea11b3 786 ValueConstIterator(ValueIterator const& other);
tgw 0:2ee762ea11b3 787
tgw 0:2ee762ea11b3 788 private:
tgw 0:2ee762ea11b3 789 /*! \internal Use by Value to create an iterator.
tgw 0:2ee762ea11b3 790 */
tgw 0:2ee762ea11b3 791 explicit ValueConstIterator(const Value::ObjectValues::iterator& current);
tgw 0:2ee762ea11b3 792 public:
tgw 0:2ee762ea11b3 793 SelfType& operator=(const ValueIteratorBase& other);
tgw 0:2ee762ea11b3 794
tgw 0:2ee762ea11b3 795 SelfType operator++(int) {
tgw 0:2ee762ea11b3 796 SelfType temp(*this);
tgw 0:2ee762ea11b3 797 ++*this;
tgw 0:2ee762ea11b3 798 return temp;
tgw 0:2ee762ea11b3 799 }
tgw 0:2ee762ea11b3 800
tgw 0:2ee762ea11b3 801 SelfType operator--(int) {
tgw 0:2ee762ea11b3 802 SelfType temp(*this);
tgw 0:2ee762ea11b3 803 --*this;
tgw 0:2ee762ea11b3 804 return temp;
tgw 0:2ee762ea11b3 805 }
tgw 0:2ee762ea11b3 806
tgw 0:2ee762ea11b3 807 SelfType& operator--() {
tgw 0:2ee762ea11b3 808 decrement();
tgw 0:2ee762ea11b3 809 return *this;
tgw 0:2ee762ea11b3 810 }
tgw 0:2ee762ea11b3 811
tgw 0:2ee762ea11b3 812 SelfType& operator++() {
tgw 0:2ee762ea11b3 813 increment();
tgw 0:2ee762ea11b3 814 return *this;
tgw 0:2ee762ea11b3 815 }
tgw 0:2ee762ea11b3 816
tgw 0:2ee762ea11b3 817 reference operator*() const { return deref(); }
tgw 0:2ee762ea11b3 818
tgw 0:2ee762ea11b3 819 pointer operator->() const { return &deref(); }
tgw 0:2ee762ea11b3 820 };
tgw 0:2ee762ea11b3 821
tgw 0:2ee762ea11b3 822 /** \brief Iterator for object and array value.
tgw 0:2ee762ea11b3 823 */
tgw 0:2ee762ea11b3 824 class JSON_API ValueIterator : public ValueIteratorBase {
tgw 0:2ee762ea11b3 825 friend class Value;
tgw 0:2ee762ea11b3 826
tgw 0:2ee762ea11b3 827 public:
tgw 0:2ee762ea11b3 828 typedef Value value_type;
tgw 0:2ee762ea11b3 829 typedef unsigned int size_t;
tgw 0:2ee762ea11b3 830 typedef int difference_type;
tgw 0:2ee762ea11b3 831 typedef Value& reference;
tgw 0:2ee762ea11b3 832 typedef Value* pointer;
tgw 0:2ee762ea11b3 833 typedef ValueIterator SelfType;
tgw 0:2ee762ea11b3 834
tgw 0:2ee762ea11b3 835 ValueIterator();
tgw 0:2ee762ea11b3 836 explicit ValueIterator(const ValueConstIterator& other);
tgw 0:2ee762ea11b3 837 ValueIterator(const ValueIterator& other);
tgw 0:2ee762ea11b3 838
tgw 0:2ee762ea11b3 839 private:
tgw 0:2ee762ea11b3 840 /*! \internal Use by Value to create an iterator.
tgw 0:2ee762ea11b3 841 */
tgw 0:2ee762ea11b3 842 explicit ValueIterator(const Value::ObjectValues::iterator& current);
tgw 0:2ee762ea11b3 843 public:
tgw 0:2ee762ea11b3 844 SelfType& operator=(const SelfType& other);
tgw 0:2ee762ea11b3 845
tgw 0:2ee762ea11b3 846 SelfType operator++(int) {
tgw 0:2ee762ea11b3 847 SelfType temp(*this);
tgw 0:2ee762ea11b3 848 ++*this;
tgw 0:2ee762ea11b3 849 return temp;
tgw 0:2ee762ea11b3 850 }
tgw 0:2ee762ea11b3 851
tgw 0:2ee762ea11b3 852 SelfType operator--(int) {
tgw 0:2ee762ea11b3 853 SelfType temp(*this);
tgw 0:2ee762ea11b3 854 --*this;
tgw 0:2ee762ea11b3 855 return temp;
tgw 0:2ee762ea11b3 856 }
tgw 0:2ee762ea11b3 857
tgw 0:2ee762ea11b3 858 SelfType& operator--() {
tgw 0:2ee762ea11b3 859 decrement();
tgw 0:2ee762ea11b3 860 return *this;
tgw 0:2ee762ea11b3 861 }
tgw 0:2ee762ea11b3 862
tgw 0:2ee762ea11b3 863 SelfType& operator++() {
tgw 0:2ee762ea11b3 864 increment();
tgw 0:2ee762ea11b3 865 return *this;
tgw 0:2ee762ea11b3 866 }
tgw 0:2ee762ea11b3 867
tgw 0:2ee762ea11b3 868 reference operator*() const { return deref(); }
tgw 0:2ee762ea11b3 869
tgw 0:2ee762ea11b3 870 pointer operator->() const { return &deref(); }
tgw 0:2ee762ea11b3 871 };
tgw 0:2ee762ea11b3 872
tgw 0:2ee762ea11b3 873 } // namespace Json
tgw 0:2ee762ea11b3 874
tgw 0:2ee762ea11b3 875
tgw 0:2ee762ea11b3 876 namespace std {
tgw 0:2ee762ea11b3 877 /// Specialize std::swap() for Json::Value.
tgw 0:2ee762ea11b3 878 template<>
tgw 0:2ee762ea11b3 879 inline void swap(Json::Value& a, Json::Value& b) { a.swap(b); }
tgw 0:2ee762ea11b3 880 }
tgw 0:2ee762ea11b3 881
tgw 0:2ee762ea11b3 882 #pragma pack(pop)
tgw 0:2ee762ea11b3 883
tgw 0:2ee762ea11b3 884 #if defined(JSONCPP_DISABLE_DLL_INTERFACE_WARNING)
tgw 0:2ee762ea11b3 885 #pragma warning(pop)
tgw 0:2ee762ea11b3 886 #endif // if defined(JSONCPP_DISABLE_DLL_INTERFACE_WARNING)
tgw 0:2ee762ea11b3 887
tgw 0:2ee762ea11b3 888 #endif // CPPTL_JSON_H_INCLUDED