Skip to content

Commit

Permalink
Fix filter not working in zero-copy mode (fixes #1697)
Browse files Browse the repository at this point in the history
  • Loading branch information
bblanchon committed Jan 13, 2022
1 parent 4f6244e commit d7f486d
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 2 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
----

* Fix crash when adding an object member in a too small `JsonDocument`
* Fix filter not working in zero-copy mode (issue #1697)

v6.19.0 (2022-01-08)
-------
Expand Down
14 changes: 14 additions & 0 deletions extras/tests/JsonDeserializer/filter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -667,6 +667,20 @@ TEST_CASE("Filtering") {
}
}

TEST_CASE("Zero-copy mode") { // issue #1697
char input[] = "{\"include\":42,\"exclude\":666}";

StaticJsonDocument<256> filter;
filter["include"] = true;

StaticJsonDocument<256> doc;
DeserializationError err =
deserializeJson(doc, input, DeserializationOption::Filter(filter));

REQUIRE(err == DeserializationError::Ok);
CHECK(doc.as<std::string>() == "{\"include\":42}");
}

TEST_CASE("Overloads") {
StaticJsonDocument<256> doc;
StaticJsonDocument<256> filter;
Expand Down
14 changes: 14 additions & 0 deletions extras/tests/MsgPackDeserializer/filter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1027,6 +1027,20 @@ TEST_CASE("deserializeMsgPack() filter") {
}
}

TEST_CASE("Zero-copy mode") { // issue #1697
char input[] = "\x82\xA7include\x01\xA6ignore\x02";

StaticJsonDocument<256> filter;
filter["include"] = true;

StaticJsonDocument<256> doc;
DeserializationError err =
deserializeMsgPack(doc, input, 18, DeserializationOption::Filter(filter));

CHECK(err == DeserializationError::Ok);
CHECK(doc.as<std::string>() == "{\"include\":1}");
}

TEST_CASE("Overloads") {
StaticJsonDocument<256> doc;
StaticJsonDocument<256> filter;
Expand Down
2 changes: 1 addition & 1 deletion src/ArduinoJson/Json/JsonDeserializer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ class JsonDeserializer {

String key = _stringStorage.str();

TFilter memberFilter = filter[key.c_str()];
TFilter memberFilter = filter[key];

if (memberFilter.allow()) {
VariantData *variant = object.getMember(adaptString(key.c_str()));
Expand Down
2 changes: 1 addition & 1 deletion src/ArduinoJson/MsgPack/MsgPackDeserializer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -418,7 +418,7 @@ class MsgPackDeserializer {
return false;

String key = _stringStorage.str();
TFilter memberFilter = filter[key.c_str()];
TFilter memberFilter = filter[key];
VariantData *member;

if (memberFilter.allow()) {
Expand Down

0 comments on commit d7f486d

Please sign in to comment.