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