2017-11-07 20:42:50 +01:00
|
|
|
// ArduinoJson - arduinojson.org
|
2019-02-15 13:31:46 +01:00
|
|
|
// Copyright Benoit Blanchon 2014-2019
|
2016-11-13 20:16:12 +01:00
|
|
|
// MIT License
|
2017-12-11 15:19:28 +01:00
|
|
|
//
|
|
|
|
// This example shows the different ways you can use Flash strings with
|
|
|
|
// ArduinoJson.
|
|
|
|
//
|
|
|
|
// Use Flash strings sparingly, because ArduinoJson duplicates them in the
|
2019-01-29 14:09:09 +01:00
|
|
|
// JsonDocument. Prefer plain old char*, as they are more efficient in term of
|
2017-12-11 15:19:28 +01:00
|
|
|
// code size, speed, and memory usage.
|
2019-03-04 12:17:41 +01:00
|
|
|
//
|
|
|
|
// https://arduinojson.org/v6/example/progmem/
|
2016-11-13 20:16:12 +01:00
|
|
|
|
|
|
|
#include <ArduinoJson.h>
|
|
|
|
|
|
|
|
void setup() {
|
2017-12-11 15:19:28 +01:00
|
|
|
#ifdef PROGMEM // <- check that Flash strings are supported
|
|
|
|
|
2019-01-14 10:32:19 +01:00
|
|
|
DynamicJsonDocument doc(1024);
|
2016-11-13 20:16:12 +01:00
|
|
|
|
|
|
|
// You can use a Flash String as your JSON input.
|
2019-02-01 11:28:27 +01:00
|
|
|
// WARNING: the strings in the input will be duplicated in the JsonDocument.
|
2018-04-17 21:27:45 +02:00
|
|
|
deserializeJson(doc, F("{\"sensor\":\"gps\",\"time\":1351824120,"
|
|
|
|
"\"data\":[48.756080,2.302038]}"));
|
2018-07-02 09:35:21 +02:00
|
|
|
JsonObject obj = doc.as<JsonObject>();
|
2016-11-13 20:16:12 +01:00
|
|
|
|
|
|
|
// You can use a Flash String to get an element of a JsonObject
|
|
|
|
// No duplication is done.
|
2018-04-17 21:27:45 +02:00
|
|
|
long time = obj[F("time")];
|
2016-11-13 20:16:12 +01:00
|
|
|
|
|
|
|
// You can use a Flash String to set an element of a JsonObject
|
|
|
|
// WARNING: the content of the Flash String will be duplicated in the
|
2019-01-29 14:09:09 +01:00
|
|
|
// JsonDocument.
|
2018-04-17 21:27:45 +02:00
|
|
|
obj[F("time")] = time;
|
2016-11-13 20:16:12 +01:00
|
|
|
|
|
|
|
// You can set a Flash String to a JsonObject or JsonArray:
|
|
|
|
// WARNING: the content of the Flash String will be duplicated in the
|
2019-01-29 14:09:09 +01:00
|
|
|
// JsonDocument.
|
2018-04-17 21:27:45 +02:00
|
|
|
obj["sensor"] = F("gps");
|
2016-11-13 20:16:12 +01:00
|
|
|
|
2018-07-12 09:08:20 +02:00
|
|
|
// It works with serialized() too:
|
|
|
|
obj["sensor"] = serialized(F("\"gps\""));
|
|
|
|
obj["sensor"] = serialized(F("\xA3gps"), 3);
|
2018-01-18 09:43:37 +01:00
|
|
|
|
2016-12-23 14:45:32 +01:00
|
|
|
// You can compare the content of a JsonVariant to a Flash String
|
2018-04-17 21:27:45 +02:00
|
|
|
if (obj["sensor"] == F("gps")) {
|
2016-12-23 14:45:32 +01:00
|
|
|
// ...
|
|
|
|
}
|
|
|
|
|
2016-11-13 20:16:12 +01:00
|
|
|
#else
|
|
|
|
|
2016-12-23 14:45:32 +01:00
|
|
|
#warning PROGMEM is not supported on this platform
|
2016-11-13 20:16:12 +01:00
|
|
|
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
void loop() {
|
|
|
|
// not used in this example
|
|
|
|
}
|
2017-12-11 15:19:28 +01:00
|
|
|
|
2019-03-04 12:17:41 +01:00
|
|
|
// See also
|
|
|
|
// --------
|
|
|
|
//
|
|
|
|
// https://arduinojson.org/ contains the documentation for all the functions
|
|
|
|
// used above. It also includes an FAQ that will help you solve any memory
|
|
|
|
// problem.
|
|
|
|
//
|
|
|
|
// The book "Mastering ArduinoJson" contains a quick C++ course that explains
|
|
|
|
// how your microcontroller stores strings in memory. It also tells why you
|
|
|
|
// should not abuse Flash strings with ArduinoJson.
|
|
|
|
// Learn more at https://arduinojson.org/book/
|
|
|
|
// Use the coupon code TWENTY for a 20% discount ❤❤❤❤❤
|