bblanchon / ArduinoJson

📟 JSON library for Arduino and embedded C++. Simple and efficient.
https://arduinojson.org
MIT License
6.73k stars 1.12k forks source link

Library fails to parse escaped single quote #2124

Closed zvirja closed 1 month ago

zvirja commented 1 month ago

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:

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" })";
bblanchon commented 1 month ago

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

zvirja commented 1 month ago

@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.

bblanchon commented 1 month ago

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.

bblanchon commented 1 month ago

The fix is available in ArduinoJson 7.2.0.

zvirja commented 1 month ago

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

bblanchon commented 1 month ago

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