forked from bblanchon/ArduinoJson
Added operators ==
and !=
for JsonDocument
This commit is contained in:
@ -1,6 +1,11 @@
|
|||||||
ArduinoJson: change log
|
ArduinoJson: change log
|
||||||
=======================
|
=======================
|
||||||
|
|
||||||
|
HEAD
|
||||||
|
----
|
||||||
|
|
||||||
|
* Added operators `==` and `!=` for `JsonDocument`
|
||||||
|
|
||||||
v6.11.2 (2019-07-08)
|
v6.11.2 (2019-07-08)
|
||||||
-------
|
-------
|
||||||
|
|
||||||
|
@ -282,6 +282,14 @@ class JsonDocument : public Visitable {
|
|||||||
return VariantConstRef(&_data);
|
return VariantConstRef(&_data);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool operator==(VariantConstRef rhs) const {
|
||||||
|
return getVariant() == rhs;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool operator!=(VariantConstRef rhs) const {
|
||||||
|
return getVariant() != rhs;
|
||||||
|
}
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
JsonDocument(MemoryPool pool) : _pool(pool) {
|
JsonDocument(MemoryPool pool) : _pool(pool) {
|
||||||
_data.setNull();
|
_data.setNull();
|
||||||
|
@ -126,7 +126,8 @@ class VariantComparisons {
|
|||||||
return compare(rhs, lhs) == 0;
|
return compare(rhs, lhs) == 0;
|
||||||
}
|
}
|
||||||
template <typename T>
|
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;
|
return compare(rhs, lhs) == 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -136,7 +137,8 @@ class VariantComparisons {
|
|||||||
return compare(lhs, rhs) == 0;
|
return compare(lhs, rhs) == 0;
|
||||||
}
|
}
|
||||||
template <typename T>
|
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;
|
return compare(lhs, rhs) == 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -146,7 +148,8 @@ class VariantComparisons {
|
|||||||
return compare(rhs, lhs) != 0;
|
return compare(rhs, lhs) != 0;
|
||||||
}
|
}
|
||||||
template <typename T>
|
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;
|
return compare(rhs, lhs) != 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -156,7 +159,8 @@ class VariantComparisons {
|
|||||||
return compare(lhs, rhs) != 0;
|
return compare(lhs, rhs) != 0;
|
||||||
}
|
}
|
||||||
template <typename T>
|
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;
|
return compare(lhs, rhs) != 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -380,5 +380,13 @@ class VariantConstRef : public VariantRefBase<const VariantData>,
|
|||||||
const CollectionData *obj = variantAsObject(_data);
|
const CollectionData *obj = variantAsObject(_data);
|
||||||
return VariantConstRef(obj ? obj->get(adaptString(key)) : 0);
|
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
|
} // namespace ARDUINOJSON_NAMESPACE
|
||||||
|
@ -5,6 +5,7 @@
|
|||||||
add_executable(JsonDocumentTests
|
add_executable(JsonDocumentTests
|
||||||
add.cpp
|
add.cpp
|
||||||
BasicJsonDocument.cpp
|
BasicJsonDocument.cpp
|
||||||
|
compare.cpp
|
||||||
containsKey.cpp
|
containsKey.cpp
|
||||||
createNested.cpp
|
createNested.cpp
|
||||||
DynamicJsonDocument.cpp
|
DynamicJsonDocument.cpp
|
||||||
|
77
test/JsonDocument/compare.cpp
Normal file
77
test/JsonDocument/compare.cpp
Normal 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);
|
||||||
|
}
|
||||||
|
}
|
Reference in New Issue
Block a user