ProgmemExample.ino 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. // ArduinoJson - arduinojson.org
  2. // Copyright Benoit Blanchon 2014-2017
  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. DynamicJsonBuffer jsonBuffer;
  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. JsonObject& root =
  19. jsonBuffer.parseObject(F("{\"sensor\":\"gps\",\"time\":1351824120,"
  20. "\"data\":[48.756080,2.302038]}"));
  21. // You can use a Flash String to get an element of a JsonObject
  22. // No duplication is done.
  23. long time = root[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. root[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. root["sensor"] = F("gps");
  32. // You can compare the content of a JsonVariant to a Flash String
  33. if (root["sensor"] == F("gps")) {
  34. // ...
  35. }
  36. #else
  37. #warning PROGMEM is not supported on this platform
  38. #endif
  39. }
  40. void loop() {
  41. // not used in this example
  42. }
  43. // See also
  44. // --------
  45. //
  46. // The website arduinojson.org contains the documentation for all the functions
  47. // used above. It also includes an FAQ that will help you solve any memory
  48. // problem.
  49. // Please check it out at: https://arduinojson.org/
  50. //
  51. // The book "Mastering ArduinoJson" contains a quick C++ course that explains
  52. // how your microcontroller stores strings in memory. It also tells why you
  53. // should not abuse Flash strings with ArduinoJson.
  54. // Please check it out at: https://leanpub.com/arduinojson/