ProgmemExample.ino 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. // ArduinoJson - https://arduinojson.org
  2. // Copyright © 2014-2022, Benoit BLANCHON
  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. // JsonDocument. Prefer plain old char*, as they are more efficient in term of
  10. // code size, speed, and memory usage.
  11. //
  12. // https://arduinojson.org/v6/example/progmem/
  13. #include <ArduinoJson.h>
  14. void setup() {
  15. DynamicJsonDocument doc(1024);
  16. // You can use a Flash String as your JSON input.
  17. // WARNING: the strings in the input will be duplicated in the JsonDocument.
  18. deserializeJson(doc, F("{\"sensor\":\"gps\",\"time\":1351824120,"
  19. "\"data\":[48.756080,2.302038]}"));
  20. // You can use a Flash String as a key to get a member from JsonDocument
  21. // No duplication is done.
  22. long time = doc[F("time")];
  23. // You can use a Flash String as a key to set a member of a JsonDocument
  24. // WARNING: the content of the Flash String will be duplicated in the
  25. // JsonDocument.
  26. doc[F("time")] = time;
  27. // You can set a Flash String as the content of a JsonVariant
  28. // WARNING: the content of the Flash String will be duplicated in the
  29. // JsonDocument.
  30. doc["sensor"] = F("gps");
  31. // It works with serialized() too:
  32. doc["sensor"] = serialized(F("\"gps\""));
  33. doc["sensor"] = serialized(F("\xA3gps"), 3);
  34. // You can compare the content of a JsonVariant to a Flash String
  35. if (doc["sensor"] == F("gps")) {
  36. // ...
  37. }
  38. }
  39. void loop() {
  40. // not used in this example
  41. }
  42. // See also
  43. // --------
  44. //
  45. // https://arduinojson.org/ contains the documentation for all the functions
  46. // used above. It also includes an FAQ that will help you solve any memory
  47. // problem.
  48. //
  49. // The book "Mastering ArduinoJson" contains a quick C++ course that explains
  50. // how your microcontroller stores strings in memory. It also tells why you
  51. // should not abuse Flash strings with ArduinoJson.
  52. // Learn more at https://arduinojson.org/book/
  53. // Use the coupon code TWENTY for a 20% discount ❤❤❤❤❤