6 #if !defined(JSON_IS_AMALGAMATION)
10 #endif // if !defined(JSON_IS_AMALGAMATION)
17 #include <cpptl/conststring.h>
22 #define JSON_ASSERT_UNREACHABLE assert(false)
29 #if defined(__ARMEL__)
30 #define ALIGNAS(byte_alignment) __attribute__((aligned(byte_alignment)))
32 #define ALIGNAS(byte_alignment)
54 #if defined(JSON_HAS_INT64)
62 #endif // defined(JSON_HAS_INT64)
67 #if !defined(JSON_USE_INT64_DOUBLE_CONVERSION)
68 template <
typename T,
typename U>
69 static inline bool InRange(
double d, T min, U max) {
73 return d >= min && d <= max;
75 #else // if !defined(JSON_USE_INT64_DOUBLE_CONVERSION)
76 static inline double integerToDouble(
Json::UInt64 value) {
77 return static_cast<double>(
Int64(value / 2)) * 2.0 + static_cast<double>(
Int64(value & 1));
80 template <
typename T>
static inline double integerToDouble(T value) {
81 return static_cast<double>(value);
84 template <
typename T,
typename U>
85 static inline bool InRange(
double d, T min, U max) {
86 return d >= integerToDouble(min) && d <= integerToDouble(max);
88 #endif // if !defined(JSON_USE_INT64_DOUBLE_CONVERSION)
105 char* newString = static_cast<char*>(malloc(length + 1));
106 if (newString == NULL) {
108 "in Json::Value::duplicateStringValue(): "
109 "Failed to allocate string value buffer");
111 memcpy(newString, value, length);
112 newString[length] = 0;
125 "in Json::Value::duplicateAndPrefixStringValue(): "
126 "length too big for prefixing");
127 unsigned actualLength = length + static_cast<unsigned>(
sizeof(
unsigned)) + 1U;
128 char* newString = static_cast<char*>(malloc(actualLength));
129 if (newString == 0) {
131 "in Json::Value::duplicateAndPrefixStringValue(): "
132 "Failed to allocate string value buffer");
134 *reinterpret_cast<unsigned*>(newString) = length;
135 memcpy(newString +
sizeof(
unsigned), value, length);
136 newString[actualLength - 1U] = 0;
140 bool isPrefixed,
char const* prefixed,
141 unsigned* length,
char const** value)
144 *length = static_cast<unsigned>(strlen(prefixed));
147 *length = *reinterpret_cast<unsigned const*>(prefixed);
148 *value = prefixed +
sizeof(unsigned);
153 #if JSONCPP_USING_SECURE_MEMORY
156 char const* valueDecoded;
158 size_t const size =
sizeof(unsigned) + length + 1U;
159 memset(value, 0, size);
164 size_t size = (length==0) ? strlen(value) : length;
165 memset(value, 0, size);
168 #else // !JSONCPP_USING_SECURE_MEMORY
175 #endif // JSONCPP_USING_SECURE_MEMORY
186 #if !defined(JSON_IS_AMALGAMATION)
189 #endif // if !defined(JSON_IS_AMALGAMATION)
214 throw LogicError(msg);
225 Value::CommentInfo::CommentInfo() : comment_(0)
228 Value::CommentInfo::~CommentInfo() {
233 void Value::CommentInfo::setComment(
const char* text,
size_t len) {
240 text[0] ==
'\0' || text[0] ==
'/',
241 "in Json::Value::setComment(): Comments must start with /");
257 Value::CZString::CZString(
ArrayIndex aindex) : cstr_(0), index_(aindex) {}
259 Value::CZString::CZString(
char const* str,
unsigned ulength, DuplicationPolicy allocate)
262 storage_.policy_ = allocate & 0x3;
263 storage_.length_ = ulength & 0x3FFFFFFF;
266 Value::CZString::CZString(
const CZString& other) {
267 cstr_ = (other.storage_.policy_ != noDuplication && other.cstr_ != 0
270 storage_.policy_ = static_cast<unsigned>(other.cstr_
271 ? (static_cast<DuplicationPolicy>(other.storage_.policy_) == noDuplication
272 ? noDuplication : duplicate)
273 : static_cast<DuplicationPolicy>(other.storage_.policy_)) & 3U;
274 storage_.length_ = other.storage_.length_;
277 #if JSON_HAS_RVALUE_REFERENCES
278 Value::CZString::CZString(CZString&& other)
279 : cstr_(other.cstr_), index_(other.index_) {
280 other.cstr_ =
nullptr;
284 Value::CZString::~CZString() {
285 if (cstr_ && storage_.policy_ == duplicate) {
290 void Value::CZString::swap(CZString& other) {
295 Value::CZString& Value::CZString::operator=(CZString other) {
300 bool Value::CZString::operator<(
const CZString& other)
const {
301 if (!cstr_)
return index_ < other.index_;
304 unsigned this_len = this->storage_.length_;
305 unsigned other_len = other.storage_.length_;
306 unsigned min_len = std::min(this_len, other_len);
308 int comp = memcmp(this->cstr_, other.cstr_, min_len);
309 if (comp < 0)
return true;
310 if (comp > 0)
return false;
311 return (this_len < other_len);
315 if (!cstr_)
return index_ == other.index_;
318 unsigned this_len = this->storage_.length_;
319 unsigned other_len = other.storage_.length_;
320 if (this_len != other_len)
return false;
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 {
return storage_.policy_ == noDuplication; }
362 value_.map_ =
new ObjectValues();
365 value_.bool_ =
false;
379 value_.uint_ = value;
381 #if defined(JSON_HAS_INT64)
388 value_.uint_ = value;
390 #endif // defined(JSON_HAS_INT64)
392 Value::Value(
double value) {
394 value_.real_ = value;
397 Value::Value(
const char* value) {
402 Value::Value(
const char* beginValue,
const char* endValue) {
416 value_.string_ = const_cast<char*>(value.
c_str());
419 #ifdef JSON_USE_CPPTL
420 Value::Value(
const CppTL::ConstString& value) {
426 Value::Value(
bool value) {
428 value_.bool_ = value;
432 : type_(other.type_), allocated_(false)
434 comments_(0), start_(other.start_), limit_(other.limit_)
442 value_ = other.value_;
445 if (other.value_.string_ && other.allocated_) {
453 value_.string_ = other.value_.string_;
459 value_.map_ =
new ObjectValues(*other.value_.map_);
464 if (other.comments_) {
467 const CommentInfo& otherComment = other.comments_[comment];
468 if (otherComment.comment_)
469 comments_[comment].setComment(
470 otherComment.comment_, strlen(otherComment.comment_));
475 #if JSON_HAS_RVALUE_REFERENCES
518 std::swap(value_, other.value_);
519 int temp2 = allocated_;
520 allocated_ = other.allocated_;
521 other.allocated_ = temp2 & 0x1;
526 std::swap(comments_, other.comments_);
527 std::swap(start_, other.start_);
528 std::swap(limit_, other.limit_);
542 int typeDelta = type_ - other.type_;
544 return typeDelta < 0 ? true :
false;
549 return value_.int_ < other.value_.int_;
551 return value_.uint_ < other.value_.uint_;
553 return value_.real_ < other.value_.real_;
555 return value_.bool_ < other.value_.bool_;
558 if ((value_.string_ == 0) || (other.value_.string_ == 0)) {
559 if (other.value_.string_)
return true;
564 char const* this_str;
565 char const* other_str;
568 unsigned min_len = std::min(this_len, other_len);
570 int comp = memcmp(this_str, other_str, min_len);
571 if (comp < 0)
return true;
572 if (comp > 0)
return false;
573 return (this_len < other_len);
577 int delta = int(value_.map_->size() - other.value_.map_->size());
580 return (*value_.map_) < (*other.value_.map_);
599 int temp = other.type_;
606 return value_.int_ == other.value_.int_;
608 return value_.uint_ == other.value_.uint_;
610 return value_.real_ == other.value_.real_;
612 return value_.bool_ == other.value_.bool_;
615 if ((value_.string_ == 0) || (other.value_.string_ == 0)) {
616 return (value_.string_ == other.value_.string_);
620 char const* this_str;
621 char const* other_str;
624 if (this_len != other_len)
return false;
626 int comp = memcmp(this_str, other_str, this_len);
631 return value_.map_->size() == other.value_.map_->size() &&
632 (*value_.map_) == (*other.value_.map_);
643 "in Json::Value::asCString(): requires stringValue");
644 if (value_.string_ == 0)
return 0;
646 char const* this_str;
651 #if JSONCPP_USING_SECURE_MEMORY
652 unsigned Value::getCStringLength()
const {
654 "in Json::Value::asCString(): requires stringValue");
655 if (value_.string_ == 0)
return 0;
657 char const* this_str;
665 if (value_.string_ == 0)
return false;
668 *cend = *str + length;
678 if (value_.string_ == 0)
return "";
680 char const* this_str;
685 return value_.bool_ ?
"true" :
"false";
697 #ifdef JSON_USE_CPPTL
698 CppTL::ConstString Value::asConstString()
const {
703 return CppTL::ConstString(str, len);
711 return Int(value_.int_);
714 return Int(value_.uint_);
717 "double out of Int range");
718 return Int(value_.real_);
722 return value_.bool_ ? 1 : 0;
733 return UInt(value_.int_);
736 return UInt(value_.uint_);
739 "double out of UInt range");
740 return UInt(value_.real_);
744 return value_.bool_ ? 1 : 0;
751 #if defined(JSON_HAS_INT64)
756 return Int64(value_.int_);
759 return Int64(value_.uint_);
762 "double out of Int64 range");
763 return Int64(value_.real_);
767 return value_.bool_ ? 1 : 0;
778 return UInt64(value_.int_);
780 return UInt64(value_.uint_);
783 "double out of UInt64 range");
784 return UInt64(value_.real_);
788 return value_.bool_ ? 1 : 0;
794 #endif // if defined(JSON_HAS_INT64)
797 #if defined(JSON_NO_INT64)
805 #if defined(JSON_NO_INT64)
815 return static_cast<double>(value_.int_);
817 #if !defined(JSON_USE_INT64_DOUBLE_CONVERSION)
818 return static_cast<double>(value_.uint_);
819 #else // if !defined(JSON_USE_INT64_DOUBLE_CONVERSION)
820 return integerToDouble(value_.uint_);
821 #endif // if !defined(JSON_USE_INT64_DOUBLE_CONVERSION)
827 return value_.bool_ ? 1.0 : 0.0;
837 return static_cast<float>(value_.int_);
839 #if !defined(JSON_USE_INT64_DOUBLE_CONVERSION)
840 return static_cast<float>(value_.uint_);
841 #else // if !defined(JSON_USE_INT64_DOUBLE_CONVERSION)
843 return static_cast<float>(integerToDouble(value_.uint_));
844 #endif // if !defined(JSON_USE_INT64_DOUBLE_CONVERSION)
846 return static_cast<float>(value_.real_);
850 return value_.bool_ ? 1.0f : 0.0f;
864 return value_.int_ ? true :
false;
866 return value_.uint_ ? true :
false;
869 return (value_.real_ != 0.0) ? true :
false;
882 (type_ ==
arrayValue && value_.map_->size() == 0) ||
883 (type_ ==
objectValue && value_.map_->size() == 0) ||
920 if (!value_.map_->empty()) {
921 ObjectValues::const_iterator itLast = value_.map_->end();
923 return (*itLast).first.index() + 1;
945 "in Json::Value::clear(): requires complex value");
951 value_.map_->clear();
960 "in Json::Value::resize(): requires arrayValue");
966 else if (newSize > oldSize)
967 (*this)[newSize - 1];
969 for (
ArrayIndex index = newSize; index < oldSize; ++index) {
970 value_.map_->erase(index);
979 "in Json::Value::operator[](ArrayIndex): requires arrayValue");
983 ObjectValues::iterator it = value_.map_->lower_bound(key);
984 if (it != value_.map_->end() && (*it).first == key)
988 it = value_.map_->insert(it, defaultValue);
995 "in Json::Value::operator[](int index): index cannot be negative");
1002 "in Json::Value::operator[](ArrayIndex)const: requires arrayValue");
1005 CZString key(index);
1006 ObjectValues::const_iterator it = value_.map_->find(key);
1007 if (it == value_.map_->end())
1009 return (*it).second;
1015 "in Json::Value::operator[](int index) const: index cannot be negative");
1019 void Value::initBasic(
ValueType vtype,
bool allocated) {
1021 allocated_ = allocated;
1030 Value& Value::resolveReference(
const char* key) {
1033 "in Json::Value::resolveReference(): requires objectValue");
1037 key, static_cast<unsigned>(strlen(key)), CZString::noDuplication);
1038 ObjectValues::iterator it = value_.map_->lower_bound(actualKey);
1039 if (it != value_.map_->end() && (*it).first == actualKey)
1040 return (*it).second;
1042 ObjectValues::value_type defaultValue(actualKey,
nullSingleton());
1043 it = value_.map_->insert(it, defaultValue);
1044 Value& value = (*it).second;
1049 Value& Value::resolveReference(
char const* key,
char const* cend)
1053 "in Json::Value::resolveReference(key, end): requires objectValue");
1057 key, static_cast<unsigned>(cend-key), CZString::duplicateOnCopy);
1058 ObjectValues::iterator it = value_.map_->lower_bound(actualKey);
1059 if (it != value_.map_->end() && (*it).first == actualKey)
1060 return (*it).second;
1062 ObjectValues::value_type defaultValue(actualKey,
nullSingleton());
1063 it = value_.map_->insert(it, defaultValue);
1064 Value& value = (*it).second;
1069 const Value* value = &((*this)[index]);
1079 "in Json::Value::find(key, end, found): requires objectValue or nullValue");
1081 CZString actualKey(key, static_cast<unsigned>(cend-key), CZString::noDuplication);
1082 ObjectValues::const_iterator it = value_.map_->find(actualKey);
1083 if (it == value_.map_->end())
return NULL;
1084 return &(*it).second;
1088 Value const* found =
find(key, key + strlen(key));
1094 Value const* found =
find(key.data(), key.data() + key.length());
1100 return resolveReference(key, key + strlen(key));
1104 return resolveReference(key.data(), key.data() + key.length());
1108 return resolveReference(key.
c_str());
1111 #ifdef JSON_USE_CPPTL
1113 return resolveReference(key.c_str(), key.end_c_str());
1117 Value const* found =
find(key.c_str(), key.end_c_str());
1128 return !found ? defaultValue : *found;
1132 return get(key, key + strlen(key), defaultValue);
1136 return get(key.data(), key.data() + key.length(), defaultValue);
1145 CZString actualKey(key, static_cast<unsigned>(cend-key), CZString::noDuplication);
1146 ObjectValues::iterator it = value_.map_->find(actualKey);
1147 if (it == value_.map_->end())
1149 *removed = it->second;
1150 value_.map_->erase(it);
1159 return removeMember(key.data(), key.data() + key.length(), removed);
1164 "in Json::Value::removeMember(): requires objectValue");
1181 CZString key(index);
1182 ObjectValues::iterator it = value_.map_->find(key);
1183 if (it == value_.map_->end()) {
1186 *removed = it->second;
1189 for (
ArrayIndex i = index; i < (oldSize - 1); ++i){
1191 (*value_.map_)[keey] = (*
this)[i + 1];
1194 CZString keyLast(oldSize - 1);
1195 ObjectValues::iterator itLast = value_.map_->find(keyLast);
1196 value_.map_->erase(itLast);
1200 #ifdef JSON_USE_CPPTL
1202 const Value& defaultValue)
const {
1203 return get(key.c_str(), key.end_c_str(), defaultValue);
1210 return NULL != value;
1214 return isMember(key, key + strlen(key));
1218 return isMember(key.data(), key.data() + key.length());
1221 #ifdef JSON_USE_CPPTL
1223 return isMember(key.c_str(), key.end_c_str());
1230 "in Json::Value::getMemberNames(), value must be objectValue");
1234 members.reserve(value_.map_->size());
1235 ObjectValues::const_iterator it = value_.map_->begin();
1236 ObjectValues::const_iterator itEnd = value_.map_->end();
1237 for (; it != itEnd; ++it) {
1239 (*it).first.length()));
1270 double integral_part;
1271 return modf(d, &integral_part) == 0.0;
1285 return value_.real_ >=
minInt && value_.real_ <=
maxInt &&
1298 return value_.uint_ <=
maxUInt;
1300 return value_.real_ >= 0 && value_.real_ <=
maxUInt &&
1309 #if defined(JSON_HAS_INT64)
1319 return value_.real_ >= double(
minInt64) &&
1324 #endif // JSON_HAS_INT64
1329 #if defined(JSON_HAS_INT64)
1332 return value_.int_ >= 0;
1344 #endif // JSON_HAS_INT64
1349 #if defined(JSON_HAS_INT64)
1369 if ((len > 0) && (comment[len-1] ==
'\n')) {
1373 comments_[placement].setComment(comment, len);
1377 setComment(comment, strlen(comment), placement);
1381 setComment(comment.c_str(), comment.length(), placement);
1385 return comments_ != 0 && comments_[placement].comment_ != 0;
1390 return comments_[placement].comment_;
1404 return writer.
write(*
this);
1438 return iterator(value_.map_->begin());
1451 return iterator(value_.map_->end());
1465 : key_(), index_(index), kind_(kindIndex) {}
1468 : key_(key), index_(), kind_(kindKey) {}
1471 : key_(key.c_str()), index_(), kind_(kindKey) {}
1491 void Path::makePath(
const JSONCPP_STRING& path,
const InArgs& in) {
1492 const char* current = path.c_str();
1493 const char* end = current + path.length();
1494 InArgs::const_iterator itInArg = in.begin();
1495 while (current != end) {
1496 if (*current ==
'[') {
1498 if (*current ==
'%')
1499 addPathInArg(path, in, itInArg, PathArgument::kindIndex);
1502 for (; current != end && *current >=
'0' && *current <=
'9'; ++current)
1503 index = index * 10 +
ArrayIndex(*current -
'0');
1504 args_.push_back(index);
1506 if (current == end || *current++ !=
']')
1507 invalidPath(path,
int(current - path.c_str()));
1508 }
else if (*current ==
'%') {
1509 addPathInArg(path, in, itInArg, PathArgument::kindKey);
1511 }
else if (*current ==
'.') {
1514 const char* beginName = current;
1515 while (current != end && !strchr(
"[.", *current))
1524 InArgs::const_iterator& itInArg,
1525 PathArgument::Kind kind) {
1526 if (itInArg == in.end()) {
1528 }
else if ((*itInArg)->kind_ != kind) {
1531 args_.push_back(**itInArg);
1540 const Value* node = &root;
1541 for (Args::const_iterator it = args_.begin(); it != args_.end(); ++it) {
1543 if (arg.kind_ == PathArgument::kindIndex) {
1547 node = &((*node)[arg.index_]);
1548 }
else if (arg.kind_ == PathArgument::kindKey) {
1552 node = &((*node)[arg.key_]);
1563 const Value* node = &root;
1564 for (Args::const_iterator it = args_.begin(); it != args_.end(); ++it) {
1566 if (arg.kind_ == PathArgument::kindIndex) {
1568 return defaultValue;
1569 node = &((*node)[arg.index_]);
1570 }
else if (arg.kind_ == PathArgument::kindKey) {
1572 return defaultValue;
1573 node = &((*node)[arg.key_]);
1575 return defaultValue;
1582 Value* node = &root;
1583 for (Args::const_iterator it = args_.begin(); it != args_.end(); ++it) {
1585 if (arg.kind_ == PathArgument::kindIndex) {
1589 node = &((*node)[arg.index_]);
1590 }
else if (arg.kind_ == PathArgument::kindKey) {
1594 node = &((*node)[arg.key_]);