Skip to content

Commit b54de58

Browse files
committed
Fixed comparison of JsonVariant with mixed strings (closes #1051)
1 parent 795e372 commit b54de58

File tree

4 files changed

+26
-9
lines changed

4 files changed

+26
-9
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ HEAD
55
----
66

77
* Added operators `==` and `!=` for `JsonDocument`, `ElementProxy`, and `MemberProxy`
8+
* Fixed comparison of `JsonVariant` when one contains a linked string and the other contains an owned string (issue #1051)
89

910
v6.11.2 (2019-07-08)
1011
-------

src/ArduinoJson/Variant/VariantContent.hpp

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,18 @@ namespace ARDUINOJSON_NAMESPACE {
1616
enum {
1717
VALUE_MASK = 0x7F,
1818

19+
OWNERSHIP_BIT = 0x01,
1920
VALUE_IS_NULL = 0,
20-
VALUE_IS_LINKED_RAW = 0x01,
21-
VALUE_IS_OWNED_RAW = 0x02,
22-
VALUE_IS_LINKED_STRING = 0x03,
23-
VALUE_IS_OWNED_STRING = 0x04,
24-
VALUE_IS_BOOLEAN = 0x05,
25-
VALUE_IS_POSITIVE_INTEGER = 0x06,
26-
VALUE_IS_NEGATIVE_INTEGER = 0x07,
27-
VALUE_IS_FLOAT = 0x08,
21+
VALUE_IS_LINKED_RAW = 0x02,
22+
VALUE_IS_OWNED_RAW = 0x03,
23+
VALUE_IS_LINKED_STRING = 0x04,
24+
VALUE_IS_OWNED_STRING = 0x05,
25+
26+
// CAUTION: no OWNERSHIP_BIT below
27+
VALUE_IS_BOOLEAN = 0x06,
28+
VALUE_IS_POSITIVE_INTEGER = 0x08,
29+
VALUE_IS_NEGATIVE_INTEGER = 0x0A,
30+
VALUE_IS_FLOAT = 0x0C,
2831

2932
COLLECTION_MASK = 0x60,
3033
VALUE_IS_OBJECT = 0x20,

src/ArduinoJson/Variant/VariantData.hpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,9 @@ class VariantData {
101101
}
102102

103103
bool equals(const VariantData &other) const {
104-
if (type() != other.type()) return false;
104+
// Check that variant have the same type, but ignore string ownership
105+
if ((type() | OWNERSHIP_BIT) != (other.type() | OWNERSHIP_BIT))
106+
return false;
105107

106108
switch (type()) {
107109
case VALUE_IS_LINKED_STRING:

test/JsonVariant/compare.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -286,6 +286,17 @@ TEST_CASE("JsonVariant comparisons") {
286286
REQUIRE_FALSE(variant1 == variant3);
287287
}
288288

289+
SECTION("Variants containing mixed strings (issue #1051)") {
290+
variant1.set("hello");
291+
variant2.set(std::string("hello"));
292+
293+
REQUIRE(variant1 == variant2);
294+
REQUIRE_FALSE(variant1 != variant2);
295+
296+
REQUIRE(variant2 == variant1);
297+
REQUIRE_FALSE(variant2 != variant1);
298+
}
299+
289300
SECTION("Variants containing double") {
290301
variant1.set(42.0);
291302
variant2.set(42.0);

0 commit comments

Comments
 (0)