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