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.

913 lines
28 KiB

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