ArduinoJSON: Working with Flash Strings

ArduinoSoftware & CodingHardware

The syntax for deserialization is as follows −

deserializeJson(doc, json)

where doc is the JSON Document in which you will store the deserialized output, and json is the buffer containing the JSON contents.

The good news is that the buffer can be taken directly from the PROGMEM. In other words, if you don't want to store a heavy JSON string into the RAM, you can store it into the program memory or the flash, using the F() macro, and the deserialization will work just fine.

Example

For example, the following snippet works perfectly fine.

StaticJsonDocument<200> doc;
DeserializationError error = deserializeJson(doc,
F("{\"name\":\"Yash\",\"designation\":\"student\"}") );

You can now go ahead an extract values from the doc.

String name = doc["name"];

Similarly, Flash strings can be used for constructing a JSON (serialization)

doc["name"] = F("Yash");

This helps save RAM. Each hard-coded string is stored by default in the RAM. If your application has a lot of such strings, the RAM can get overwhelmed. Flash strings can be of immense help here.

You can also refer to the inbuilt ProgmemExample to look at more examples of how to work with Flash strings.

raja
Published on 26-Jul-2021 10:24:01
Advertisements