📄 json_value.cpp.svn-base
字号:
return old;
#else
Value *value = value_.map_->find( key );
if (value){
Value old(*value);
value_.map_.remove( key );
return old;
} else {
return null;
}
#endif
}
Value
Value::removeMember( const std::string &key )
{
return removeMember( key.c_str() );
}
# ifdef JSON_USE_CPPTL
Value
Value::get( const CppTL::ConstString &key,
const Value &defaultValue ) const
{
return get( key.c_str(), defaultValue );
}
# endif
bool
Value::isMember( const char *key ) const
{
const Value *value = &((*this)[key]);
return value != &null;
}
bool
Value::isMember( const std::string &key ) const
{
return isMember( key.c_str() );
}
# ifdef JSON_USE_CPPTL
bool
Value::isMember( const CppTL::ConstString &key ) const
{
return isMember( key.c_str() );
}
#endif
Value::Members
Value::getMemberNames() const
{
JSON_ASSERT( type_ == nullValue || type_ == objectValue );
if ( type_ == nullValue )
return Value::Members();
Members members;
members.reserve( value_.map_->size() );
#ifndef JSON_VALUE_USE_INTERNAL_MAP
ObjectValues::const_iterator it = value_.map_->begin();
ObjectValues::const_iterator itEnd = value_.map_->end();
for ( ; it != itEnd; ++it )
members.push_back( std::string( (*it).first.c_str() ) );
#else
ValueInternalMap::IteratorState it;
ValueInternalMap::IteratorState itEnd;
value_.map_->makeBeginIterator( it );
value_.map_->makeEndIterator( itEnd );
for ( ; !ValueInternalMap::equals( it, itEnd ); ValueInternalMap::increment(it) )
members.push_back( std::string( ValueInternalMap::key( it ) ) );
#endif
return members;
}
//
//# ifdef JSON_USE_CPPTL
//EnumMemberNames
//Value::enumMemberNames() const
//{
// if ( type_ == objectValue )
// {
// return CppTL::Enum::any( CppTL::Enum::transform(
// CppTL::Enum::keys( *(value_.map_), CppTL::Type<const CZString &>() ),
// MemberNamesTransform() ) );
// }
// return EnumMemberNames();
//}
//
//
//EnumValues
//Value::enumValues() const
//{
// if ( type_ == objectValue || type_ == arrayValue )
// return CppTL::Enum::anyValues( *(value_.map_),
// CppTL::Type<const Value &>() );
// return EnumValues();
//}
//
//# endif
bool
Value::isNull() const
{
return type_ == nullValue;
}
bool
Value::isBool() const
{
return type_ == booleanValue;
}
bool
Value::isInt() const
{
return type_ == intValue;
}
bool
Value::isUInt() const
{
return type_ == uintValue;
}
bool
Value::isIntegral() const
{
return type_ == intValue
|| type_ == uintValue
|| type_ == booleanValue;
}
bool
Value::isDouble() const
{
return type_ == realValue;
}
bool
Value::isNumeric() const
{
return isIntegral() || isDouble();
}
bool
Value::isString() const
{
return type_ == stringValue;
}
bool
Value::isArray() const
{
return type_ == nullValue || type_ == arrayValue;
}
bool
Value::isObject() const
{
return type_ == nullValue || type_ == objectValue;
}
void
Value::setComment( const char *comment,
CommentPlacement placement )
{
if ( !comments_ )
comments_ = new CommentInfo[numberOfCommentPlacement];
comments_[placement].setComment( comment );
}
void
Value::setComment( const std::string &comment,
CommentPlacement placement )
{
setComment( comment.c_str(), placement );
}
bool
Value::hasComment( CommentPlacement placement ) const
{
return comments_ != 0 && comments_[placement].comment_ != 0;
}
std::string
Value::getComment( CommentPlacement placement ) const
{
if ( hasComment(placement) )
return comments_[placement].comment_;
return "";
}
std::string
Value::toStyledString() const
{
StyledWriter writer;
return writer.write( *this );
}
Value::const_iterator
Value::begin() const
{
switch ( type_ )
{
#ifdef JSON_VALUE_USE_INTERNAL_MAP
case arrayValue:
if ( value_.array_ )
{
ValueInternalArray::IteratorState it;
value_.array_->makeBeginIterator( it );
return const_iterator( it );
}
break;
case objectValue:
if ( value_.map_ )
{
ValueInternalMap::IteratorState it;
value_.map_->makeBeginIterator( it );
return const_iterator( it );
}
break;
#else
case arrayValue:
case objectValue:
if ( value_.map_ )
return const_iterator( value_.map_->begin() );
break;
#endif
default:
break;
}
return const_iterator();
}
Value::const_iterator
Value::end() const
{
switch ( type_ )
{
#ifdef JSON_VALUE_USE_INTERNAL_MAP
case arrayValue:
if ( value_.array_ )
{
ValueInternalArray::IteratorState it;
value_.array_->makeEndIterator( it );
return const_iterator( it );
}
break;
case objectValue:
if ( value_.map_ )
{
ValueInternalMap::IteratorState it;
value_.map_->makeEndIterator( it );
return const_iterator( it );
}
break;
#else
case arrayValue:
case objectValue:
if ( value_.map_ )
return const_iterator( value_.map_->end() );
break;
#endif
default:
break;
}
return const_iterator();
}
Value::iterator
Value::begin()
{
switch ( type_ )
{
#ifdef JSON_VALUE_USE_INTERNAL_MAP
case arrayValue:
if ( value_.array_ )
{
ValueInternalArray::IteratorState it;
value_.array_->makeBeginIterator( it );
return iterator( it );
}
break;
case objectValue:
if ( value_.map_ )
{
ValueInternalMap::IteratorState it;
value_.map_->makeBeginIterator( it );
return iterator( it );
}
break;
#else
case arrayValue:
case objectValue:
if ( value_.map_ )
return iterator( value_.map_->begin() );
break;
#endif
default:
break;
}
return iterator();
}
Value::iterator
Value::end()
{
switch ( type_ )
{
#ifdef JSON_VALUE_USE_INTERNAL_MAP
case arrayValue:
if ( value_.array_ )
{
ValueInternalArray::IteratorState it;
value_.array_->makeEndIterator( it );
return iterator( it );
}
break;
case objectValue:
if ( value_.map_ )
{
ValueInternalMap::IteratorState it;
value_.map_->makeEndIterator( it );
return iterator( it );
}
break;
#else
case arrayValue:
case objectValue:
if ( value_.map_ )
return iterator( value_.map_->end() );
break;
#endif
default:
break;
}
return iterator();
}
// class PathArgument
// //////////////////////////////////////////////////////////////////
PathArgument::PathArgument()
: kind_( kindNone )
{
}
PathArgument::PathArgument( Value::UInt index )
: index_( index )
, kind_( kindIndex )
{
}
PathArgument::PathArgument( const char *key )
: key_( key )
, kind_( kindKey )
{
}
PathArgument::PathArgument( const std::string &key )
: key_( key.c_str() )
, kind_( kindKey )
{
}
// class Path
// //////////////////////////////////////////////////////////////////
Path::Path( const std::string &path,
const PathArgument &a1,
const PathArgument &a2,
const PathArgument &a3,
const PathArgument &a4,
const PathArgument &a5 )
{
InArgs in;
in.push_back( &a1 );
in.push_back( &a2 );
in.push_back( &a3 );
in.push_back( &a4 );
in.push_back( &a5 );
makePath( path, in );
}
void
Path::makePath( const std::string &path,
const InArgs &in )
{
const char *current = path.c_str();
const char *end = current + path.length();
InArgs::const_iterator itInArg = in.begin();
while ( current != end )
{
if ( *current == '[' )
{
++current;
if ( *current == '%' )
addPathInArg( path, in, itInArg, PathArgument::kindIndex );
else
{
Value::UInt index = 0;
for ( ; current != end && *current >= '0' && *current <= '9'; ++current )
index = index * 10 + Value::UInt(*current - '0');
args_.push_back( index );
}
if ( current == end || *current++ != ']' )
invalidPath( path, int(current - path.c_str()) );
}
else if ( *current == '%' )
{
addPathInArg( path, in, itInArg, PathArgument::kindKey );
++current;
}
else if ( *current == '.' )
{
++current;
}
else
{
const char *beginName = current;
while ( current != end && !strchr( "[.", *current ) )
++current;
args_.push_back( std::string( beginName, current ) );
}
}
}
void
Path::addPathInArg( const std::string &path,
const InArgs &in,
InArgs::const_iterator &itInArg,
PathArgument::Kind kind )
{
if ( itInArg == in.end() )
{
// Error: missing argument %d
}
else if ( (*itInArg)->kind_ != kind )
{
// Error: bad argument type
}
else
{
args_.push_back( **itInArg );
}
}
void
Path::invalidPath( const std::string &path,
int location )
{
// Error: invalid path.
}
const Value &
Path::resolve( const Value &root ) const
{
const Value *node = &root;
for ( Args::const_iterator it = args_.begin(); it != args_.end(); ++it )
{
const PathArgument &arg = *it;
if ( arg.kind_ == PathArgument::kindIndex )
{
if ( !node->isArray() || node->isValidIndex( arg.index_ ) )
{
// Error: unable to resolve path (array value expected at position...
}
node = &((*node)[arg.index_]);
}
else if ( arg.kind_ == PathArgument::kindKey )
{
if ( !node->isObject() )
{
// Error: unable to resolve path (object value expected at position...)
}
node = &((*node)[arg.key_]);
if ( node == &Value::null )
{
// Error: unable to resolve path (object has no member named '' at position...)
}
}
}
return *node;
}
Value
Path::resolve( const Value &root,
const Value &defaultValue ) const
{
const Value *node = &root;
for ( Args::const_iterator it = args_.begin(); it != args_.end(); ++it )
{
const PathArgument &arg = *it;
if ( arg.kind_ == PathArgument::kindIndex )
{
if ( !node->isArray() || node->isValidIndex( arg.index_ ) )
return defaultValue;
node = &((*node)[arg.index_]);
}
else if ( arg.kind_ == PathArgument::kindKey )
{
if ( !node->isObject() )
return defaultValue;
node = &((*node)[arg.key_]);
if ( node == &Value::null )
return defaultValue;
}
}
return *node;
}
Value &
Path::make( Value &root ) const
{
Value *node = &root;
for ( Args::const_iterator it = args_.begin(); it != args_.end(); ++it )
{
const PathArgument &arg = *it;
if ( arg.kind_ == PathArgument::kindIndex )
{
if ( !node->isArray() )
{
// Error: node is not an array at position ...
}
node = &((*node)[arg.index_]);
}
else if ( arg.kind_ == PathArgument::kindKey )
{
if ( !node->isObject() )
{
// Error: node is not an object at position...
}
node = &((*node)[arg.key_]);
}
}
return *node;
}
} // namespace Json
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -