6 #if !defined(JSON_IS_AMALGAMATION)
10 #ifndef JSON_USE_SIMPLE_INTERNAL_ALLOCATOR
12 #endif // #ifndef JSON_USE_SIMPLE_INTERNAL_ALLOCATOR
13 #endif // if !defined(JSON_IS_AMALGAMATION)
20 #include <cpptl/conststring.h>
24 #define JSON_ASSERT_UNREACHABLE assert(false)
31 #if defined(__ARMEL__)
32 #define ALIGNAS(byte_alignment) __attribute__((aligned(byte_alignment)))
34 #define ALIGNAS(byte_alignment)
42 #if defined(JSON_HAS_INT64)
50 #endif // defined(JSON_HAS_INT64)
56 static const unsigned int unknown = (unsigned)-1;
58 #if !defined(JSON_USE_INT64_DOUBLE_CONVERSION)
59 template <
typename T,
typename U>
60 static inline bool InRange(
double d, T min, U max) {
61 return d >= min && d <= max;
63 #else // if !defined(JSON_USE_INT64_DOUBLE_CONVERSION)
64 static inline double integerToDouble(
Json::UInt64 value) {
65 return static_cast<double>(
Int64(value / 2)) * 2.0 +
Int64(value & 1);
68 template <
typename T>
static inline double integerToDouble(T value) {
69 return static_cast<double>(value);
72 template <
typename T,
typename U>
73 static inline bool InRange(
double d, T min, U max) {
74 return d >= integerToDouble(min) && d <= integerToDouble(max);
76 #endif // if !defined(JSON_USE_INT64_DOUBLE_CONVERSION)
86 unsigned int length =
unknown) {
88 length = (
unsigned int)strlen(value);
95 char *newString = static_cast<char *>(malloc(length + 1));
97 "in Json::Value::duplicateStringValue(): "
98 "Failed to allocate string value buffer");
99 memcpy(newString, value, length);
100 newString[length] = 0;
120 #if !defined(JSON_IS_AMALGAMATION)
121 #ifdef JSON_VALUE_USE_INTERNAL_MAP
124 #endif // JSON_VALUE_USE_INTERNAL_MAP
127 #endif // if !defined(JSON_IS_AMALGAMATION)
139 Value::CommentInfo::CommentInfo() : comment_(0) {}
141 Value::CommentInfo::~CommentInfo() {
146 void Value::CommentInfo::setComment(
const char *text) {
151 text[0] ==
'\0' || text[0] ==
'/',
152 "in Json::Value::setComment(): Comments must start with /");
164 #ifndef JSON_VALUE_USE_INTERNAL_MAP
169 Value::CZString::CZString(
ArrayIndex index) : cstr_(0), index_(index) {}
171 Value::CZString::CZString(
const char *cstr, DuplicationPolicy allocate)
175 Value::CZString::CZString(
const CZString &other)
176 : cstr_(other.index_ != noDuplication && other.cstr_ != 0
180 ? (other.index_ == noDuplication ? noDuplication : duplicate)
183 Value::CZString::~CZString() {
184 if (cstr_ && index_ == duplicate)
188 void Value::CZString::swap(CZString &other) {
189 std::swap(cstr_, other.cstr_);
190 std::swap(index_, other.index_);
193 Value::CZString &Value::CZString::operator=(
const CZString &other) {
194 CZString temp(other);
199 bool Value::CZString::operator<(
const CZString &other)
const {
201 return strcmp(cstr_, other.cstr_) < 0;
202 return index_ < other.index_;
205 bool Value::CZString::operator==(
const CZString &other)
const {
207 return strcmp(cstr_, other.cstr_) == 0;
208 return index_ == other.index_;
211 ArrayIndex Value::CZString::index()
const {
return index_; }
213 const char *Value::CZString::c_str()
const {
return cstr_; }
215 bool Value::CZString::isStaticString()
const {
return index_ == noDuplication; }
217 #endif // ifndef JSON_VALUE_USE_INTERNAL_MAP
232 : type_(type), allocated_(false)
233 #ifdef JSON_VALUE_USE_INTERNAL_MAP
238 comments_(0), start_(0), limit_(0) {
252 #ifndef JSON_VALUE_USE_INTERNAL_MAP
255 value_.map_ =
new ObjectValues();
266 value_.bool_ =
false;
275 #ifdef JSON_VALUE_USE_INTERNAL_MAP
280 comments_(0), start_(0), limit_(0) {
281 value_.uint_ = value;
285 : type_(
intValue), allocated_(false)
286 #ifdef JSON_VALUE_USE_INTERNAL_MAP
291 comments_(0), start_(0), limit_(0) {
295 #if defined(JSON_HAS_INT64)
297 : type_(
intValue), allocated_(false)
298 #ifdef JSON_VALUE_USE_INTERNAL_MAP
303 comments_(0), start_(0), limit_(0) {
309 #ifdef JSON_VALUE_USE_INTERNAL_MAP
314 comments_(0), start_(0), limit_(0) {
315 value_.uint_ = value;
317 #endif // defined(JSON_HAS_INT64)
321 #ifdef JSON_VALUE_USE_INTERNAL_MAP
326 comments_(0), start_(0), limit_(0) {
327 value_.real_ = value;
332 #ifdef JSON_VALUE_USE_INTERNAL_MAP
337 comments_(0), start_(0), limit_(0) {
343 #ifdef JSON_VALUE_USE_INTERNAL_MAP
348 comments_(0), start_(0), limit_(0) {
355 #ifdef JSON_VALUE_USE_INTERNAL_MAP
360 comments_(0), start_(0), limit_(0) {
367 #ifdef JSON_VALUE_USE_INTERNAL_MAP
372 comments_(0), start_(0), limit_(0) {
373 value_.string_ = const_cast<char *>(value.
c_str());
376 #ifdef JSON_USE_CPPTL
379 #ifdef JSON_VALUE_USE_INTERNAL_MAP
384 comments_(0), start_(0), limit_(0) {
391 #ifdef JSON_VALUE_USE_INTERNAL_MAP
396 comments_(0), start_(0), limit_(0) {
397 value_.bool_ = value;
401 : type_(other.type_), allocated_(false)
402 #ifdef JSON_VALUE_USE_INTERNAL_MAP
407 comments_(0), start_(other.start_), limit_(other.limit_) {
414 value_ = other.value_;
417 if (other.value_.string_) {
425 #ifndef JSON_VALUE_USE_INTERNAL_MAP
428 value_.map_ =
new ObjectValues(*other.value_.map_);
441 if (other.comments_) {
444 const CommentInfo &otherComment = other.comments_[comment];
445 if (otherComment.comment_)
446 comments_[comment].setComment(otherComment.comment_);
463 #ifndef JSON_VALUE_USE_INTERNAL_MAP
494 std::swap(value_, other.value_);
495 int temp2 = allocated_;
496 allocated_ = other.allocated_;
497 other.allocated_ = temp2;
498 std::swap(start_, other.start_);
499 std::swap(limit_, other.limit_);
513 int typeDelta = type_ - other.type_;
515 return typeDelta < 0 ? true :
false;
520 return value_.int_ < other.value_.int_;
522 return value_.uint_ < other.value_.uint_;
524 return value_.real_ < other.value_.real_;
526 return value_.bool_ < other.value_.bool_;
528 return (value_.string_ == 0 && other.value_.string_) ||
529 (other.value_.string_ && value_.string_ &&
530 strcmp(value_.string_, other.value_.string_) < 0);
531 #ifndef JSON_VALUE_USE_INTERNAL_MAP
534 int delta = int(value_.map_->size() - other.value_.map_->size());
537 return (*value_.map_) < (*other.value_.map_);
541 return value_.array_->compare(*(other.value_.array_)) < 0;
543 return value_.map_->compare(*(other.value_.map_)) < 0;
562 int temp = other.type_;
569 return value_.int_ == other.value_.int_;
571 return value_.uint_ == other.value_.uint_;
573 return value_.real_ == other.value_.real_;
575 return value_.bool_ == other.value_.bool_;
577 return (value_.string_ == other.value_.string_) ||
578 (other.value_.string_ && value_.string_ &&
579 strcmp(value_.string_, other.value_.string_) == 0);
580 #ifndef JSON_VALUE_USE_INTERNAL_MAP
583 return value_.map_->size() == other.value_.map_->size() &&
584 (*value_.map_) == (*other.value_.map_);
587 return value_.array_->compare(*(other.value_.array_)) == 0;
589 return value_.map_->compare(*(other.value_.map_)) == 0;
601 "in Json::Value::asCString(): requires stringValue");
602 return value_.string_;
610 return value_.string_ ? value_.string_ :
"";
612 return value_.bool_ ?
"true" :
"false";
624 #ifdef JSON_USE_CPPTL
625 CppTL::ConstString Value::asConstString()
const {
626 return CppTL::ConstString(
asString().c_str());
634 return Int(value_.int_);
637 return Int(value_.uint_);
640 "double out of Int range");
641 return Int(value_.real_);
645 return value_.bool_ ? 1 : 0;
656 return UInt(value_.int_);
659 return UInt(value_.uint_);
662 "double out of UInt range");
663 return UInt(value_.real_);
667 return value_.bool_ ? 1 : 0;
674 #if defined(JSON_HAS_INT64)
679 return Int64(value_.int_);
682 return Int64(value_.uint_);
685 "double out of Int64 range");
686 return Int64(value_.real_);
690 return value_.bool_ ? 1 : 0;
701 return UInt64(value_.int_);
703 return UInt64(value_.uint_);
706 "double out of UInt64 range");
707 return UInt64(value_.real_);
711 return value_.bool_ ? 1 : 0;
717 #endif // if defined(JSON_HAS_INT64)
720 #if defined(JSON_NO_INT64)
728 #if defined(JSON_NO_INT64)
738 return static_cast<double>(value_.int_);
740 #if !defined(JSON_USE_INT64_DOUBLE_CONVERSION)
741 return static_cast<double>(value_.uint_);
742 #else // if !defined(JSON_USE_INT64_DOUBLE_CONVERSION)
743 return integerToDouble(value_.uint_);
744 #endif // if !defined(JSON_USE_INT64_DOUBLE_CONVERSION)
750 return value_.bool_ ? 1.0 : 0.0;
760 return static_cast<float>(value_.int_);
762 #if !defined(JSON_USE_INT64_DOUBLE_CONVERSION)
763 return static_cast<float>(value_.uint_);
764 #else // if !defined(JSON_USE_INT64_DOUBLE_CONVERSION)
765 return integerToDouble(value_.uint_);
766 #endif // if !defined(JSON_USE_INT64_DOUBLE_CONVERSION)
768 return static_cast<float>(value_.real_);
772 return value_.bool_ ? 1.0f : 0.0f;
786 return value_.int_ ? true :
false;
788 return value_.uint_ ? true :
false;
790 return value_.real_ ? true :
false;
803 (type_ ==
arrayValue && value_.map_->size() == 0) ||
804 (type_ ==
objectValue && value_.map_->size() == 0) ||
840 #ifndef JSON_VALUE_USE_INTERNAL_MAP
842 if (!value_.map_->empty()) {
843 ObjectValues::const_iterator itLast = value_.map_->end();
845 return (*itLast).first.index() + 1;
852 return Int(value_.array_->size());
854 return Int(value_.map_->size());
873 "in Json::Value::clear(): requires complex value");
877 #ifndef JSON_VALUE_USE_INTERNAL_MAP
880 value_.map_->clear();
884 value_.array_->clear();
887 value_.map_->clear();
897 "in Json::Value::resize(): requires arrayValue");
900 #ifndef JSON_VALUE_USE_INTERNAL_MAP
904 else if (newSize > oldSize)
905 (*this)[newSize - 1];
907 for (
ArrayIndex index = newSize; index < oldSize; ++index) {
908 value_.map_->erase(index);
910 assert(
size() == newSize);
913 value_.array_->resize(newSize);
920 "in Json::Value::operator[](ArrayIndex): requires arrayValue");
923 #ifndef JSON_VALUE_USE_INTERNAL_MAP
925 ObjectValues::iterator it = value_.map_->lower_bound(key);
926 if (it != value_.map_->end() && (*it).first == key)
929 ObjectValues::value_type defaultValue(key,
null);
930 it = value_.map_->insert(it, defaultValue);
933 return value_.array_->resolveReference(index);
940 "in Json::Value::operator[](int index): index cannot be negative");
947 "in Json::Value::operator[](ArrayIndex)const: requires arrayValue");
950 #ifndef JSON_VALUE_USE_INTERNAL_MAP
952 ObjectValues::const_iterator it = value_.map_->find(key);
953 if (it == value_.map_->end())
957 Value *value = value_.array_->find(index);
958 return value ? *value :
null;
965 "in Json::Value::operator[](int index) const: index cannot be negative");
970 return resolveReference(key,
false);
973 Value &Value::resolveReference(
const char *key,
bool isStatic) {
976 "in Json::Value::resolveReference(): requires objectValue");
979 #ifndef JSON_VALUE_USE_INTERNAL_MAP
981 key, isStatic ? CZString::noDuplication : CZString::duplicateOnCopy);
982 ObjectValues::iterator it = value_.map_->lower_bound(actualKey);
983 if (it != value_.map_->end() && (*it).first == actualKey)
986 ObjectValues::value_type defaultValue(actualKey,
null);
987 it = value_.map_->insert(it, defaultValue);
988 Value &value = (*it).second;
991 return value_.map_->resolveReference(key, isStatic);
996 const Value *value = &((*this)[index]);
997 return value == &
null ? defaultValue : *value;
1005 "in Json::Value::operator[](char const*)const: requires objectValue");
1008 #ifndef JSON_VALUE_USE_INTERNAL_MAP
1009 CZString actualKey(key, CZString::noDuplication);
1010 ObjectValues::const_iterator it = value_.map_->find(actualKey);
1011 if (it == value_.map_->end())
1013 return (*it).second;
1015 const Value *value = value_.map_->find(key);
1016 return value ? *value :
null;
1021 return (*
this)[key.c_str()];
1025 return (*
this)[key.c_str()];
1029 return resolveReference(key,
true);
1032 #ifdef JSON_USE_CPPTL
1034 return (*
this)[key.c_str()];
1038 return (*
this)[key.c_str()];
1045 const Value *value = &((*this)[key]);
1046 return value == &
null ? defaultValue : *value;
1050 return get(key.c_str(), defaultValue);
1055 "in Json::Value::removeMember(): requires objectValue");
1058 #ifndef JSON_VALUE_USE_INTERNAL_MAP
1059 CZString actualKey(key, CZString::noDuplication);
1060 ObjectValues::iterator it = value_.map_->find(actualKey);
1061 if (it == value_.map_->end())
1063 Value old(it->second);
1064 value_.map_->erase(it);
1067 Value *value = value_.map_->find(key);
1070 value_.map_.remove(key);
1082 #ifdef JSON_USE_CPPTL
1084 const Value &defaultValue)
const {
1085 return get(key.c_str(), defaultValue);
1090 const Value *value = &((*this)[key]);
1091 return value != &
null;
1098 #ifdef JSON_USE_CPPTL
1107 "in Json::Value::getMemberNames(), value must be objectValue");
1111 members.reserve(value_.map_->size());
1112 #ifndef JSON_VALUE_USE_INTERNAL_MAP
1113 ObjectValues::const_iterator it = value_.map_->begin();
1114 ObjectValues::const_iterator itEnd = value_.map_->end();
1115 for (; it != itEnd; ++it)
1116 members.push_back(std::string((*it).first.c_str()));
1118 ValueInternalMap::IteratorState it;
1119 ValueInternalMap::IteratorState itEnd;
1120 value_.map_->makeBeginIterator(it);
1121 value_.map_->makeEndIterator(itEnd);
1122 for (; !ValueInternalMap::equals(it, itEnd); ValueInternalMap::increment(it))
1123 members.push_back(std::string(ValueInternalMap::key(it)));
1154 double integral_part;
1155 return modf(d, &integral_part) == 0.0;
1169 return value_.real_ >=
minInt && value_.real_ <=
maxInt &&
1182 return value_.uint_ <=
maxUInt;
1184 return value_.real_ >= 0 && value_.real_ <=
maxUInt &&
1193 #if defined(JSON_HAS_INT64)
1203 return value_.real_ >= double(
minInt64) &&
1208 #endif // JSON_HAS_INT64
1213 #if defined(JSON_HAS_INT64)
1216 return value_.int_ >= 0;
1228 #endif // JSON_HAS_INT64
1233 #if defined(JSON_HAS_INT64)
1253 comments_[placement].setComment(comment);
1261 return comments_ != 0 && comments_[placement].comment_ != 0;
1266 return comments_[placement].comment_;
1280 return writer.
write(*
this);
1285 #ifdef JSON_VALUE_USE_INTERNAL_MAP
1287 if (value_.array_) {
1288 ValueInternalArray::IteratorState it;
1289 value_.array_->makeBeginIterator(it);
1295 ValueInternalMap::IteratorState it;
1296 value_.map_->makeBeginIterator(it);
1315 #ifdef JSON_VALUE_USE_INTERNAL_MAP
1317 if (value_.array_) {
1318 ValueInternalArray::IteratorState it;
1319 value_.array_->makeEndIterator(it);
1325 ValueInternalMap::IteratorState it;
1326 value_.map_->makeEndIterator(it);
1345 #ifdef JSON_VALUE_USE_INTERNAL_MAP
1347 if (value_.array_) {
1348 ValueInternalArray::IteratorState it;
1349 value_.array_->makeBeginIterator(it);
1355 ValueInternalMap::IteratorState it;
1356 value_.map_->makeBeginIterator(it);
1364 return iterator(value_.map_->begin());
1375 #ifdef JSON_VALUE_USE_INTERNAL_MAP
1377 if (value_.array_) {
1378 ValueInternalArray::IteratorState it;
1379 value_.array_->makeEndIterator(it);
1385 ValueInternalMap::IteratorState it;
1386 value_.map_->makeEndIterator(it);
1394 return iterator(value_.map_->end());
1409 : key_(), index_(index), kind_(kindIndex) {}
1412 : key_(key), index_(), kind_(kindKey) {}
1415 : key_(key.c_str()), index_(), kind_(kindKey) {}
1435 void Path::makePath(
const std::string &path,
const InArgs &
in) {
1436 const char *current = path.c_str();
1437 const char *end = current + path.length();
1438 InArgs::const_iterator itInArg =
in.begin();
1439 while (current != end) {
1440 if (*current ==
'[') {
1442 if (*current ==
'%')
1443 addPathInArg(path,
in, itInArg, PathArgument::kindIndex);
1446 for (; current != end && *current >=
'0' && *current <=
'9'; ++current)
1447 index = index * 10 +
ArrayIndex(*current -
'0');
1448 args_.push_back(index);
1450 if (current == end || *current++ !=
']')
1451 invalidPath(path,
int(current - path.c_str()));
1452 }
else if (*current ==
'%') {
1453 addPathInArg(path,
in, itInArg, PathArgument::kindKey);
1455 }
else if (*current ==
'.') {
1458 const char *beginName = current;
1459 while (current != end && !strchr(
"[.", *current))
1461 args_.push_back(std::string(beginName, current));
1466 void Path::addPathInArg(
const std::string & ,
1468 InArgs::const_iterator &itInArg,
1469 PathArgument::Kind kind) {
1470 if (itInArg ==
in.end()) {
1472 }
else if ((*itInArg)->kind_ != kind) {
1475 args_.push_back(**itInArg);
1479 void Path::invalidPath(
const std::string & ,
int ) {
1484 const Value *node = &root;
1485 for (Args::const_iterator it = args_.begin(); it != args_.end(); ++it) {
1487 if (arg.kind_ == PathArgument::kindIndex) {
1491 node = &((*node)[arg.index_]);
1492 }
else if (arg.kind_ == PathArgument::kindKey) {
1496 node = &((*node)[arg.key_]);
1507 const Value *node = &root;
1508 for (Args::const_iterator it = args_.begin(); it != args_.end(); ++it) {
1510 if (arg.kind_ == PathArgument::kindIndex) {
1512 return defaultValue;
1513 node = &((*node)[arg.index_]);
1514 }
else if (arg.kind_ == PathArgument::kindKey) {
1516 return defaultValue;
1517 node = &((*node)[arg.key_]);
1519 return defaultValue;
1526 Value *node = &root;
1527 for (Args::const_iterator it = args_.begin(); it != args_.end(); ++it) {
1529 if (arg.kind_ == PathArgument::kindIndex) {
1533 node = &((*node)[arg.index_]);
1534 }
else if (arg.kind_ == PathArgument::kindKey) {
1538 node = &((*node)[arg.key_]);