From f806a42cc254337608aa06b770ee2bbb584fbdb4 Mon Sep 17 00:00:00 2001 From: Benoit Blanchon Date: Tue, 17 Sep 2024 10:25:25 +0200 Subject: [PATCH] Add support for escape sequence `\'` Fixes #2124 --- CHANGELOG.md | 1 + extras/tests/JsonDeserializer/string.cpp | 16 ++++++++++++++++ extras/tests/JsonSerializer/JsonVariant.cpp | 4 ++++ src/ArduinoJson/Json/EscapeSequence.hpp | 4 ++-- 4 files changed, 23 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 48347bc5a..f8c1e055d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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()` +* Add support for escape sequence `\'` (issue #2124) | Architecture | before | after | |--------------|----------|----------| diff --git a/extras/tests/JsonDeserializer/string.cpp b/extras/tests/JsonDeserializer/string.cpp index a47ab76e5..a760ae6e6 100644 --- a/extras/tests/JsonDeserializer/string.cpp +++ b/extras/tests/JsonDeserializer/string.cpp @@ -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() == "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() == "ab\"cd"); +} + TEST_CASE("Invalid JSON string") { const char* testCases[] = {"'\\u'", "'\\u000g'", "'\\u000'", "'\\u000G'", "'\\u000/'", "'\\x1234'"}; diff --git a/extras/tests/JsonSerializer/JsonVariant.cpp b/extras/tests/JsonSerializer/JsonVariant.cpp index 120bddc62..1ddaea62d 100644 --- a/extras/tests/JsonSerializer/JsonVariant.cpp +++ b/extras/tests/JsonSerializer/JsonVariant.cpp @@ -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\""); } diff --git a/src/ArduinoJson/Json/EscapeSequence.hpp b/src/ArduinoJson/Json/EscapeSequence.hpp index e1b7199ee..35a99d66c 100644 --- a/src/ArduinoJson/Json/EscapeSequence.hpp +++ b/src/ArduinoJson/Json/EscapeSequence.hpp @@ -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]; } };