Commit e27f7e71 authored by Milo Yip's avatar Milo Yip

Merge pull request #119 from miloyip/issue116extramemberapi

Three new APIs are added for JSON object type.
parents 991aeaa0 284dcf3d
......@@ -534,9 +534,8 @@ public:
break;
case kObjectFlag:
for (MemberIterator m = MemberBegin(); m != MemberEnd(); ++m) {
for (MemberIterator m = MemberBegin(); m != MemberEnd(); ++m)
m->~Member();
}
Allocator::Free(data_.o.members);
break;
......@@ -745,6 +744,12 @@ public:
/*! \post IsObject() == true */
GenericValue& SetObject() { this->~GenericValue(); new (this) GenericValue(kObjectType); return *this; }
//! Get the number of members in the object.
SizeType MemberCount() const { RAPIDJSON_ASSERT(IsObject()); return data_.o.size; }
//! Check whether the object is empty.
bool ObjectEmpty() const { RAPIDJSON_ASSERT(IsObject()); return data_.o.size == 0; }
//! Get the value associated with the name.
/*!
\note In version 0.1x, if the member is not found, this function returns a null value. This makes issue 7.
......@@ -936,6 +941,17 @@ public:
return AddMember(n, v, allocator);
}
//! Remove all members in the object.
/*! This function do not deallocate memory in the object, i.e. the capacity is unchanged.
\note Linear time complexity.
*/
void RemoveAllMembers() {
RAPIDJSON_ASSERT(IsObject());
for (MemberIterator m = MemberBegin(); m != MemberEnd(); ++m)
m->~Member();
data_.o.size = 0;
}
//! Remove a member in object by its name.
/*! \param name Name of member to be removed.
\return Whether the member existed.
......
......@@ -763,14 +763,21 @@ TEST(Value, Object) {
EXPECT_EQ(kObjectType, x.GetType());
EXPECT_TRUE(x.IsObject());
EXPECT_TRUE(x.ObjectEmpty());
EXPECT_EQ(0u, x.MemberCount());
EXPECT_EQ(kObjectType, y.GetType());
EXPECT_TRUE(y.IsObject());
EXPECT_TRUE(y.ObjectEmpty());
EXPECT_EQ(0u, y.MemberCount());
// AddMember()
x.AddMember("A", "Apple", allocator);
EXPECT_FALSE(x.ObjectEmpty());
EXPECT_EQ(1u, x.MemberCount());
Value value("Banana", 6);
x.AddMember("B", "Banana", allocator);
EXPECT_EQ(2u, x.MemberCount());
// AddMember<T>(StringRefType, T, Allocator)
{
......@@ -791,6 +798,7 @@ TEST(Value, Object) {
EXPECT_EQ(INT64_C(-4294967296), o["int64"].GetInt64());
EXPECT_EQ(UINT64_C(4294967296), o["uint64"].GetUint64());
EXPECT_STREQ("Jelly",o["string"].GetString());
EXPECT_EQ(8u, o.MemberCount());
}
// Tests a member with null character
......@@ -955,6 +963,11 @@ TEST(Value, Object) {
}
}
// RemoveAllMembers()
x.RemoveAllMembers();
EXPECT_TRUE(x.ObjectEmpty());
EXPECT_EQ(0u, x.MemberCount());
// SetObject()
Value z;
z.SetObject();
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment