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. /// otherwise returns defaultValue.
Value get( UInt index, Value get( UInt index,
const Value &defaultValue ) const; const Value &defaultValue ) const;
/// Returns true if index < size(). /// Return true if index < size().
bool isValidIndex( UInt index ) const; 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; /// Equivalent to jsonvalue[jsonvalue.size()] = value;
Value &append( const Value &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. /// Access an object value by name, returns null if there is no member with that name.
const Value &operator[]( const CppTL::ConstString &key ) const; const Value &operator[]( const CppTL::ConstString &key ) const;
# endif # 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, Value get( const char *key,
const Value &defaultValue ) const; 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, Value get( const std::string &key,
const Value &defaultValue ) const; const Value &defaultValue ) const;
# ifdef JSON_USE_CPPTL # 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, Value get( const CppTL::ConstString &key,
const Value &defaultValue ) const; const Value &defaultValue ) const;
# endif # 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; 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; bool isMember( const std::string &key ) const;
# ifdef JSON_USE_CPPTL # 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; bool isMember( const CppTL::ConstString &key ) const;
# endif # 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; Members getMemberNames() const;
//# ifdef JSON_USE_CPPTL //# ifdef JSON_USE_CPPTL
@ -337,6 +352,7 @@ namespace Json {
void setComment( const std::string &comment, void setComment( const std::string &comment,
CommentPlacement placement ); CommentPlacement placement );
bool hasComment( CommentPlacement placement ) const; bool hasComment( CommentPlacement placement ) const;
/// Include delimiters and embedded newlines.
std::string getComment( CommentPlacement placement ) const; std::string getComment( CommentPlacement placement ) const;
std::string toStyledString() const; std::string toStyledString() const;
@ -864,13 +880,13 @@ public: // overridden from ValueArrayAllocator
return computeDistance( other ); 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; 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; 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; const char *memberName() const;
protected: protected:

View File

@ -1103,6 +1103,37 @@ Value::get( const std::string &key,
return get( key.c_str(), defaultValue ); 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 # ifdef JSON_USE_CPPTL
Value Value
@ -1140,6 +1171,8 @@ Value::Members
Value::getMemberNames() const Value::getMemberNames() const
{ {
JSON_ASSERT( type_ == nullValue || type_ == objectValue ); JSON_ASSERT( type_ == nullValue || type_ == objectValue );
if ( type_ == nullValue )
return Value::Members();
Members members; Members members;
members.reserve( value_.map_->size() ); members.reserve( value_.map_->size() );
#ifndef JSON_VALUE_USE_INTERNAL_MAP #ifndef JSON_VALUE_USE_INTERNAL_MAP