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 21b2c76
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 1 deletion.
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
16 changes: 16 additions & 0 deletions extras/tests/JsonDeserializer/object.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,22 @@ TEST_CASE("deserialize JSON object") {
REQUIRE(err == DeserializationError::Ok);
REQUIRE(doc["a"] == 2);
}

SECTION("Repeated key with zero copy mode") { // issue #1697
char input[] = "{a:{b:{c:1}},a:2}";
DeserializationError err = deserializeJson(doc, input);

REQUIRE(err == DeserializationError::Ok);
REQUIRE(doc["a"] == 2);
}

SECTION("NUL in keys") { // we don't support NULs in keys
DeserializationError err =
deserializeJson(doc, "{\"x\\u0000a\":1,\"x\\u0000b\":2}");

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

SECTION("Should clear the JsonObject") {
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/StringStorage/StringMover.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ class StringMover {
}

FORCE_INLINE String save() {
_writePtr[0] = 0; // terminator
String s = str();
_writePtr++;
return s;
Expand All @@ -33,6 +32,7 @@ class StringMover {
}

String str() const {
_writePtr[0] = 0; // terminator
return String(_startPtr, size(), true);
}

Expand Down

0 comments on commit 21b2c76

Please sign in to comment.