ProgmemExample.ino 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. // ArduinoJson - arduinojson.org
  2. // Copyright Benoit Blanchon 2014-2018
  3. // MIT License
  4. //
  5. // This example shows the different ways you can use Flash strings with
  6. // ArduinoJson.
  7. //
  8. // Use Flash strings sparingly, because ArduinoJson duplicates them in the
  9. // JsonDocument. Prefer plain old char*, as they are more efficient in term of
  10. // code size, speed, and memory usage.
  11. #include <ArduinoJson.h>
  12. void setup() {
  13. #ifdef PROGMEM // <- check that Flash strings are supported
  14. DynamicJsonDocument doc(1024);
  15. // You can use a Flash String as your JSON input.
  16. // WARNING: the string in the input will be duplicated in the JsonDocument.
  17. deserializeJson(doc, F("{\"sensor\":\"gps\",\"time\":1351824120,"
  18. "\"data\":[48.756080,2.302038]}"));
  19. JsonObject obj = doc.as<JsonObject>();
  20. // You can use a Flash String to get an element of a JsonObject
  21. // No duplication is done.
  22. long time = obj[F("time")];
  23. // You can use a Flash String to set an element of a JsonObject
  24. // WARNING: the content of the Flash String will be duplicated in the
  25. // JsonDocument.
  26. obj[F("time")] = time;
  27. // You can set a Flash String to a JsonObject or JsonArray:
  28. // WARNING: the content of the Flash String will be duplicated in the
  29. // JsonDocument.
  30. obj["sensor"] = F("gps");
  31. // It works with serialized() too:
  32. obj["sensor"] = serialized(F("\"gps\""));
  33. obj["sensor"] = serialized(F("\xA3gps"), 3);
  34. // You can compare the content of a JsonVariant to a Flash String
  35. if (obj["sensor"] == F("gps")) {
  36. // ...
  37. }
  38. #else
  39. #warning PROGMEM is not supported on this platform
  40. #endif
  41. }
  42. void loop() {
  43. // not used in this example
  44. }
  45. // Visit https://arduinojson.org/v6/example/progmem/ for more.