📄 json_value.cpp.svn-base
字号:
&& value_.string_
&& strcmp( value_.string_, other.value_.string_ ) < 0 );
#ifndef JSON_VALUE_USE_INTERNAL_MAP
case arrayValue:
case objectValue:
{
int delta = int( value_.map_->size() - other.value_.map_->size() );
if ( delta )
return delta < 0;
return (*value_.map_) < (*other.value_.map_);
}
#else
case arrayValue:
return value_.array_->compare( *(other.value_.array_) ) < 0;
case objectValue:
return value_.map_->compare( *(other.value_.map_) ) < 0;
#endif
default:
JSON_ASSERT_UNREACHABLE;
}
return 0; // unreachable
}
bool
Value::operator <=( const Value &other ) const
{
return !(other > *this);
}
bool
Value::operator >=( const Value &other ) const
{
return !(*this < other);
}
bool
Value::operator >( const Value &other ) const
{
return other < *this;
}
bool
Value::operator ==( const Value &other ) const
{
//if ( type_ != other.type_ )
// GCC 2.95.3 says:
// attempt to take address of bit-field structure member `Json::Value::type_'
// Beats me, but a temp solves the problem.
int temp = other.type_;
if ( type_ != temp )
return false;
switch ( type_ )
{
case nullValue:
return true;
case intValue:
return value_.int_ == other.value_.int_;
case uintValue:
return value_.uint_ == other.value_.uint_;
case realValue:
return value_.real_ == other.value_.real_;
case booleanValue:
return value_.bool_ == other.value_.bool_;
case stringValue:
return ( value_.string_ == other.value_.string_ )
|| ( other.value_.string_
&& value_.string_
&& strcmp( value_.string_, other.value_.string_ ) == 0 );
#ifndef JSON_VALUE_USE_INTERNAL_MAP
case arrayValue:
case objectValue:
return value_.map_->size() == other.value_.map_->size()
&& (*value_.map_) == (*other.value_.map_);
#else
case arrayValue:
return value_.array_->compare( *(other.value_.array_) ) == 0;
case objectValue:
return value_.map_->compare( *(other.value_.map_) ) == 0;
#endif
default:
JSON_ASSERT_UNREACHABLE;
}
return 0; // unreachable
}
bool
Value::operator !=( const Value &other ) const
{
return !( *this == other );
}
const char *
Value::asCString() const
{
JSON_ASSERT( type_ == stringValue );
return value_.string_;
}
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:
case realValue:
case arrayValue:
case objectValue:
JSON_ASSERT_MESSAGE( false, "Type is not convertible to string" );
default:
JSON_ASSERT_UNREACHABLE;
}
return ""; // unreachable
}
# ifdef JSON_USE_CPPTL
CppTL::ConstString
Value::asConstString() const
{
return CppTL::ConstString( asString().c_str() );
}
# endif
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:
case arrayValue:
case objectValue:
JSON_ASSERT_MESSAGE( false, "Type is not convertible to int" );
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_ );
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;
}
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;
}
bool
Value::asBool() const
{
switch ( type_ )
{
case nullValue:
return false;
case intValue:
case uintValue:
return value_.int_ != 0;
case realValue:
return value_.real_ != 0.0;
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;
}
bool
Value::isConvertibleTo( ValueType other ) const
{
switch ( type_ )
{
case nullValue:
return true;
case intValue:
return ( other == nullValue && value_.int_ == 0 )
|| other == intValue
|| ( other == uintValue && value_.int_ >= 0 )
|| other == realValue
|| other == stringValue
|| other == booleanValue;
case uintValue:
return ( other == nullValue && value_.uint_ == 0 )
|| ( other == intValue && value_.uint_ <= (unsigned)maxInt )
|| other == uintValue
|| other == realValue
|| other == stringValue
|| other == booleanValue;
case realValue:
return ( other == nullValue && value_.real_ == 0.0 )
|| ( other == intValue && value_.real_ >= minInt && value_.real_ <= maxInt )
|| ( other == uintValue && value_.real_ >= 0 && value_.real_ <= maxUInt )
|| other == realValue
|| other == stringValue
|| other == booleanValue;
case booleanValue:
return ( other == nullValue && value_.bool_ == false )
|| other == intValue
|| other == uintValue
|| other == realValue
|| other == stringValue
|| other == booleanValue;
case stringValue:
return other == stringValue
|| ( other == nullValue && (!value_.string_ || value_.string_[0] == 0) );
case arrayValue:
return other == arrayValue
|| ( other == nullValue && value_.map_->size() == 0 );
case objectValue:
return other == objectValue
|| ( other == nullValue && value_.map_->size() == 0 );
default:
JSON_ASSERT_UNREACHABLE;
}
return false; // unreachable;
}
/// Number of values in array or object
Value::UInt
Value::size() const
{
switch ( type_ )
{
case nullValue:
case intValue:
case uintValue:
case realValue:
case booleanValue:
case stringValue:
return 0;
#ifndef JSON_VALUE_USE_INTERNAL_MAP
case arrayValue: // size of the array is highest index + 1
if ( !value_.map_->empty() )
{
ObjectValues::const_iterator itLast = value_.map_->end();
--itLast;
return (*itLast).first.index()+1;
}
return 0;
case objectValue:
return Int( value_.map_->size() );
#else
case arrayValue:
return Int( value_.array_->size() );
case objectValue:
return Int( value_.map_->size() );
#endif
default:
JSON_ASSERT_UNREACHABLE;
}
return 0; // unreachable;
}
bool
Value::empty() const
{
if ( isNull() || isArray() || isObject() )
return size() == 0u;
else
return false;
}
bool
Value::operator!() const
{
return isNull();
}
void
Value::clear()
{
JSON_ASSERT( type_ == nullValue || type_ == arrayValue || type_ == objectValue );
switch ( type_ )
{
#ifndef JSON_VALUE_USE_INTERNAL_MAP
case arrayValue:
case objectValue:
value_.map_->clear();
break;
#else
case arrayValue:
value_.array_->clear();
break;
case objectValue:
value_.map_->clear();
break;
#endif
default:
break;
}
}
void
Value::resize( UInt newSize )
{
JSON_ASSERT( type_ == nullValue || type_ == arrayValue );
if ( type_ == nullValue )
*this = Value( arrayValue );
#ifndef JSON_VALUE_USE_INTERNAL_MAP
UInt oldSize = size();
if ( newSize == 0 )
clear();
else if ( newSize > oldSize )
(*this)[ newSize - 1 ];
else
{
for ( UInt index = newSize; index < oldSize; ++index )
value_.map_->erase( index );
assert( size() == newSize );
}
#else
value_.array_->resize( newSize );
#endif
}
Value &
Value::operator[]( UInt index )
{
JSON_ASSERT( type_ == nullValue || type_ == arrayValue );
if ( type_ == nullValue )
*this = Value( arrayValue );
#ifndef JSON_VALUE_USE_INTERNAL_MAP
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, null );
it = value_.map_->insert( it, defaultValue );
return (*it).second;
#else
return value_.array_->resolveReference( index );
#endif
}
const Value &
Value::operator[]( UInt index ) const
{
JSON_ASSERT( type_ == nullValue || type_ == arrayValue );
if ( type_ == nullValue )
return null;
#ifndef JSON_VALUE_USE_INTERNAL_MAP
CZString key( index );
ObjectValues::const_iterator it = value_.map_->find( key );
if ( it == value_.map_->end() )
return null;
return (*it).second;
#else
Value *value = value_.array_->find( index );
return value ? *value : null;
#endif
}
Value &
Value::operator[]( const char *key )
{
return resolveReference( key, false );
}
Value &
Value::resolveReference( const char *key,
bool isStatic )
{
JSON_ASSERT( type_ == nullValue || type_ == objectValue );
if ( type_ == nullValue )
*this = Value( objectValue );
#ifndef JSON_VALUE_USE_INTERNAL_MAP
CZString actualKey( key, isStatic ? CZString::noDuplication
: CZString::duplicateOnCopy );
ObjectValues::iterator it = value_.map_->lower_bound( actualKey );
if ( it != value_.map_->end() && (*it).first == actualKey )
return (*it).second;
ObjectValues::value_type defaultValue( actualKey, null );
it = value_.map_->insert( it, defaultValue );
Value &value = (*it).second;
return value;
#else
return value_.map_->resolveReference( key, isStatic );
#endif
}
Value
Value::get( UInt index,
const Value &defaultValue ) const
{
const Value *value = &((*this)[index]);
return value == &null ? defaultValue : *value;
}
bool
Value::isValidIndex( UInt index ) const
{
return index < size();
}
const Value &
Value::operator[]( const char *key ) const
{
JSON_ASSERT( type_ == nullValue || type_ == objectValue );
if ( type_ == nullValue )
return null;
#ifndef JSON_VALUE_USE_INTERNAL_MAP
CZString actualKey( key, CZString::noDuplication );
ObjectValues::const_iterator it = value_.map_->find( actualKey );
if ( it == value_.map_->end() )
return null;
return (*it).second;
#else
const Value *value = value_.map_->find( key );
return value ? *value : null;
#endif
}
Value &
Value::operator[]( const std::string &key )
{
return (*this)[ key.c_str() ];
}
const Value &
Value::operator[]( const std::string &key ) const
{
return (*this)[ key.c_str() ];
}
Value &
Value::operator[]( const StaticString &key )
{
return resolveReference( key, true );
}
# ifdef JSON_USE_CPPTL
Value &
Value::operator[]( const CppTL::ConstString &key )
{
return (*this)[ key.c_str() ];
}
const Value &
Value::operator[]( const CppTL::ConstString &key ) const
{
return (*this)[ key.c_str() ];
}
# endif
Value &
Value::append( const Value &value )
{
return (*this)[size()] = value;
}
Value
Value::get( const char *key,
const Value &defaultValue ) const
{
const Value *value = &((*this)[key]);
return value == &null ? defaultValue : *value;
}
Value
Value::get( const std::string &key,
const Value &defaultValue ) const
{
return get( key.c_str(), defaultValue );
}
Value
Value::removeMember( const char* key )
{
JSON_ASSERT( type_ == nullValue || type_ == objectValue );
if ( type_ == nullValue )
return null;
#ifndef JSON_VALUE_USE_INTERNAL_MAP
CZString actualKey( key, CZString::noDuplication );
ObjectValues::iterator it = value_.map_->find( actualKey );
if ( it == value_.map_->end() )
return null;
Value old(it->second);
value_.map_->erase(it);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -