Added operators == and != for JsonDocument

This commit is contained in:
Benoit Blanchon
2019-07-19 10:10:16 +02:00
parent 815326d42e
commit aba42faf69
6 changed files with 107 additions and 4 deletions

View File

@ -1,6 +1,11 @@
ArduinoJson: change log
=======================
HEAD
----
* Added operators `==` and `!=` for `JsonDocument`
v6.11.2 (2019-07-08)
-------

View File

@ -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();

View File

@ -126,7 +126,8 @@ class VariantComparisons {
return compare(rhs, lhs) == 0;
}
template <typename T>
friend bool operator==(const T &lhs, TVariant rhs) {
friend typename enable_if<!IsVisitable<T>::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 <typename T>
friend bool operator==(TVariant lhs, const T &rhs) {
friend typename enable_if<!IsVisitable<T>::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 <typename T>
friend bool operator!=(const T &lhs, TVariant rhs) {
friend typename enable_if<!IsVisitable<T>::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 <typename T>
friend bool operator!=(TVariant lhs, const T &rhs) {
friend typename enable_if<!IsVisitable<T>::value, bool>::type operator!=(
TVariant lhs, const T &rhs) {
return compare(lhs, rhs) != 0;
}

View File

@ -380,5 +380,13 @@ class VariantConstRef : public VariantRefBase<const VariantData>,
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

View File

@ -5,6 +5,7 @@
add_executable(JsonDocumentTests
add.cpp
BasicJsonDocument.cpp
compare.cpp
containsKey.cpp
createNested.cpp
DynamicJsonDocument.cpp

View File

@ -0,0 +1,77 @@
// ArduinoJson - arduinojson.org
// Copyright Benoit Blanchon 2014-2019
// MIT License
#include <ArduinoJson.h>
#include <catch.hpp>
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);
}
}