Support NUL inside string values (issue #1646)

This commit is contained in:
Benoit Blanchon
2021-11-21 15:07:56 +01:00
parent a27398e445
commit be70f6ddd7
22 changed files with 144 additions and 59 deletions

View File

@ -8,7 +8,7 @@
#endif
TEST_CASE("string_view") {
StaticJsonDocument<128> doc;
StaticJsonDocument<256> doc;
JsonVariant variant = doc.to<JsonVariant>();
SECTION("deserializeJson()") {
@ -57,6 +57,12 @@ TEST_CASE("string_view") {
doc.add(std::string_view("example two", 7));
REQUIRE(doc.memoryUsage() == JSON_ARRAY_SIZE(2) + 8);
doc.add(std::string_view("example\0tree", 12));
REQUIRE(doc.memoryUsage() == JSON_ARRAY_SIZE(3) + 21);
doc.add(std::string_view("example\0tree and a half", 12));
REQUIRE(doc.memoryUsage() == JSON_ARRAY_SIZE(4) + 21);
}
SECTION("as<std::string_view>()") {
@ -72,6 +78,12 @@ TEST_CASE("string_view") {
REQUIRE(doc["s"].is<std::string_view>() == true);
REQUIRE(doc["i"].is<std::string_view>() == false);
}
SECTION("String containing NUL") {
doc.set(std::string("hello\0world", 11));
REQUIRE(doc.as<std::string_view>().size() == 11);
REQUIRE(doc.as<std::string_view>() == std::string_view("hello\0world", 11));
}
}
using ARDUINOJSON_NAMESPACE::adaptString;