ArduinoJson icon indicating copy to clipboard operation
ArduinoJson copied to clipboard

"_json" user defined string-literal suffix

Open hattesen opened this issue 8 months ago • 1 comments

Adding a _json user defined string-literal suffix to ArduinoJson would provide a cleaner and more declarative source code at virtually no cost, and allow a JsonDocument to be created from a JSON string literal in one line of source code.

Example (from the current ArduinoJson documentation), generating a JsonDocument from a JSON string literal:

const char* json = "{\"hello\":\"world\"}";
JsonDocument doc;
deserializeJson(doc, json);

With a _json user defined string-literal suffix, this following source code would be functionally equivalent:

JsonDocument doc = "{\"hello\":\"world\"}"_json;

It removes all the boilerplate code when parsing (unmarshalling, deserializing) a literal JSON string. It would furthermore possibly allow the parsing to be performed at compile time, since all the required resources are available then.

Using a raw string literal for the JSON string literal, would make the example even simpler on the eye (and easier to type):

JsonDocument doc = R"({"hello":"world"})"_json;

This feature would add no additional runtime cost, would be fully backward compatible, and would not require much time to implement (I believe).

hattesen avatar Nov 14 '23 23:11 hattesen