diff --git a/CHANGELOG.md b/CHANGELOG.md index 9c8a1fe8..7368a288 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,11 @@ ArduinoJson: change log ======================= +HEAD +---- + +* Added operators `==` and `!=` for `JsonDocument` + v6.11.2 (2019-07-08) ------- diff --git a/src/ArduinoJson/Document/JsonDocument.hpp b/src/ArduinoJson/Document/JsonDocument.hpp index 0934572e..48f89baf 100644 --- a/src/ArduinoJson/Document/JsonDocument.hpp +++ b/src/ArduinoJson/Document/JsonDocument.hpp @@ -282,6 +282,14 @@ class JsonDocument : public Visitable { return VariantConstRef(&_data); } + bool operator==(VariantConstRef rhs) const { + return getVariant() == rhs; + } + + bool operator!=(VariantConstRef rhs) const { + return getVariant() != rhs; + } + protected: JsonDocument(MemoryPool pool) : _pool(pool) { _data.setNull(); diff --git a/src/ArduinoJson/Operators/VariantComparisons.hpp b/src/ArduinoJson/Operators/VariantComparisons.hpp index 80094c38..db673dc6 100644 --- a/src/ArduinoJson/Operators/VariantComparisons.hpp +++ b/src/ArduinoJson/Operators/VariantComparisons.hpp @@ -126,7 +126,8 @@ class VariantComparisons { return compare(rhs, lhs) == 0; } template - friend bool operator==(const T &lhs, TVariant rhs) { + friend typename enable_if::value, bool>::type operator==( + const T &lhs, TVariant rhs) { return compare(rhs, lhs) == 0; } @@ -136,7 +137,8 @@ class VariantComparisons { return compare(lhs, rhs) == 0; } template - friend bool operator==(TVariant lhs, const T &rhs) { + friend typename enable_if::value, bool>::type operator==( + TVariant lhs, const T &rhs) { return compare(lhs, rhs) == 0; } @@ -146,7 +148,8 @@ class VariantComparisons { return compare(rhs, lhs) != 0; } template - friend bool operator!=(const T &lhs, TVariant rhs) { + friend typename enable_if::value, bool>::type operator!=( + const T &lhs, TVariant rhs) { return compare(rhs, lhs) != 0; } @@ -156,7 +159,8 @@ class VariantComparisons { return compare(lhs, rhs) != 0; } template - friend bool operator!=(TVariant lhs, const T &rhs) { + friend typename enable_if::value, bool>::type operator!=( + TVariant lhs, const T &rhs) { return compare(lhs, rhs) != 0; } diff --git a/src/ArduinoJson/Variant/VariantRef.hpp b/src/ArduinoJson/Variant/VariantRef.hpp index 00f82057..f6449326 100644 --- a/src/ArduinoJson/Variant/VariantRef.hpp +++ b/src/ArduinoJson/Variant/VariantRef.hpp @@ -380,5 +380,13 @@ class VariantConstRef : public VariantRefBase, const CollectionData *obj = variantAsObject(_data); return VariantConstRef(obj ? obj->get(adaptString(key)) : 0); } + + FORCE_INLINE bool operator==(VariantConstRef lhs) const { + return variantEquals(_data, lhs._data); + } + + FORCE_INLINE bool operator!=(VariantConstRef lhs) const { + return !variantEquals(_data, lhs._data); + } }; } // namespace ARDUINOJSON_NAMESPACE diff --git a/test/JsonDocument/CMakeLists.txt b/test/JsonDocument/CMakeLists.txt index 2bf05cf9..1e2d7b8a 100644 --- a/test/JsonDocument/CMakeLists.txt +++ b/test/JsonDocument/CMakeLists.txt @@ -5,6 +5,7 @@ add_executable(JsonDocumentTests add.cpp BasicJsonDocument.cpp + compare.cpp containsKey.cpp createNested.cpp DynamicJsonDocument.cpp diff --git a/test/JsonDocument/compare.cpp b/test/JsonDocument/compare.cpp new file mode 100644 index 00000000..2c0660bf --- /dev/null +++ b/test/JsonDocument/compare.cpp @@ -0,0 +1,77 @@ +// ArduinoJson - arduinojson.org +// Copyright Benoit Blanchon 2014-2019 +// MIT License + +#include +#include + +TEST_CASE("DynamicJsonDocument::operator==(const DynamicJsonDocument&)") { + DynamicJsonDocument doc1(4096); + DynamicJsonDocument doc2(4096); + + SECTION("Empty") { + REQUIRE(doc1 == doc2); + REQUIRE_FALSE(doc1 != doc2); + } + + SECTION("With same object") { + doc1["hello"] = "world"; + doc2["hello"] = "world"; + REQUIRE(doc1 == doc2); + REQUIRE_FALSE(doc1 != doc2); + } + SECTION("With different object") { + doc1["hello"] = "world"; + doc2["world"] = "hello"; + REQUIRE_FALSE(doc1 == doc2); + REQUIRE(doc1 != doc2); + } +} + +TEST_CASE("DynamicJsonDocument::operator==(const StaticJsonDocument&)") { + DynamicJsonDocument doc1(4096); + StaticJsonDocument<256> doc2; + + SECTION("Empty") { + REQUIRE(doc1 == doc2); + REQUIRE_FALSE(doc1 != doc2); + } + + SECTION("With same object") { + doc1["hello"] = "world"; + doc2["hello"] = "world"; + REQUIRE(doc1 == doc2); + REQUIRE_FALSE(doc1 != doc2); + } + + SECTION("With different object") { + doc1["hello"] = "world"; + doc2["world"] = "hello"; + REQUIRE_FALSE(doc1 == doc2); + REQUIRE(doc1 != doc2); + } +} + +TEST_CASE("StaticJsonDocument::operator==(const DynamicJsonDocument&)") { + StaticJsonDocument<256> doc1; + DynamicJsonDocument doc2(4096); + + SECTION("Empty") { + REQUIRE(doc1 == doc2); + REQUIRE_FALSE(doc1 != doc2); + } + + SECTION("With same object") { + doc1["hello"] = "world"; + doc2["hello"] = "world"; + REQUIRE(doc1 == doc2); + REQUIRE_FALSE(doc1 != doc2); + } + + SECTION("With different object") { + doc1["hello"] = "world"; + doc2["world"] = "hello"; + REQUIRE_FALSE(doc1 == doc2); + REQUIRE(doc1 != doc2); + } +}