Added removeMember() and altered a few comments.

This commit is contained in:
Christopher Dunn 2007-03-23 08:30:20 +00:00
parent 02ff716d2f
commit 1aa20f9e58
2 changed files with 61 additions and 12 deletions

View File

@ -270,9 +270,10 @@ namespace Json {
/// otherwise returns defaultValue.
Value get( UInt index,
const Value &defaultValue ) const;
/// Returns true if index < size().
/// Return true if index < size().
bool isValidIndex( UInt index ) const;
/// Append value to array at the end.
/// \brief Append value to array at the end.
///
/// Equivalent to jsonvalue[jsonvalue.size()] = value;
Value &append( const Value &value );
@ -302,27 +303,41 @@ namespace Json {
/// Access an object value by name, returns null if there is no member with that name.
const Value &operator[]( const CppTL::ConstString &key ) const;
# endif
/// Returns the member named key if it exist, defaultValue otherwise.
/// Return the member named key if it exist, defaultValue otherwise.
Value get( const char *key,
const Value &defaultValue ) const;
/// Returns the member named key if it exist, defaultValue otherwise.
/// Return the member named key if it exist, defaultValue otherwise.
Value get( const std::string &key,
const Value &defaultValue ) const;
# ifdef JSON_USE_CPPTL
/// Returns the member named key if it exist, defaultValue otherwise.
/// Return the member named key if it exist, defaultValue otherwise.
Value get( const CppTL::ConstString &key,
const Value &defaultValue ) const;
# endif
/// Returns true if the object has a member named key.
/// \brief Remove and return the named member.
///
/// Do nothing if it did not exist.
/// \return the removed Value, or null.
/// \pre type() is objectValue or nullValue
/// \post type() is unchanged
Value removeMember( const char* key );
/// Same as removeMember(const char*)
Value removeMember( const std::string &key );
/// Return true if the object has a member named key.
bool isMember( const char *key ) const;
/// Returns true if the object has a member named key.
/// Return true if the object has a member named key.
bool isMember( const std::string &key ) const;
# ifdef JSON_USE_CPPTL
/// Returns true if the object has a member named key.
/// Return true if the object has a member named key.
bool isMember( const CppTL::ConstString &key ) const;
# endif
// Returns a list of the member names.
/// \brief Return a list of the member names.
///
/// If null, return an empty list.
/// \pre type() is objectValue or nullValue
/// \post if type() was nullValue, it remains nullValue
Members getMemberNames() const;
//# ifdef JSON_USE_CPPTL
@ -337,6 +352,7 @@ namespace Json {
void setComment( const std::string &comment,
CommentPlacement placement );
bool hasComment( CommentPlacement placement ) const;
/// Include delimiters and embedded newlines.
std::string getComment( CommentPlacement placement ) const;
std::string toStyledString() const;
@ -864,13 +880,13 @@ public: // overridden from ValueArrayAllocator
return computeDistance( other );
}
/// Returns either the index or the member name of the referenced value as a Value.
/// Return either the index or the member name of the referenced value as a Value.
Value key() const;
/// Returns the index of the referenced Value. -1 if it is not an arrayValue.
/// Return the index of the referenced Value. -1 if it is not an arrayValue.
Value::UInt index() const;
/// Returns the member name of the referenced Value. "" if it is not an objectValue.
/// Return the member name of the referenced Value. "" if it is not an objectValue.
const char *memberName() const;
protected:

View File

@ -1103,6 +1103,37 @@ Value::get( const std::string &key,
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);
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
@ -1140,6 +1171,8 @@ 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