You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1022 lines
29 KiB

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