diff --git a/CHANGELOG.md b/CHANGELOG.md index b329d441..55fa89b0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,6 +9,7 @@ HEAD - `as()` returns `0` if the integer `T` overflows - `is()` returns `false` if the integer `T` overflows * Added `BasicJsonDocument` to support custom allocator (issue #876) +* Added `JsonDocument::containsKey()` (issue #938) v6.9.1 (2019-03-01) ------ diff --git a/src/ArduinoJson/Document/JsonDocument.hpp b/src/ArduinoJson/Document/JsonDocument.hpp index c8ad783a..7789e6fd 100644 --- a/src/ArduinoJson/Document/JsonDocument.hpp +++ b/src/ArduinoJson/Document/JsonDocument.hpp @@ -124,6 +124,21 @@ class JsonDocument : public Visitable { return getOrAddMember(key).template to(); } + // containsKey(char*) const + // containsKey(const char*) const + // containsKey(const __FlashStringHelper*) const + template + bool containsKey(TChar* key) const { + return as().containsKey(key); + } + + // containsKey(const std::string&) const + // containsKey(const String&) const + template + bool containsKey(const TString& key) const { + return as().containsKey(key); + } + // operator[](const std::string&) // operator[](const String&) template diff --git a/test/JsonDocument/CMakeLists.txt b/test/JsonDocument/CMakeLists.txt index 29d78dc6..2bf05cf9 100644 --- a/test/JsonDocument/CMakeLists.txt +++ b/test/JsonDocument/CMakeLists.txt @@ -5,6 +5,7 @@ add_executable(JsonDocumentTests add.cpp BasicJsonDocument.cpp + containsKey.cpp createNested.cpp DynamicJsonDocument.cpp isNull.cpp diff --git a/test/JsonDocument/containsKey.cpp b/test/JsonDocument/containsKey.cpp new file mode 100644 index 00000000..6b48831c --- /dev/null +++ b/test/JsonDocument/containsKey.cpp @@ -0,0 +1,44 @@ +// ArduinoJson - arduinojson.org +// Copyright Benoit Blanchon 2014-2019 +// MIT License + +#include +#include + +TEST_CASE("JsonDocument::containsKey()") { + DynamicJsonDocument doc(4096); + + SECTION("returns true on object") { + doc["hello"] = "world"; + + REQUIRE(doc.containsKey("hello") == true); + } + + SECTION("returns true when value is null") { + doc["hello"] = static_cast(0); + + REQUIRE(doc.containsKey("hello") == true); + } + + SECTION("returns true when key is a std::string") { + doc["hello"] = "world"; + + REQUIRE(doc.containsKey(std::string("hello")) == true); + } + + SECTION("returns false on object") { + doc["world"] = "hello"; + + REQUIRE(doc.containsKey("hello") == false); + } + + SECTION("returns false on array") { + doc.add("hello"); + + REQUIRE(doc.containsKey("hello") == false); + } + + SECTION("returns false on null") { + REQUIRE(doc.containsKey("hello") == false); + } +}