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.

1958 lines
48 KiB

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