Skip to content

Commit

Permalink
Add support for escape sequence \'
Browse files Browse the repository at this point in the history
Fixes #2124
  • Loading branch information
bblanchon committed Sep 17, 2024
1 parent c1a507c commit f806a42
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 |
|--------------|----------|----------|
Expand Down
16 changes: 16 additions & 0 deletions extras/tests/JsonDeserializer/string.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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'"};
Expand Down
4 changes: 4 additions & 0 deletions extras/tests/JsonSerializer/JsonVariant.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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\"");
}
Expand Down
4 changes: 2 additions & 2 deletions src/ArduinoJson/Json/EscapeSequence.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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];
}
};

Expand Down

0 comments on commit f806a42

Please sign in to comment.