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)))
34 #define ALIGNAS(byte_alignment)
43 #if defined(JSON_HAS_INT64)
51 #endif // defined(JSON_HAS_INT64)
56 #if !defined(JSON_USE_INT64_DOUBLE_CONVERSION)
57 template <
typename T,
typename U>
58 static inline bool InRange(
double d, T min, U max) {
59 return d >= min && d <= max;
61 #else // if !defined(JSON_USE_INT64_DOUBLE_CONVERSION)
62 static inline double integerToDouble(
Json::UInt64 value) {
63 return static_cast<double>(
Int64(value / 2)) * 2.0 +
Int64(value & 1);
66 template <
typename T>
static inline double integerToDouble(T value) {
67 return static_cast<double>(value);
70 template <
typename T,
typename U>
71 static inline bool InRange(
double d, T min, U max) {
72 return d >= integerToDouble(min) && d <= integerToDouble(max);
74 #endif // if !defined(JSON_USE_INT64_DOUBLE_CONVERSION)
88 length = Value::maxInt - 1;
90 char* newString =
static_cast<char*
>(malloc(length + 1));
91 if (newString == NULL) {
93 "in Json::Value::duplicateStringValue(): "
94 "Failed to allocate string value buffer");
96 memcpy(newString, value, length);
97 newString[length] = 0;
110 "in Json::Value::duplicateAndPrefixStringValue(): "
111 "length too big for prefixing");
112 unsigned actualLength = length +
sizeof(unsigned) + 1U;
113 char* newString =
static_cast<char*
>(malloc(actualLength));
114 if (newString == 0) {
116 "in Json::Value::duplicateAndPrefixStringValue(): "
117 "Failed to allocate string value buffer");
119 *
reinterpret_cast<unsigned*
>(newString) = length;
120 memcpy(newString +
sizeof(
unsigned), value, length);
121 newString[actualLength - 1U] = 0;
125 bool isPrefixed,
char const* prefixed,
126 unsigned* length,
char const** value)
129 *length = strlen(prefixed);
132 *length = *
reinterpret_cast<unsigned const*
>(prefixed);
133 *value = prefixed +
sizeof(unsigned);
149 #if !defined(JSON_IS_AMALGAMATION)
152 #endif // if !defined(JSON_IS_AMALGAMATION)
156 class JSON_API Exception :
public std::exception {
158 Exception(std::string
const& msg);
159 virtual ~Exception() throw();
160 virtual
char const* what() const throw();
162 std::
string const msg_;
164 class
JSON_API RuntimeError : public Exception {
166 RuntimeError(std::string
const& msg);
168 class JSON_API LogicError :
public Exception {
170 LogicError(std::string
const& msg);
173 Exception::Exception(std::string
const& msg)
176 Exception::~Exception() throw()
178 char const* Exception::what()
const throw()
182 RuntimeError::RuntimeError(std::string
const& msg)
185 LogicError::LogicError(std::string
const& msg)
190 throw RuntimeError(msg);
194 throw LogicError(msg);
205 Value::CommentInfo::CommentInfo() : comment_(0) {}
207 Value::CommentInfo::~CommentInfo() {
212 void Value::CommentInfo::setComment(
const char* text,
size_t len) {
219 text[0] ==
'\0' || text[0] ==
'/',
220 "in Json::Value::setComment(): Comments must start with /");
236 Value::CZString::CZString(
ArrayIndex index) : cstr_(0), index_(index) {}
238 Value::CZString::CZString(
char const* str,
unsigned length, DuplicationPolicy allocate)
242 storage_.policy_ = allocate;
243 storage_.length_ = length;
246 Value::CZString::CZString(
const CZString& other)
247 : cstr_(other.storage_.policy_ != noDuplication && other.cstr_ != 0
251 storage_.policy_ = (other.cstr_
252 ? (other.storage_.policy_ == noDuplication
253 ? noDuplication : duplicate)
254 : other.storage_.policy_);
255 storage_.length_ = other.storage_.length_;
258 Value::CZString::~CZString() {
259 if (cstr_ && storage_.policy_ == duplicate)
263 void Value::CZString::swap(CZString& other) {
264 std::swap(cstr_, other.cstr_);
265 std::swap(index_, other.index_);
268 Value::CZString& Value::CZString::operator=(CZString other) {
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 unsigned min_len = std::min(this_len, other_len);
280 int comp = memcmp(this->cstr_, other.cstr_, min_len);
281 if (comp < 0)
return true;
282 if (comp > 0)
return false;
283 return (this_len < other_len);
286 bool Value::CZString::operator==(
const CZString& other)
const {
287 if (!cstr_)
return index_ == other.index_;
290 unsigned this_len = this->storage_.length_;
291 unsigned other_len = other.storage_.length_;
292 if (this_len != other_len)
return false;
293 int comp = memcmp(this->cstr_, other.cstr_, this_len);
297 ArrayIndex Value::CZString::index()
const {
return index_; }
300 const char* Value::CZString::data()
const {
return cstr_; }
301 unsigned Value::CZString::length()
const {
return storage_.length_; }
302 bool Value::CZString::isStaticString()
const {
return storage_.policy_ == noDuplication; }
333 value_.map_ =
new ObjectValues();
336 value_.bool_ =
false;
350 value_.uint_ = value;
352 #if defined(JSON_HAS_INT64)
359 value_.uint_ = value;
361 #endif // defined(JSON_HAS_INT64)
363 Value::Value(
double value) {
365 value_.real_ = value;
368 Value::Value(
const char* value) {
373 Value::Value(
const char* beginValue,
const char* endValue) {
379 Value::Value(
const std::string& value) {
387 value_.string_ =
const_cast<char*
>(value.
c_str());
390 #ifdef JSON_USE_CPPTL
391 Value::Value(
const CppTL::ConstString& value) {
397 Value::Value(
bool value) {
399 value_.bool_ = value;
403 : type_(other.type_), allocated_(false)
413 value_ = other.value_;
416 if (other.value_.string_ && other.allocated_) {
424 value_.string_ = other.value_.string_;
430 value_.map_ =
new ObjectValues(*other.value_.map_);
435 if (other.comments_) {
438 const CommentInfo& otherComment = other.comments_[comment];
439 if (otherComment.comment_)
440 comments_[comment].setComment(
441 otherComment.comment_, strlen(otherComment.comment_));
481 int temp2 = allocated_;
482 allocated_ = other.allocated_;
483 other.allocated_ = temp2;
502 int typeDelta = type_ - other.type_;
504 return typeDelta < 0 ?
true :
false;
509 return value_.int_ < other.value_.int_;
511 return value_.uint_ < other.value_.uint_;
513 return value_.real_ < other.value_.real_;
515 return value_.bool_ < other.value_.bool_;
518 if ((value_.string_ == 0) || (other.value_.string_ == 0)) {
519 if (other.value_.string_)
return true;
524 char const* this_str;
525 char const* other_str;
528 unsigned min_len = std::min(this_len, other_len);
529 int comp = memcmp(this_str, other_str, min_len);
530 if (comp < 0)
return true;
531 if (comp > 0)
return false;
532 return (this_len < other_len);
536 int delta = int(value_.map_->size() - other.value_.map_->size());
539 return (*value_.map_) < (*other.value_.map_);
558 int temp = other.type_;
565 return value_.int_ == other.value_.int_;
567 return value_.uint_ == other.value_.uint_;
569 return value_.real_ == other.value_.real_;
571 return value_.bool_ == other.value_.bool_;
574 if ((value_.string_ == 0) || (other.value_.string_ == 0)) {
575 return (value_.string_ == other.value_.string_);
579 char const* this_str;
580 char const* other_str;
583 if (this_len != other_len)
return false;
584 int comp = memcmp(this_str, other_str, this_len);
589 return value_.map_->size() == other.value_.map_->size() &&
590 (*value_.map_) == (*other.value_.map_);
601 "in Json::Value::asCString(): requires stringValue");
602 if (value_.string_ == 0)
return 0;
604 char const* this_str;
611 if (value_.string_ == 0)
return false;
614 *end = *str + length;
624 if (value_.string_ == 0)
return "";
626 char const* this_str;
628 return std::string(this_str, this_len);
631 return value_.bool_ ?
"true" :
"false";
643 #ifdef JSON_USE_CPPTL
644 CppTL::ConstString Value::asConstString()
const {
649 return CppTL::ConstString(str, len);
657 return Int(value_.int_);
660 return Int(value_.uint_);
663 "double out of Int range");
664 return Int(value_.real_);
668 return value_.bool_ ? 1 : 0;
679 return UInt(value_.int_);
682 return UInt(value_.uint_);
685 "double out of UInt range");
686 return UInt(value_.real_);
690 return value_.bool_ ? 1 : 0;
697 #if defined(JSON_HAS_INT64)
702 return Int64(value_.int_);
705 return Int64(value_.uint_);
708 "double out of Int64 range");
709 return Int64(value_.real_);
713 return value_.bool_ ? 1 : 0;
724 return UInt64(value_.int_);
726 return UInt64(value_.uint_);
729 "double out of UInt64 range");
730 return UInt64(value_.real_);
734 return value_.bool_ ? 1 : 0;
740 #endif // if defined(JSON_HAS_INT64)
743 #if defined(JSON_NO_INT64)
751 #if defined(JSON_NO_INT64)
761 return static_cast<double>(value_.int_);
763 #if !defined(JSON_USE_INT64_DOUBLE_CONVERSION)
764 return static_cast<double>(value_.uint_);
765 #else // if !defined(JSON_USE_INT64_DOUBLE_CONVERSION)
766 return integerToDouble(value_.uint_);
767 #endif // if !defined(JSON_USE_INT64_DOUBLE_CONVERSION)
773 return value_.bool_ ? 1.0 : 0.0;
783 return static_cast<float>(value_.int_);
785 #if !defined(JSON_USE_INT64_DOUBLE_CONVERSION)
786 return static_cast<float>(value_.uint_);
787 #else // if !defined(JSON_USE_INT64_DOUBLE_CONVERSION)
788 return integerToDouble(value_.uint_);
789 #endif // if !defined(JSON_USE_INT64_DOUBLE_CONVERSION)
791 return static_cast<float>(value_.real_);
795 return value_.bool_ ? 1.0f : 0.0f;
809 return value_.int_ ?
true :
false;
811 return value_.uint_ ?
true :
false;
813 return value_.real_ ?
true :
false;
826 (type_ ==
arrayValue && value_.map_->size() == 0) ||
827 (type_ ==
objectValue && value_.map_->size() == 0) ||
864 if (!value_.map_->empty()) {
865 ObjectValues::const_iterator itLast = value_.map_->end();
867 return (*itLast).first.index() + 1;
889 "in Json::Value::clear(): requires complex value");
893 value_.map_->clear();
902 "in Json::Value::resize(): requires arrayValue");
908 else if (newSize > oldSize)
909 (*this)[newSize - 1];
911 for (
ArrayIndex index = newSize; index < oldSize; ++index) {
912 value_.map_->erase(index);
914 assert(
size() == newSize);
921 "in Json::Value::operator[](ArrayIndex): requires arrayValue");
925 ObjectValues::iterator it = value_.map_->lower_bound(key);
926 if (it != value_.map_->end() && (*it).first == key)
929 ObjectValues::value_type defaultValue(key,
nullRef);
930 it = value_.map_->insert(it, defaultValue);
937 "in Json::Value::operator[](int index): index cannot be negative");
944 "in Json::Value::operator[](ArrayIndex)const: requires arrayValue");
948 ObjectValues::const_iterator it = value_.map_->find(key);
949 if (it == value_.map_->end())
957 "in Json::Value::operator[](int index) const: index cannot be negative");
961 void Value::initBasic(
ValueType type,
bool allocated) {
963 allocated_ = allocated;
970 Value& Value::resolveReference(
const char* key) {
973 "in Json::Value::resolveReference(): requires objectValue");
977 key, static_cast<unsigned>(strlen(key)), CZString::noDuplication);
978 ObjectValues::iterator it = value_.map_->lower_bound(actualKey);
979 if (it != value_.map_->end() && (*it).first == actualKey)
982 ObjectValues::value_type defaultValue(actualKey,
nullRef);
983 it = value_.map_->insert(it, defaultValue);
984 Value& value = (*it).second;
989 Value& Value::resolveReference(
char const* key,
char const* end)
993 "in Json::Value::resolveReference(key, end): requires objectValue");
997 key, static_cast<unsigned>(end-key), CZString::duplicateOnCopy);
998 ObjectValues::iterator it = value_.map_->lower_bound(actualKey);
999 if (it != value_.map_->end() && (*it).first == actualKey)
1000 return (*it).second;
1002 ObjectValues::value_type defaultValue(actualKey,
nullRef);
1003 it = value_.map_->insert(it, defaultValue);
1004 Value& value = (*it).second;
1009 const Value* value = &((*this)[index]);
1010 return value == &
nullRef ? defaultValue : *value;
1019 "in Json::Value::find(key, end, found): requires objectValue or nullValue");
1021 CZString actualKey(key, static_cast<unsigned>(end-key), CZString::noDuplication);
1022 ObjectValues::const_iterator it = value_.map_->find(actualKey);
1023 if (it == value_.map_->end())
return NULL;
1024 return &(*it).second;
1028 Value const* found =
find(key, key + strlen(key));
1034 Value const* found =
find(key.data(), key.data() + key.length());
1040 return resolveReference(key, key + strlen(key));
1044 return resolveReference(key.data(), key.data() + key.length());
1048 return resolveReference(key.
c_str());
1051 #ifdef JSON_USE_CPPTL
1053 return resolveReference(key.c_str(), key.end_c_str());
1057 Value const* found =
find(key.c_str(), key.end_c_str());
1068 return !found ? defaultValue : *found;
1072 return get(key, key + strlen(key), defaultValue);
1076 return get(key.data(), key.data() + key.length(), defaultValue);
1085 CZString actualKey(key, static_cast<unsigned>(end-key), CZString::noDuplication);
1086 ObjectValues::iterator it = value_.map_->find(actualKey);
1087 if (it == value_.map_->end())
1089 *removed = it->second;
1090 value_.map_->erase(it);
1099 return removeMember(key.data(), key.data() + key.length(), removed);
1104 "in Json::Value::removeMember(): requires objectValue");
1121 CZString key(index);
1122 ObjectValues::iterator it = value_.map_->find(key);
1123 if (it == value_.map_->end()) {
1126 *removed = it->second;
1129 for (
ArrayIndex i = index; i < (oldSize - 1); ++i){
1131 (*value_.map_)[key] = (*
this)[i + 1];
1134 CZString keyLast(oldSize - 1);
1135 ObjectValues::iterator itLast = value_.map_->find(keyLast);
1136 value_.map_->erase(itLast);
1140 #ifdef JSON_USE_CPPTL
1142 const Value& defaultValue)
const {
1143 return get(key.c_str(), key.end_c_str(), defaultValue);
1150 return NULL != value;
1154 return isMember(key, key + strlen(key));
1158 return isMember(key.data(), key.data() + key.length());
1161 #ifdef JSON_USE_CPPTL
1163 return isMember(key.c_str(), key.end_c_str());
1170 "in Json::Value::getMemberNames(), value must be objectValue");
1174 members.reserve(value_.map_->size());
1175 ObjectValues::const_iterator it = value_.map_->begin();
1176 ObjectValues::const_iterator itEnd = value_.map_->end();
1177 for (; it != itEnd; ++it) {
1178 members.push_back(std::string((*it).first.data(),
1179 (*it).first.length()));
1210 double integral_part;
1211 return modf(d, &integral_part) == 0.0;
1225 return value_.real_ >=
minInt && value_.real_ <=
maxInt &&
1238 return value_.uint_ <=
maxUInt;
1240 return value_.real_ >= 0 && value_.real_ <=
maxUInt &&
1249 #if defined(JSON_HAS_INT64)
1259 return value_.real_ >= double(
minInt64) &&
1264 #endif // JSON_HAS_INT64
1269 #if defined(JSON_HAS_INT64)
1272 return value_.int_ >= 0;
1284 #endif // JSON_HAS_INT64
1289 #if defined(JSON_HAS_INT64)
1309 if ((len > 0) && (comment[len-1] ==
'\n')) {
1313 comments_[placement].setComment(comment, len);
1317 setComment(comment, strlen(comment), placement);
1321 setComment(comment.c_str(), comment.length(), placement);
1325 return comments_ != 0 && comments_[placement].comment_ != 0;
1330 return comments_[placement].comment_;
1336 return writer.
write(*
this);
1370 return iterator(value_.map_->begin());
1383 return iterator(value_.map_->end());
1397 : key_(), index_(index), kind_(kindIndex) {}
1400 : key_(key), index_(), kind_(kindKey) {}
1403 : key_(key.c_str()), index_(), kind_(kindKey) {}
1423 void Path::makePath(
const std::string& path,
const InArgs& in) {
1424 const char* current = path.c_str();
1425 const char* end = current + path.length();
1426 InArgs::const_iterator itInArg = in.begin();
1427 while (current != end) {
1428 if (*current ==
'[') {
1430 if (*current ==
'%')
1431 addPathInArg(path, in, itInArg, PathArgument::kindIndex);
1434 for (; current != end && *current >=
'0' && *current <=
'9'; ++current)
1435 index = index * 10 +
ArrayIndex(*current -
'0');
1436 args_.push_back(index);
1438 if (current == end || *current++ !=
']')
1439 invalidPath(path,
int(current - path.c_str()));
1440 }
else if (*current ==
'%') {
1441 addPathInArg(path, in, itInArg, PathArgument::kindKey);
1443 }
else if (*current ==
'.') {
1446 const char* beginName = current;
1447 while (current != end && !strchr(
"[.", *current))
1449 args_.push_back(std::string(beginName, current));
1454 void Path::addPathInArg(
const std::string& ,
1456 InArgs::const_iterator& itInArg,
1457 PathArgument::Kind kind) {
1458 if (itInArg == in.end()) {
1460 }
else if ((*itInArg)->kind_ != kind) {
1463 args_.push_back(**itInArg);
1467 void Path::invalidPath(
const std::string& ,
int ) {
1472 const Value* node = &root;
1473 for (Args::const_iterator it = args_.begin(); it != args_.end(); ++it) {
1475 if (arg.kind_ == PathArgument::kindIndex) {
1479 node = &((*node)[arg.index_]);
1480 }
else if (arg.kind_ == PathArgument::kindKey) {
1484 node = &((*node)[arg.key_]);
1495 const Value* node = &root;
1496 for (Args::const_iterator it = args_.begin(); it != args_.end(); ++it) {
1498 if (arg.kind_ == PathArgument::kindIndex) {
1500 return defaultValue;
1501 node = &((*node)[arg.index_]);
1502 }
else if (arg.kind_ == PathArgument::kindKey) {
1504 return defaultValue;
1505 node = &((*node)[arg.key_]);
1507 return defaultValue;
1514 Value* node = &root;
1515 for (Args::const_iterator it = args_.begin(); it != args_.end(); ++it) {
1517 if (arg.kind_ == PathArgument::kindIndex) {
1521 node = &((*node)[arg.index_]);
1522 }
else if (arg.kind_ == PathArgument::kindKey) {
1526 node = &((*node)[arg.key_]);