Added JsonObject::containsKey()

This commit is contained in:
Benoit Blanchon
2014-11-07 17:27:30 +01:00
parent b4b475d692
commit d38131d495
3 changed files with 18 additions and 1 deletions

View File

@ -78,6 +78,9 @@ class JsonObject : public Internals::JsonPrintable<JsonObject>,
// This is a shortcut for JsonBuffer::createObject() and JsonObject::add(). // This is a shortcut for JsonBuffer::createObject() and JsonObject::add().
JsonObject &createNestedObject(key_type key); JsonObject &createNestedObject(key_type key);
// Tells weither the specified key is present and associated with a value.
bool containsKey(key_type key) const { return at(key).success(); }
// Removes the specified key and the associated value. // Removes the specified key and the associated value.
void remove(key_type key); void remove(key_type key);

View File

@ -38,7 +38,7 @@ class JsonVariant : public Internals::JsonPrintable<JsonVariant> {
} }
// Tells weither the variant is valid. // Tells weither the variant is valid.
bool success() { bool success() const {
return _type != Internals::JSON_INVALID && return _type != Internals::JSON_INVALID &&
_type != Internals::JSON_UNDEFINED; _type != Internals::JSON_UNDEFINED;
} }

View File

@ -114,3 +114,17 @@ TEST_F(JsonObject_Container_Tests, CanStoreInnerObjects) {
EXPECT_EQ(&innerObject1, &object["hello"].asObject()); EXPECT_EQ(&innerObject1, &object["hello"].asObject());
EXPECT_EQ(&innerObject2, &object["world"].asObject()); EXPECT_EQ(&innerObject2, &object["world"].asObject());
} }
TEST_F(JsonObject_Container_Tests, ContainsKeyReturnFalseForNonExistingKey) {
EXPECT_FALSE(object.containsKey("hello"));
}
TEST_F(JsonObject_Container_Tests, ContainsKeyReturnTrueForDefinedValue) {
object.add("hello", 42);
EXPECT_TRUE(object.containsKey("hello"));
}
TEST_F(JsonObject_Container_Tests, ContainsKeyReturnFalseForUndefinedValue) {
object.add("hello");
EXPECT_FALSE(object.containsKey("hello"));
}