Value::UInt Value::asUInt () const { switch ( type_ ) { case nullValue: return 0; case intValue: JSON_ASSERT_MESSAGE ( value_.int_ >= 0, "Negative integer can not be converted to unsigned integer" ); return value_.int_; case uintValue: return value_.uint_; case realValue: JSON_ASSERT_MESSAGE ( value_.real_ >= 0 && value_.real_ <= maxUInt, "Real out of unsigned integer range" ); return UInt ( value_.real_ ); case booleanValue: return value_.bool_ ? 1 : 0; case stringValue: return beast::lexicalCastThrow <unsigned int> (value_.string_); case arrayValue: case objectValue: JSON_ASSERT_MESSAGE ( false, "Type is not convertible to uint" ); default: JSON_ASSERT_UNREACHABLE; } return 0; // unreachable; }
Value::Int Value::asInt() const { switch ( type_ ) { case nullValue: return 0; case intValue: return value_.int_; case uintValue: JSON_ASSERT_MESSAGE( value_.uint_ < (unsigned)maxInt, "integer out of signed integer range" ); return value_.uint_; case realValue: JSON_ASSERT_MESSAGE( value_.real_ >= minInt && value_.real_ <= maxInt, "Real out of signed integer range" ); return Int( value_.real_ ); case booleanValue: return value_.bool_ ? 1 : 0; case stringValue: return value_.string_ ? atoi(value_.string_) : 0; case arrayValue: case objectValue: return 0; default: JSON_ASSERT_UNREACHABLE; } return 0; // unreachable; }
Value::UInt Value::asUInt() const { switch ( type_ ) { case nullValue: return 0; case intValue: JSON_ASSERT_MESSAGE( value_.int_ >= 0, "Negative integer can not be converted to unsigned integer" ); return value_.int_; case uintValue: return value_.uint_; case realValue: JSON_ASSERT_MESSAGE( value_.real_ >= 0 && value_.real_ <= maxUInt, "Real out of unsigned integer range" ); return UInt( value_.real_ ); /* begin ---------- insert by ÂÀ³¿¹â 2011/7/10 --------- */ case int64Value: JSON_ASSERT_MESSAGE( value_.int64_ >= 0 && value_.int64_ <= maxUInt, "long long out of signed integer range" ); return Int( value_.int64_); case uint64Value: JSON_ASSERT_MESSAGE( value_.uint64_ >= 0 && value_.uint64_ <= maxUInt, "unsigned long long out of signed integer range" ); return Int( value_.uint64_); /* end ---------- insert by ÂÀ³¿¹â 2011/7/10 --------- */ case booleanValue: return value_.bool_ ? 1 : 0; case stringValue: case arrayValue: case objectValue: JSON_ASSERT_MESSAGE( false, "Type is not convertible to uint" ); default: JSON_ASSERT_UNREACHABLE; } return 0; // unreachable; }
Value::Int64 Value::asInt64() const { switch ( type_ ) { case nullValue: return 0; case intValue: return value_.int_; case uintValue: JSON_ASSERT_MESSAGE( value_.uint_ <= UInt64(maxInt64), "unsigned integer out of Int64 range" ); return value_.uint_; case realValue: JSON_ASSERT_MESSAGE( value_.real_ >= minInt64 && value_.real_ <= maxInt64, "Real out of Int64 range" ); return Int( value_.real_ ); case booleanValue: return value_.bool_ ? 1 : 0; case stringValue: case arrayValue: case objectValue: JSON_ASSERT_MESSAGE( false, "Type is not convertible to Int64" ); default: JSON_ASSERT_UNREACHABLE; } return 0; // unreachable; }
Value::Int Value::asInt() const { switch ( type_ ) { case nullValue: return 0; case intValue: JSON_ASSERT_MESSAGE( value_.int_ >= minInt && value_.int_ <= maxInt, "unsigned integer out of signed int range" ); return Int(value_.int_); case uintValue: #ifdef MODIFY_FUNCTION_SIGN // do nothing #else JSON_ASSERT_MESSAGE(value_.uint_ <= UInt(maxInt), "unsigned integer out of signed int range"); #endif return Int(value_.uint_); case realValue: JSON_ASSERT_MESSAGE( value_.real_ >= minInt && value_.real_ <= maxInt, "Real out of signed integer range" ); return Int( value_.real_ ); case booleanValue: return value_.bool_ ? 1 : 0; case stringValue: case arrayValue: case objectValue: JSON_FAIL_MESSAGE( "Type is not convertible to int" ); default: JSON_ASSERT_UNREACHABLE; } return 0; // unreachable; }
Value::UInt64 Value::asUInt64() const { switch ( type_ ) { case nullValue: return 0; case intValue: JSON_ASSERT_MESSAGE( value_.int_ >= 0, "Negative integer can not be converted to UInt64" ); return value_.int_; case uintValue: return value_.uint_; case realValue: JSON_ASSERT_MESSAGE( value_.real_ >= 0 && value_.real_ <= maxUInt64, "Real out of UInt64 range" ); return UInt( value_.real_ ); case booleanValue: return value_.bool_ ? 1 : 0; case stringValue: case arrayValue: case objectValue: JSON_ASSERT_MESSAGE( false, "Type is not convertible to UInt64" ); default: JSON_ASSERT_UNREACHABLE; } return 0; // unreachable; }
Value::UInt64 Value::asUInt64() const { switch ( type_ ) { case nullValue: return false; case intValue: return value_.int_; case uintValue: return value_.uint_; case realValue: JSON_ASSERT_MESSAGE( value_.real_ >= 0 && value_.real_ <= maxUInt64, "Real out of signed integer 64 range" ); return UInt64( value_.real_ ); case int64Value: JSON_ASSERT_MESSAGE( value_.int64_ >= 0, "Negative integer 64 can not be converted to unsigned integer 64" ); return value_.int64_; case uint64Value: return value_.uint64_; case booleanValue: return value_.bool_; case stringValue: return value_.string_ && value_.string_[0] != 0; case arrayValue: case objectValue: return value_.map_->size() != 0; default: JSON_ASSERT_UNREACHABLE; } return false; // unreachable; }
/* begin ---------- insert by ÂÀ³¿¹â 2011/7/10 --------- */ Value::Int64 Value::asInt64() const { switch ( type_ ) { case nullValue: return false; case intValue: return value_.int_; case uintValue: return value_.uint_; case realValue: JSON_ASSERT_MESSAGE( value_.real_ >= minInt64 && value_.real_ <= maxInt64, "Real out of signed integer 64 range" ); return Int64( value_.real_ ); case int64Value: return value_.int64_; case uint64Value: JSON_ASSERT_MESSAGE( value_.uint64_ < (unsigned)maxInt64, "integer out of signed integer 64 range" ); return value_.uint64_; case booleanValue: return value_.bool_; case stringValue: return value_.string_ && value_.string_[0] != 0; case arrayValue: case objectValue: return value_.map_->size() != 0; default: JSON_ASSERT_UNREACHABLE; } return false; // unreachable; }
Value::Int Value::asInt () const { switch ( type_ ) { case nullValue: return 0; case intValue: return value_.int_; case uintValue: JSON_ASSERT_MESSAGE ( value_.uint_ < (unsigned)maxInt, "integer out of signed integer range" ); return value_.uint_; case realValue: JSON_ASSERT_MESSAGE ( value_.real_ >= minInt && value_.real_ <= maxInt, "Real out of signed integer range" ); return Int ( value_.real_ ); case booleanValue: return value_.bool_ ? 1 : 0; case stringValue: return beast::lexicalCastThrow <int> (value_.string_); case arrayValue: case objectValue: JSON_ASSERT_MESSAGE ( false, "Type is not convertible to int" ); default: JSON_ASSERT_UNREACHABLE; } return 0; // unreachable; }
std::string Value::asString () const { switch ( type_ ) { case nullValue: return ""; case stringValue: return value_.string_ ? value_.string_ : ""; case booleanValue: return value_.bool_ ? "true" : "false"; case intValue: return beast::lexicalCastThrow <std::string> (value_.int_); case uintValue: case realValue: case arrayValue: case objectValue: JSON_ASSERT_MESSAGE ( false, "Type is not convertible to string" ); default: JSON_ASSERT_UNREACHABLE; } return ""; // unreachable }
Value::Value(const char* value) { initBasic(stringValue, true); JSON_ASSERT_MESSAGE(value != nullptr, "Null Value Passed to Value Constructor"); value_.string_ = duplicateAndPrefixStringValue( value, static_cast<unsigned>(strlen(value))); }
float Value::asFloat() const { switch ( type_ ) { case nullValue: return 0.0f; case intValue: return static_cast<float>( value_.int_ ); case uintValue: #if !defined(JSON_USE_INT64_DOUBLE_CONVERSION) return static_cast<float>( value_.uint_ ); #else // if !defined(JSON_USE_INT64_DOUBLE_CONVERSION) return static_cast<float>( Int(value_.uint_/2) ) * 2 + Int(value_.uint_ & 1); #endif // if !defined(JSON_USE_INT64_DOUBLE_CONVERSION) case realValue: return static_cast<float>( value_.real_ ); case booleanValue: return value_.bool_ ? 1.0f : 0.0f; case stringValue: case arrayValue: case objectValue: JSON_ASSERT_MESSAGE( false, "Type is not convertible to float" ); default: JSON_ASSERT_UNREACHABLE; } return 0.0f; // unreachable; }
std::string Value::asString() const { switch ( type_ ) { case nullValue: return ""; case stringValue: return value_.string_ ? value_.string_ : ""; case booleanValue: return value_.bool_ ? "true" : "false"; case intValue: case uintValue: { std::stringstream ss; ss << asInt(); return ss.str(); } break; case realValue: case arrayValue: case objectValue: JSON_ASSERT_MESSAGE( false, "Type is not convertible to string" ); default: JSON_ASSERT_UNREACHABLE; } return ""; // unreachable }
double Value::asDouble () const { switch ( type_ ) { case nullValue: return 0.0; case intValue: return value_.int_; case uintValue: return value_.uint_; case realValue: return value_.real_; case booleanValue: return value_.bool_ ? 1.0 : 0.0; case stringValue: case arrayValue: case objectValue: JSON_ASSERT_MESSAGE ( false, "Type is not convertible to double" ); default: JSON_ASSERT_UNREACHABLE; } return 0; // unreachable; }
void Value::CommentInfo::setComment(const char *text) { if (comment_) valueAllocator()->releaseStringValue(comment_); JSON_ASSERT(text); JSON_ASSERT_MESSAGE(text[0] == '\0' || text[0] == '/', "Comments must start with /"); // It seems that /**/ style comments are acceptable as well. comment_ = valueAllocator()->duplicateStringValue(text); }
unsigned Value::getCStringLength() const { JSON_ASSERT_MESSAGE(type_ == stringValue, "in Json::Value::asCString(): requires stringValue"); if (value_.string_ == 0) return 0; unsigned this_len; char const* this_str; decodePrefixedString(this->allocated_, this->value_.string_, &this_len, &this_str); return this_len; }
void Value::removeMember(const char* key) { JSON_ASSERT_MESSAGE(type() == nullValue || type() == objectValue, "in Json::Value::removeMember(): requires objectValue"); if (type() == nullValue) return; CZString actualKey(key, unsigned(strlen(key)), CZString::noDuplication); value_.map_->erase(actualKey); }
Value::UInt64 Value::asUInt64() const { switch (type_) { case intValue: JSON_ASSERT_MESSAGE(isUInt64(), "LargestInt out of UInt64 range"); return UInt64(value_.int_); case uintValue: return UInt64(value_.uint_); case realValue: JSON_ASSERT_MESSAGE(InRange(value_.real_, 0, maxUInt64), "double out of UInt64 range"); return UInt64(value_.real_); case nullValue: return 0; case booleanValue: return value_.bool_ ? 1 : 0; default: break; } JSON_FAIL_MESSAGE("Value is not convertible to UInt64."); }
Value Value::removeMember(const char* key) { JSON_ASSERT_MESSAGE(type_ == nullValue || type_ == objectValue, "in Json::Value::removeMember(): requires objectValue"); if (type_ == nullValue) return nullSingleton(); Value removed; // null removeMember(key, key + strlen(key), &removed); return removed; // still null if removeMember() did nothing }
Value const* Value::find(char const* key, char const* cend) const { JSON_ASSERT_MESSAGE( type_ == nullValue || type_ == objectValue, "in Json::Value::find(key, end, found): requires objectValue or nullValue"); if (type_ == nullValue) return NULL; CZString actualKey(key, static_cast<unsigned>(cend-key), CZString::noDuplication); ObjectValues::const_iterator it = value_.map_->find(actualKey); if (it == value_.map_->end()) return NULL; return &(*it).second; }
void Value::setComment(String comment, CommentPlacement placement) { if (!comment.empty() && (comment.back() == '\n')) { // Always discard trailing newline, to aid indentation. comment.pop_back(); } JSON_ASSERT(!comment.empty()); JSON_ASSERT_MESSAGE( comment[0] == '\0' || comment[0] == '/', "in Json::Value::setComment(): Comments must start with /"); comments_.set(placement, std::move(comment)); }
const char* Value::asCString() const { JSON_ASSERT_MESSAGE(type() == stringValue, "in Json::Value::asCString(): requires stringValue"); if (value_.string_ == nullptr) return nullptr; unsigned this_len; char const* this_str; decodePrefixedString(this->isAllocated(), this->value_.string_, &this_len, &this_str); return this_str; }
void Value::CommentInfo::setComment(const char* text, size_t len) { if (comment_) { releaseStringValue(comment_, 0u); comment_ = 0; } JSON_ASSERT(text != 0); JSON_ASSERT_MESSAGE( text[0] == '\0' || text[0] == '/', "in Json::Value::setComment(): Comments must start with /"); // It seems that /**/ style comments are acceptable as well. comment_ = duplicateStringValue(text, len); }
/** Duplicates the specified string value. * @param value Pointer to the string to duplicate. Must be zero-terminated if * length is "unknown". * @param length Length of the value. if equals to unknown, then it will be * computed using strlen(value). * @return Pointer on the duplicate instance of string. */ static inline char * duplicateStringValue( const char *value, unsigned int length = unknown ) { if ( length == unknown ) length = (unsigned int)strlen(value); char *newString = static_cast<char *>( malloc( length + 1 ) ); JSON_ASSERT_MESSAGE( newString != 0, "Failed to allocate string value buffer" ); memcpy( newString, value, length ); newString[length] = 0; return newString; }
Value::Int Value::asInt() const { switch (type_) { case intValue: JSON_ASSERT_MESSAGE(isInt(), "LargestInt out of Int range"); return Int(value_.int_); case uintValue: JSON_ASSERT_MESSAGE(isInt(), "LargestUInt out of Int range"); return Int(value_.uint_); case realValue: JSON_ASSERT_MESSAGE(InRange(value_.real_, minInt, maxInt), "double out of Int range"); return Int(value_.real_); case nullValue: return 0; case booleanValue: return value_.bool_ ? 1 : 0; default: break; } JSON_FAIL_MESSAGE("Value is not convertible to Int."); }
const Value& Value::operator[](ArrayIndex index) const { JSON_ASSERT_MESSAGE( type_ == nullValue || type_ == arrayValue, "in Json::Value::operator[](ArrayIndex)const: requires arrayValue"); if (type_ == nullValue) return nullSingleton(); CZString key(index); ObjectValues::const_iterator it = value_.map_->find(key); if (it == value_.map_->end()) return nullSingleton(); return (*it).second; }
Value const* Value::find(char const* begin, char const* end) const { JSON_ASSERT_MESSAGE(type() == nullValue || type() == objectValue, "in Json::Value::find(begin, end): requires " "objectValue or nullValue"); if (type() == nullValue) return nullptr; CZString actualKey(begin, static_cast<unsigned>(end - begin), CZString::noDuplication); ObjectValues::const_iterator it = value_.map_->find(actualKey); if (it == value_.map_->end()) return nullptr; return &(*it).second; }
Value::Members Value::getMemberNames() const { JSON_ASSERT_MESSAGE( type() == nullValue || type() == objectValue, "in Json::Value::getMemberNames(), value must be objectValue"); if (type() == nullValue) return Value::Members(); Members members; members.reserve(value_.map_->size()); ObjectValues::const_iterator it = value_.map_->begin(); ObjectValues::const_iterator itEnd = value_.map_->end(); for (; it != itEnd; ++it) { members.push_back(String((*it).first.data(), (*it).first.length())); } return members; }
void Value::clear() { JSON_ASSERT_MESSAGE(type_ == nullValue || type_ == arrayValue || type_ == objectValue, "in Json::Value::clear(): requires complex value"); start_ = 0; limit_ = 0; switch (type_) { case arrayValue: case objectValue: value_.map_->clear(); break; default: break; } }
Value& Value::operator[](ArrayIndex index) { JSON_ASSERT_MESSAGE( type_ == nullValue || type_ == arrayValue, "in Json::Value::operator[](ArrayIndex): requires arrayValue"); if (type_ == nullValue) *this = Value(arrayValue); CZString key(index); ObjectValues::iterator it = value_.map_->lower_bound(key); if (it != value_.map_->end() && (*it).first == key) return (*it).second; ObjectValues::value_type defaultValue(key, nullSingleton()); it = value_.map_->insert(it, defaultValue); return (*it).second; }