Add support for escape sequence \'

Fixes #2124
This commit is contained in:
Benoit Blanchon
2024-09-17 10:25:25 +02:00
parent c1a507c158
commit f806a42cc2
4 changed files with 23 additions and 2 deletions

View File

@ -10,6 +10,7 @@ HEAD
* Improve message when user forgets third arg of `serializeJson()` et al.
* Set `ARDUINOJSON_USE_DOUBLE` to `0` by default on 8-bit architectures
* Deprecate `containsKey()` in favor of `doc["key"].is<T>()`
* Add support for escape sequence `\'` (issue #2124)
| Architecture | before | after |
|--------------|----------|----------|

View File

@ -83,6 +83,22 @@ TEST_CASE("Truncated JSON string") {
}
}
TEST_CASE("Escape single quote in single quoted string") {
JsonDocument doc;
DeserializationError err = deserializeJson(doc, "'ab\\\'cd'");
REQUIRE(err == DeserializationError::Ok);
CHECK(doc.as<std::string>() == "ab\'cd");
}
TEST_CASE("Escape double quote in double quoted string") {
JsonDocument doc;
DeserializationError err = deserializeJson(doc, "'ab\\\"cd'");
REQUIRE(err == DeserializationError::Ok);
CHECK(doc.as<std::string>() == "ab\"cd");
}
TEST_CASE("Invalid JSON string") {
const char* testCases[] = {"'\\u'", "'\\u000g'", "'\\u000'",
"'\\u000G'", "'\\u000/'", "'\\x1234'"};

View File

@ -46,6 +46,10 @@ TEST_CASE("serializeJson(JsonVariant)") {
check("fifty/fifty"_s, "\"fifty/fifty\"");
}
SECTION("Don't escape single quote") {
check("hello'world"_s, "\"hello'world\"");
}
SECTION("Escape backspace") {
check("hello\bworld"_s, "\"hello\\bworld\"");
}

View File

@ -32,8 +32,8 @@ class EscapeSequence {
}
private:
static const char* escapeTable(bool excludeSolidus) {
return &"//\"\"\\\\b\bf\fn\nr\rt\t"[excludeSolidus ? 2 : 0];
static const char* escapeTable(bool isSerializing) {
return &"//''\"\"\\\\b\bf\fn\nr\rt\t"[isSerializing ? 4 : 0];
}
};