Skip to content

Commit

Permalink
Add GenericValue::EraseMember(string types) APIs
Browse files Browse the repository at this point in the history
  • Loading branch information
miloyip committed May 21, 2015
1 parent 1a570c3 commit 6e1d10e
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 0 deletions.
25 changes: 25 additions & 0 deletions include/rapidjson/document.h
Original file line number Diff line number Diff line change
Expand Up @@ -1213,6 +1213,31 @@ class GenericValue {
return pos;
}

//! Erase a member in object by its name.
/*! \param name Name of member to be removed.
\return Whether the member existed.
\note Linear time complexity.
*/
bool EraseMember(const Ch* name) {
GenericValue n(StringRef(name));
return EraseMember(n);
}

#if RAPIDJSON_HAS_STDSTRING
bool EraseMember(const std::basic_string<Ch>& name) { return EraseMember(GenericValue(StringRef(name))); }
#endif

template <typename SourceAllocator>
bool EraseMember(const GenericValue<Encoding, SourceAllocator>& name) {
MemberIterator m = FindMember(name);
if (m != MemberEnd()) {
EraseMember(m);
return true;
}
else
return false;
}

//@}

//!@name Array
Expand Down
18 changes: 18 additions & 0 deletions test/unittest/valuetest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1182,6 +1182,24 @@ TEST(Value, Object) {
EXPECT_TRUE(z.IsObject());
}

TEST(Value, EraseMember_String) {
Value::AllocatorType allocator;
Value x(kObjectType);
x.AddMember("A", "Apple", allocator);
x.AddMember("B", "Banana", allocator);

EXPECT_TRUE(x.EraseMember("B"));
EXPECT_FALSE(x.HasMember("B"));

EXPECT_FALSE(x.EraseMember("nonexist"));

GenericValue<UTF8<>, CrtAllocator> othername("A");
EXPECT_TRUE(x.EraseMember(othername));
EXPECT_FALSE(x.HasMember("A"));

EXPECT_TRUE(x.MemberBegin() == x.MemberEnd());
}

TEST(Value, BigNestedArray) {
MemoryPoolAllocator<> allocator;
Value x(kArrayType);
Expand Down

0 comments on commit 6e1d10e

Please sign in to comment.