ProgmemExample.ino 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. // Copyright Benoit Blanchon 2014-2016
  2. // MIT License
  3. //
  4. // Arduino JSON library
  5. // https://github.com/bblanchon/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. #else
  35. #warning PROGMEM is only supported on AVR architecture
  36. #endif
  37. }
  38. void loop() {
  39. // not used in this example
  40. }