ProgmemExample.ino 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  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. // JsonBuffer. 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;
  15. // You can use a Flash String as your JSON input.
  16. // WARNING: the content of the Flash String will be duplicated in the
  17. // JsonBuffer.
  18. deserializeJson(doc, F("{\"sensor\":\"gps\",\"time\":1351824120,"
  19. "\"data\":[48.756080,2.302038]}"));
  20. JsonObject obj = doc.as<JsonObject>();
  21. // You can use a Flash String to get an element of a JsonObject
  22. // No duplication is done.
  23. long time = obj[F("time")];
  24. // You can use a Flash String to set an element of a JsonObject
  25. // WARNING: the content of the Flash String will be duplicated in the
  26. // JsonBuffer.
  27. obj[F("time")] = time;
  28. // You can set a Flash String to a JsonObject or JsonArray:
  29. // WARNING: the content of the Flash String will be duplicated in the
  30. // JsonBuffer.
  31. obj["sensor"] = F("gps");
  32. // It works with serialized() too:
  33. obj["sensor"] = serialized(F("\"gps\""));
  34. obj["sensor"] = serialized(F("\xA3gps"), 3);
  35. // You can compare the content of a JsonVariant to a Flash String
  36. if (obj["sensor"] == F("gps")) {
  37. // ...
  38. }
  39. #else
  40. #warning PROGMEM is not supported on this platform
  41. #endif
  42. }
  43. void loop() {
  44. // not used in this example
  45. }
  46. // Visit https://arduinojson.org/v6/example/progmem/ for more.