Skip to content

Commit 8d95042

Browse files
committed
Fix comparisons operators with const JsonDocument&
1 parent bf5d0c7 commit 8d95042

File tree

3 files changed

+30
-3
lines changed

3 files changed

+30
-3
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ HEAD
77
* Add `ElementProxy::memoryUsage()`
88
* Add `MemberProxy::memoryUsage()` (issue #1730)
99
* Add implicit conversion from `JsonDocument` to `JsonVariant`
10+
* Fix comparisons operators with `const JsonDocument&`
1011

1112
v6.19.3 (2022-03-08)
1213
-------

extras/tests/JsonDocument/compare.cpp

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,3 +75,29 @@ TEST_CASE("StaticJsonDocument::operator==(const DynamicJsonDocument&)") {
7575
REQUIRE(doc1 != doc2);
7676
}
7777
}
78+
79+
TEST_CASE("JsonDocument::operator==(const JsonDocument&)") {
80+
StaticJsonDocument<256> doc1;
81+
StaticJsonDocument<256> doc2;
82+
const JsonDocument& ref1 = doc1;
83+
const JsonDocument& ref2 = doc2;
84+
85+
SECTION("Empty") {
86+
REQUIRE(ref1 == ref2);
87+
REQUIRE_FALSE(ref1 != ref2);
88+
}
89+
90+
SECTION("With same object") {
91+
doc1["hello"] = "world";
92+
doc2["hello"] = "world";
93+
REQUIRE(ref1 == ref2);
94+
REQUIRE_FALSE(ref1 != ref2);
95+
}
96+
97+
SECTION("With different object") {
98+
doc1["hello"] = "world";
99+
doc2["world"] = "hello";
100+
REQUIRE_FALSE(ref1 == ref2);
101+
REQUIRE(ref1 != ref2);
102+
}
103+
}

src/ArduinoJson/Variant/VariantCompare.hpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -131,9 +131,9 @@ struct RawComparer : ComparerBase {
131131
template <typename T>
132132
struct Comparer<T, typename enable_if<IsVisitable<T>::value>::type>
133133
: ComparerBase {
134-
T rhs;
134+
const T *rhs; // TODO: should be a VariantConstRef
135135

136-
explicit Comparer(T value) : rhs(value) {}
136+
explicit Comparer(const T &value) : rhs(&value) {}
137137

138138
CompareResult visitArray(const CollectionData &lhs) {
139139
ArrayComparer comparer(lhs);
@@ -183,7 +183,7 @@ struct Comparer<T, typename enable_if<IsVisitable<T>::value>::type>
183183
private:
184184
template <typename TComparer>
185185
CompareResult accept(TComparer &comparer) {
186-
CompareResult reversedResult = rhs.accept(comparer);
186+
CompareResult reversedResult = rhs->accept(comparer);
187187
switch (reversedResult) {
188188
case COMPARE_RESULT_GREATER:
189189
return COMPARE_RESULT_LESS;

0 commit comments

Comments
 (0)