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)
42 #if defined(JSON_HAS_INT64)
50 #endif // defined(JSON_HAS_INT64)
55 #if !defined(JSON_USE_INT64_DOUBLE_CONVERSION)
56 template <
typename T,
typename U>
57 static inline bool InRange(
double d, T min, U max) {
58 return d >= min && d <= max;
60 #else // if !defined(JSON_USE_INT64_DOUBLE_CONVERSION)
61 static inline double integerToDouble(
Json::UInt64 value) {
62 return static_cast<double>(
Int64(value / 2)) * 2.0 +
Int64(value & 1);
65 template <
typename T>
static inline double integerToDouble(T value) {
66 return static_cast<double>(value);
69 template <
typename T,
typename U>
70 static inline bool InRange(
double d, T min, U max) {
71 return d >= integerToDouble(min) && d <= integerToDouble(max);
73 #endif // if !defined(JSON_USE_INT64_DOUBLE_CONVERSION)
87 length = Value::maxInt - 1;
89 char* newString =
static_cast<char*
>(malloc(length + 1));
90 if (newString == NULL) {
92 "in Json::Value::duplicateStringValue(): "
93 "Failed to allocate string value buffer");
95 memcpy(newString, value, length);
96 newString[length] = 0;
109 "in Json::Value::duplicateAndPrefixStringValue(): "
110 "length too big for prefixing");
111 unsigned actualLength = length +
static_cast<unsigned>(
sizeof(unsigned)) + 1U;
112 char* newString =
static_cast<char*
>(malloc(actualLength));
113 if (newString == 0) {
115 "in Json::Value::duplicateAndPrefixStringValue(): "
116 "Failed to allocate string value buffer");
118 *
reinterpret_cast<unsigned*
>(newString) = length;
119 memcpy(newString +
sizeof(
unsigned), value, length);
120 newString[actualLength - 1U] = 0;
124 bool isPrefixed,
char const* prefixed,
125 unsigned* length,
char const** value)
128 *length =
static_cast<unsigned>(strlen(prefixed));
131 *length = *
reinterpret_cast<unsigned const*
>(prefixed);
132 *value = prefixed +
sizeof(unsigned);
148 #if !defined(JSON_IS_AMALGAMATION)
151 #endif // if !defined(JSON_IS_AMALGAMATION)
187 Value::CommentInfo::CommentInfo() : comment_(0) {}
189 Value::CommentInfo::~CommentInfo() {
194 void Value::CommentInfo::setComment(
const char* text,
size_t len) {
201 text[0] ==
'\0' || text[0] ==
'/',
202 "in Json::Value::setComment(): Comments must start with /");
218 Value::CZString::CZString(
ArrayIndex aindex) : cstr_(0), index_(aindex) {}
220 Value::CZString::CZString(
char const* str,
unsigned ulength, DuplicationPolicy allocate)
223 storage_.policy_ = allocate & 0x3;
224 storage_.length_ = ulength & 0x3FFFFFFF;
227 Value::CZString::CZString(
const CZString& other)
228 : cstr_(other.storage_.policy_ != noDuplication && other.cstr_ != 0
231 storage_.policy_ = (other.cstr_
232 ? (
static_cast<DuplicationPolicy
>(other.storage_.policy_) == noDuplication
233 ? noDuplication : duplicate)
234 :
static_cast<DuplicationPolicy
>(other.storage_.policy_));
235 storage_.length_ = other.storage_.length_;
238 #if JSON_HAS_RVALUE_REFERENCES
239 Value::CZString::CZString(CZString&& other)
240 : cstr_(other.cstr_), index_(other.index_) {
241 other.cstr_ =
nullptr;
245 Value::CZString::~CZString() {
246 if (cstr_ && storage_.policy_ == duplicate)
250 void Value::CZString::swap(CZString& other) {
251 std::swap(cstr_, other.cstr_);
252 std::swap(index_, other.index_);
255 Value::CZString& Value::CZString::operator=(CZString other) {
260 bool Value::CZString::operator<(
const CZString& other)
const {
261 if (!cstr_)
return index_ < other.index_;
264 unsigned this_len = this->storage_.length_;
265 unsigned other_len = other.storage_.length_;
266 unsigned min_len = std::min(this_len, other_len);
267 int comp = memcmp(this->cstr_, other.cstr_, min_len);
268 if (comp < 0)
return true;
269 if (comp > 0)
return false;
270 return (this_len < other_len);
273 bool Value::CZString::operator==(
const CZString& other)
const {
274 if (!cstr_)
return index_ == other.index_;
277 unsigned this_len = this->storage_.length_;
278 unsigned other_len = other.storage_.length_;
279 if (this_len != other_len)
return false;
280 int comp = memcmp(this->cstr_, other.cstr_, this_len);
284 ArrayIndex Value::CZString::index()
const {
return index_; }
287 const char* Value::CZString::data()
const {
return cstr_; }
288 unsigned Value::CZString::length()
const {
return storage_.length_; }
289 bool Value::CZString::isStaticString()
const {
return storage_.policy_ == noDuplication; }
320 value_.map_ =
new ObjectValues();
323 value_.bool_ =
false;
337 value_.uint_ = value;
339 #if defined(JSON_HAS_INT64)
346 value_.uint_ = value;
348 #endif // defined(JSON_HAS_INT64)
350 Value::Value(
double value) {
352 value_.real_ = value;
355 Value::Value(
const char* value) {
360 Value::Value(
const char* beginValue,
const char* endValue) {
366 Value::Value(
const std::string& value) {
374 value_.string_ =
const_cast<char*
>(value.
c_str());
377 #ifdef JSON_USE_CPPTL
378 Value::Value(
const CppTL::ConstString& value) {
384 Value::Value(
bool value) {
386 value_.bool_ = value;
390 : type_(other.type_), allocated_(false)
392 comments_(0), start_(other.start_), limit_(other.limit_)
400 value_ = other.value_;
403 if (other.value_.string_ && other.allocated_) {
411 value_.string_ = other.value_.string_;
417 value_.map_ =
new ObjectValues(*other.value_.map_);
422 if (other.comments_) {
425 const CommentInfo& otherComment = other.comments_[comment];
426 if (otherComment.comment_)
427 comments_[comment].setComment(
428 otherComment.comment_, strlen(otherComment.comment_));
433 #if JSON_HAS_RVALUE_REFERENCES
474 std::swap(value_, other.value_);
475 int temp2 = allocated_;
476 allocated_ = other.allocated_;
477 other.allocated_ = temp2 & 0x1;
482 std::swap(comments_, other.comments_);
483 std::swap(start_, other.start_);
484 std::swap(limit_, other.limit_);
498 int typeDelta = type_ - other.type_;
500 return typeDelta < 0 ?
true :
false;
505 return value_.int_ < other.value_.int_;
507 return value_.uint_ < other.value_.uint_;
509 return value_.real_ < other.value_.real_;
511 return value_.bool_ < other.value_.bool_;
514 if ((value_.string_ == 0) || (other.value_.string_ == 0)) {
515 if (other.value_.string_)
return true;
520 char const* this_str;
521 char const* other_str;
524 unsigned min_len = std::min(this_len, other_len);
525 int comp = memcmp(this_str, other_str, min_len);
526 if (comp < 0)
return true;
527 if (comp > 0)
return false;
528 return (this_len < other_len);
532 int delta = int(value_.map_->size() - other.value_.map_->size());
535 return (*value_.map_) < (*other.value_.map_);
554 int temp = other.type_;
561 return value_.int_ == other.value_.int_;
563 return value_.uint_ == other.value_.uint_;
565 return value_.real_ == other.value_.real_;
567 return value_.bool_ == other.value_.bool_;
570 if ((value_.string_ == 0) || (other.value_.string_ == 0)) {
571 return (value_.string_ == other.value_.string_);
575 char const* this_str;
576 char const* other_str;
579 if (this_len != other_len)
return false;
580 int comp = memcmp(this_str, other_str, this_len);
585 return value_.map_->size() == other.value_.map_->size() &&
586 (*value_.map_) == (*other.value_.map_);
597 "in Json::Value::asCString(): requires stringValue");
598 if (value_.string_ == 0)
return 0;
600 char const* this_str;
607 if (value_.string_ == 0)
return false;
610 *cend = *str + length;
620 if (value_.string_ == 0)
return "";
622 char const* this_str;
624 return std::string(this_str, this_len);
627 return value_.bool_ ?
"true" :
"false";
639 #ifdef JSON_USE_CPPTL
640 CppTL::ConstString Value::asConstString()
const {
645 return CppTL::ConstString(str, len);
653 return Int(value_.int_);
656 return Int(value_.uint_);
659 "double out of Int range");
660 return Int(value_.real_);
664 return value_.bool_ ? 1 : 0;
675 return UInt(value_.int_);
678 return UInt(value_.uint_);
681 "double out of UInt range");
682 return UInt(value_.real_);
686 return value_.bool_ ? 1 : 0;
693 #if defined(JSON_HAS_INT64)
698 return Int64(value_.int_);
701 return Int64(value_.uint_);
704 "double out of Int64 range");
705 return Int64(value_.real_);
709 return value_.bool_ ? 1 : 0;
720 return UInt64(value_.int_);
722 return UInt64(value_.uint_);
725 "double out of UInt64 range");
726 return UInt64(value_.real_);
730 return value_.bool_ ? 1 : 0;
736 #endif // if defined(JSON_HAS_INT64)
739 #if defined(JSON_NO_INT64)
747 #if defined(JSON_NO_INT64)
757 return static_cast<double>(value_.int_);
759 #if !defined(JSON_USE_INT64_DOUBLE_CONVERSION)
760 return static_cast<double>(value_.uint_);
761 #else // if !defined(JSON_USE_INT64_DOUBLE_CONVERSION)
762 return integerToDouble(value_.uint_);
763 #endif // if !defined(JSON_USE_INT64_DOUBLE_CONVERSION)
769 return value_.bool_ ? 1.0 : 0.0;
779 return static_cast<float>(value_.int_);
781 #if !defined(JSON_USE_INT64_DOUBLE_CONVERSION)
782 return static_cast<float>(value_.uint_);
783 #else // if !defined(JSON_USE_INT64_DOUBLE_CONVERSION)
784 return integerToDouble(value_.uint_);
785 #endif // if !defined(JSON_USE_INT64_DOUBLE_CONVERSION)
787 return static_cast<float>(value_.real_);
791 return value_.bool_ ? 1.0f : 0.0f;
805 return value_.int_ ?
true :
false;
807 return value_.uint_ ?
true :
false;
810 return (value_.real_ != 0.0) ?
true :
false;
823 (type_ ==
arrayValue && value_.map_->size() == 0) ||
824 (type_ ==
objectValue && value_.map_->size() == 0) ||
861 if (!value_.map_->empty()) {
862 ObjectValues::const_iterator itLast = value_.map_->end();
864 return (*itLast).first.index() + 1;
886 "in Json::Value::clear(): requires complex value");
892 value_.map_->clear();
901 "in Json::Value::resize(): requires arrayValue");
907 else if (newSize > oldSize)
908 (*this)[newSize - 1];
910 for (
ArrayIndex index = newSize; index < oldSize; ++index) {
911 value_.map_->erase(index);
913 assert(
size() == newSize);
920 "in Json::Value::operator[](ArrayIndex): requires arrayValue");
924 ObjectValues::iterator it = value_.map_->lower_bound(key);
925 if (it != value_.map_->end() && (*it).first == key)
928 ObjectValues::value_type defaultValue(key,
nullRef);
929 it = value_.map_->insert(it, defaultValue);
936 "in Json::Value::operator[](int index): index cannot be negative");
943 "in Json::Value::operator[](ArrayIndex)const: requires arrayValue");
947 ObjectValues::const_iterator it = value_.map_->find(key);
948 if (it == value_.map_->end())
956 "in Json::Value::operator[](int index) const: index cannot be negative");
960 void Value::initBasic(
ValueType vtype,
bool allocated) {
962 allocated_ = allocated;
971 Value& Value::resolveReference(
const char* key) {
974 "in Json::Value::resolveReference(): requires objectValue");
978 key, static_cast<unsigned>(strlen(key)), CZString::noDuplication);
979 ObjectValues::iterator it = value_.map_->lower_bound(actualKey);
980 if (it != value_.map_->end() && (*it).first == actualKey)
983 ObjectValues::value_type defaultValue(actualKey,
nullRef);
984 it = value_.map_->insert(it, defaultValue);
985 Value& value = (*it).second;
990 Value& Value::resolveReference(
char const* key,
char const* cend)
994 "in Json::Value::resolveReference(key, end): requires objectValue");
998 key, static_cast<unsigned>(cend-key), CZString::duplicateOnCopy);
999 ObjectValues::iterator it = value_.map_->lower_bound(actualKey);
1000 if (it != value_.map_->end() && (*it).first == actualKey)
1001 return (*it).second;
1003 ObjectValues::value_type defaultValue(actualKey,
nullRef);
1004 it = value_.map_->insert(it, defaultValue);
1005 Value& value = (*it).second;
1010 const Value* value = &((*this)[index]);
1011 return value == &
nullRef ? defaultValue : *value;
1020 "in Json::Value::find(key, end, found): requires objectValue or nullValue");
1022 CZString actualKey(key, static_cast<unsigned>(cend-key), CZString::noDuplication);
1023 ObjectValues::const_iterator it = value_.map_->find(actualKey);
1024 if (it == value_.map_->end())
return NULL;
1025 return &(*it).second;
1029 Value const* found =
find(key, key + strlen(key));
1035 Value const* found =
find(key.data(), key.data() + key.length());
1041 return resolveReference(key, key + strlen(key));
1045 return resolveReference(key.data(), key.data() + key.length());
1049 return resolveReference(key.
c_str());
1052 #ifdef JSON_USE_CPPTL
1054 return resolveReference(key.c_str(), key.end_c_str());
1058 Value const* found =
find(key.c_str(), key.end_c_str());
1069 return !found ? defaultValue : *found;
1073 return get(key, key + strlen(key), defaultValue);
1077 return get(key.data(), key.data() + key.length(), defaultValue);
1086 CZString actualKey(key, static_cast<unsigned>(cend-key), CZString::noDuplication);
1087 ObjectValues::iterator it = value_.map_->find(actualKey);
1088 if (it == value_.map_->end())
1090 *removed = it->second;
1091 value_.map_->erase(it);
1100 return removeMember(key.data(), key.data() + key.length(), removed);
1105 "in Json::Value::removeMember(): requires objectValue");
1122 CZString key(index);
1123 ObjectValues::iterator it = value_.map_->find(key);
1124 if (it == value_.map_->end()) {
1127 *removed = it->second;
1130 for (
ArrayIndex i = index; i < (oldSize - 1); ++i){
1132 (*value_.map_)[keey] = (*
this)[i + 1];
1135 CZString keyLast(oldSize - 1);
1136 ObjectValues::iterator itLast = value_.map_->find(keyLast);
1137 value_.map_->erase(itLast);
1141 #ifdef JSON_USE_CPPTL
1143 const Value& defaultValue)
const {
1144 return get(key.c_str(), key.end_c_str(), defaultValue);
1151 return NULL != value;
1155 return isMember(key, key + strlen(key));
1159 return isMember(key.data(), key.data() + key.length());
1162 #ifdef JSON_USE_CPPTL
1164 return isMember(key.c_str(), key.end_c_str());
1171 "in Json::Value::getMemberNames(), value must be objectValue");
1175 members.reserve(value_.map_->size());
1176 ObjectValues::const_iterator it = value_.map_->begin();
1177 ObjectValues::const_iterator itEnd = value_.map_->end();
1178 for (; it != itEnd; ++it) {
1179 members.push_back(std::string((*it).first.data(),
1180 (*it).first.length()));
1211 double integral_part;
1212 return modf(d, &integral_part) == 0.0;
1226 return value_.real_ >=
minInt && value_.real_ <=
maxInt &&
1239 return value_.uint_ <=
maxUInt;
1241 return value_.real_ >= 0 && value_.real_ <=
maxUInt &&
1250 #if defined(JSON_HAS_INT64)
1260 return value_.real_ >= double(
minInt64) &&
1265 #endif // JSON_HAS_INT64
1270 #if defined(JSON_HAS_INT64)
1273 return value_.int_ >= 0;
1285 #endif // JSON_HAS_INT64
1290 #if defined(JSON_HAS_INT64)
1310 if ((len > 0) && (comment[len-1] ==
'\n')) {
1314 comments_[placement].setComment(comment, len);
1318 setComment(comment, strlen(comment), placement);
1322 setComment(comment.c_str(), comment.length(), placement);
1326 return comments_ != 0 && comments_[placement].comment_ != 0;
1331 return comments_[placement].comment_;
1345 return writer.
write(*
this);
1379 return iterator(value_.map_->begin());
1392 return iterator(value_.map_->end());
1406 : key_(), index_(index), kind_(kindIndex) {}
1409 : key_(key), index_(), kind_(kindKey) {}
1412 : key_(key.c_str()), index_(), kind_(kindKey) {}
1432 void Path::makePath(
const std::string& path,
const InArgs& in) {
1433 const char* current = path.c_str();
1434 const char* end = current + path.length();
1435 InArgs::const_iterator itInArg = in.begin();
1436 while (current != end) {
1437 if (*current ==
'[') {
1439 if (*current ==
'%')
1440 addPathInArg(path, in, itInArg, PathArgument::kindIndex);
1443 for (; current != end && *current >=
'0' && *current <=
'9'; ++current)
1444 index = index * 10 +
ArrayIndex(*current -
'0');
1445 args_.push_back(index);
1447 if (current == end || *current++ !=
']')
1448 invalidPath(path,
int(current - path.c_str()));
1449 }
else if (*current ==
'%') {
1450 addPathInArg(path, in, itInArg, PathArgument::kindKey);
1452 }
else if (*current ==
'.') {
1455 const char* beginName = current;
1456 while (current != end && !strchr(
"[.", *current))
1458 args_.push_back(std::string(beginName, current));
1463 void Path::addPathInArg(
const std::string& ,
1465 InArgs::const_iterator& itInArg,
1466 PathArgument::Kind kind) {
1467 if (itInArg == in.end()) {
1469 }
else if ((*itInArg)->kind_ != kind) {
1472 args_.push_back(**itInArg);
1476 void Path::invalidPath(
const std::string& ,
int ) {
1481 const Value* node = &root;
1482 for (Args::const_iterator it = args_.begin(); it != args_.end(); ++it) {
1484 if (arg.kind_ == PathArgument::kindIndex) {
1488 node = &((*node)[arg.index_]);
1489 }
else if (arg.kind_ == PathArgument::kindKey) {
1493 node = &((*node)[arg.key_]);
1504 const Value* node = &root;
1505 for (Args::const_iterator it = args_.begin(); it != args_.end(); ++it) {
1507 if (arg.kind_ == PathArgument::kindIndex) {
1509 return defaultValue;
1510 node = &((*node)[arg.index_]);
1511 }
else if (arg.kind_ == PathArgument::kindKey) {
1513 return defaultValue;
1514 node = &((*node)[arg.key_]);
1516 return defaultValue;
1523 Value* node = &root;
1524 for (Args::const_iterator it = args_.begin(); it != args_.end(); ++it) {
1526 if (arg.kind_ == PathArgument::kindIndex) {
1530 node = &((*node)[arg.index_]);
1531 }
else if (arg.kind_ == PathArgument::kindKey) {
1535 node = &((*node)[arg.key_]);