6 #if !defined(JSON_IS_AMALGAMATION)
10 #endif // if !defined(JSON_IS_AMALGAMATION)
21 #if defined(_MSC_VER) && _MSC_VER < 1900
24 const char* format, va_list ap) {
27 count = _vsnprintf_s(outBuf, size, _TRUNCATE, format, ap);
29 count = _vscprintf(format, ap);
34 const char* format, ...) {
45 #pragma warning(disable : 4702)
48 #define JSON_ASSERT_UNREACHABLE assert(false)
52 static std::unique_ptr<T>
cloneUnique(
const std::unique_ptr<T>& p) {
55 r = std::unique_ptr<T>(
new T(*p));
63 #if defined(__ARMEL__)
64 #define ALIGNAS(byte_alignment) __attribute__((aligned(byte_alignment)))
66 #define ALIGNAS(byte_alignment)
71 static Value const nullStatic;
85 #if !defined(JSON_USE_INT64_DOUBLE_CONVERSION)
86 template <
typename T,
typename U>
87 static inline bool InRange(
double d, T min, U max) {
90 return d >=
static_cast<double>(min) && d <=
static_cast<double>(max);
92 #else // if !defined(JSON_USE_INT64_DOUBLE_CONVERSION)
93 static inline double integerToDouble(
Json::UInt64 value) {
94 return static_cast<double>(
Int64(value / 2)) * 2.0 +
95 static_cast<double>(
Int64(value & 1));
98 template <
typename T>
static inline double integerToDouble(T value) {
99 return static_cast<double>(value);
102 template <
typename T,
typename U>
103 static inline bool InRange(
double d, T min, U max) {
104 return d >= integerToDouble(min) && d <= integerToDouble(max);
106 #endif // if !defined(JSON_USE_INT64_DOUBLE_CONVERSION)
121 auto newString =
static_cast<char*
>(malloc(length + 1));
122 if (newString ==
nullptr) {
123 throwRuntimeError(
"in Json::Value::duplicateStringValue(): "
124 "Failed to allocate string value buffer");
126 memcpy(newString, value, length);
127 newString[length] = 0;
134 unsigned int length) {
138 sizeof(
unsigned) - 1U,
139 "in Json::Value::duplicateAndPrefixStringValue(): "
140 "length too big for prefixing");
141 size_t actualLength =
sizeof(length) + length + 1;
142 auto newString =
static_cast<char*
>(malloc(actualLength));
143 if (newString ==
nullptr) {
144 throwRuntimeError(
"in Json::Value::duplicateAndPrefixStringValue(): "
145 "Failed to allocate string value buffer");
147 *
reinterpret_cast<unsigned*
>(newString) = length;
148 memcpy(newString +
sizeof(
unsigned), value, length);
149 newString[actualLength - 1U] =
154 unsigned* length,
char const** value) {
156 *length =
static_cast<unsigned>(strlen(prefixed));
159 *length = *
reinterpret_cast<unsigned const*
>(prefixed);
160 *value = prefixed +
sizeof(unsigned);
166 #if JSONCPP_USING_SECURE_MEMORY
169 char const* valueDecoded;
171 size_t const size =
sizeof(unsigned) + length + 1U;
172 memset(value, 0, size);
177 size_t size = (length == 0) ? strlen(value) : length;
178 memset(value, 0, size);
181 #else // !JSONCPP_USING_SECURE_MEMORY
184 #endif // JSONCPP_USING_SECURE_MEMORY
195 #if !defined(JSON_IS_AMALGAMATION)
198 #endif // if !defined(JSON_IS_AMALGAMATION)
202 #if JSON_USE_EXCEPTION
205 char const*
Exception::what() const noexcept {
return msg_.c_str(); }
212 throw LogicError(msg);
214 #else // !JSON_USE_EXCEPTION
216 std::cerr << msg << std::endl;
220 std::cerr << msg << std::endl;
236 Value::CZString::CZString(
ArrayIndex index) : cstr_(nullptr), index_(index) {}
238 Value::CZString::CZString(
char const* str,
unsigned length,
239 DuplicationPolicy allocate)
242 storage_.policy_ = allocate & 0x3;
243 storage_.length_ = length & 0x3FFFFFFF;
246 Value::CZString::CZString(
const CZString& other) {
247 cstr_ = (other.storage_.policy_ != noDuplication && other.cstr_ !=
nullptr
251 static_cast<unsigned>(
253 ? (
static_cast<DuplicationPolicy
>(other.storage_.policy_) ==
257 :
static_cast<DuplicationPolicy
>(other.storage_.policy_)) &
259 storage_.length_ = other.storage_.length_;
262 Value::CZString::CZString(CZString&& other)
263 : cstr_(other.cstr_), index_(other.index_) {
264 other.cstr_ =
nullptr;
267 Value::CZString::~CZString() {
268 if (cstr_ && storage_.policy_ == duplicate) {
270 storage_.length_ + 1U);
282 Value::CZString& Value::CZString::operator=(
const CZString& other) {
284 index_ = other.index_;
288 Value::CZString& Value::CZString::operator=(CZString&& other) {
290 index_ = other.index_;
291 other.cstr_ =
nullptr;
295 bool Value::CZString::operator<(
const CZString& other)
const {
297 return index_ < other.index_;
300 unsigned this_len = this->storage_.length_;
301 unsigned other_len = other.storage_.length_;
302 unsigned min_len = std::min<unsigned>(this_len, other_len);
304 int comp = memcmp(this->cstr_, other.cstr_, min_len);
309 return (this_len < other_len);
314 return index_ == other.index_;
317 unsigned this_len = this->storage_.length_;
318 unsigned other_len = other.storage_.length_;
319 if (this_len != other_len)
322 int comp = memcmp(this->cstr_, other.cstr_, this_len);
326 ArrayIndex Value::CZString::index()
const {
return index_; }
329 const char* Value::CZString::data()
const {
return cstr_; }
330 unsigned Value::CZString::length()
const {
return storage_.length_; }
331 bool Value::CZString::isStaticString()
const {
332 return storage_.policy_ == noDuplication;
348 static char const emptyString[] =
"";
362 value_.string_ =
const_cast<char*
>(
static_cast<char const*
>(emptyString));
366 value_.map_ =
new ObjectValues();
369 value_.bool_ =
false;
383 value_.uint_ = value;
385 #if defined(JSON_HAS_INT64)
392 value_.uint_ = value;
394 #endif // defined(JSON_HAS_INT64)
396 Value::Value(
double value) {
398 value_.real_ = value;
401 Value::Value(
const char* value) {
404 "Null Value Passed to Value Constructor");
406 value,
static_cast<unsigned>(strlen(value)));
409 Value::Value(
const char* begin,
const char* end) {
418 value.data(),
static_cast<unsigned>(value.length()));
423 value_.string_ =
const_cast<char*
>(value.
c_str());
426 Value::Value(
bool value) {
428 value_.bool_ = value;
456 void Value::swapPayload(
Value& other) {
461 void Value::copyPayload(
const Value& other) {
473 void Value::copy(
const Value& other) {
479 return static_cast<ValueType>(bits_.value_type_);
482 int Value::compare(
const Value& other)
const {
490 bool Value::operator<(
const Value& other)
const {
491 int typeDelta = type() - other.
type();
493 return typeDelta < 0;
498 return value_.int_ < other.value_.int_;
500 return value_.uint_ < other.value_.uint_;
502 return value_.real_ < other.value_.real_;
504 return value_.bool_ < other.value_.bool_;
506 if ((value_.string_ ==
nullptr) || (other.value_.string_ ==
nullptr)) {
507 return other.value_.string_ !=
nullptr;
511 char const* this_str;
512 char const* other_str;
517 unsigned min_len = std::min<unsigned>(this_len, other_len);
519 int comp = memcmp(this_str, other_str, min_len);
524 return (this_len < other_len);
528 auto thisSize = value_.map_->size();
529 auto otherSize = other.value_.map_->size();
530 if (thisSize != otherSize)
531 return thisSize < otherSize;
532 return (*value_.map_) < (*other.value_.map_);
540 bool Value::operator<=(
const Value& other)
const {
return !(other < *
this); }
542 bool Value::operator>=(
const Value& other)
const {
return !(*
this < other); }
544 bool Value::operator>(
const Value& other)
const {
return other < *
this; }
547 if (type() != other.
type())
553 return value_.int_ == other.value_.int_;
555 return value_.uint_ == other.value_.uint_;
557 return value_.real_ == other.value_.real_;
559 return value_.bool_ == other.value_.bool_;
561 if ((value_.string_ ==
nullptr) || (other.value_.string_ ==
nullptr)) {
562 return (value_.string_ == other.value_.string_);
566 char const* this_str;
567 char const* other_str;
572 if (this_len != other_len)
575 int comp = memcmp(this_str, other_str, this_len);
580 return value_.map_->size() == other.value_.map_->size() &&
581 (*value_.map_) == (*other.value_.map_);
590 const char* Value::asCString()
const {
592 "in Json::Value::asCString(): requires stringValue");
593 if (value_.string_ ==
nullptr)
596 char const* this_str;
602 #if JSONCPP_USING_SECURE_MEMORY
603 unsigned Value::getCStringLength()
const {
605 "in Json::Value::asCString(): requires stringValue");
606 if (value_.string_ == 0)
609 char const* this_str;
616 bool Value::getString(
char const** begin,
char const** end)
const {
619 if (value_.string_ ==
nullptr)
624 *end = *begin + length;
633 if (value_.string_ ==
nullptr)
636 char const* this_str;
639 return String(this_str, this_len);
642 return value_.bool_ ?
"true" :
"false";
658 return Int(value_.int_);
661 return Int(value_.uint_);
664 "double out of Int range");
665 return Int(value_.real_);
669 return value_.bool_ ? 1 : 0;
680 return UInt(value_.int_);
683 return UInt(value_.uint_);
686 "double out of UInt range");
687 return UInt(value_.real_);
691 return value_.bool_ ? 1 : 0;
698 #if defined(JSON_HAS_INT64)
703 return Int64(value_.int_);
706 return Int64(value_.uint_);
709 "double out of Int64 range");
710 return Int64(value_.real_);
714 return value_.bool_ ? 1 : 0;
725 return UInt64(value_.int_);
727 return UInt64(value_.uint_);
730 "double out of UInt64 range");
731 return UInt64(value_.real_);
735 return value_.bool_ ? 1 : 0;
741 #endif // if defined(JSON_HAS_INT64)
744 #if defined(JSON_NO_INT64)
752 #if defined(JSON_NO_INT64)
759 double Value::asDouble()
const {
762 return static_cast<double>(value_.int_);
764 #if !defined(JSON_USE_INT64_DOUBLE_CONVERSION)
765 return static_cast<double>(value_.uint_);
766 #else // if !defined(JSON_USE_INT64_DOUBLE_CONVERSION)
767 return integerToDouble(value_.uint_);
768 #endif // if !defined(JSON_USE_INT64_DOUBLE_CONVERSION)
774 return value_.bool_ ? 1.0 : 0.0;
781 float Value::asFloat()
const {
784 return static_cast<float>(value_.int_);
786 #if !defined(JSON_USE_INT64_DOUBLE_CONVERSION)
787 return static_cast<float>(value_.uint_);
788 #else // if !defined(JSON_USE_INT64_DOUBLE_CONVERSION)
790 return static_cast<float>(integerToDouble(value_.uint_));
791 #endif // if !defined(JSON_USE_INT64_DOUBLE_CONVERSION)
793 return static_cast<float>(value_.real_);
797 return value_.bool_ ? 1.0F : 0.0F;
804 bool Value::asBool()
const {
811 return value_.int_ != 0;
813 return value_.uint_ != 0;
816 const auto value_classification = std::fpclassify(value_.real_);
817 return value_classification != FP_ZERO && value_classification != FP_NAN;
828 return (isNumeric() && asDouble() == 0.0) ||
831 (type() ==
arrayValue && value_.map_->empty()) ||
869 if (!value_.map_->empty()) {
870 ObjectValues::const_iterator itLast = value_.map_->end();
872 return (*itLast).first.index() + 1;
882 bool Value::empty()
const {
883 if (isNull() || isArray() || isObject())
888 Value::operator bool()
const {
return !isNull(); }
890 void Value::clear() {
893 "in Json::Value::clear(): requires complex value");
899 value_.map_->clear();
908 "in Json::Value::resize(): requires arrayValue");
914 else if (newSize > oldSize)
915 this->operator[](newSize - 1);
917 for (
ArrayIndex index = newSize; index < oldSize; ++index) {
918 value_.map_->erase(index);
927 "in Json::Value::operator[](ArrayIndex): requires arrayValue");
931 auto it = value_.map_->lower_bound(key);
932 if (it != value_.map_->end() && (*it).first == key)
935 ObjectValues::value_type defaultValue(key, nullSingleton());
936 it = value_.map_->insert(it, defaultValue);
940 Value& Value::operator[](
int index) {
943 "in Json::Value::operator[](int index): index cannot be negative");
950 "in Json::Value::operator[](ArrayIndex)const: requires arrayValue");
952 return nullSingleton();
954 ObjectValues::const_iterator it = value_.map_->find(key);
955 if (it == value_.map_->end())
956 return nullSingleton();
960 const Value& Value::operator[](
int index)
const {
963 "in Json::Value::operator[](int index) const: index cannot be negative");
967 void Value::initBasic(
ValueType type,
bool allocated) {
969 setIsAllocated(allocated);
970 comments_ = Comments{};
975 void Value::dupPayload(
const Value& other) {
976 setType(other.type());
977 setIsAllocated(
false);
984 value_ = other.value_;
987 if (other.value_.string_ && other.isAllocated()) {
993 setIsAllocated(
true);
995 value_.string_ = other.value_.string_;
1000 value_.map_ =
new ObjectValues(*other.value_.map_);
1007 void Value::releasePayload() {
1028 void Value::dupMeta(
const Value& other) {
1029 comments_ = other.comments_;
1030 start_ = other.start_;
1031 limit_ = other.limit_;
1037 Value& Value::resolveReference(
const char* key) {
1040 "in Json::Value::resolveReference(): requires objectValue");
1043 CZString actualKey(key,
static_cast<unsigned>(strlen(key)),
1044 CZString::noDuplication);
1045 auto it = value_.map_->lower_bound(actualKey);
1046 if (it != value_.map_->end() && (*it).first == actualKey)
1047 return (*it).second;
1049 ObjectValues::value_type defaultValue(actualKey, nullSingleton());
1050 it = value_.map_->insert(it, defaultValue);
1051 Value& value = (*it).second;
1056 Value& Value::resolveReference(
char const* key,
char const* end) {
1059 "in Json::Value::resolveReference(key, end): requires objectValue");
1062 CZString actualKey(key,
static_cast<unsigned>(end - key),
1063 CZString::duplicateOnCopy);
1064 auto it = value_.map_->lower_bound(actualKey);
1065 if (it != value_.map_->end() && (*it).first == actualKey)
1066 return (*it).second;
1068 ObjectValues::value_type defaultValue(actualKey, nullSingleton());
1069 it = value_.map_->insert(it, defaultValue);
1070 Value& value = (*it).second;
1075 const Value* value = &((*this)[index]);
1076 return value == &nullSingleton() ? defaultValue : *value;
1079 bool Value::isValidIndex(
ArrayIndex index)
const {
return index < size(); }
1081 Value const* Value::find(
char const* begin,
char const* end)
const {
1083 "in Json::Value::find(begin, end): requires "
1084 "objectValue or nullValue");
1087 CZString actualKey(begin,
static_cast<unsigned>(end - begin),
1088 CZString::noDuplication);
1089 ObjectValues::const_iterator it = value_.map_->find(actualKey);
1090 if (it == value_.map_->end())
1092 return &(*it).second;
1094 Value* Value::demand(
char const* begin,
char const* end) {
1096 "in Json::Value::demand(begin, end): requires "
1097 "objectValue or nullValue");
1098 return &resolveReference(begin, end);
1100 const Value& Value::operator[](
const char* key)
const {
1101 Value const* found = find(key, key + strlen(key));
1103 return nullSingleton();
1107 Value const* found = find(key.data(), key.data() + key.length());
1109 return nullSingleton();
1113 Value& Value::operator[](
const char* key) {
1114 return resolveReference(key, key + strlen(key));
1118 return resolveReference(key.data(), key.data() + key.length());
1122 return resolveReference(key.
c_str());
1129 "in Json::Value::append: requires arrayValue");
1133 return this->value_.map_->emplace(size(), std::move(value)).first->second;
1137 return insert(index,
Value(newValue));
1142 "in Json::Value::insert: requires arrayValue");
1144 if (index > length) {
1147 for (
ArrayIndex i = length; i > index; i--) {
1148 (*this)[i] = std::move((*
this)[i - 1]);
1150 (*this)[index] = std::move(newValue);
1154 Value Value::get(
char const* begin,
char const* end,
1155 Value const& defaultValue)
const {
1156 Value const* found = find(begin, end);
1157 return !found ? defaultValue : *found;
1159 Value Value::get(
char const* key,
Value const& defaultValue)
const {
1160 return get(key, key + strlen(key), defaultValue);
1163 return get(key.data(), key.data() + key.length(), defaultValue);
1166 bool Value::removeMember(
const char* begin,
const char* end,
Value* removed) {
1170 CZString actualKey(begin,
static_cast<unsigned>(end - begin),
1171 CZString::noDuplication);
1172 auto it = value_.map_->find(actualKey);
1173 if (it == value_.map_->end())
1176 *removed = std::move(it->second);
1177 value_.map_->erase(it);
1180 bool Value::removeMember(
const char* key,
Value* removed) {
1181 return removeMember(key, key + strlen(key), removed);
1184 return removeMember(key.data(), key.data() + key.length(), removed);
1186 void Value::removeMember(
const char* key) {
1188 "in Json::Value::removeMember(): requires objectValue");
1192 CZString actualKey(key,
unsigned(strlen(key)), CZString::noDuplication);
1193 value_.map_->erase(actualKey);
1195 void Value::removeMember(
const String& key) { removeMember(key.c_str()); }
1201 CZString key(index);
1202 auto it = value_.map_->find(key);
1203 if (it == value_.map_->end()) {
1207 *removed = it->second;
1210 for (
ArrayIndex i = index; i < (oldSize - 1); ++i) {
1212 (*value_.map_)[keey] = (*
this)[i + 1];
1215 CZString keyLast(oldSize - 1);
1216 auto itLast = value_.map_->find(keyLast);
1217 value_.map_->erase(itLast);
1221 bool Value::isMember(
char const* begin,
char const* end)
const {
1222 Value const* value = find(begin, end);
1223 return nullptr != value;
1225 bool Value::isMember(
char const* key)
const {
1226 return isMember(key, key + strlen(key));
1228 bool Value::isMember(
String const& key)
const {
1229 return isMember(key.data(), key.data() + key.length());
1235 "in Json::Value::getMemberNames(), value must be objectValue");
1239 members.reserve(value_.map_->size());
1240 ObjectValues::const_iterator it = value_.map_->begin();
1241 ObjectValues::const_iterator itEnd = value_.map_->end();
1242 for (; it != itEnd; ++it) {
1243 members.push_back(
String((*it).first.data(), (*it).first.length()));
1249 double integral_part;
1250 return modf(d, &integral_part) == 0.0;
1257 bool Value::isInt()
const {
1260 #if defined(JSON_HAS_INT64)
1261 return value_.int_ >= minInt && value_.int_ <= maxInt;
1266 return value_.uint_ <=
UInt(maxInt);
1268 return value_.real_ >= minInt && value_.real_ <= maxInt &&
1276 bool Value::isUInt()
const {
1279 #if defined(JSON_HAS_INT64)
1282 return value_.int_ >= 0;
1285 #if defined(JSON_HAS_INT64)
1286 return value_.uint_ <= maxUInt;
1291 return value_.real_ >= 0 && value_.real_ <= maxUInt &&
1299 bool Value::isInt64()
const {
1300 #if defined(JSON_HAS_INT64)
1305 return value_.uint_ <=
UInt64(maxInt64);
1310 return value_.real_ >= double(minInt64) &&
1311 value_.real_ < double(maxInt64) &&
IsIntegral(value_.real_);
1315 #endif // JSON_HAS_INT64
1319 bool Value::isUInt64()
const {
1320 #if defined(JSON_HAS_INT64)
1323 return value_.int_ >= 0;
1330 return value_.real_ >= 0 && value_.real_ < maxUInt64AsDouble &&
1335 #endif // JSON_HAS_INT64
1339 bool Value::isIntegral()
const {
1345 #if defined(JSON_HAS_INT64)
1349 return value_.real_ >= double(minInt64) &&
1350 value_.real_ < maxUInt64AsDouble &&
IsIntegral(value_.real_);
1352 return value_.real_ >= minInt && value_.real_ <= maxUInt &&
1354 #endif // JSON_HAS_INT64
1361 bool Value::isDouble()
const {
1365 bool Value::isNumeric()
const {
return isDouble(); }
1373 Value::Comments::Comments(
const Comments& that)
1376 Value::Comments::Comments(Comments&& that) : ptr_{std::move(that.ptr_)} {}
1378 Value::Comments& Value::Comments::operator=(
const Comments& that) {
1383 Value::Comments& Value::Comments::operator=(Comments&& that) {
1384 ptr_ = std::move(that.ptr_);
1389 return ptr_ && !(*ptr_)[slot].empty();
1395 return (*ptr_)[slot];
1400 ptr_ = std::unique_ptr<Array>(
new Array());
1404 (*ptr_)[slot] = std::move(comment);
1409 if (!comment.empty() && (comment.back() ==
'\n')) {
1415 comment[0] ==
'\0' || comment[0] ==
'/',
1416 "in Json::Value::setComment(): Comments must start with /");
1417 comments_.set(
placement, std::move(comment));
1477 return iterator(value_.map_->begin());
1490 return iterator(value_.map_->end());
1504 : index_(index), kind_(kindIndex) {}
1526 void Path::makePath(
const String& path,
const InArgs& in) {
1527 const char* current = path.c_str();
1528 const char* end = current + path.length();
1529 auto itInArg = in.begin();
1530 while (current != end) {
1531 if (*current ==
'[') {
1533 if (*current ==
'%')
1534 addPathInArg(path, in, itInArg, PathArgument::kindIndex);
1537 for (; current != end && *current >=
'0' && *current <=
'9'; ++current)
1538 index = index * 10 +
ArrayIndex(*current -
'0');
1539 args_.push_back(index);
1541 if (current == end || *++current !=
']')
1542 invalidPath(path,
int(current - path.c_str()));
1543 }
else if (*current ==
'%') {
1544 addPathInArg(path, in, itInArg, PathArgument::kindKey);
1546 }
else if (*current ==
'.' || *current ==
']') {
1549 const char* beginName = current;
1550 while (current != end && !strchr(
"[.", *current))
1552 args_.push_back(
String(beginName, current));
1557 void Path::addPathInArg(
const String& ,
const InArgs& in,
1558 InArgs::const_iterator& itInArg,
1559 PathArgument::Kind kind) {
1560 if (itInArg == in.end()) {
1562 }
else if ((*itInArg)->kind_ != kind) {
1565 args_.push_back(**itInArg++);
1569 void Path::invalidPath(
const String& ,
int ) {
1574 const Value* node = &root;
1575 for (
const auto& arg : args_) {
1576 if (arg.kind_ == PathArgument::kindIndex) {
1581 node = &((*node)[arg.index_]);
1582 }
else if (arg.kind_ == PathArgument::kindKey) {
1587 node = &((*node)[arg.key_]);
1599 const Value* node = &root;
1600 for (
const auto& arg : args_) {
1601 if (arg.kind_ == PathArgument::kindIndex) {
1603 return defaultValue;
1604 node = &((*node)[arg.index_]);
1605 }
else if (arg.kind_ == PathArgument::kindKey) {
1607 return defaultValue;
1608 node = &((*node)[arg.key_]);
1610 return defaultValue;
1617 Value* node = &root;
1618 for (
const auto& arg : args_) {
1619 if (arg.kind_ == PathArgument::kindIndex) {
1623 node = &((*node)[arg.index_]);
1624 }
else if (arg.kind_ == PathArgument::kindKey) {
1628 node = &((*node)[arg.key_]);