bblanchon/ArduinoJson

Library fails to parse escaped single quote

zvirja opened this issue · 6 comments

Describe the bug
Library fails to parse JSON when using single quotes for JSON structure and escaping it inside the value. I know that it's a bit weird to use single quote for JSON elements, but that's what HomeAssistant & ESPHome are doing 😢

Troubleshooter report
Irrelevant

Environment
Here is the environment that I used:

  • Microcontroller: ESP8266, ESP32
  • Core/runtime: ESPHome

Reproduction
Here is a small snippet that reproduces the issue.

auto json = R"({ 'summary': 'a\'b' })"
          
ArduinoJson::JsonDocument doc;
auto res = ArduinoJson::deserializeJson(doc, json);
auto summary = doc.as<ArduinoJson::JsonObject>()["summary"].as<std::string>();

Program output

Expected output:

res: OK
summary: a'b

Actual output:

res: InvalidInput
summary: null

Note:

It works when using double-quotes and escape those:

auto json = R"({ "summary": "x a\"b y" })";

Thank you very much for reporting this issue.
The fix will be included in ArduinoJson 7.2.0 very soon.

@bblanchon Wow, thanks for a really fast fix! 😯☺

Is it worth to have logic that escape sequences are different depending on whether we are in single or double quotes surrounding context? So that in the { "summary": "a\\'b" } context sequence is kept as-is and vice versa? I.e. value is a\'b after deserializing instead of a'b.

Maybe that's what is already happening or maybe it makes not much sense - it was just a thought.

This one should work as before since it's the backslash that is escaped, not the quote.

{"summary": "a\\'b"}

However, the following one used to return InvalidInput but will now return Ok:

{"summary": "a\'b"}

I think it's correct, even if it's not standard.
ArduinoJson never intended to be a strict parser.

The fix is available in ArduinoJson 7.2.0.

@bblanchon Just verified it with Home Assistant - works like a charm! Thanks for your fast reply and being so nice ❤️

You're welcome, @zvirja.
Thank you for using ArduinoJson!
Don't forget to cast a star to support the project 😉