Skip to content

Commit

Permalink
Fixed comparison of JsonVariant with mixed strings (closes #1051)
Browse files Browse the repository at this point in the history
  • Loading branch information
bblanchon committed Jul 19, 2019
1 parent 795e372 commit b54de58
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 9 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ HEAD
----

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

v6.11.2 (2019-07-08)
-------
Expand Down
19 changes: 11 additions & 8 deletions src/ArduinoJson/Variant/VariantContent.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,18 @@ namespace ARDUINOJSON_NAMESPACE {
enum {
VALUE_MASK = 0x7F,

OWNERSHIP_BIT = 0x01,
VALUE_IS_NULL = 0,
VALUE_IS_LINKED_RAW = 0x01,
VALUE_IS_OWNED_RAW = 0x02,
VALUE_IS_LINKED_STRING = 0x03,
VALUE_IS_OWNED_STRING = 0x04,
VALUE_IS_BOOLEAN = 0x05,
VALUE_IS_POSITIVE_INTEGER = 0x06,
VALUE_IS_NEGATIVE_INTEGER = 0x07,
VALUE_IS_FLOAT = 0x08,
VALUE_IS_LINKED_RAW = 0x02,
VALUE_IS_OWNED_RAW = 0x03,
VALUE_IS_LINKED_STRING = 0x04,
VALUE_IS_OWNED_STRING = 0x05,

// CAUTION: no OWNERSHIP_BIT below
VALUE_IS_BOOLEAN = 0x06,
VALUE_IS_POSITIVE_INTEGER = 0x08,
VALUE_IS_NEGATIVE_INTEGER = 0x0A,
VALUE_IS_FLOAT = 0x0C,

COLLECTION_MASK = 0x60,
VALUE_IS_OBJECT = 0x20,
Expand Down
4 changes: 3 additions & 1 deletion src/ArduinoJson/Variant/VariantData.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,9 @@ class VariantData {
}

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

switch (type()) {
case VALUE_IS_LINKED_STRING:
Expand Down
11 changes: 11 additions & 0 deletions test/JsonVariant/compare.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,17 @@ TEST_CASE("JsonVariant comparisons") {
REQUIRE_FALSE(variant1 == variant3);
}

SECTION("Variants containing mixed strings (issue #1051)") {
variant1.set("hello");
variant2.set(std::string("hello"));

REQUIRE(variant1 == variant2);
REQUIRE_FALSE(variant1 != variant2);

REQUIRE(variant2 == variant1);
REQUIRE_FALSE(variant2 != variant1);
}

SECTION("Variants containing double") {
variant1.set(42.0);
variant2.set(42.0);
Expand Down

0 comments on commit b54de58

Please sign in to comment.