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.

1701 lines
49 KiB

7 years ago
​
​
​
​
​
​
​
7 years ago
​
​
​
​
​
​
7 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. // Copyright 2011 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. #if !defined(JSON_IS_AMALGAMATION)
  6. #include <json/assertions.h>
  7. #include <json/value.h>
  8. #include <json/writer.h>
  9. #endif // if !defined(JSON_IS_AMALGAMATION)
  10. #include <cassert>
  11. #include <cmath>
  12. #include <cstring>
  13. #include <sstream>
  14. #include <utility>
  15. #ifdef JSON_USE_CPPTL
  16. #include <cpptl/conststring.h>
  17. #endif
  18. #include <algorithm> // min()
  19. #include <cstddef> // size_t
  20. // Provide implementation equivalent of std::snprintf for older _MSC compilers
  21. #if defined(_MSC_VER) && _MSC_VER < 1900
  22. #include <stdarg.h>
  23. static int msvc_pre1900_c99_vsnprintf(char* outBuf,
  24. size_t size,
  25. const char* format,
  26. va_list ap) {
  27. int count = -1;
  28. if (size != 0)
  29. count = _vsnprintf_s(outBuf, size, _TRUNCATE, format, ap);
  30. if (count == -1)
  31. count = _vscprintf(format, ap);
  32. return count;
  33. }
  34. int JSON_API msvc_pre1900_c99_snprintf(char* outBuf,
  35. size_t size,
  36. const char* format,
  37. ...) {
  38. va_list ap;
  39. va_start(ap, format);
  40. const int count = msvc_pre1900_c99_vsnprintf(outBuf, size, format, ap);
  41. va_end(ap);
  42. return count;
  43. }
  44. #endif
  45. // Disable warning C4702 : unreachable code
  46. #if defined(_MSC_VER)
  47. #pragma warning(disable : 4702)
  48. #endif
  49. #define JSON_ASSERT_UNREACHABLE assert(false)
  50. namespace Json {
  51. template <typename T>
  52. static std::unique_ptr<T> cloneUnique(const std::unique_ptr<T>& p) {
  53. std::unique_ptr<T> r;
  54. if (p) {
  55. r = std::unique_ptr<T>(new T(*p));
  56. }
  57. return r;
  58. }
  59. // This is a walkaround to avoid the static initialization of Value::null.
  60. // kNull must be word-aligned to avoid crashing on ARM. We use an alignment of
  61. // 8 (instead of 4) as a bit of future-proofing.
  62. #if defined(__ARMEL__)
  63. #define ALIGNAS(byte_alignment) __attribute__((aligned(byte_alignment)))
  64. #else
  65. #define ALIGNAS(byte_alignment)
  66. #endif
  67. // static const unsigned char ALIGNAS(8) kNull[sizeof(Value)] = { 0 };
  68. // const unsigned char& kNullRef = kNull[0];
  69. // const Value& Value::null = reinterpret_cast<const Value&>(kNullRef);
  70. // const Value& Value::nullRef = null;
  71. // static
  72. Value const& Value::nullSingleton() {
  73. static Value const nullStatic;
  74. return nullStatic;
  75. }
  76. // for backwards compatibility, we'll leave these global references around, but
  77. // DO NOT use them in JSONCPP library code any more!
  78. Value const& Value::null = Value::nullSingleton();
  79. Value const& Value::nullRef = Value::nullSingleton();
  80. const Int Value::minInt = Int(~(UInt(-1) / 2));
  81. const Int Value::maxInt = Int(UInt(-1) / 2);
  82. const UInt Value::maxUInt = UInt(-1);
  83. #if defined(JSON_HAS_INT64)
  84. const Int64 Value::minInt64 = Int64(~(UInt64(-1) / 2));
  85. const Int64 Value::maxInt64 = Int64(UInt64(-1) / 2);
  86. const UInt64 Value::maxUInt64 = UInt64(-1);
  87. // The constant is hard-coded because some compiler have trouble
  88. // converting Value::maxUInt64 to a double correctly (AIX/xlC).
  89. // Assumes that UInt64 is a 64 bits integer.
  90. static const double maxUInt64AsDouble = 18446744073709551615.0;
  91. #endif // defined(JSON_HAS_INT64)
  92. const LargestInt Value::minLargestInt = LargestInt(~(LargestUInt(-1) / 2));
  93. const LargestInt Value::maxLargestInt = LargestInt(LargestUInt(-1) / 2);
  94. const LargestUInt Value::maxLargestUInt = LargestUInt(-1);
  95. const UInt Value::defaultRealPrecision = 17;
  96. #if !defined(JSON_USE_INT64_DOUBLE_CONVERSION)
  97. template <typename T, typename U>
  98. static inline bool InRange(double d, T min, U max) {
  99. // The casts can lose precision, but we are looking only for
  100. // an approximate range. Might fail on edge cases though. ~cdunn
  101. // return d >= static_cast<double>(min) && d <= static_cast<double>(max);
  102. return d >= min && d <= max;
  103. }
  104. #else // if !defined(JSON_USE_INT64_DOUBLE_CONVERSION)
  105. static inline double integerToDouble(Json::UInt64 value) {
  106. return static_cast<double>(Int64(value / 2)) * 2.0 +
  107. static_cast<double>(Int64(value & 1));
  108. }
  109. template <typename T> static inline double integerToDouble(T value) {
  110. return static_cast<double>(value);
  111. }
  112. template <typename T, typename U>
  113. static inline bool InRange(double d, T min, U max) {
  114. return d >= integerToDouble(min) && d <= integerToDouble(max);
  115. }
  116. #endif // if !defined(JSON_USE_INT64_DOUBLE_CONVERSION)
  117. /** Duplicates the specified string value.
  118. * @param value Pointer to the string to duplicate. Must be zero-terminated if
  119. * length is "unknown".
  120. * @param length Length of the value. if equals to unknown, then it will be
  121. * computed using strlen(value).
  122. * @return Pointer on the duplicate instance of string.
  123. */
  124. static inline char* duplicateStringValue(const char* value, size_t length) {
  125. // Avoid an integer overflow in the call to malloc below by limiting length
  126. // to a sane value.
  127. if (length >= static_cast<size_t>(Value::maxInt))
  128. length = Value::maxInt - 1;
  129. char* newString = static_cast<char*>(malloc(length + 1));
  130. if (newString == nullptr) {
  131. throwRuntimeError("in Json::Value::duplicateStringValue(): "
  132. "Failed to allocate string value buffer");
  133. }
  134. memcpy(newString, value, length);
  135. newString[length] = 0;
  136. return newString;
  137. }
  138. /* Record the length as a prefix.
  139. */
  140. static inline char* duplicateAndPrefixStringValue(const char* value,
  141. unsigned int length) {
  142. // Avoid an integer overflow in the call to malloc below by limiting length
  143. // to a sane value.
  144. JSON_ASSERT_MESSAGE(length <= static_cast<unsigned>(Value::maxInt) -
  145. sizeof(unsigned) - 1U,
  146. "in Json::Value::duplicateAndPrefixStringValue(): "
  147. "length too big for prefixing");
  148. unsigned actualLength = length + static_cast<unsigned>(sizeof(unsigned)) + 1U;
  149. char* newString = static_cast<char*>(malloc(actualLength));
  150. if (newString == nullptr) {
  151. throwRuntimeError("in Json::Value::duplicateAndPrefixStringValue(): "
  152. "Failed to allocate string value buffer");
  153. }
  154. *reinterpret_cast<unsigned*>(newString) = length;
  155. memcpy(newString + sizeof(unsigned), value, length);
  156. newString[actualLength - 1U] =
  157. 0; // to avoid buffer over-run accidents by users later
  158. return newString;
  159. }
  160. inline static void decodePrefixedString(bool isPrefixed,
  161. char const* prefixed,
  162. unsigned* length,
  163. char const** value) {
  164. if (!isPrefixed) {
  165. *length = static_cast<unsigned>(strlen(prefixed));
  166. *value = prefixed;
  167. } else {
  168. *length = *reinterpret_cast<unsigned const*>(prefixed);
  169. *value = prefixed + sizeof(unsigned);
  170. }
  171. }
  172. /** Free the string duplicated by
  173. * duplicateStringValue()/duplicateAndPrefixStringValue().
  174. */
  175. #if JSONCPP_USING_SECURE_MEMORY
  176. static inline void releasePrefixedStringValue(char* value) {
  177. unsigned length = 0;
  178. char const* valueDecoded;
  179. decodePrefixedString(true, value, &length, &valueDecoded);
  180. size_t const size = sizeof(unsigned) + length + 1U;
  181. memset(value, 0, size);
  182. free(value);
  183. }
  184. static inline void releaseStringValue(char* value, unsigned length) {
  185. // length==0 => we allocated the strings memory
  186. size_t size = (length == 0) ? strlen(value) : length;
  187. memset(value, 0, size);
  188. free(value);
  189. }
  190. #else // !JSONCPP_USING_SECURE_MEMORY
  191. static inline void releasePrefixedStringValue(char* value) { free(value); }
  192. static inline void releaseStringValue(char* value, unsigned) { free(value); }
  193. #endif // JSONCPP_USING_SECURE_MEMORY
  194. } // namespace Json
  195. // //////////////////////////////////////////////////////////////////
  196. // //////////////////////////////////////////////////////////////////
  197. // //////////////////////////////////////////////////////////////////
  198. // ValueInternals...
  199. // //////////////////////////////////////////////////////////////////
  200. // //////////////////////////////////////////////////////////////////
  201. // //////////////////////////////////////////////////////////////////
  202. #if !defined(JSON_IS_AMALGAMATION)
  203. #include "json_valueiterator.inl"
  204. #endif // if !defined(JSON_IS_AMALGAMATION)
  205. namespace Json {
  206. Exception::Exception(String msg) : msg_(std::move(msg)) {}
  207. Exception::~Exception() JSONCPP_NOEXCEPT {}
  208. char const* Exception::what() const JSONCPP_NOEXCEPT { return msg_.c_str(); }
  209. RuntimeError::RuntimeError(String const& msg) : Exception(msg) {}
  210. LogicError::LogicError(String const& msg) : Exception(msg) {}
  211. JSONCPP_NORETURN void throwRuntimeError(String const& msg) {
  212. throw RuntimeError(msg);
  213. }
  214. JSONCPP_NORETURN void throwLogicError(String const& msg) {
  215. throw LogicError(msg);
  216. }
  217. // //////////////////////////////////////////////////////////////////
  218. // //////////////////////////////////////////////////////////////////
  219. // //////////////////////////////////////////////////////////////////
  220. // class Value::CZString
  221. // //////////////////////////////////////////////////////////////////
  222. // //////////////////////////////////////////////////////////////////
  223. // //////////////////////////////////////////////////////////////////
  224. // Notes: policy_ indicates if the string was allocated when
  225. // a string is stored.
  226. Value::CZString::CZString(ArrayIndex index) : cstr_(nullptr), index_(index) {}
  227. Value::CZString::CZString(char const* str,
  228. unsigned length,
  229. DuplicationPolicy allocate)
  230. : cstr_(str) {
  231. // allocate != duplicate
  232. storage_.policy_ = allocate & 0x3;
  233. storage_.length_ = length & 0x3FFFFFFF;
  234. }
  235. Value::CZString::CZString(const CZString& other) {
  236. cstr_ = (other.storage_.policy_ != noDuplication && other.cstr_ != nullptr
  237. ? duplicateStringValue(other.cstr_, other.storage_.length_)
  238. : other.cstr_);
  239. storage_.policy_ =
  240. static_cast<unsigned>(
  241. other.cstr_
  242. ? (static_cast<DuplicationPolicy>(other.storage_.policy_) ==
  243. noDuplication
  244. ? noDuplication
  245. : duplicate)
  246. : static_cast<DuplicationPolicy>(other.storage_.policy_)) &
  247. 3U;
  248. storage_.length_ = other.storage_.length_;
  249. }
  250. Value::CZString::CZString(CZString&& other)
  251. : cstr_(other.cstr_), index_(other.index_) {
  252. other.cstr_ = nullptr;
  253. }
  254. Value::CZString::~CZString() {
  255. if (cstr_ && storage_.policy_ == duplicate) {
  256. releaseStringValue(const_cast<char*>(cstr_),
  257. storage_.length_ + 1u); // +1 for null terminating
  258. // character for sake of
  259. // completeness but not actually
  260. // necessary
  261. }
  262. }
  263. void Value::CZString::swap(CZString& other) {
  264. std::swap(cstr_, other.cstr_);
  265. std::swap(index_, other.index_);
  266. }
  267. Value::CZString& Value::CZString::operator=(const CZString& other) {
  268. cstr_ = other.cstr_;
  269. index_ = other.index_;
  270. return *this;
  271. }
  272. Value::CZString& Value::CZString::operator=(CZString&& other) {
  273. cstr_ = other.cstr_;
  274. index_ = other.index_;
  275. other.cstr_ = nullptr;
  276. return *this;
  277. }
  278. bool Value::CZString::operator<(const CZString& other) const {
  279. if (!cstr_)
  280. return index_ < other.index_;
  281. // return strcmp(cstr_, other.cstr_) < 0;
  282. // Assume both are strings.
  283. unsigned this_len = this->storage_.length_;
  284. unsigned other_len = other.storage_.length_;
  285. unsigned min_len = std::min<unsigned>(this_len, other_len);
  286. JSON_ASSERT(this->cstr_ && other.cstr_);
  287. int comp = memcmp(this->cstr_, other.cstr_, min_len);
  288. if (comp < 0)
  289. return true;
  290. if (comp > 0)
  291. return false;
  292. return (this_len < other_len);
  293. }
  294. bool Value::CZString::operator==(const CZString& other) const {
  295. if (!cstr_)
  296. return index_ == other.index_;
  297. // return strcmp(cstr_, other.cstr_) == 0;
  298. // Assume both are strings.
  299. unsigned this_len = this->storage_.length_;
  300. unsigned other_len = other.storage_.length_;
  301. if (this_len != other_len)
  302. return false;
  303. JSON_ASSERT(this->cstr_ && other.cstr_);
  304. int comp = memcmp(this->cstr_, other.cstr_, this_len);
  305. return comp == 0;
  306. }
  307. ArrayIndex Value::CZString::index() const { return index_; }
  308. // const char* Value::CZString::c_str() const { return cstr_; }
  309. const char* Value::CZString::data() const { return cstr_; }
  310. unsigned Value::CZString::length() const { return storage_.length_; }
  311. bool Value::CZString::isStaticString() const {
  312. return storage_.policy_ == noDuplication;
  313. }
  314. // //////////////////////////////////////////////////////////////////
  315. // //////////////////////////////////////////////////////////////////
  316. // //////////////////////////////////////////////////////////////////
  317. // class Value::Value
  318. // //////////////////////////////////////////////////////////////////
  319. // //////////////////////////////////////////////////////////////////
  320. // //////////////////////////////////////////////////////////////////
  321. /*! \internal Default constructor initialization must be equivalent to:
  322. * memset( this, 0, sizeof(Value) )
  323. * This optimization is used in ValueInternalMap fast allocator.
  324. */
  325. Value::Value(ValueType type) {
  326. static char const emptyString[] = "";
  327. initBasic(type);
  328. switch (type) {
  329. case nullValue:
  330. break;
  331. case intValue:
  332. case uintValue:
  333. value_.int_ = 0;
  334. break;
  335. case realValue:
  336. value_.real_ = 0.0;
  337. break;
  338. case stringValue:
  339. // allocated_ == false, so this is safe.
  340. value_.string_ = const_cast<char*>(static_cast<char const*>(emptyString));
  341. break;
  342. case arrayValue:
  343. case objectValue:
  344. value_.map_ = new ObjectValues();
  345. break;
  346. case booleanValue:
  347. value_.bool_ = false;
  348. break;
  349. default:
  350. JSON_ASSERT_UNREACHABLE;
  351. }
  352. }
  353. Value::Value(Int value) {
  354. initBasic(intValue);
  355. value_.int_ = value;
  356. }
  357. Value::Value(UInt value) {
  358. initBasic(uintValue);
  359. value_.uint_ = value;
  360. }
  361. #if defined(JSON_HAS_INT64)
  362. Value::Value(Int64 value) {
  363. initBasic(intValue);
  364. value_.int_ = value;
  365. }
  366. Value::Value(UInt64 value) {
  367. initBasic(uintValue);
  368. value_.uint_ = value;
  369. }
  370. #endif // defined(JSON_HAS_INT64)
  371. Value::Value(double value) {
  372. initBasic(realValue);
  373. value_.real_ = value;
  374. }
  375. Value::Value(const char* value) {
  376. initBasic(stringValue, true);
  377. JSON_ASSERT_MESSAGE(value != nullptr,
  378. "Null Value Passed to Value Constructor");
  379. value_.string_ = duplicateAndPrefixStringValue(
  380. value, static_cast<unsigned>(strlen(value)));
  381. }
  382. Value::Value(const char* begin, const char* end) {
  383. initBasic(stringValue, true);
  384. value_.string_ =
  385. duplicateAndPrefixStringValue(begin, static_cast<unsigned>(end - begin));
  386. }
  387. Value::Value(const String& value) {
  388. initBasic(stringValue, true);
  389. value_.string_ = duplicateAndPrefixStringValue(
  390. value.data(), static_cast<unsigned>(value.length()));
  391. }
  392. Value::Value(const StaticString& value) {
  393. initBasic(stringValue);
  394. value_.string_ = const_cast<char*>(value.c_str());
  395. }
  396. #ifdef JSON_USE_CPPTL
  397. Value::Value(const CppTL::ConstString& value) {
  398. initBasic(stringValue, true);
  399. value_.string_ = duplicateAndPrefixStringValue(
  400. value, static_cast<unsigned>(value.length()));
  401. }
  402. #endif
  403. Value::Value(bool value) {
  404. initBasic(booleanValue);
  405. value_.bool_ = value;
  406. }
  407. Value::Value(const Value& other) {
  408. dupPayload(other);
  409. dupMeta(other);
  410. }
  411. Value::Value(Value&& other) {
  412. initBasic(nullValue);
  413. swap(other);
  414. }
  415. Value::~Value() {
  416. releasePayload();
  417. value_.uint_ = 0;
  418. }
  419. Value& Value::operator=(const Value& other) {
  420. Value(other).swap(*this);
  421. return *this;
  422. }
  423. Value& Value::operator=(Value&& other) {
  424. other.swap(*this);
  425. return *this;
  426. }
  427. void Value::swapPayload(Value& other) {
  428. std::swap(bits_, other.bits_);
  429. std::swap(value_, other.value_);
  430. }
  431. void Value::copyPayload(const Value& other) {
  432. releasePayload();
  433. dupPayload(other);
  434. }
  435. void Value::swap(Value& other) {
  436. swapPayload(other);
  437. std::swap(comments_, other.comments_);
  438. std::swap(start_, other.start_);
  439. std::swap(limit_, other.limit_);
  440. }
  441. void Value::copy(const Value& other) {
  442. copyPayload(other);
  443. dupMeta(other);
  444. }
  445. ValueType Value::type() const {
  446. return static_cast<ValueType>(bits_.value_type_);
  447. }
  448. int Value::compare(const Value& other) const {
  449. if (*this < other)
  450. return -1;
  451. if (*this > other)
  452. return 1;
  453. return 0;
  454. }
  455. bool Value::operator<(const Value& other) const {
  456. int typeDelta = type() - other.type();
  457. if (typeDelta)
  458. return typeDelta < 0 ? true : false;
  459. switch (type()) {
  460. case nullValue:
  461. return false;
  462. case intValue:
  463. return value_.int_ < other.value_.int_;
  464. case uintValue:
  465. return value_.uint_ < other.value_.uint_;
  466. case realValue:
  467. return value_.real_ < other.value_.real_;
  468. case booleanValue:
  469. return value_.bool_ < other.value_.bool_;
  470. case stringValue: {
  471. if ((value_.string_ == nullptr) || (other.value_.string_ == nullptr)) {
  472. if (other.value_.string_)
  473. return true;
  474. else
  475. return false;
  476. }
  477. unsigned this_len;
  478. unsigned other_len;
  479. char const* this_str;
  480. char const* other_str;
  481. decodePrefixedString(this->isAllocated(), this->value_.string_, &this_len,
  482. &this_str);
  483. decodePrefixedString(other.isAllocated(), other.value_.string_, &other_len,
  484. &other_str);
  485. unsigned min_len = std::min<unsigned>(this_len, other_len);
  486. JSON_ASSERT(this_str && other_str);
  487. int comp = memcmp(this_str, other_str, min_len);
  488. if (comp < 0)
  489. return true;
  490. if (comp > 0)
  491. return false;
  492. return (this_len < other_len);
  493. }
  494. case arrayValue:
  495. case objectValue: {
  496. int delta = int(value_.map_->size() - other.value_.map_->size());
  497. if (delta)
  498. return delta < 0;
  499. return (*value_.map_) < (*other.value_.map_);
  500. }
  501. default:
  502. JSON_ASSERT_UNREACHABLE;
  503. }
  504. return false; // unreachable
  505. }
  506. bool Value::operator<=(const Value& other) const { return !(other < *this); }
  507. bool Value::operator>=(const Value& other) const { return !(*this < other); }
  508. bool Value::operator>(const Value& other) const { return other < *this; }
  509. bool Value::operator==(const Value& other) const {
  510. if (type() != other.type())
  511. return false;
  512. switch (type()) {
  513. case nullValue:
  514. return true;
  515. case intValue:
  516. return value_.int_ == other.value_.int_;
  517. case uintValue:
  518. return value_.uint_ == other.value_.uint_;
  519. case realValue:
  520. return value_.real_ == other.value_.real_;
  521. case booleanValue:
  522. return value_.bool_ == other.value_.bool_;
  523. case stringValue: {
  524. if ((value_.string_ == nullptr) || (other.value_.string_ == nullptr)) {
  525. return (value_.string_ == other.value_.string_);
  526. }
  527. unsigned this_len;
  528. unsigned other_len;
  529. char const* this_str;
  530. char const* other_str;
  531. decodePrefixedString(this->isAllocated(), this->value_.string_, &this_len,
  532. &this_str);
  533. decodePrefixedString(other.isAllocated(), other.value_.string_, &other_len,
  534. &other_str);
  535. if (this_len != other_len)
  536. return false;
  537. JSON_ASSERT(this_str && other_str);
  538. int comp = memcmp(this_str, other_str, this_len);
  539. return comp == 0;
  540. }
  541. case arrayValue:
  542. case objectValue:
  543. return value_.map_->size() == other.value_.map_->size() &&
  544. (*value_.map_) == (*other.value_.map_);
  545. default:
  546. JSON_ASSERT_UNREACHABLE;
  547. }
  548. return false; // unreachable
  549. }
  550. bool Value::operator!=(const Value& other) const { return !(*this == other); }
  551. const char* Value::asCString() const {
  552. JSON_ASSERT_MESSAGE(type() == stringValue,
  553. "in Json::Value::asCString(): requires stringValue");
  554. if (value_.string_ == nullptr)
  555. return nullptr;
  556. unsigned this_len;
  557. char const* this_str;
  558. decodePrefixedString(this->isAllocated(), this->value_.string_, &this_len,
  559. &this_str);
  560. return this_str;
  561. }
  562. #if JSONCPP_USING_SECURE_MEMORY
  563. unsigned Value::getCStringLength() const {
  564. JSON_ASSERT_MESSAGE(type() == stringValue,
  565. "in Json::Value::asCString(): requires stringValue");
  566. if (value_.string_ == 0)
  567. return 0;
  568. unsigned this_len;
  569. char const* this_str;
  570. decodePrefixedString(this->isAllocated(), this->value_.string_, &this_len,
  571. &this_str);
  572. return this_len;
  573. }
  574. #endif
  575. bool Value::getString(char const** begin, char const** end) const {
  576. if (type() != stringValue)
  577. return false;
  578. if (value_.string_ == nullptr)
  579. return false;
  580. unsigned length;
  581. decodePrefixedString(this->isAllocated(), this->value_.string_, &length,
  582. begin);
  583. *end = *begin + length;
  584. return true;
  585. }
  586. String Value::asString() const {
  587. switch (type()) {
  588. case nullValue:
  589. return "";
  590. case stringValue: {
  591. if (value_.string_ == nullptr)
  592. return "";
  593. unsigned this_len;
  594. char const* this_str;
  595. decodePrefixedString(this->isAllocated(), this->value_.string_, &this_len,
  596. &this_str);
  597. return String(this_str, this_len);
  598. }
  599. case booleanValue:
  600. return value_.bool_ ? "true" : "false";
  601. case intValue:
  602. return valueToString(value_.int_);
  603. case uintValue:
  604. return valueToString(value_.uint_);
  605. case realValue:
  606. return valueToString(value_.real_);
  607. default:
  608. JSON_FAIL_MESSAGE("Type is not convertible to string");
  609. }
  610. }
  611. #ifdef JSON_USE_CPPTL
  612. CppTL::ConstString Value::asConstString() const {
  613. unsigned len;
  614. char const* str;
  615. decodePrefixedString(isAllocated(), value_.string_, &len, &str);
  616. return CppTL::ConstString(str, len);
  617. }
  618. #endif
  619. Value::Int Value::asInt() const {
  620. switch (type()) {
  621. case intValue:
  622. JSON_ASSERT_MESSAGE(isInt(), "LargestInt out of Int range");
  623. return Int(value_.int_);
  624. case uintValue:
  625. JSON_ASSERT_MESSAGE(isInt(), "LargestUInt out of Int range");
  626. return Int(value_.uint_);
  627. case realValue:
  628. JSON_ASSERT_MESSAGE(InRange(value_.real_, minInt, maxInt),
  629. "double out of Int range");
  630. return Int(value_.real_);
  631. case nullValue:
  632. return 0;
  633. case booleanValue:
  634. return value_.bool_ ? 1 : 0;
  635. default:
  636. break;
  637. }
  638. JSON_FAIL_MESSAGE("Value is not convertible to Int.");
  639. }
  640. Value::UInt Value::asUInt() const {
  641. switch (type()) {
  642. case intValue:
  643. JSON_ASSERT_MESSAGE(isUInt(), "LargestInt out of UInt range");
  644. return UInt(value_.int_);
  645. case uintValue:
  646. JSON_ASSERT_MESSAGE(isUInt(), "LargestUInt out of UInt range");
  647. return UInt(value_.uint_);
  648. case realValue:
  649. JSON_ASSERT_MESSAGE(InRange(value_.real_, 0, maxUInt),
  650. "double out of UInt range");
  651. return UInt(value_.real_);
  652. case nullValue:
  653. return 0;
  654. case booleanValue:
  655. return value_.bool_ ? 1 : 0;
  656. default:
  657. break;
  658. }
  659. JSON_FAIL_MESSAGE("Value is not convertible to UInt.");
  660. }
  661. #if defined(JSON_HAS_INT64)
  662. Value::Int64 Value::asInt64() const {
  663. switch (type()) {
  664. case intValue:
  665. return Int64(value_.int_);
  666. case uintValue:
  667. JSON_ASSERT_MESSAGE(isInt64(), "LargestUInt out of Int64 range");
  668. return Int64(value_.uint_);
  669. case realValue:
  670. JSON_ASSERT_MESSAGE(InRange(value_.real_, minInt64, maxInt64),
  671. "double out of Int64 range");
  672. return Int64(value_.real_);
  673. case nullValue:
  674. return 0;
  675. case booleanValue:
  676. return value_.bool_ ? 1 : 0;
  677. default:
  678. break;
  679. }
  680. JSON_FAIL_MESSAGE("Value is not convertible to Int64.");
  681. }
  682. Value::UInt64 Value::asUInt64() const {
  683. switch (type()) {
  684. case intValue:
  685. JSON_ASSERT_MESSAGE(isUInt64(), "LargestInt out of UInt64 range");
  686. return UInt64(value_.int_);
  687. case uintValue:
  688. return UInt64(value_.uint_);
  689. case realValue:
  690. JSON_ASSERT_MESSAGE(InRange(value_.real_, 0, maxUInt64),
  691. "double out of UInt64 range");
  692. return UInt64(value_.real_);
  693. case nullValue:
  694. return 0;
  695. case booleanValue:
  696. return value_.bool_ ? 1 : 0;
  697. default:
  698. break;
  699. }
  700. JSON_FAIL_MESSAGE("Value is not convertible to UInt64.");
  701. }
  702. #endif // if defined(JSON_HAS_INT64)
  703. LargestInt Value::asLargestInt() const {
  704. #if defined(JSON_NO_INT64)
  705. return asInt();
  706. #else
  707. return asInt64();
  708. #endif
  709. }
  710. LargestUInt Value::asLargestUInt() const {
  711. #if defined(JSON_NO_INT64)
  712. return asUInt();
  713. #else
  714. return asUInt64();
  715. #endif
  716. }
  717. double Value::asDouble() const {
  718. switch (type()) {
  719. case intValue:
  720. return static_cast<double>(value_.int_);
  721. case uintValue:
  722. #if !defined(JSON_USE_INT64_DOUBLE_CONVERSION)
  723. return static_cast<double>(value_.uint_);
  724. #else // if !defined(JSON_USE_INT64_DOUBLE_CONVERSION)
  725. return integerToDouble(value_.uint_);
  726. #endif // if !defined(JSON_USE_INT64_DOUBLE_CONVERSION)
  727. case realValue:
  728. return value_.real_;
  729. case nullValue:
  730. return 0.0;
  731. case booleanValue:
  732. return value_.bool_ ? 1.0 : 0.0;
  733. default:
  734. break;
  735. }
  736. JSON_FAIL_MESSAGE("Value is not convertible to double.");
  737. }
  738. float Value::asFloat() const {
  739. switch (type()) {
  740. case intValue:
  741. return static_cast<float>(value_.int_);
  742. case uintValue:
  743. #if !defined(JSON_USE_INT64_DOUBLE_CONVERSION)
  744. return static_cast<float>(value_.uint_);
  745. #else // if !defined(JSON_USE_INT64_DOUBLE_CONVERSION)
  746. // This can fail (silently?) if the value is bigger than MAX_FLOAT.
  747. return static_cast<float>(integerToDouble(value_.uint_));
  748. #endif // if !defined(JSON_USE_INT64_DOUBLE_CONVERSION)
  749. case realValue:
  750. return static_cast<float>(value_.real_);
  751. case nullValue:
  752. return 0.0;
  753. case booleanValue:
  754. return value_.bool_ ? 1.0f : 0.0f;
  755. default:
  756. break;
  757. }
  758. JSON_FAIL_MESSAGE("Value is not convertible to float.");
  759. }
  760. bool Value::asBool() const {
  761. switch (type()) {
  762. case booleanValue:
  763. return value_.bool_;
  764. case nullValue:
  765. return false;
  766. case intValue:
  767. return value_.int_ ? true : false;
  768. case uintValue:
  769. return value_.uint_ ? true : false;
  770. case realValue:
  771. // This is kind of strange. Not recommended.
  772. return (value_.real_ != 0.0) ? true : false;
  773. default:
  774. break;
  775. }
  776. JSON_FAIL_MESSAGE("Value is not convertible to bool.");
  777. }
  778. bool Value::isConvertibleTo(ValueType other) const {
  779. switch (other) {
  780. case nullValue:
  781. return (isNumeric() && asDouble() == 0.0) ||
  782. (type() == booleanValue && value_.bool_ == false) ||
  783. (type() == stringValue && asString().empty()) ||
  784. (type() == arrayValue && value_.map_->empty()) ||
  785. (type() == objectValue && value_.map_->empty()) ||
  786. type() == nullValue;
  787. case intValue:
  788. return isInt() ||
  789. (type() == realValue && InRange(value_.real_, minInt, maxInt)) ||
  790. type() == booleanValue || type() == nullValue;
  791. case uintValue:
  792. return isUInt() ||
  793. (type() == realValue && InRange(value_.real_, 0, maxUInt)) ||
  794. type() == booleanValue || type() == nullValue;
  795. case realValue:
  796. return isNumeric() || type() == booleanValue || type() == nullValue;
  797. case booleanValue:
  798. return isNumeric() || type() == booleanValue || type() == nullValue;
  799. case stringValue:
  800. return isNumeric() || type() == booleanValue || type() == stringValue ||
  801. type() == nullValue;
  802. case arrayValue:
  803. return type() == arrayValue || type() == nullValue;
  804. case objectValue:
  805. return type() == objectValue || type() == nullValue;
  806. }
  807. JSON_ASSERT_UNREACHABLE;
  808. return false;
  809. }
  810. /// Number of values in array or object
  811. ArrayIndex Value::size() const {
  812. switch (type()) {
  813. case nullValue:
  814. case intValue:
  815. case uintValue:
  816. case realValue:
  817. case booleanValue:
  818. case stringValue:
  819. return 0;
  820. case arrayValue: // size of the array is highest index + 1
  821. if (!value_.map_->empty()) {
  822. ObjectValues::const_iterator itLast = value_.map_->end();
  823. --itLast;
  824. return (*itLast).first.index() + 1;
  825. }
  826. return 0;
  827. case objectValue:
  828. return ArrayIndex(value_.map_->size());
  829. }
  830. JSON_ASSERT_UNREACHABLE;
  831. return 0; // unreachable;
  832. }
  833. bool Value::empty() const {
  834. if (isNull() || isArray() || isObject())
  835. return size() == 0u;
  836. else
  837. return false;
  838. }
  839. Value::operator bool() const { return !isNull(); }
  840. void Value::clear() {
  841. JSON_ASSERT_MESSAGE(type() == nullValue || type() == arrayValue ||
  842. type() == objectValue,
  843. "in Json::Value::clear(): requires complex value");
  844. start_ = 0;
  845. limit_ = 0;
  846. switch (type()) {
  847. case arrayValue:
  848. case objectValue:
  849. value_.map_->clear();
  850. break;
  851. default:
  852. break;
  853. }
  854. }
  855. void Value::resize(ArrayIndex newSize) {
  856. JSON_ASSERT_MESSAGE(type() == nullValue || type() == arrayValue,
  857. "in Json::Value::resize(): requires arrayValue");
  858. if (type() == nullValue)
  859. *this = Value(arrayValue);
  860. ArrayIndex oldSize = size();
  861. if (newSize == 0)
  862. clear();
  863. else if (newSize > oldSize)
  864. this->operator[](newSize - 1);
  865. else {
  866. for (ArrayIndex index = newSize; index < oldSize; ++index) {
  867. value_.map_->erase(index);
  868. }
  869. JSON_ASSERT(size() == newSize);
  870. }
  871. }
  872. Value& Value::operator[](ArrayIndex index) {
  873. JSON_ASSERT_MESSAGE(
  874. type() == nullValue || type() == arrayValue,
  875. "in Json::Value::operator[](ArrayIndex): requires arrayValue");
  876. if (type() == nullValue)
  877. *this = Value(arrayValue);
  878. CZString key(index);
  879. auto it = value_.map_->lower_bound(key);
  880. if (it != value_.map_->end() && (*it).first == key)
  881. return (*it).second;
  882. ObjectValues::value_type defaultValue(key, nullSingleton());
  883. it = value_.map_->insert(it, defaultValue);
  884. return (*it).second;
  885. }
  886. Value& Value::operator[](int index) {
  887. JSON_ASSERT_MESSAGE(
  888. index >= 0,
  889. "in Json::Value::operator[](int index): index cannot be negative");
  890. return (*this)[ArrayIndex(index)];
  891. }
  892. const Value& Value::operator[](ArrayIndex index) const {
  893. JSON_ASSERT_MESSAGE(
  894. type() == nullValue || type() == arrayValue,
  895. "in Json::Value::operator[](ArrayIndex)const: requires arrayValue");
  896. if (type() == nullValue)
  897. return nullSingleton();
  898. CZString key(index);
  899. ObjectValues::const_iterator it = value_.map_->find(key);
  900. if (it == value_.map_->end())
  901. return nullSingleton();
  902. return (*it).second;
  903. }
  904. const Value& Value::operator[](int index) const {
  905. JSON_ASSERT_MESSAGE(
  906. index >= 0,
  907. "in Json::Value::operator[](int index) const: index cannot be negative");
  908. return (*this)[ArrayIndex(index)];
  909. }
  910. void Value::initBasic(ValueType type, bool allocated) {
  911. setType(type);
  912. setIsAllocated(allocated);
  913. comments_ = Comments{};
  914. start_ = 0;
  915. limit_ = 0;
  916. }
  917. void Value::dupPayload(const Value& other) {
  918. setType(other.type());
  919. setIsAllocated(false);
  920. switch (type()) {
  921. case nullValue:
  922. case intValue:
  923. case uintValue:
  924. case realValue:
  925. case booleanValue:
  926. value_ = other.value_;
  927. break;
  928. case stringValue:
  929. if (other.value_.string_ && other.isAllocated()) {
  930. unsigned len;
  931. char const* str;
  932. decodePrefixedString(other.isAllocated(), other.value_.string_, &len,
  933. &str);
  934. value_.string_ = duplicateAndPrefixStringValue(str, len);
  935. setIsAllocated(true);
  936. } else {
  937. value_.string_ = other.value_.string_;
  938. }
  939. break;
  940. case arrayValue:
  941. case objectValue:
  942. value_.map_ = new ObjectValues(*other.value_.map_);
  943. break;
  944. default:
  945. JSON_ASSERT_UNREACHABLE;
  946. }
  947. }
  948. void Value::releasePayload() {
  949. switch (type()) {
  950. case nullValue:
  951. case intValue:
  952. case uintValue:
  953. case realValue:
  954. case booleanValue:
  955. break;
  956. case stringValue:
  957. if (isAllocated())
  958. releasePrefixedStringValue(value_.string_);
  959. break;
  960. case arrayValue:
  961. case objectValue:
  962. delete value_.map_;
  963. break;
  964. default:
  965. JSON_ASSERT_UNREACHABLE;
  966. }
  967. }
  968. void Value::dupMeta(const Value& other) {
  969. comments_ = other.comments_;
  970. start_ = other.start_;
  971. limit_ = other.limit_;
  972. }
  973. // Access an object value by name, create a null member if it does not exist.
  974. // @pre Type of '*this' is object or null.
  975. // @param key is null-terminated.
  976. Value& Value::resolveReference(const char* key) {
  977. JSON_ASSERT_MESSAGE(
  978. type() == nullValue || type() == objectValue,
  979. "in Json::Value::resolveReference(): requires objectValue");
  980. if (type() == nullValue)
  981. *this = Value(objectValue);
  982. CZString actualKey(key, static_cast<unsigned>(strlen(key)),
  983. CZString::noDuplication); // NOTE!
  984. auto it = value_.map_->lower_bound(actualKey);
  985. if (it != value_.map_->end() && (*it).first == actualKey)
  986. return (*it).second;
  987. ObjectValues::value_type defaultValue(actualKey, nullSingleton());
  988. it = value_.map_->insert(it, defaultValue);
  989. Value& value = (*it).second;
  990. return value;
  991. }
  992. // @param key is not null-terminated.
  993. Value& Value::resolveReference(char const* key, char const* end) {
  994. JSON_ASSERT_MESSAGE(
  995. type() == nullValue || type() == objectValue,
  996. "in Json::Value::resolveReference(key, end): requires objectValue");
  997. if (type() == nullValue)
  998. *this = Value(objectValue);
  999. CZString actualKey(key, static_cast<unsigned>(end - key),
  1000. CZString::duplicateOnCopy);
  1001. auto it = value_.map_->lower_bound(actualKey);
  1002. if (it != value_.map_->end() && (*it).first == actualKey)
  1003. return (*it).second;
  1004. ObjectValues::value_type defaultValue(actualKey, nullSingleton());
  1005. it = value_.map_->insert(it, defaultValue);
  1006. Value& value = (*it).second;
  1007. return value;
  1008. }
  1009. Value Value::get(ArrayIndex index, const Value& defaultValue) const {
  1010. const Value* value = &((*this)[index]);
  1011. return value == &nullSingleton() ? defaultValue : *value;
  1012. }
  1013. bool Value::isValidIndex(ArrayIndex index) const { return index < size(); }
  1014. Value const* Value::find(char const* begin, char const* end) const {
  1015. JSON_ASSERT_MESSAGE(type() == nullValue || type() == objectValue,
  1016. "in Json::Value::find(begin, end): requires "
  1017. "objectValue or nullValue");
  1018. if (type() == nullValue)
  1019. return nullptr;
  1020. CZString actualKey(begin, static_cast<unsigned>(end - begin),
  1021. CZString::noDuplication);
  1022. ObjectValues::const_iterator it = value_.map_->find(actualKey);
  1023. if (it == value_.map_->end())
  1024. return nullptr;
  1025. return &(*it).second;
  1026. }
  1027. Value* Value::demand(char const* begin, char const* end) {
  1028. JSON_ASSERT_MESSAGE(type() == nullValue || type() == objectValue,
  1029. "in Json::Value::demand(begin, end): requires "
  1030. "objectValue or nullValue");
  1031. return &resolveReference(begin, end);
  1032. }
  1033. const Value& Value::operator[](const char* key) const {
  1034. Value const* found = find(key, key + strlen(key));
  1035. if (!found)
  1036. return nullSingleton();
  1037. return *found;
  1038. }
  1039. Value const& Value::operator[](const String& key) const {
  1040. Value const* found = find(key.data(), key.data() + key.length());
  1041. if (!found)
  1042. return nullSingleton();
  1043. return *found;
  1044. }
  1045. Value& Value::operator[](const char* key) {
  1046. return resolveReference(key, key + strlen(key));
  1047. }
  1048. Value& Value::operator[](const String& key) {
  1049. return resolveReference(key.data(), key.data() + key.length());
  1050. }
  1051. Value& Value::operator[](const StaticString& key) {
  1052. return resolveReference(key.c_str());
  1053. }
  1054. #ifdef JSON_USE_CPPTL
  1055. Value& Value::operator[](const CppTL::ConstString& key) {
  1056. return resolveReference(key.c_str(), key.end_c_str());
  1057. }
  1058. Value const& Value::operator[](CppTL::ConstString const& key) const {
  1059. Value const* found = find(key.c_str(), key.end_c_str());
  1060. if (!found)
  1061. return nullSingleton();
  1062. return *found;
  1063. }
  1064. #endif
  1065. Value& Value::append(const Value& value) { return (*this)[size()] = value; }
  1066. Value& Value::append(Value&& value) {
  1067. return (*this)[size()] = std::move(value);
  1068. }
  1069. Value Value::get(char const* begin,
  1070. char const* end,
  1071. Value const& defaultValue) const {
  1072. Value const* found = find(begin, end);
  1073. return !found ? defaultValue : *found;
  1074. }
  1075. Value Value::get(char const* key, Value const& defaultValue) const {
  1076. return get(key, key + strlen(key), defaultValue);
  1077. }
  1078. Value Value::get(String const& key, Value const& defaultValue) const {
  1079. return get(key.data(), key.data() + key.length(), defaultValue);
  1080. }
  1081. bool Value::removeMember(const char* begin, const char* end, Value* removed) {
  1082. if (type() != objectValue) {
  1083. return false;
  1084. }
  1085. CZString actualKey(begin, static_cast<unsigned>(end - begin),
  1086. CZString::noDuplication);
  1087. auto it = value_.map_->find(actualKey);
  1088. if (it == value_.map_->end())
  1089. return false;
  1090. if (removed)
  1091. *removed = std::move(it->second);
  1092. value_.map_->erase(it);
  1093. return true;
  1094. }
  1095. bool Value::removeMember(const char* key, Value* removed) {
  1096. return removeMember(key, key + strlen(key), removed);
  1097. }
  1098. bool Value::removeMember(String const& key, Value* removed) {
  1099. return removeMember(key.data(), key.data() + key.length(), removed);
  1100. }
  1101. void Value::removeMember(const char* key) {
  1102. JSON_ASSERT_MESSAGE(type() == nullValue || type() == objectValue,
  1103. "in Json::Value::removeMember(): requires objectValue");
  1104. if (type() == nullValue)
  1105. return;
  1106. CZString actualKey(key, unsigned(strlen(key)), CZString::noDuplication);
  1107. value_.map_->erase(actualKey);
  1108. }
  1109. void Value::removeMember(const String& key) { removeMember(key.c_str()); }
  1110. bool Value::removeIndex(ArrayIndex index, Value* removed) {
  1111. if (type() != arrayValue) {
  1112. return false;
  1113. }
  1114. CZString key(index);
  1115. auto it = value_.map_->find(key);
  1116. if (it == value_.map_->end()) {
  1117. return false;
  1118. }
  1119. if (removed)
  1120. *removed = it->second;
  1121. ArrayIndex oldSize = size();
  1122. // shift left all items left, into the place of the "removed"
  1123. for (ArrayIndex i = index; i < (oldSize - 1); ++i) {
  1124. CZString keey(i);
  1125. (*value_.map_)[keey] = (*this)[i + 1];
  1126. }
  1127. // erase the last one ("leftover")
  1128. CZString keyLast(oldSize - 1);
  1129. auto itLast = value_.map_->find(keyLast);
  1130. value_.map_->erase(itLast);
  1131. return true;
  1132. }
  1133. #ifdef JSON_USE_CPPTL
  1134. Value Value::get(const CppTL::ConstString& key,
  1135. const Value& defaultValue) const {
  1136. return get(key.c_str(), key.end_c_str(), defaultValue);
  1137. }
  1138. #endif
  1139. bool Value::isMember(char const* begin, char const* end) const {
  1140. Value const* value = find(begin, end);
  1141. return nullptr != value;
  1142. }
  1143. bool Value::isMember(char const* key) const {
  1144. return isMember(key, key + strlen(key));
  1145. }
  1146. bool Value::isMember(String const& key) const {
  1147. return isMember(key.data(), key.data() + key.length());
  1148. }
  1149. #ifdef JSON_USE_CPPTL
  1150. bool Value::isMember(const CppTL::ConstString& key) const {
  1151. return isMember(key.c_str(), key.end_c_str());
  1152. }
  1153. #endif
  1154. Value::Members Value::getMemberNames() const {
  1155. JSON_ASSERT_MESSAGE(
  1156. type() == nullValue || type() == objectValue,
  1157. "in Json::Value::getMemberNames(), value must be objectValue");
  1158. if (type() == nullValue)
  1159. return Value::Members();
  1160. Members members;
  1161. members.reserve(value_.map_->size());
  1162. ObjectValues::const_iterator it = value_.map_->begin();
  1163. ObjectValues::const_iterator itEnd = value_.map_->end();
  1164. for (; it != itEnd; ++it) {
  1165. members.push_back(String((*it).first.data(), (*it).first.length()));
  1166. }
  1167. return members;
  1168. }
  1169. //
  1170. //# ifdef JSON_USE_CPPTL
  1171. // EnumMemberNames
  1172. // Value::enumMemberNames() const
  1173. //{
  1174. // if ( type() == objectValue )
  1175. // {
  1176. // return CppTL::Enum::any( CppTL::Enum::transform(
  1177. // CppTL::Enum::keys( *(value_.map_), CppTL::Type<const CZString &>() ),
  1178. // MemberNamesTransform() ) );
  1179. // }
  1180. // return EnumMemberNames();
  1181. //}
  1182. //
  1183. //
  1184. // EnumValues
  1185. // Value::enumValues() const
  1186. //{
  1187. // if ( type() == objectValue || type() == arrayValue )
  1188. // return CppTL::Enum::anyValues( *(value_.map_),
  1189. // CppTL::Type<const Value &>() );
  1190. // return EnumValues();
  1191. //}
  1192. //
  1193. //# endif
  1194. static bool IsIntegral(double d) {
  1195. double integral_part;
  1196. return modf(d, &integral_part) == 0.0;
  1197. }
  1198. bool Value::isNull() const { return type() == nullValue; }
  1199. bool Value::isBool() const { return type() == booleanValue; }
  1200. bool Value::isInt() const {
  1201. switch (type()) {
  1202. case intValue:
  1203. #if defined(JSON_HAS_INT64)
  1204. return value_.int_ >= minInt && value_.int_ <= maxInt;
  1205. #else
  1206. return true;
  1207. #endif
  1208. case uintValue:
  1209. return value_.uint_ <= UInt(maxInt);
  1210. case realValue:
  1211. return value_.real_ >= minInt && value_.real_ <= maxInt &&
  1212. IsIntegral(value_.real_);
  1213. default:
  1214. break;
  1215. }
  1216. return false;
  1217. }
  1218. bool Value::isUInt() const {
  1219. switch (type()) {
  1220. case intValue:
  1221. #if defined(JSON_HAS_INT64)
  1222. return value_.int_ >= 0 && LargestUInt(value_.int_) <= LargestUInt(maxUInt);
  1223. #else
  1224. return value_.int_ >= 0;
  1225. #endif
  1226. case uintValue:
  1227. #if defined(JSON_HAS_INT64)
  1228. return value_.uint_ <= maxUInt;
  1229. #else
  1230. return true;
  1231. #endif
  1232. case realValue:
  1233. return value_.real_ >= 0 && value_.real_ <= maxUInt &&
  1234. IsIntegral(value_.real_);
  1235. default:
  1236. break;
  1237. }
  1238. return false;
  1239. }
  1240. bool Value::isInt64() const {
  1241. #if defined(JSON_HAS_INT64)
  1242. switch (type()) {
  1243. case intValue:
  1244. return true;
  1245. case uintValue:
  1246. return value_.uint_ <= UInt64(maxInt64);
  1247. case realValue:
  1248. // Note that maxInt64 (= 2^63 - 1) is not exactly representable as a
  1249. // double, so double(maxInt64) will be rounded up to 2^63. Therefore we
  1250. // require the value to be strictly less than the limit.
  1251. return value_.real_ >= double(minInt64) &&
  1252. value_.real_ < double(maxInt64) && IsIntegral(value_.real_);
  1253. default:
  1254. break;
  1255. }
  1256. #endif // JSON_HAS_INT64
  1257. return false;
  1258. }
  1259. bool Value::isUInt64() const {
  1260. #if defined(JSON_HAS_INT64)
  1261. switch (type()) {
  1262. case intValue:
  1263. return value_.int_ >= 0;
  1264. case uintValue:
  1265. return true;
  1266. case realValue:
  1267. // Note that maxUInt64 (= 2^64 - 1) is not exactly representable as a
  1268. // double, so double(maxUInt64) will be rounded up to 2^64. Therefore we
  1269. // require the value to be strictly less than the limit.
  1270. return value_.real_ >= 0 && value_.real_ < maxUInt64AsDouble &&
  1271. IsIntegral(value_.real_);
  1272. default:
  1273. break;
  1274. }
  1275. #endif // JSON_HAS_INT64
  1276. return false;
  1277. }
  1278. bool Value::isIntegral() const {
  1279. switch (type()) {
  1280. case intValue:
  1281. case uintValue:
  1282. return true;
  1283. case realValue:
  1284. #if defined(JSON_HAS_INT64)
  1285. // Note that maxUInt64 (= 2^64 - 1) is not exactly representable as a
  1286. // double, so double(maxUInt64) will be rounded up to 2^64. Therefore we
  1287. // require the value to be strictly less than the limit.
  1288. return value_.real_ >= double(minInt64) &&
  1289. value_.real_ < maxUInt64AsDouble && IsIntegral(value_.real_);
  1290. #else
  1291. return value_.real_ >= minInt && value_.real_ <= maxUInt &&
  1292. IsIntegral(value_.real_);
  1293. #endif // JSON_HAS_INT64
  1294. default:
  1295. break;
  1296. }
  1297. return false;
  1298. }
  1299. bool Value::isDouble() const {
  1300. return type() == intValue || type() == uintValue || type() == realValue;
  1301. }
  1302. bool Value::isNumeric() const { return isDouble(); }
  1303. bool Value::isString() const { return type() == stringValue; }
  1304. bool Value::isArray() const { return type() == arrayValue; }
  1305. bool Value::isObject() const { return type() == objectValue; }
  1306. Value::Comments::Comments(const Comments& that)
  1307. : ptr_{cloneUnique(that.ptr_)} {}
  1308. Value::Comments::Comments(Comments&& that)
  1309. : ptr_{std::move(that.ptr_)} {}
  1310. Value::Comments& Value::Comments::operator=(const Comments& that) {
  1311. ptr_ = cloneUnique(that.ptr_);
  1312. return *this;
  1313. }
  1314. Value::Comments& Value::Comments::operator=(Comments&& that) {
  1315. ptr_ = std::move(that.ptr_);
  1316. return *this;
  1317. }
  1318. bool Value::Comments::has(CommentPlacement slot) const {
  1319. return ptr_ && !(*ptr_)[slot].empty();
  1320. }
  1321. String Value::Comments::get(CommentPlacement slot) const {
  1322. if (!ptr_)
  1323. return {};
  1324. return (*ptr_)[slot];
  1325. }
  1326. void Value::Comments::set(CommentPlacement slot, String comment) {
  1327. if (!ptr_) {
  1328. ptr_ = std::unique_ptr<Array>(new Array());
  1329. }
  1330. (*ptr_)[slot] = std::move(comment);
  1331. }
  1332. void Value::setComment(String comment, CommentPlacement placement) {
  1333. if (!comment.empty() && (comment.back() == '\n')) {
  1334. // Always discard trailing newline, to aid indentation.
  1335. comment.pop_back();
  1336. }
  1337. JSON_ASSERT(!comment.empty());
  1338. JSON_ASSERT_MESSAGE(
  1339. comment[0] == '\0' || comment[0] == '/',
  1340. "in Json::Value::setComment(): Comments must start with /");
  1341. comments_.set(placement, std::move(comment));
  1342. }
  1343. bool Value::hasComment(CommentPlacement placement) const {
  1344. return comments_.has(placement);
  1345. }
  1346. String Value::getComment(CommentPlacement placement) const {
  1347. return comments_.get(placement);
  1348. }
  1349. void Value::setOffsetStart(ptrdiff_t start) { start_ = start; }
  1350. void Value::setOffsetLimit(ptrdiff_t limit) { limit_ = limit; }
  1351. ptrdiff_t Value::getOffsetStart() const { return start_; }
  1352. ptrdiff_t Value::getOffsetLimit() const { return limit_; }
  1353. String Value::toStyledString() const {
  1354. StreamWriterBuilder builder;
  1355. String out = this->hasComment(commentBefore) ? "\n" : "";
  1356. out += Json::writeString(builder, *this);
  1357. out += '\n';
  1358. return out;
  1359. }
  1360. Value::const_iterator Value::begin() const {
  1361. switch (type()) {
  1362. case arrayValue:
  1363. case objectValue:
  1364. if (value_.map_)
  1365. return const_iterator(value_.map_->begin());
  1366. break;
  1367. default:
  1368. break;
  1369. }
  1370. return {};
  1371. }
  1372. Value::const_iterator Value::end() const {
  1373. switch (type()) {
  1374. case arrayValue:
  1375. case objectValue:
  1376. if (value_.map_)
  1377. return const_iterator(value_.map_->end());
  1378. break;
  1379. default:
  1380. break;
  1381. }
  1382. return {};
  1383. }
  1384. Value::iterator Value::begin() {
  1385. switch (type()) {
  1386. case arrayValue:
  1387. case objectValue:
  1388. if (value_.map_)
  1389. return iterator(value_.map_->begin());
  1390. break;
  1391. default:
  1392. break;
  1393. }
  1394. return iterator();
  1395. }
  1396. Value::iterator Value::end() {
  1397. switch (type()) {
  1398. case arrayValue:
  1399. case objectValue:
  1400. if (value_.map_)
  1401. return iterator(value_.map_->end());
  1402. break;
  1403. default:
  1404. break;
  1405. }
  1406. return iterator();
  1407. }
  1408. // class PathArgument
  1409. // //////////////////////////////////////////////////////////////////
  1410. PathArgument::PathArgument() : key_() {}
  1411. PathArgument::PathArgument(ArrayIndex index)
  1412. : key_(), index_(index), kind_(kindIndex) {}
  1413. PathArgument::PathArgument(const char* key)
  1414. : key_(key), index_(), kind_(kindKey) {}
  1415. PathArgument::PathArgument(const String& key)
  1416. : key_(key.c_str()), index_(), kind_(kindKey) {}
  1417. // class Path
  1418. // //////////////////////////////////////////////////////////////////
  1419. Path::Path(const String& path,
  1420. const PathArgument& a1,
  1421. const PathArgument& a2,
  1422. const PathArgument& a3,
  1423. const PathArgument& a4,
  1424. const PathArgument& a5) {
  1425. InArgs in;
  1426. in.reserve(5);
  1427. in.push_back(&a1);
  1428. in.push_back(&a2);
  1429. in.push_back(&a3);
  1430. in.push_back(&a4);
  1431. in.push_back(&a5);
  1432. makePath(path, in);
  1433. }
  1434. void Path::makePath(const String& path, const InArgs& in) {
  1435. const char* current = path.c_str();
  1436. const char* end = current + path.length();
  1437. auto itInArg = in.begin();
  1438. while (current != end) {
  1439. if (*current == '[') {
  1440. ++current;
  1441. if (*current == '%')
  1442. addPathInArg(path, in, itInArg, PathArgument::kindIndex);
  1443. else {
  1444. ArrayIndex index = 0;
  1445. for (; current != end && *current >= '0' && *current <= '9'; ++current)
  1446. index = index * 10 + ArrayIndex(*current - '0');
  1447. args_.push_back(index);
  1448. }
  1449. if (current == end || *++current != ']')
  1450. invalidPath(path, int(current - path.c_str()));
  1451. } else if (*current == '%') {
  1452. addPathInArg(path, in, itInArg, PathArgument::kindKey);
  1453. ++current;
  1454. } else if (*current == '.' || *current == ']') {
  1455. ++current;
  1456. } else {
  1457. const char* beginName = current;
  1458. while (current != end && !strchr("[.", *current))
  1459. ++current;
  1460. args_.push_back(String(beginName, current));
  1461. }
  1462. }
  1463. }
  1464. void Path::addPathInArg(const String& /*path*/,
  1465. const InArgs& in,
  1466. InArgs::const_iterator& itInArg,
  1467. PathArgument::Kind kind) {
  1468. if (itInArg == in.end()) {
  1469. // Error: missing argument %d
  1470. } else if ((*itInArg)->kind_ != kind) {
  1471. // Error: bad argument type
  1472. } else {
  1473. args_.push_back(**itInArg++);
  1474. }
  1475. }
  1476. void Path::invalidPath(const String& /*path*/, int /*location*/) {
  1477. // Error: invalid path.
  1478. }
  1479. const Value& Path::resolve(const Value& root) const {
  1480. const Value* node = &root;
  1481. for (const auto& arg : args_) {
  1482. if (arg.kind_ == PathArgument::kindIndex) {
  1483. if (!node->isArray() || !node->isValidIndex(arg.index_)) {
  1484. // Error: unable to resolve path (array value expected at position...
  1485. return Value::null;
  1486. }
  1487. node = &((*node)[arg.index_]);
  1488. } else if (arg.kind_ == PathArgument::kindKey) {
  1489. if (!node->isObject()) {
  1490. // Error: unable to resolve path (object value expected at position...)
  1491. return Value::null;
  1492. }
  1493. node = &((*node)[arg.key_]);
  1494. if (node == &Value::nullSingleton()) {
  1495. // Error: unable to resolve path (object has no member named '' at
  1496. // position...)
  1497. return Value::null;
  1498. }
  1499. }
  1500. }
  1501. return *node;
  1502. }
  1503. Value Path::resolve(const Value& root, const Value& defaultValue) const {
  1504. const Value* node = &root;
  1505. for (const auto& arg : args_) {
  1506. if (arg.kind_ == PathArgument::kindIndex) {
  1507. if (!node->isArray() || !node->isValidIndex(arg.index_))
  1508. return defaultValue;
  1509. node = &((*node)[arg.index_]);
  1510. } else if (arg.kind_ == PathArgument::kindKey) {
  1511. if (!node->isObject())
  1512. return defaultValue;
  1513. node = &((*node)[arg.key_]);
  1514. if (node == &Value::nullSingleton())
  1515. return defaultValue;
  1516. }
  1517. }
  1518. return *node;
  1519. }
  1520. Value& Path::make(Value& root) const {
  1521. Value* node = &root;
  1522. for (const auto& arg : args_) {
  1523. if (arg.kind_ == PathArgument::kindIndex) {
  1524. if (!node->isArray()) {
  1525. // Error: node is not an array at position ...
  1526. }
  1527. node = &((*node)[arg.index_]);
  1528. } else if (arg.kind_ == PathArgument::kindKey) {
  1529. if (!node->isObject()) {
  1530. // Error: node is not an object at position...
  1531. }
  1532. node = &((*node)[arg.key_]);
  1533. }
  1534. }
  1535. return *node;
  1536. }
  1537. } // namespace Json