00001
00002
00003
00004
00005
00006 #if !defined(JSON_IS_AMALGAMATION)
00007 #include <json/assertions.h>
00008 #include <json/value.h>
00009 #include <json/writer.h>
00010 #endif // if !defined(JSON_IS_AMALGAMATION)
00011 #include <math.h>
00012 #include <sstream>
00013 #include <utility>
00014 #include <cstring>
00015 #include <cassert>
00016 #ifdef JSON_USE_CPPTL
00017 #include <cpptl/conststring.h>
00018 #endif
00019 #include <cstddef>
00020 #include <algorithm>
00021
00022 #define JSON_ASSERT_UNREACHABLE assert(false)
00023
00024 namespace Json {
00025
00026
00027
00028
00029 #if defined(__ARMEL__)
00030 #define ALIGNAS(byte_alignment) __attribute__((aligned(byte_alignment)))
00031 #else
00032
00033 const Value Value::null;
00034 #define ALIGNAS(byte_alignment)
00035 #endif
00036 static const unsigned char ALIGNAS(8) kNull[sizeof(Value)] = { 0 };
00037 const unsigned char& kNullRef = kNull[0];
00038 const Value& Value::nullRef = reinterpret_cast<const Value&>(kNullRef);
00039
00040 const Int Value::minInt = Int(~(UInt(-1) / 2));
00041 const Int Value::maxInt = Int(UInt(-1) / 2);
00042 const UInt Value::maxUInt = UInt(-1);
00043 #if defined(JSON_HAS_INT64)
00044 const Int64 Value::minInt64 = Int64(~(UInt64(-1) / 2));
00045 const Int64 Value::maxInt64 = Int64(UInt64(-1) / 2);
00046 const UInt64 Value::maxUInt64 = UInt64(-1);
00047
00048
00049
00050 static const double maxUInt64AsDouble = 18446744073709551615.0;
00051 #endif // defined(JSON_HAS_INT64)
00052 const LargestInt Value::minLargestInt = LargestInt(~(LargestUInt(-1) / 2));
00053 const LargestInt Value::maxLargestInt = LargestInt(LargestUInt(-1) / 2);
00054 const LargestUInt Value::maxLargestUInt = LargestUInt(-1);
00055
00056 #if !defined(JSON_USE_INT64_DOUBLE_CONVERSION)
00057 template <typename T, typename U>
00058 static inline bool InRange(double d, T min, U max) {
00059 return d >= min && d <= max;
00060 }
00061 #else // if !defined(JSON_USE_INT64_DOUBLE_CONVERSION)
00062 static inline double integerToDouble(Json::UInt64 value) {
00063 return static_cast<double>(Int64(value / 2)) * 2.0 + Int64(value & 1);
00064 }
00065
00066 template <typename T> static inline double integerToDouble(T value) {
00067 return static_cast<double>(value);
00068 }
00069
00070 template <typename T, typename U>
00071 static inline bool InRange(double d, T min, U max) {
00072 return d >= integerToDouble(min) && d <= integerToDouble(max);
00073 }
00074 #endif // if !defined(JSON_USE_INT64_DOUBLE_CONVERSION)
00075
00083 static inline char* duplicateStringValue(const char* value,
00084 size_t length) {
00085
00086
00087 if (length >= (size_t)Value::maxInt)
00088 length = Value::maxInt - 1;
00089
00090 char* newString = static_cast<char*>(malloc(length + 1));
00091 if (newString == NULL) {
00092 throwRuntimeError(
00093 "in Json::Value::duplicateStringValue(): "
00094 "Failed to allocate string value buffer");
00095 }
00096 memcpy(newString, value, length);
00097 newString[length] = 0;
00098 return newString;
00099 }
00100
00101
00102
00103 static inline char* duplicateAndPrefixStringValue(
00104 const char* value,
00105 unsigned int length)
00106 {
00107
00108
00109 JSON_ASSERT_MESSAGE(length <= (unsigned)Value::maxInt - sizeof(unsigned) - 1U,
00110 "in Json::Value::duplicateAndPrefixStringValue(): "
00111 "length too big for prefixing");
00112 unsigned actualLength = length + sizeof(unsigned) + 1U;
00113 char* newString = static_cast<char*>(malloc(actualLength));
00114 if (newString == 0) {
00115 throwRuntimeError(
00116 "in Json::Value::duplicateAndPrefixStringValue(): "
00117 "Failed to allocate string value buffer");
00118 }
00119 *reinterpret_cast<unsigned*>(newString) = length;
00120 memcpy(newString + sizeof(unsigned), value, length);
00121 newString[actualLength - 1U] = 0;
00122 return newString;
00123 }
00124 inline static void decodePrefixedString(
00125 bool isPrefixed, char const* prefixed,
00126 unsigned* length, char const** value)
00127 {
00128 if (!isPrefixed) {
00129 *length = strlen(prefixed);
00130 *value = prefixed;
00131 } else {
00132 *length = *reinterpret_cast<unsigned const*>(prefixed);
00133 *value = prefixed + sizeof(unsigned);
00134 }
00135 }
00138 static inline void releaseStringValue(char* value) { free(value); }
00139
00140 }
00141
00142
00143
00144
00145
00146
00147
00148
00149 #if !defined(JSON_IS_AMALGAMATION)
00150
00151 #include "json_valueiterator.inl"
00152 #endif // if !defined(JSON_IS_AMALGAMATION)
00153
00154 namespace Json {
00155
00156 class JSON_API Exception : public std::exception {
00157 public:
00158 Exception(std::string const& msg);
00159 virtual ~Exception() throw();
00160 virtual char const* what() const throw();
00161 protected:
00162 std::string const msg_;
00163 };
00164 class JSON_API RuntimeError : public Exception {
00165 public:
00166 RuntimeError(std::string const& msg);
00167 };
00168 class JSON_API LogicError : public Exception {
00169 public:
00170 LogicError(std::string const& msg);
00171 };
00172
00173 Exception::Exception(std::string const& msg)
00174 : msg_(msg)
00175 {}
00176 Exception::~Exception() throw()
00177 {}
00178 char const* Exception::what() const throw()
00179 {
00180 return msg_.c_str();
00181 }
00182 RuntimeError::RuntimeError(std::string const& msg)
00183 : Exception(msg)
00184 {}
00185 LogicError::LogicError(std::string const& msg)
00186 : Exception(msg)
00187 {}
00188 void throwRuntimeError(std::string const& msg)
00189 {
00190 throw RuntimeError(msg);
00191 }
00192 void throwLogicError(std::string const& msg)
00193 {
00194 throw LogicError(msg);
00195 }
00196
00197
00198
00199
00200
00201
00202
00203
00204
00205 Value::CommentInfo::CommentInfo() : comment_(0) {}
00206
00207 Value::CommentInfo::~CommentInfo() {
00208 if (comment_)
00209 releaseStringValue(comment_);
00210 }
00211
00212 void Value::CommentInfo::setComment(const char* text, size_t len) {
00213 if (comment_) {
00214 releaseStringValue(comment_);
00215 comment_ = 0;
00216 }
00217 JSON_ASSERT(text != 0);
00218 JSON_ASSERT_MESSAGE(
00219 text[0] == '\0' || text[0] == '/',
00220 "in Json::Value::setComment(): Comments must start with /");
00221
00222 comment_ = duplicateStringValue(text, len);
00223 }
00224
00225
00226
00227
00228
00229
00230
00231
00232
00233
00234
00235
00236 Value::CZString::CZString(ArrayIndex index) : cstr_(0), index_(index) {}
00237
00238 Value::CZString::CZString(char const* str, unsigned length, DuplicationPolicy allocate)
00239 : cstr_(str)
00240 {
00241
00242 storage_.policy_ = allocate;
00243 storage_.length_ = length;
00244 }
00245
00246 Value::CZString::CZString(const CZString& other)
00247 : cstr_(other.storage_.policy_ != noDuplication && other.cstr_ != 0
00248 ? duplicateStringValue(other.cstr_, other.storage_.length_)
00249 : other.cstr_)
00250 {
00251 storage_.policy_ = (other.cstr_
00252 ? (other.storage_.policy_ == noDuplication
00253 ? noDuplication : duplicate)
00254 : other.storage_.policy_);
00255 storage_.length_ = other.storage_.length_;
00256 }
00257
00258 Value::CZString::~CZString() {
00259 if (cstr_ && storage_.policy_ == duplicate)
00260 releaseStringValue(const_cast<char*>(cstr_));
00261 }
00262
00263 void Value::CZString::swap(CZString& other) {
00264 std::swap(cstr_, other.cstr_);
00265 std::swap(index_, other.index_);
00266 }
00267
00268 Value::CZString& Value::CZString::operator=(CZString other) {
00269 swap(other);
00270 return *this;
00271 }
00272
00273 bool Value::CZString::operator<(const CZString& other) const {
00274 if (!cstr_) return index_ < other.index_;
00275
00276
00277 unsigned this_len = this->storage_.length_;
00278 unsigned other_len = other.storage_.length_;
00279 unsigned min_len = std::min(this_len, other_len);
00280 int comp = memcmp(this->cstr_, other.cstr_, min_len);
00281 if (comp < 0) return true;
00282 if (comp > 0) return false;
00283 return (this_len < other_len);
00284 }
00285
00286 bool Value::CZString::operator==(const CZString& other) const {
00287 if (!cstr_) return index_ == other.index_;
00288
00289
00290 unsigned this_len = this->storage_.length_;
00291 unsigned other_len = other.storage_.length_;
00292 if (this_len != other_len) return false;
00293 int comp = memcmp(this->cstr_, other.cstr_, this_len);
00294 return comp == 0;
00295 }
00296
00297 ArrayIndex Value::CZString::index() const { return index_; }
00298
00299
00300 const char* Value::CZString::data() const { return cstr_; }
00301 unsigned Value::CZString::length() const { return storage_.length_; }
00302 bool Value::CZString::isStaticString() const { return storage_.policy_ == noDuplication; }
00303
00304
00305
00306
00307
00308
00309
00310
00311
00316 Value::Value(ValueType type) {
00317 initBasic(type);
00318 switch (type) {
00319 case nullValue:
00320 break;
00321 case intValue:
00322 case uintValue:
00323 value_.int_ = 0;
00324 break;
00325 case realValue:
00326 value_.real_ = 0.0;
00327 break;
00328 case stringValue:
00329 value_.string_ = 0;
00330 break;
00331 case arrayValue:
00332 case objectValue:
00333 value_.map_ = new ObjectValues();
00334 break;
00335 case booleanValue:
00336 value_.bool_ = false;
00337 break;
00338 default:
00339 JSON_ASSERT_UNREACHABLE;
00340 }
00341 }
00342
00343 Value::Value(Int value) {
00344 initBasic(intValue);
00345 value_.int_ = value;
00346 }
00347
00348 Value::Value(UInt value) {
00349 initBasic(uintValue);
00350 value_.uint_ = value;
00351 }
00352 #if defined(JSON_HAS_INT64)
00353 Value::Value(Int64 value) {
00354 initBasic(intValue);
00355 value_.int_ = value;
00356 }
00357 Value::Value(UInt64 value) {
00358 initBasic(uintValue);
00359 value_.uint_ = value;
00360 }
00361 #endif // defined(JSON_HAS_INT64)
00362
00363 Value::Value(double value) {
00364 initBasic(realValue);
00365 value_.real_ = value;
00366 }
00367
00368 Value::Value(const char* value) {
00369 initBasic(stringValue, true);
00370 value_.string_ = duplicateAndPrefixStringValue(value, static_cast<unsigned>(strlen(value)));
00371 }
00372
00373 Value::Value(const char* beginValue, const char* endValue) {
00374 initBasic(stringValue, true);
00375 value_.string_ =
00376 duplicateAndPrefixStringValue(beginValue, static_cast<unsigned>(endValue - beginValue));
00377 }
00378
00379 Value::Value(const std::string& value) {
00380 initBasic(stringValue, true);
00381 value_.string_ =
00382 duplicateAndPrefixStringValue(value.data(), static_cast<unsigned>(value.length()));
00383 }
00384
00385 Value::Value(const StaticString& value) {
00386 initBasic(stringValue);
00387 value_.string_ = const_cast<char*>(value.c_str());
00388 }
00389
00390 #ifdef JSON_USE_CPPTL
00391 Value::Value(const CppTL::ConstString& value) {
00392 initBasic(stringValue, true);
00393 value_.string_ = duplicateAndPrefixStringValue(value, static_cast<unsigned>(value.length()));
00394 }
00395 #endif
00396
00397 Value::Value(bool value) {
00398 initBasic(booleanValue);
00399 value_.bool_ = value;
00400 }
00401
00402 Value::Value(Value const& other)
00403 : type_(other.type_), allocated_(false)
00404 ,
00405 comments_(0)
00406 {
00407 switch (type_) {
00408 case nullValue:
00409 case intValue:
00410 case uintValue:
00411 case realValue:
00412 case booleanValue:
00413 value_ = other.value_;
00414 break;
00415 case stringValue:
00416 if (other.value_.string_ && other.allocated_) {
00417 unsigned len;
00418 char const* str;
00419 decodePrefixedString(other.allocated_, other.value_.string_,
00420 &len, &str);
00421 value_.string_ = duplicateAndPrefixStringValue(str, len);
00422 allocated_ = true;
00423 } else {
00424 value_.string_ = other.value_.string_;
00425 allocated_ = false;
00426 }
00427 break;
00428 case arrayValue:
00429 case objectValue:
00430 value_.map_ = new ObjectValues(*other.value_.map_);
00431 break;
00432 default:
00433 JSON_ASSERT_UNREACHABLE;
00434 }
00435 if (other.comments_) {
00436 comments_ = new CommentInfo[numberOfCommentPlacement];
00437 for (int comment = 0; comment < numberOfCommentPlacement; ++comment) {
00438 const CommentInfo& otherComment = other.comments_[comment];
00439 if (otherComment.comment_)
00440 comments_[comment].setComment(
00441 otherComment.comment_, strlen(otherComment.comment_));
00442 }
00443 }
00444 }
00445
00446 Value::~Value() {
00447 switch (type_) {
00448 case nullValue:
00449 case intValue:
00450 case uintValue:
00451 case realValue:
00452 case booleanValue:
00453 break;
00454 case stringValue:
00455 if (allocated_)
00456 releaseStringValue(value_.string_);
00457 break;
00458 case arrayValue:
00459 case objectValue:
00460 delete value_.map_;
00461 break;
00462 default:
00463 JSON_ASSERT_UNREACHABLE;
00464 }
00465
00466 if (comments_)
00467 delete[] comments_;
00468 }
00469
00470 Value &Value::operator=(const Value &other) {
00471 Value temp(other);
00472 swap(temp);
00473 return *this;
00474 }
00475
00476 void Value::swapPayload(Value& other) {
00477 ValueType temp = type_;
00478 type_ = other.type_;
00479 other.type_ = temp;
00480 std::swap(value_, other.value_);
00481 int temp2 = allocated_;
00482 allocated_ = other.allocated_;
00483 other.allocated_ = temp2;
00484 }
00485
00486 void Value::swap(Value& other) {
00487 swapPayload(other);
00488 std::swap(comments_, other.comments_);
00489 }
00490
00491 ValueType Value::type() const { return type_; }
00492
00493 int Value::compare(const Value& other) const {
00494 if (*this < other)
00495 return -1;
00496 if (*this > other)
00497 return 1;
00498 return 0;
00499 }
00500
00501 bool Value::operator<(const Value& other) const {
00502 int typeDelta = type_ - other.type_;
00503 if (typeDelta)
00504 return typeDelta < 0 ? true : false;
00505 switch (type_) {
00506 case nullValue:
00507 return false;
00508 case intValue:
00509 return value_.int_ < other.value_.int_;
00510 case uintValue:
00511 return value_.uint_ < other.value_.uint_;
00512 case realValue:
00513 return value_.real_ < other.value_.real_;
00514 case booleanValue:
00515 return value_.bool_ < other.value_.bool_;
00516 case stringValue:
00517 {
00518 if ((value_.string_ == 0) || (other.value_.string_ == 0)) {
00519 if (other.value_.string_) return true;
00520 else return false;
00521 }
00522 unsigned this_len;
00523 unsigned other_len;
00524 char const* this_str;
00525 char const* other_str;
00526 decodePrefixedString(this->allocated_, this->value_.string_, &this_len, &this_str);
00527 decodePrefixedString(other.allocated_, other.value_.string_, &other_len, &other_str);
00528 unsigned min_len = std::min(this_len, other_len);
00529 int comp = memcmp(this_str, other_str, min_len);
00530 if (comp < 0) return true;
00531 if (comp > 0) return false;
00532 return (this_len < other_len);
00533 }
00534 case arrayValue:
00535 case objectValue: {
00536 int delta = int(value_.map_->size() - other.value_.map_->size());
00537 if (delta)
00538 return delta < 0;
00539 return (*value_.map_) < (*other.value_.map_);
00540 }
00541 default:
00542 JSON_ASSERT_UNREACHABLE;
00543 }
00544 return false;
00545 }
00546
00547 bool Value::operator<=(const Value& other) const { return !(other < *this); }
00548
00549 bool Value::operator>=(const Value& other) const { return !(*this < other); }
00550
00551 bool Value::operator>(const Value& other) const { return other < *this; }
00552
00553 bool Value::operator==(const Value& other) const {
00554
00555
00556
00557
00558 int temp = other.type_;
00559 if (type_ != temp)
00560 return false;
00561 switch (type_) {
00562 case nullValue:
00563 return true;
00564 case intValue:
00565 return value_.int_ == other.value_.int_;
00566 case uintValue:
00567 return value_.uint_ == other.value_.uint_;
00568 case realValue:
00569 return value_.real_ == other.value_.real_;
00570 case booleanValue:
00571 return value_.bool_ == other.value_.bool_;
00572 case stringValue:
00573 {
00574 if ((value_.string_ == 0) || (other.value_.string_ == 0)) {
00575 return (value_.string_ == other.value_.string_);
00576 }
00577 unsigned this_len;
00578 unsigned other_len;
00579 char const* this_str;
00580 char const* other_str;
00581 decodePrefixedString(this->allocated_, this->value_.string_, &this_len, &this_str);
00582 decodePrefixedString(other.allocated_, other.value_.string_, &other_len, &other_str);
00583 if (this_len != other_len) return false;
00584 int comp = memcmp(this_str, other_str, this_len);
00585 return comp == 0;
00586 }
00587 case arrayValue:
00588 case objectValue:
00589 return value_.map_->size() == other.value_.map_->size() &&
00590 (*value_.map_) == (*other.value_.map_);
00591 default:
00592 JSON_ASSERT_UNREACHABLE;
00593 }
00594 return false;
00595 }
00596
00597 bool Value::operator!=(const Value& other) const { return !(*this == other); }
00598
00599 const char* Value::asCString() const {
00600 JSON_ASSERT_MESSAGE(type_ == stringValue,
00601 "in Json::Value::asCString(): requires stringValue");
00602 if (value_.string_ == 0) return 0;
00603 unsigned this_len;
00604 char const* this_str;
00605 decodePrefixedString(this->allocated_, this->value_.string_, &this_len, &this_str);
00606 return this_str;
00607 }
00608
00609 bool Value::getString(char const** str, char const** end) const {
00610 if (type_ != stringValue) return false;
00611 if (value_.string_ == 0) return false;
00612 unsigned length;
00613 decodePrefixedString(this->allocated_, this->value_.string_, &length, str);
00614 *end = *str + length;
00615 return true;
00616 }
00617
00618 std::string Value::asString() const {
00619 switch (type_) {
00620 case nullValue:
00621 return "";
00622 case stringValue:
00623 {
00624 if (value_.string_ == 0) return "";
00625 unsigned this_len;
00626 char const* this_str;
00627 decodePrefixedString(this->allocated_, this->value_.string_, &this_len, &this_str);
00628 return std::string(this_str, this_len);
00629 }
00630 case booleanValue:
00631 return value_.bool_ ? "true" : "false";
00632 case intValue:
00633 return valueToString(value_.int_);
00634 case uintValue:
00635 return valueToString(value_.uint_);
00636 case realValue:
00637 return valueToString(value_.real_);
00638 default:
00639 JSON_FAIL_MESSAGE("Type is not convertible to string");
00640 }
00641 }
00642
00643 #ifdef JSON_USE_CPPTL
00644 CppTL::ConstString Value::asConstString() const {
00645 unsigned len;
00646 char const* str;
00647 decodePrefixedString(allocated_, value_.string_,
00648 &len, &str);
00649 return CppTL::ConstString(str, len);
00650 }
00651 #endif
00652
00653 Value::Int Value::asInt() const {
00654 switch (type_) {
00655 case intValue:
00656 JSON_ASSERT_MESSAGE(isInt(), "LargestInt out of Int range");
00657 return Int(value_.int_);
00658 case uintValue:
00659 JSON_ASSERT_MESSAGE(isInt(), "LargestUInt out of Int range");
00660 return Int(value_.uint_);
00661 case realValue:
00662 JSON_ASSERT_MESSAGE(InRange(value_.real_, minInt, maxInt),
00663 "double out of Int range");
00664 return Int(value_.real_);
00665 case nullValue:
00666 return 0;
00667 case booleanValue:
00668 return value_.bool_ ? 1 : 0;
00669 default:
00670 break;
00671 }
00672 JSON_FAIL_MESSAGE("Value is not convertible to Int.");
00673 }
00674
00675 Value::UInt Value::asUInt() const {
00676 switch (type_) {
00677 case intValue:
00678 JSON_ASSERT_MESSAGE(isUInt(), "LargestInt out of UInt range");
00679 return UInt(value_.int_);
00680 case uintValue:
00681 JSON_ASSERT_MESSAGE(isUInt(), "LargestUInt out of UInt range");
00682 return UInt(value_.uint_);
00683 case realValue:
00684 JSON_ASSERT_MESSAGE(InRange(value_.real_, 0, maxUInt),
00685 "double out of UInt range");
00686 return UInt(value_.real_);
00687 case nullValue:
00688 return 0;
00689 case booleanValue:
00690 return value_.bool_ ? 1 : 0;
00691 default:
00692 break;
00693 }
00694 JSON_FAIL_MESSAGE("Value is not convertible to UInt.");
00695 }
00696
00697 #if defined(JSON_HAS_INT64)
00698
00699 Value::Int64 Value::asInt64() const {
00700 switch (type_) {
00701 case intValue:
00702 return Int64(value_.int_);
00703 case uintValue:
00704 JSON_ASSERT_MESSAGE(isInt64(), "LargestUInt out of Int64 range");
00705 return Int64(value_.uint_);
00706 case realValue:
00707 JSON_ASSERT_MESSAGE(InRange(value_.real_, minInt64, maxInt64),
00708 "double out of Int64 range");
00709 return Int64(value_.real_);
00710 case nullValue:
00711 return 0;
00712 case booleanValue:
00713 return value_.bool_ ? 1 : 0;
00714 default:
00715 break;
00716 }
00717 JSON_FAIL_MESSAGE("Value is not convertible to Int64.");
00718 }
00719
00720 Value::UInt64 Value::asUInt64() const {
00721 switch (type_) {
00722 case intValue:
00723 JSON_ASSERT_MESSAGE(isUInt64(), "LargestInt out of UInt64 range");
00724 return UInt64(value_.int_);
00725 case uintValue:
00726 return UInt64(value_.uint_);
00727 case realValue:
00728 JSON_ASSERT_MESSAGE(InRange(value_.real_, 0, maxUInt64),
00729 "double out of UInt64 range");
00730 return UInt64(value_.real_);
00731 case nullValue:
00732 return 0;
00733 case booleanValue:
00734 return value_.bool_ ? 1 : 0;
00735 default:
00736 break;
00737 }
00738 JSON_FAIL_MESSAGE("Value is not convertible to UInt64.");
00739 }
00740 #endif // if defined(JSON_HAS_INT64)
00741
00742 LargestInt Value::asLargestInt() const {
00743 #if defined(JSON_NO_INT64)
00744 return asInt();
00745 #else
00746 return asInt64();
00747 #endif
00748 }
00749
00750 LargestUInt Value::asLargestUInt() const {
00751 #if defined(JSON_NO_INT64)
00752 return asUInt();
00753 #else
00754 return asUInt64();
00755 #endif
00756 }
00757
00758 double Value::asDouble() const {
00759 switch (type_) {
00760 case intValue:
00761 return static_cast<double>(value_.int_);
00762 case uintValue:
00763 #if !defined(JSON_USE_INT64_DOUBLE_CONVERSION)
00764 return static_cast<double>(value_.uint_);
00765 #else // if !defined(JSON_USE_INT64_DOUBLE_CONVERSION)
00766 return integerToDouble(value_.uint_);
00767 #endif // if !defined(JSON_USE_INT64_DOUBLE_CONVERSION)
00768 case realValue:
00769 return value_.real_;
00770 case nullValue:
00771 return 0.0;
00772 case booleanValue:
00773 return value_.bool_ ? 1.0 : 0.0;
00774 default:
00775 break;
00776 }
00777 JSON_FAIL_MESSAGE("Value is not convertible to double.");
00778 }
00779
00780 float Value::asFloat() const {
00781 switch (type_) {
00782 case intValue:
00783 return static_cast<float>(value_.int_);
00784 case uintValue:
00785 #if !defined(JSON_USE_INT64_DOUBLE_CONVERSION)
00786 return static_cast<float>(value_.uint_);
00787 #else // if !defined(JSON_USE_INT64_DOUBLE_CONVERSION)
00788 return integerToDouble(value_.uint_);
00789 #endif // if !defined(JSON_USE_INT64_DOUBLE_CONVERSION)
00790 case realValue:
00791 return static_cast<float>(value_.real_);
00792 case nullValue:
00793 return 0.0;
00794 case booleanValue:
00795 return value_.bool_ ? 1.0f : 0.0f;
00796 default:
00797 break;
00798 }
00799 JSON_FAIL_MESSAGE("Value is not convertible to float.");
00800 }
00801
00802 bool Value::asBool() const {
00803 switch (type_) {
00804 case booleanValue:
00805 return value_.bool_;
00806 case nullValue:
00807 return false;
00808 case intValue:
00809 return value_.int_ ? true : false;
00810 case uintValue:
00811 return value_.uint_ ? true : false;
00812 case realValue:
00813 return value_.real_ ? true : false;
00814 default:
00815 break;
00816 }
00817 JSON_FAIL_MESSAGE("Value is not convertible to bool.");
00818 }
00819
00820 bool Value::isConvertibleTo(ValueType other) const {
00821 switch (other) {
00822 case nullValue:
00823 return (isNumeric() && asDouble() == 0.0) ||
00824 (type_ == booleanValue && value_.bool_ == false) ||
00825 (type_ == stringValue && asString() == "") ||
00826 (type_ == arrayValue && value_.map_->size() == 0) ||
00827 (type_ == objectValue && value_.map_->size() == 0) ||
00828 type_ == nullValue;
00829 case intValue:
00830 return isInt() ||
00831 (type_ == realValue && InRange(value_.real_, minInt, maxInt)) ||
00832 type_ == booleanValue || type_ == nullValue;
00833 case uintValue:
00834 return isUInt() ||
00835 (type_ == realValue && InRange(value_.real_, 0, maxUInt)) ||
00836 type_ == booleanValue || type_ == nullValue;
00837 case realValue:
00838 return isNumeric() || type_ == booleanValue || type_ == nullValue;
00839 case booleanValue:
00840 return isNumeric() || type_ == booleanValue || type_ == nullValue;
00841 case stringValue:
00842 return isNumeric() || type_ == booleanValue || type_ == stringValue ||
00843 type_ == nullValue;
00844 case arrayValue:
00845 return type_ == arrayValue || type_ == nullValue;
00846 case objectValue:
00847 return type_ == objectValue || type_ == nullValue;
00848 }
00849 JSON_ASSERT_UNREACHABLE;
00850 return false;
00851 }
00852
00854 ArrayIndex Value::size() const {
00855 switch (type_) {
00856 case nullValue:
00857 case intValue:
00858 case uintValue:
00859 case realValue:
00860 case booleanValue:
00861 case stringValue:
00862 return 0;
00863 case arrayValue:
00864 if (!value_.map_->empty()) {
00865 ObjectValues::const_iterator itLast = value_.map_->end();
00866 --itLast;
00867 return (*itLast).first.index() + 1;
00868 }
00869 return 0;
00870 case objectValue:
00871 return ArrayIndex(value_.map_->size());
00872 }
00873 JSON_ASSERT_UNREACHABLE;
00874 return 0;
00875 }
00876
00877 bool Value::empty() const {
00878 if (isNull() || isArray() || isObject())
00879 return size() == 0u;
00880 else
00881 return false;
00882 }
00883
00884 bool Value::operator!() const { return isNull(); }
00885
00886 void Value::clear() {
00887 JSON_ASSERT_MESSAGE(type_ == nullValue || type_ == arrayValue ||
00888 type_ == objectValue,
00889 "in Json::Value::clear(): requires complex value");
00890 switch (type_) {
00891 case arrayValue:
00892 case objectValue:
00893 value_.map_->clear();
00894 break;
00895 default:
00896 break;
00897 }
00898 }
00899
00900 void Value::resize(ArrayIndex newSize) {
00901 JSON_ASSERT_MESSAGE(type_ == nullValue || type_ == arrayValue,
00902 "in Json::Value::resize(): requires arrayValue");
00903 if (type_ == nullValue)
00904 *this = Value(arrayValue);
00905 ArrayIndex oldSize = size();
00906 if (newSize == 0)
00907 clear();
00908 else if (newSize > oldSize)
00909 (*this)[newSize - 1];
00910 else {
00911 for (ArrayIndex index = newSize; index < oldSize; ++index) {
00912 value_.map_->erase(index);
00913 }
00914 assert(size() == newSize);
00915 }
00916 }
00917
00918 Value& Value::operator[](ArrayIndex index) {
00919 JSON_ASSERT_MESSAGE(
00920 type_ == nullValue || type_ == arrayValue,
00921 "in Json::Value::operator[](ArrayIndex): requires arrayValue");
00922 if (type_ == nullValue)
00923 *this = Value(arrayValue);
00924 CZString key(index);
00925 ObjectValues::iterator it = value_.map_->lower_bound(key);
00926 if (it != value_.map_->end() && (*it).first == key)
00927 return (*it).second;
00928
00929 ObjectValues::value_type defaultValue(key, nullRef);
00930 it = value_.map_->insert(it, defaultValue);
00931 return (*it).second;
00932 }
00933
00934 Value& Value::operator[](int index) {
00935 JSON_ASSERT_MESSAGE(
00936 index >= 0,
00937 "in Json::Value::operator[](int index): index cannot be negative");
00938 return (*this)[ArrayIndex(index)];
00939 }
00940
00941 const Value& Value::operator[](ArrayIndex index) const {
00942 JSON_ASSERT_MESSAGE(
00943 type_ == nullValue || type_ == arrayValue,
00944 "in Json::Value::operator[](ArrayIndex)const: requires arrayValue");
00945 if (type_ == nullValue)
00946 return nullRef;
00947 CZString key(index);
00948 ObjectValues::const_iterator it = value_.map_->find(key);
00949 if (it == value_.map_->end())
00950 return nullRef;
00951 return (*it).second;
00952 }
00953
00954 const Value& Value::operator[](int index) const {
00955 JSON_ASSERT_MESSAGE(
00956 index >= 0,
00957 "in Json::Value::operator[](int index) const: index cannot be negative");
00958 return (*this)[ArrayIndex(index)];
00959 }
00960
00961 void Value::initBasic(ValueType type, bool allocated) {
00962 type_ = type;
00963 allocated_ = allocated;
00964 comments_ = 0;
00965 }
00966
00967
00968
00969
00970 Value& Value::resolveReference(const char* key) {
00971 JSON_ASSERT_MESSAGE(
00972 type_ == nullValue || type_ == objectValue,
00973 "in Json::Value::resolveReference(): requires objectValue");
00974 if (type_ == nullValue)
00975 *this = Value(objectValue);
00976 CZString actualKey(
00977 key, static_cast<unsigned>(strlen(key)), CZString::noDuplication);
00978 ObjectValues::iterator it = value_.map_->lower_bound(actualKey);
00979 if (it != value_.map_->end() && (*it).first == actualKey)
00980 return (*it).second;
00981
00982 ObjectValues::value_type defaultValue(actualKey, nullRef);
00983 it = value_.map_->insert(it, defaultValue);
00984 Value& value = (*it).second;
00985 return value;
00986 }
00987
00988
00989 Value& Value::resolveReference(char const* key, char const* end)
00990 {
00991 JSON_ASSERT_MESSAGE(
00992 type_ == nullValue || type_ == objectValue,
00993 "in Json::Value::resolveReference(key, end): requires objectValue");
00994 if (type_ == nullValue)
00995 *this = Value(objectValue);
00996 CZString actualKey(
00997 key, static_cast<unsigned>(end-key), CZString::duplicateOnCopy);
00998 ObjectValues::iterator it = value_.map_->lower_bound(actualKey);
00999 if (it != value_.map_->end() && (*it).first == actualKey)
01000 return (*it).second;
01001
01002 ObjectValues::value_type defaultValue(actualKey, nullRef);
01003 it = value_.map_->insert(it, defaultValue);
01004 Value& value = (*it).second;
01005 return value;
01006 }
01007
01008 Value Value::get(ArrayIndex index, const Value& defaultValue) const {
01009 const Value* value = &((*this)[index]);
01010 return value == &nullRef ? defaultValue : *value;
01011 }
01012
01013 bool Value::isValidIndex(ArrayIndex index) const { return index < size(); }
01014
01015 Value const* Value::find(char const* key, char const* end) const
01016 {
01017 JSON_ASSERT_MESSAGE(
01018 type_ == nullValue || type_ == objectValue,
01019 "in Json::Value::find(key, end, found): requires objectValue or nullValue");
01020 if (type_ == nullValue) return NULL;
01021 CZString actualKey(key, static_cast<unsigned>(end-key), CZString::noDuplication);
01022 ObjectValues::const_iterator it = value_.map_->find(actualKey);
01023 if (it == value_.map_->end()) return NULL;
01024 return &(*it).second;
01025 }
01026 const Value& Value::operator[](const char* key) const
01027 {
01028 Value const* found = find(key, key + strlen(key));
01029 if (!found) return nullRef;
01030 return *found;
01031 }
01032 Value const& Value::operator[](std::string const& key) const
01033 {
01034 Value const* found = find(key.data(), key.data() + key.length());
01035 if (!found) return nullRef;
01036 return *found;
01037 }
01038
01039 Value& Value::operator[](const char* key) {
01040 return resolveReference(key, key + strlen(key));
01041 }
01042
01043 Value& Value::operator[](const std::string& key) {
01044 return resolveReference(key.data(), key.data() + key.length());
01045 }
01046
01047 Value& Value::operator[](const StaticString& key) {
01048 return resolveReference(key.c_str());
01049 }
01050
01051 #ifdef JSON_USE_CPPTL
01052 Value& Value::operator[](const CppTL::ConstString& key) {
01053 return resolveReference(key.c_str(), key.end_c_str());
01054 }
01055 Value const& Value::operator[](CppTL::ConstString const& key) const
01056 {
01057 Value const* found = find(key.c_str(), key.end_c_str());
01058 if (!found) return nullRef;
01059 return *found;
01060 }
01061 #endif
01062
01063 Value& Value::append(const Value& value) { return (*this)[size()] = value; }
01064
01065 Value Value::get(char const* key, char const* end, Value const& defaultValue) const
01066 {
01067 Value const* found = find(key, end);
01068 return !found ? defaultValue : *found;
01069 }
01070 Value Value::get(char const* key, Value const& defaultValue) const
01071 {
01072 return get(key, key + strlen(key), defaultValue);
01073 }
01074 Value Value::get(std::string const& key, Value const& defaultValue) const
01075 {
01076 return get(key.data(), key.data() + key.length(), defaultValue);
01077 }
01078
01079
01080 bool Value::removeMember(const char* key, const char* end, Value* removed)
01081 {
01082 if (type_ != objectValue) {
01083 return false;
01084 }
01085 CZString actualKey(key, static_cast<unsigned>(end-key), CZString::noDuplication);
01086 ObjectValues::iterator it = value_.map_->find(actualKey);
01087 if (it == value_.map_->end())
01088 return false;
01089 *removed = it->second;
01090 value_.map_->erase(it);
01091 return true;
01092 }
01093 bool Value::removeMember(const char* key, Value* removed)
01094 {
01095 return removeMember(key, key + strlen(key), removed);
01096 }
01097 bool Value::removeMember(std::string const& key, Value* removed)
01098 {
01099 return removeMember(key.data(), key.data() + key.length(), removed);
01100 }
01101 Value Value::removeMember(const char* key)
01102 {
01103 JSON_ASSERT_MESSAGE(type_ == nullValue || type_ == objectValue,
01104 "in Json::Value::removeMember(): requires objectValue");
01105 if (type_ == nullValue)
01106 return nullRef;
01107
01108 Value removed;
01109 removeMember(key, key + strlen(key), &removed);
01110 return removed;
01111 }
01112 Value Value::removeMember(const std::string& key)
01113 {
01114 return removeMember(key.c_str());
01115 }
01116
01117 bool Value::removeIndex(ArrayIndex index, Value* removed) {
01118 if (type_ != arrayValue) {
01119 return false;
01120 }
01121 CZString key(index);
01122 ObjectValues::iterator it = value_.map_->find(key);
01123 if (it == value_.map_->end()) {
01124 return false;
01125 }
01126 *removed = it->second;
01127 ArrayIndex oldSize = size();
01128
01129 for (ArrayIndex i = index; i < (oldSize - 1); ++i){
01130 CZString key(i);
01131 (*value_.map_)[key] = (*this)[i + 1];
01132 }
01133
01134 CZString keyLast(oldSize - 1);
01135 ObjectValues::iterator itLast = value_.map_->find(keyLast);
01136 value_.map_->erase(itLast);
01137 return true;
01138 }
01139
01140 #ifdef JSON_USE_CPPTL
01141 Value Value::get(const CppTL::ConstString& key,
01142 const Value& defaultValue) const {
01143 return get(key.c_str(), key.end_c_str(), defaultValue);
01144 }
01145 #endif
01146
01147 bool Value::isMember(char const* key, char const* end) const
01148 {
01149 Value const* value = find(key, end);
01150 return NULL != value;
01151 }
01152 bool Value::isMember(char const* key) const
01153 {
01154 return isMember(key, key + strlen(key));
01155 }
01156 bool Value::isMember(std::string const& key) const
01157 {
01158 return isMember(key.data(), key.data() + key.length());
01159 }
01160
01161 #ifdef JSON_USE_CPPTL
01162 bool Value::isMember(const CppTL::ConstString& key) const {
01163 return isMember(key.c_str(), key.end_c_str());
01164 }
01165 #endif
01166
01167 Value::Members Value::getMemberNames() const {
01168 JSON_ASSERT_MESSAGE(
01169 type_ == nullValue || type_ == objectValue,
01170 "in Json::Value::getMemberNames(), value must be objectValue");
01171 if (type_ == nullValue)
01172 return Value::Members();
01173 Members members;
01174 members.reserve(value_.map_->size());
01175 ObjectValues::const_iterator it = value_.map_->begin();
01176 ObjectValues::const_iterator itEnd = value_.map_->end();
01177 for (; it != itEnd; ++it) {
01178 members.push_back(std::string((*it).first.data(),
01179 (*it).first.length()));
01180 }
01181 return members;
01182 }
01183
01184
01185
01186
01187
01188
01189
01190
01191
01192
01193
01194
01195
01196
01197
01198
01199
01200
01201
01202
01203
01204
01205
01206
01207
01208
01209 static bool IsIntegral(double d) {
01210 double integral_part;
01211 return modf(d, &integral_part) == 0.0;
01212 }
01213
01214 bool Value::isNull() const { return type_ == nullValue; }
01215
01216 bool Value::isBool() const { return type_ == booleanValue; }
01217
01218 bool Value::isInt() const {
01219 switch (type_) {
01220 case intValue:
01221 return value_.int_ >= minInt && value_.int_ <= maxInt;
01222 case uintValue:
01223 return value_.uint_ <= UInt(maxInt);
01224 case realValue:
01225 return value_.real_ >= minInt && value_.real_ <= maxInt &&
01226 IsIntegral(value_.real_);
01227 default:
01228 break;
01229 }
01230 return false;
01231 }
01232
01233 bool Value::isUInt() const {
01234 switch (type_) {
01235 case intValue:
01236 return value_.int_ >= 0 && LargestUInt(value_.int_) <= LargestUInt(maxUInt);
01237 case uintValue:
01238 return value_.uint_ <= maxUInt;
01239 case realValue:
01240 return value_.real_ >= 0 && value_.real_ <= maxUInt &&
01241 IsIntegral(value_.real_);
01242 default:
01243 break;
01244 }
01245 return false;
01246 }
01247
01248 bool Value::isInt64() const {
01249 #if defined(JSON_HAS_INT64)
01250 switch (type_) {
01251 case intValue:
01252 return true;
01253 case uintValue:
01254 return value_.uint_ <= UInt64(maxInt64);
01255 case realValue:
01256
01257
01258
01259 return value_.real_ >= double(minInt64) &&
01260 value_.real_ < double(maxInt64) && IsIntegral(value_.real_);
01261 default:
01262 break;
01263 }
01264 #endif // JSON_HAS_INT64
01265 return false;
01266 }
01267
01268 bool Value::isUInt64() const {
01269 #if defined(JSON_HAS_INT64)
01270 switch (type_) {
01271 case intValue:
01272 return value_.int_ >= 0;
01273 case uintValue:
01274 return true;
01275 case realValue:
01276
01277
01278
01279 return value_.real_ >= 0 && value_.real_ < maxUInt64AsDouble &&
01280 IsIntegral(value_.real_);
01281 default:
01282 break;
01283 }
01284 #endif // JSON_HAS_INT64
01285 return false;
01286 }
01287
01288 bool Value::isIntegral() const {
01289 #if defined(JSON_HAS_INT64)
01290 return isInt64() || isUInt64();
01291 #else
01292 return isInt() || isUInt();
01293 #endif
01294 }
01295
01296 bool Value::isDouble() const { return type_ == realValue || isIntegral(); }
01297
01298 bool Value::isNumeric() const { return isIntegral() || isDouble(); }
01299
01300 bool Value::isString() const { return type_ == stringValue; }
01301
01302 bool Value::isArray() const { return type_ == arrayValue; }
01303
01304 bool Value::isObject() const { return type_ == objectValue; }
01305
01306 void Value::setComment(const char* comment, size_t len, CommentPlacement placement) {
01307 if (!comments_)
01308 comments_ = new CommentInfo[numberOfCommentPlacement];
01309 if ((len > 0) && (comment[len-1] == '\n')) {
01310
01311 len -= 1;
01312 }
01313 comments_[placement].setComment(comment, len);
01314 }
01315
01316 void Value::setComment(const char* comment, CommentPlacement placement) {
01317 setComment(comment, strlen(comment), placement);
01318 }
01319
01320 void Value::setComment(const std::string& comment, CommentPlacement placement) {
01321 setComment(comment.c_str(), comment.length(), placement);
01322 }
01323
01324 bool Value::hasComment(CommentPlacement placement) const {
01325 return comments_ != 0 && comments_[placement].comment_ != 0;
01326 }
01327
01328 std::string Value::getComment(CommentPlacement placement) const {
01329 if (hasComment(placement))
01330 return comments_[placement].comment_;
01331 return "";
01332 }
01333
01334 std::string Value::toStyledString() const {
01335 StyledWriter writer;
01336 return writer.write(*this);
01337 }
01338
01339 Value::const_iterator Value::begin() const {
01340 switch (type_) {
01341 case arrayValue:
01342 case objectValue:
01343 if (value_.map_)
01344 return const_iterator(value_.map_->begin());
01345 break;
01346 default:
01347 break;
01348 }
01349 return const_iterator();
01350 }
01351
01352 Value::const_iterator Value::end() const {
01353 switch (type_) {
01354 case arrayValue:
01355 case objectValue:
01356 if (value_.map_)
01357 return const_iterator(value_.map_->end());
01358 break;
01359 default:
01360 break;
01361 }
01362 return const_iterator();
01363 }
01364
01365 Value::iterator Value::begin() {
01366 switch (type_) {
01367 case arrayValue:
01368 case objectValue:
01369 if (value_.map_)
01370 return iterator(value_.map_->begin());
01371 break;
01372 default:
01373 break;
01374 }
01375 return iterator();
01376 }
01377
01378 Value::iterator Value::end() {
01379 switch (type_) {
01380 case arrayValue:
01381 case objectValue:
01382 if (value_.map_)
01383 return iterator(value_.map_->end());
01384 break;
01385 default:
01386 break;
01387 }
01388 return iterator();
01389 }
01390
01391
01392
01393
01394 PathArgument::PathArgument() : key_(), index_(), kind_(kindNone) {}
01395
01396 PathArgument::PathArgument(ArrayIndex index)
01397 : key_(), index_(index), kind_(kindIndex) {}
01398
01399 PathArgument::PathArgument(const char* key)
01400 : key_(key), index_(), kind_(kindKey) {}
01401
01402 PathArgument::PathArgument(const std::string& key)
01403 : key_(key.c_str()), index_(), kind_(kindKey) {}
01404
01405
01406
01407
01408 Path::Path(const std::string& path,
01409 const PathArgument& a1,
01410 const PathArgument& a2,
01411 const PathArgument& a3,
01412 const PathArgument& a4,
01413 const PathArgument& a5) {
01414 InArgs in;
01415 in.push_back(&a1);
01416 in.push_back(&a2);
01417 in.push_back(&a3);
01418 in.push_back(&a4);
01419 in.push_back(&a5);
01420 makePath(path, in);
01421 }
01422
01423 void Path::makePath(const std::string& path, const InArgs& in) {
01424 const char* current = path.c_str();
01425 const char* end = current + path.length();
01426 InArgs::const_iterator itInArg = in.begin();
01427 while (current != end) {
01428 if (*current == '[') {
01429 ++current;
01430 if (*current == '%')
01431 addPathInArg(path, in, itInArg, PathArgument::kindIndex);
01432 else {
01433 ArrayIndex index = 0;
01434 for (; current != end && *current >= '0' && *current <= '9'; ++current)
01435 index = index * 10 + ArrayIndex(*current - '0');
01436 args_.push_back(index);
01437 }
01438 if (current == end || *current++ != ']')
01439 invalidPath(path, int(current - path.c_str()));
01440 } else if (*current == '%') {
01441 addPathInArg(path, in, itInArg, PathArgument::kindKey);
01442 ++current;
01443 } else if (*current == '.') {
01444 ++current;
01445 } else {
01446 const char* beginName = current;
01447 while (current != end && !strchr("[.", *current))
01448 ++current;
01449 args_.push_back(std::string(beginName, current));
01450 }
01451 }
01452 }
01453
01454 void Path::addPathInArg(const std::string& ,
01455 const InArgs& in,
01456 InArgs::const_iterator& itInArg,
01457 PathArgument::Kind kind) {
01458 if (itInArg == in.end()) {
01459
01460 } else if ((*itInArg)->kind_ != kind) {
01461
01462 } else {
01463 args_.push_back(**itInArg);
01464 }
01465 }
01466
01467 void Path::invalidPath(const std::string& , int ) {
01468
01469 }
01470
01471 const Value& Path::resolve(const Value& root) const {
01472 const Value* node = &root;
01473 for (Args::const_iterator it = args_.begin(); it != args_.end(); ++it) {
01474 const PathArgument& arg = *it;
01475 if (arg.kind_ == PathArgument::kindIndex) {
01476 if (!node->isArray() || !node->isValidIndex(arg.index_)) {
01477
01478 }
01479 node = &((*node)[arg.index_]);
01480 } else if (arg.kind_ == PathArgument::kindKey) {
01481 if (!node->isObject()) {
01482
01483 }
01484 node = &((*node)[arg.key_]);
01485 if (node == &Value::nullRef) {
01486
01487
01488 }
01489 }
01490 }
01491 return *node;
01492 }
01493
01494 Value Path::resolve(const Value& root, const Value& defaultValue) const {
01495 const Value* node = &root;
01496 for (Args::const_iterator it = args_.begin(); it != args_.end(); ++it) {
01497 const PathArgument& arg = *it;
01498 if (arg.kind_ == PathArgument::kindIndex) {
01499 if (!node->isArray() || !node->isValidIndex(arg.index_))
01500 return defaultValue;
01501 node = &((*node)[arg.index_]);
01502 } else if (arg.kind_ == PathArgument::kindKey) {
01503 if (!node->isObject())
01504 return defaultValue;
01505 node = &((*node)[arg.key_]);
01506 if (node == &Value::nullRef)
01507 return defaultValue;
01508 }
01509 }
01510 return *node;
01511 }
01512
01513 Value& Path::make(Value& root) const {
01514 Value* node = &root;
01515 for (Args::const_iterator it = args_.begin(); it != args_.end(); ++it) {
01516 const PathArgument& arg = *it;
01517 if (arg.kind_ == PathArgument::kindIndex) {
01518 if (!node->isArray()) {
01519
01520 }
01521 node = &((*node)[arg.index_]);
01522 } else if (arg.kind_ == PathArgument::kindKey) {
01523 if (!node->isObject()) {
01524
01525 }
01526 node = &((*node)[arg.key_]);
01527 }
01528 }
01529 return *node;
01530 }
01531
01532 }