ProgmemExample.ino 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. // Copyright Benoit Blanchon 2014-2017
  2. // MIT License
  3. //
  4. // Arduino JSON library
  5. // https://bblanchon.github.io/ArduinoJson/
  6. // If you like this project, please add a star!
  7. #include <ArduinoJson.h>
  8. // About
  9. // -----
  10. // This example shows the different ways you can use PROGMEM with ArduinoJson.
  11. // Please don't see this as an invitation to use PROGMEM.
  12. // On the contrary, you should always use char[] when possible, it's much more
  13. // efficient in term of code size, speed and memory usage.
  14. void setup() {
  15. #ifdef PROGMEM
  16. DynamicJsonBuffer jsonBuffer;
  17. // You can use a Flash String as your JSON input.
  18. // WARNING: the content of the Flash String will be duplicated in the
  19. // JsonBuffer.
  20. JsonObject& root =
  21. jsonBuffer.parseObject(F("{\"sensor\":\"gps\",\"time\":1351824120,"
  22. "\"data\":[48.756080,2.302038]}"));
  23. // You can use a Flash String to get an element of a JsonObject
  24. // No duplication is done.
  25. long time = root[F("time")];
  26. // You can use a Flash String to set an element of a JsonObject
  27. // WARNING: the content of the Flash String will be duplicated in the
  28. // JsonBuffer.
  29. root[F("time")] = time;
  30. // You can set a Flash String to a JsonObject or JsonArray:
  31. // WARNING: the content of the Flash String will be duplicated in the
  32. // JsonBuffer.
  33. root["sensor"] = F("gps");
  34. // You can compare the content of a JsonVariant to a Flash String
  35. if (root["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. }