diff --git a/ESPWebThingAdapter.h b/ESPWebThingAdapter.h index 3ba034a..61cecb3 100644 --- a/ESPWebThingAdapter.h +++ b/ESPWebThingAdapter.h @@ -85,10 +85,9 @@ class WebThingAdapter { #ifndef WITHOUT_WS void sendChangedPropsOrEvents(ThingDevice* device, const char* type, ThingItem* rootItem) { // Prepare one buffer per device - DynamicJsonBuffer buf(1024); - JsonObject& message = buf.createObject(); + DynamicJsonDocument message(1024); message["messageType"] = type; - JsonObject& prop = message.createNestedObject("data"); + JsonObject prop = message.createNestedObject("data"); bool dataToSend = false; ThingItem* item = rootItem; while (item != nullptr) { @@ -101,7 +100,7 @@ class WebThingAdapter { } if (dataToSend) { String jsonStr; - message.printTo(jsonStr); + serializeJson(message, jsonStr); // Inform all connected ws clients of a Thing about changed properties ((AsyncWebSocket*)device->ws)->textAll(jsonStr.c_str(), jsonStr.length()); } @@ -173,12 +172,11 @@ class WebThingAdapter { } #ifndef WITHOUT_WS - void sendErrorMsg(DynamicJsonBuffer &buffer, AsyncWebSocketClient& client, int status, const char* msg) { - JsonObject& prop = buffer.createObject(); + void sendErrorMsg(DynamicJsonDocument &prop, AsyncWebSocketClient& client, int status, const char* msg) { prop["error"] = msg; prop["status"] = status; String jsonStr; - prop.printTo(jsonStr); + serializeJson(prop, jsonStr); client.text(jsonStr.c_str(), jsonStr.length()); } @@ -198,25 +196,25 @@ class WebThingAdapter { // spec. For now each Thing stores its own Websocket connection object therefore. // Parse request - DynamicJsonBuffer newBuffer(1024); - JsonObject& newProp = newBuffer.parseObject(rawData); - if (!newProp.success()) { - sendErrorMsg(newBuffer, *client, 400, "Invalid json"); + DynamicJsonDocument newProp(1024); + auto error = deserializeJson(newProp, rawData); + if (error) { + sendErrorMsg(newProp, *client, 400, "Invalid json"); return; } String messageType = newProp["messageType"].as(); - const JsonVariant& dataVariant = newProp["data"]; + JsonVariant dataVariant = newProp["data"]; if (!dataVariant.is()) { - sendErrorMsg(newBuffer, *client, 400, "data must be an object"); + sendErrorMsg(newProp, *client, 400, "data must be an object"); return; } - const JsonObject &data = dataVariant.as(); + JsonObject data = dataVariant.as(); if (messageType == "setProperty") { for (auto kv : data) { - ThingProperty* property = device->findProperty(kv.key); + ThingProperty* property = device->findProperty(kv.key().c_str()); if (property) { setThingProperty(data, property); } @@ -224,10 +222,10 @@ class WebThingAdapter { // Send confirmation by sending back the received property object String jsonStr; - data.printTo(jsonStr); + serializeJson(data, jsonStr); client->text(jsonStr.c_str(), jsonStr.length()); } else if (messageType == "requestAction") { - sendErrorMsg(newBuffer, *client, 400, "Not supported yet"); + sendErrorMsg(newProp, *client, 400, "Not supported yet"); } else if (messageType == "addEventSubscription") { // We report back all property state changes. We'd require a map // of subscribed properties per websocket connection otherwise @@ -248,26 +246,26 @@ class WebThingAdapter { } AsyncResponseStream *response = request->beginResponseStream("application/json"); - DynamicJsonBuffer buf(1024); - JsonArray& things = buf.createArray(); + DynamicJsonDocument buf(1024); + JsonArray things = buf.to(); ThingDevice* device = this->firstDevice; while (device != nullptr) { - JsonObject& descr = things.createNestedObject(); + JsonObject descr = things.createNestedObject(); this->serializeDevice(descr, device); descr["href"] = "/things/" + device->id; device = device->next; } - things.printTo(*response); + serializeJson(things, *response); request->send(response); } - void serializePropertyOrEvent(JsonObject& descr, ThingDevice* device, const char* type, bool isProp, ThingItem* item) { + void serializePropertyOrEvent(JsonObject descr, ThingDevice* device, const char* type, bool isProp, ThingItem* item) { String basePath = "/things/" + device->id + "/"+ type + "/"; - JsonObject& props = descr.createNestedObject(type); + JsonObject props = descr.createNestedObject(type); while (item != nullptr) { - JsonObject& prop = props.createNestedObject(item->id); + JsonObject prop = props.createNestedObject(item->id); switch (item->type) { case NO_STATE: break; @@ -313,7 +311,7 @@ class WebThingAdapter { if (hasEnum) { enumVal = property->propertyEnum; - JsonArray &propEnum = prop.createNestedArray("enum"); + JsonArray propEnum = prop.createNestedArray("enum"); while (property->propertyEnum != nullptr && (*enumVal) != nullptr){ propEnum.add(*enumVal); enumVal++; @@ -326,47 +324,47 @@ class WebThingAdapter { } // 2.9 Property object: A links array (An array of Link objects linking to one or more representations of a Property resource, each with an implied default rel=property.) - JsonArray& inline_links = prop.createNestedArray("links"); - JsonObject& inline_links_prop = inline_links.createNestedObject(); + JsonArray inline_links = prop.createNestedArray("links"); + JsonObject inline_links_prop = inline_links.createNestedObject(); inline_links_prop["href"] = basePath + item->id; item = item->next; } } - void serializeDevice(JsonObject& descr, ThingDevice* device) { + void serializeDevice(JsonObject descr, ThingDevice* device) { descr["id"] = device->id; descr["title"] = device->title; descr["@context"] = "https://iot.mozilla.org/schemas"; // TODO: descr["base"] = ??? - JsonObject& securityDefinitions = descr.createNestedObject("securityDefinitions"); - JsonObject& nosecSc = securityDefinitions.createNestedObject("nosec_sc"); + JsonObject securityDefinitions = descr.createNestedObject("securityDefinitions"); + JsonObject nosecSc = securityDefinitions.createNestedObject("nosec_sc"); nosecSc["scheme"] = "nosec"; - JsonArray& typeJson = descr.createNestedArray("@type"); + JsonArray typeJson = descr.createNestedArray("@type"); const char** type = device->type; while ((*type) != nullptr) { typeJson.add(*type); type++; } - JsonArray& links = descr.createNestedArray("links"); + JsonArray links = descr.createNestedArray("links"); { - JsonObject& links_prop = links.createNestedObject(); + JsonObject links_prop = links.createNestedObject(); links_prop["rel"] = "properties"; links_prop["href"] = "/things/" + device->id + "/properties"; } { - JsonObject& links_prop = links.createNestedObject(); + JsonObject links_prop = links.createNestedObject(); links_prop["rel"] = "events"; links_prop["href"] = "/things/" + device->id + "/events"; } #ifndef WITHOUT_WS { - JsonObject& links_prop = links.createNestedObject(); + JsonObject links_prop = links.createNestedObject(); links_prop["rel"] = "alternate"; char buffer [33]; itoa(port, buffer, 10); @@ -391,15 +389,15 @@ class WebThingAdapter { } AsyncResponseStream *response = request->beginResponseStream("application/json"); - DynamicJsonBuffer buf(1024); - JsonObject& descr = buf.createObject(); + DynamicJsonDocument buf(1024); + JsonObject descr = buf.to(); this->serializeDevice(descr, device); - descr.printTo(*response); + serializeJson(descr, *response); request->send(response); } - void serializeThingItem(ThingItem* item, JsonObject& prop) { + void serializeThingItem(ThingItem* item, JsonObject prop) { switch (item->type) { case NO_STATE: break; @@ -421,24 +419,24 @@ class WebThingAdapter { } AsyncResponseStream *response = request->beginResponseStream("application/json"); - DynamicJsonBuffer buf(256); - JsonObject& prop = buf.createObject(); + DynamicJsonDocument doc(256); + JsonObject prop = doc.to(); serializeThingItem(item, prop); - prop.printTo(*response); + serializeJson(prop, *response); request->send(response); } void handleThingGetAll(AsyncWebServerRequest *request, ThingDevice* device, ThingItem* rootItem) { AsyncResponseStream *response = request->beginResponseStream("application/json"); - DynamicJsonBuffer buf(256); - JsonObject& prop = buf.createObject(); + DynamicJsonDocument doc(256); + JsonObject prop = doc.to(); ThingItem *item = rootItem; while (item != nullptr) { serializeThingItem(item, prop); item = item->next; } - prop.printTo(*response); + serializeJson(prop, *response); request->send(response); } @@ -451,7 +449,7 @@ class WebThingAdapter { b_has_body_data = true; } - void setThingProperty(const JsonObject& newProp, ThingProperty* property) { + void setThingProperty(const JsonObject newProp, ThingProperty* property) { const JsonVariant newValue = newProp[property->id]; switch (property->type) { @@ -485,19 +483,20 @@ class WebThingAdapter { return; } - DynamicJsonBuffer newBuffer(256); - JsonObject& newProp = newBuffer.parseObject(body_data); - if (!newProp.success()) { // unable to parse json + DynamicJsonDocument newBuffer(256); + auto error = deserializeJson(newBuffer, body_data); + if (error) { // unable to parse json b_has_body_data = false; memset(body_data, 0, sizeof(body_data)); request->send(500); return; } + JsonObject newProp = newBuffer.to(); setThingProperty(newProp, property); AsyncResponseStream *response = request->beginResponseStream("application/json"); - newProp.printTo(*response); + serializeJson(newProp, *response); request->send(response); b_has_body_data = false; diff --git a/EthernetWebThingAdapter.h b/EthernetWebThingAdapter.h index 700d570..2d975f0 100644 --- a/EthernetWebThingAdapter.h +++ b/EthernetWebThingAdapter.h @@ -332,17 +332,17 @@ class WebThingAdapter { sendOk(); sendHeaders(); - DynamicJsonBuffer buf; - JsonArray& things = buf.createArray(); + DynamicJsonDocument buf(256); + JsonArray things = buf.to(); ThingDevice* device = firstDevice; while (device != nullptr) { - JsonObject& descr = things.createNestedObject(); + JsonObject descr = things.createNestedObject(); serializeDevice(descr, device); descr["href"] = "/things/" + device->id; device = device->next; } - things.printTo(client); + serializeJson(things, client); delay(1); client.stop(); } @@ -352,11 +352,11 @@ class WebThingAdapter { sendOk(); sendHeaders(); - DynamicJsonBuffer buf; - JsonObject& descr = buf.createObject(); + DynamicJsonDocument buf(256); + JsonObject descr = buf.to(); serializeDevice(descr, device); - descr.printTo(client); + serializeJson(descr, client); delay(1); client.stop(); } @@ -365,8 +365,7 @@ class WebThingAdapter { sendOk(); sendHeaders(); - DynamicJsonBuffer buf; - JsonObject& prop = buf.createObject(); + DynamicJsonDocument prop(256); switch (property->type) { case BOOLEAN: prop[property->id] = property->getValue().boolean; @@ -378,7 +377,7 @@ class WebThingAdapter { prop[property->id] = *property->getValue().string; break; } - prop.printTo(client); + serializeJson(prop, client); delay(1); client.stop(); } @@ -386,8 +385,9 @@ class WebThingAdapter { void handlePropertyPut(ThingProperty* property) { sendOk(); sendHeaders(); - DynamicJsonBuffer newBuffer; - JsonObject& newProp = newBuffer.parseObject(content); + DynamicJsonDocument newBuffer(256); + deserializeJson(newBuffer, content); + JsonObject newProp = newBuffer.to(); JsonVariant newValue = newProp[property->id]; switch (property->type) { @@ -431,28 +431,28 @@ class WebThingAdapter { retries = 0; } - void serializeDevice(JsonObject& descr, ThingDevice* device) { + void serializeDevice(JsonObject descr, ThingDevice* device) { descr["id"] = device->id; descr["title"] = device->title; descr["@context"] = "https://iot.mozilla.org/schemas"; // TODO: descr["base"] = ??? - JsonObject& securityDefinitions = descr.createNestedObject("securityDefinitions"); - JsonObject& nosecSc = securityDefinitions.createNestedObject("nosec_sc"); + JsonObject securityDefinitions = descr.createNestedObject("securityDefinitions"); + JsonObject nosecSc = securityDefinitions.createNestedObject("nosec_sc"); nosecSc["scheme"] = "nosec"; - JsonArray& typeJson = descr.createNestedArray("@type"); + JsonArray typeJson = descr.createNestedArray("@type"); const char** type = device->type; while ((*type) != nullptr) { typeJson.add(*type); type++; } - JsonObject& props = descr.createNestedObject("properties"); + JsonObject props = descr.createNestedObject("properties"); ThingProperty* property = device->firstProperty; while (property != nullptr) { - JsonObject& prop = props.createNestedObject(property->id); + JsonObject prop = props.createNestedObject(property->id); switch (property->type) { case BOOLEAN: prop["type"] = "boolean"; @@ -498,7 +498,7 @@ class WebThingAdapter { if (hasEnum) { enumVal = property->propertyEnum; - JsonArray &propEnum = prop.createNestedArray("enum"); + JsonArray propEnum = prop.createNestedArray("enum"); while (property->propertyEnum != nullptr && (*enumVal) != nullptr){ propEnum.add(*enumVal); enumVal++; diff --git a/Makefile b/Makefile index dfd4e51..e909e29 100644 --- a/Makefile +++ b/Makefile @@ -44,7 +44,7 @@ USER_LIB_PATH?=${extra_dir}/Arduino/libraries ArduinoJson_url?=https://github.com/bblanchon/ArduinoJson ArduinoJson_dir?=${extra_dir}/Arduino/libraries/ArduinoJson -ArduinoJson_version?=v5.13.4 +ArduinoJson_version?=v6.13.0 arduino_lib_dirs+=${ArduinoJson_dir} ${ArduinoJson_dir}: diff --git a/README.md b/README.md index ffd9637..e75dab2 100644 --- a/README.md +++ b/README.md @@ -29,9 +29,8 @@ it in your sketchbook's libraries folder. ### Continuing onwards -Make sure to install the ArduinoJson library if you don't have it installed -already. Note that you must select ArduinoJson version 5, not the prerelease -beta version 6. +Make sure to install the current release of the ArduinoJson library (6) if you +don't have it installed already. ![ArduinoJson install process](https://github.com/mozilla-iot/webthing-arduino/raw/master/docs/arduinojson.png) diff --git a/WiFi101WebThingAdapter.h b/WiFi101WebThingAdapter.h index 040ffc7..4d8fcf0 100644 --- a/WiFi101WebThingAdapter.h +++ b/WiFi101WebThingAdapter.h @@ -309,17 +309,17 @@ class WebThingAdapter { sendOk(); sendHeaders(); - DynamicJsonBuffer buf; - JsonArray& things = buf.createArray(); + DynamicJsonDocument buf(256); + JsonArray things = buf.to(); ThingDevice* device = firstDevice; while (device != nullptr) { - JsonObject& descr = things.createNestedObject(); + JsonObject descr = things.createNestedObject(); serializeDevice(descr, device); descr["href"] = "/things/" + device->id; device = device->next; } - things.printTo(client); + serializeJson(things, client); delay(1); client.stop(); } @@ -329,11 +329,11 @@ class WebThingAdapter { sendOk(); sendHeaders(); - DynamicJsonBuffer buf; - JsonObject& descr = buf.createObject(); + DynamicJsonDocument buf(256); + JsonObject descr = buf.to(); serializeDevice(descr, device); - descr.printTo(client); + serializeJson(descr, client); delay(1); client.stop(); } @@ -342,8 +342,7 @@ class WebThingAdapter { sendOk(); sendHeaders(); - DynamicJsonBuffer buf; - JsonObject& prop = buf.createObject(); + DynamicJsonDocument prop(256); switch (property->type) { case NO_STATE: break; @@ -357,7 +356,7 @@ class WebThingAdapter { prop[property->id] = *property->getValue().string; break; } - prop.printTo(client); + serializeJson(prop, client); delay(1); client.stop(); } @@ -365,8 +364,10 @@ class WebThingAdapter { void handlePropertyPut(ThingProperty* property) { sendOk(); sendHeaders(); - DynamicJsonBuffer newBuffer; - JsonObject& newProp = newBuffer.parseObject(content); + DynamicJsonDocument newBuffer(256); + deserializeJson(newBuffer, content); + JsonObject newProp = newBuffer.to(); + JsonVariant newValue = newProp[property->id]; switch (property->type) { @@ -412,28 +413,28 @@ class WebThingAdapter { retries = 0; } - void serializeDevice(JsonObject& descr, ThingDevice* device) { + void serializeDevice(JsonObject descr, ThingDevice* device) { descr["id"] = device->id; descr["title"] = device->title; descr["@context"] = "https://iot.mozilla.org/schemas"; // TODO: descr["base"] = ??? - JsonObject& securityDefinitions = descr.createNestedObject("securityDefinitions"); - JsonObject& nosecSc = securityDefinitions.createNestedObject("nosec_sc"); + JsonObject securityDefinitions = descr.createNestedObject("securityDefinitions"); + JsonObject nosecSc = securityDefinitions.createNestedObject("nosec_sc"); nosecSc["scheme"] = "nosec"; - JsonArray& typeJson = descr.createNestedArray("@type"); + JsonArray typeJson = descr.createNestedArray("@type"); const char** type = device->type; while ((*type) != nullptr) { typeJson.add(*type); type++; } - JsonObject& props = descr.createNestedObject("properties"); + JsonObject props = descr.createNestedObject("properties"); ThingProperty* property = device->firstProperty; while (property != nullptr) { - JsonObject& prop = props.createNestedObject(property->id); + JsonObject prop = props.createNestedObject(property->id); switch (property->type) { case NO_STATE: break; @@ -477,7 +478,7 @@ class WebThingAdapter { if (hasEnum) { enumVal = property->propertyEnum; - JsonArray &propEnum = prop.createNestedArray("enum"); + JsonArray propEnum = prop.createNestedArray("enum"); while (property->propertyEnum != nullptr && (*enumVal) != nullptr){ propEnum.add(*enumVal); enumVal++; diff --git a/examples/PlatformIO/BME280/platformio.ini b/examples/PlatformIO/BME280/platformio.ini index fee42b8..8f861ca 100755 --- a/examples/PlatformIO/BME280/platformio.ini +++ b/examples/PlatformIO/BME280/platformio.ini @@ -19,7 +19,7 @@ env_default = samw25 ; https://platformio.org/lib/show/2848/ArduinoMDNS lib_deps = https://github.com/mozilla-iot/webthing-arduino.git - ArduinoJson@5.13.4 + ArduinoJson@6.13.0 WiFi101 ArduinoMDNS BME280 diff --git a/examples/PlatformIO/BME280/src/BME280.cpp b/examples/PlatformIO/BME280/src/BME280.cpp index ac23cb5..72690f8 100755 --- a/examples/PlatformIO/BME280/src/BME280.cpp +++ b/examples/PlatformIO/BME280/src/BME280.cpp @@ -59,7 +59,7 @@ BME280I2C::Settings settings( BME280::StandbyTime_1000ms, BME280::Filter_Off, BME280::SpiEnable_False, - (BME280I2C::I2CAddr) 0x76 // I2C address. I2C specific. + 0x76 // I2C address. I2C specific. ); BME280I2C bme(settings); diff --git a/examples/PlatformIO/LED/platformio.ini b/examples/PlatformIO/LED/platformio.ini index faf88f6..6c0a772 100644 --- a/examples/PlatformIO/LED/platformio.ini +++ b/examples/PlatformIO/LED/platformio.ini @@ -18,7 +18,7 @@ [global] lib_deps = https://github.com/mozilla-iot/webthing-arduino.git - ArduinoJson@5.13.4 + ArduinoJson@6.13.0 monitor_speed = 115200 [env:d1] diff --git a/examples/PlatformIO/TextDisplay/platformio.ini b/examples/PlatformIO/TextDisplay/platformio.ini index 6aa4f24..de5c682 100644 --- a/examples/PlatformIO/TextDisplay/platformio.ini +++ b/examples/PlatformIO/TextDisplay/platformio.ini @@ -17,7 +17,7 @@ [global] lib_deps = https://github.com/mozilla-iot/webthing-arduino.git - ArduinoJson@5.13.4 + ArduinoJson@6.13.0 Adafruit GFX Library Adafruit SSD1306 diff --git a/library.json b/library.json index 0cd8d9c..ec4047f 100644 --- a/library.json +++ b/library.json @@ -14,7 +14,7 @@ "license": "Mozilla Public License Version 2.0", "platforms": "espressif8266,espressif32,atmelavr", "dependencies": { - "ArduinoJson": "5.13" + "ArduinoJson": "6.13.0" }, "export": { "include": @@ -26,4 +26,4 @@ "examples": [ "examples/PlatformIO/*/src/*.cpp" ] -} \ No newline at end of file +}