ProgmemExample.ino 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. // ArduinoJson - arduinojson.org
  2. // Copyright Benoit Blanchon 2014-2019
  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. // It works with RawJson too:
  33. root["sensor"] = RawJson(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. }
  45. // See also
  46. // --------
  47. //
  48. // https://arduinojson.org/ contains the documentation for all the functions
  49. // used above. It also includes an FAQ that will help you solve any memory
  50. // problem.
  51. //
  52. // The book "Mastering ArduinoJson" contains a quick C++ course that explains
  53. // how your microcontroller stores strings in memory. It also tells why you
  54. // should not abuse Flash strings with ArduinoJson.
  55. // Learn more at https://arduinojson.org/book/
  56. // Use the coupon code TWENTY for a 20% discount ❤❤❤❤❤