Skip to content

Commit f806a42

Browse files
committed
Add support for escape sequence \'
Fixes #2124
1 parent c1a507c commit f806a42

File tree

4 files changed

+23
-2
lines changed

4 files changed

+23
-2
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ HEAD
1010
* Improve message when user forgets third arg of `serializeJson()` et al.
1111
* Set `ARDUINOJSON_USE_DOUBLE` to `0` by default on 8-bit architectures
1212
* Deprecate `containsKey()` in favor of `doc["key"].is<T>()`
13+
* Add support for escape sequence `\'` (issue #2124)
1314

1415
| Architecture | before | after |
1516
|--------------|----------|----------|

extras/tests/JsonDeserializer/string.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,22 @@ TEST_CASE("Truncated JSON string") {
8383
}
8484
}
8585

86+
TEST_CASE("Escape single quote in single quoted string") {
87+
JsonDocument doc;
88+
89+
DeserializationError err = deserializeJson(doc, "'ab\\\'cd'");
90+
REQUIRE(err == DeserializationError::Ok);
91+
CHECK(doc.as<std::string>() == "ab\'cd");
92+
}
93+
94+
TEST_CASE("Escape double quote in double quoted string") {
95+
JsonDocument doc;
96+
97+
DeserializationError err = deserializeJson(doc, "'ab\\\"cd'");
98+
REQUIRE(err == DeserializationError::Ok);
99+
CHECK(doc.as<std::string>() == "ab\"cd");
100+
}
101+
86102
TEST_CASE("Invalid JSON string") {
87103
const char* testCases[] = {"'\\u'", "'\\u000g'", "'\\u000'",
88104
"'\\u000G'", "'\\u000/'", "'\\x1234'"};

extras/tests/JsonSerializer/JsonVariant.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,10 @@ TEST_CASE("serializeJson(JsonVariant)") {
4646
check("fifty/fifty"_s, "\"fifty/fifty\"");
4747
}
4848

49+
SECTION("Don't escape single quote") {
50+
check("hello'world"_s, "\"hello'world\"");
51+
}
52+
4953
SECTION("Escape backspace") {
5054
check("hello\bworld"_s, "\"hello\\bworld\"");
5155
}

src/ArduinoJson/Json/EscapeSequence.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,8 @@ class EscapeSequence {
3232
}
3333

3434
private:
35-
static const char* escapeTable(bool excludeSolidus) {
36-
return &"//\"\"\\\\b\bf\fn\nr\rt\t"[excludeSolidus ? 2 : 0];
35+
static const char* escapeTable(bool isSerializing) {
36+
return &"//''\"\"\\\\b\bf\fn\nr\rt\t"[isSerializing ? 4 : 0];
3737
}
3838
};
3939

0 commit comments

Comments
 (0)