diff --git a/pio_examples/gui/platformio.ini b/pio_examples/gui/platformio.ini index ae409ea..d9afbbc 100644 --- a/pio_examples/gui/platformio.ini +++ b/pio_examples/gui/platformio.ini @@ -17,7 +17,8 @@ framework = arduino board_build.filesystem = littlefs lib_extra_dirs = ../../ lib_deps = - bblanchon/ArduinoJson @ ^6.18.5 +; bblanchon/ArduinoJson @ ^6.18.5 + bblanchon/ArduinoJson @ ^7.0.4 https://github.com/bmedici/ESPAsyncWebServer ; Use a fork of the library that has a bugfix for the compile.... https://github.com/esphome/ESPAsyncWebServer/pull/17 lib_ignore = @@ -43,7 +44,9 @@ monitor_filters = esp32_exception_decoder board_build.flash_mode = dout build_flags = -D TEST_HUGE_TEXT + -D TEST_FILEDISPLAY ; -D DEBUG_ESPUI + lib_deps = ${env.lib_deps} me-no-dev/AsyncTCP diff --git a/src/ESPUI.cpp b/src/ESPUI.cpp index 34bff21..92e657f 100644 --- a/src/ESPUI.cpp +++ b/src/ESPUI.cpp @@ -927,7 +927,7 @@ void ESPUIClass::clearGraph(uint16_t id, int clientId) break; } - DynamicJsonDocument document(jsonUpdateDocumentSize); + AllocateJsonDocument(document, jsonUpdateDocumentSize); JsonObject root = document.to(); root[F("type")] = (int)ControlType::Graph + UpdateOffset; @@ -949,7 +949,7 @@ void ESPUIClass::addGraphPoint(uint16_t id, int nValue, int clientId) break; } - DynamicJsonDocument document(jsonUpdateDocumentSize); + AllocateJsonDocument(document, jsonUpdateDocumentSize); JsonObject root = document.to(); root[F("type")] = (int)ControlType::GraphPoint; @@ -961,7 +961,7 @@ void ESPUIClass::addGraphPoint(uint16_t id, int nValue, int clientId) } while (false); } -bool ESPUIClass::SendJsonDocToWebSocket(ArduinoJson::DynamicJsonDocument& document, uint16_t clientId) +bool ESPUIClass::SendJsonDocToWebSocket(ArduinoJson::JsonDocument& document, uint16_t clientId) { bool Response = false; diff --git a/src/ESPUI.h b/src/ESPUI.h index d02659a..0e70a70 100644 --- a/src/ESPUI.h +++ b/src/ESPUI.h @@ -5,7 +5,20 @@ #define WS_AUTHENTICATION false #include + #include +#if ARDUINOJSON_VERSION_MAJOR > 6 + #define AllocateJsonDocument(name, size) JsonDocument name + #define AllocateJsonArray(doc, name) doc[name].to() + #define AllocateJsonObject(doc) doc.add() + #define AllocateNamedJsonObject(t, s, n) t[n] = s +#else + #define AllocateJsonDocument(name, size) DynamicJsonDocument name(size) + #define AllocateJsonArray(doc, name) doc.createNestedArray(name) + #define AllocateJsonObject(doc) doc.createNestedObject() + #define AllocateNamedJsonObject(t, s, n) t = s.createNestedObject(n) +#endif + #include #ifdef ESP32 #if (ESP_IDF_VERSION_MAJOR == 4 && ESP_IDF_VERSION_MINOR >= 4) || ESP_IDF_VERSION_MAJOR > 4 @@ -271,7 +284,7 @@ protected: void NotifyClients(ClientUpdateType_t newState); void NotifyClient(uint32_t WsClientId, ClientUpdateType_t newState); - bool SendJsonDocToWebSocket(ArduinoJson::DynamicJsonDocument& document, uint16_t clientId); + bool SendJsonDocToWebSocket(ArduinoJson::JsonDocument& document, uint16_t clientId); std::map MapOfClients; diff --git a/src/ESPUIclient.cpp b/src/ESPUIclient.cpp index df4c077..0becacf 100644 --- a/src/ESPUIclient.cpp +++ b/src/ESPUIclient.cpp @@ -74,14 +74,14 @@ bool ESPUIclient::CanSend() return Response; } -void ESPUIclient::FillInHeader(DynamicJsonDocument& document) +void ESPUIclient::FillInHeader(JsonDocument& document) { document[F("type")] = UI_EXTEND_GUI; document[F("sliderContinuous")] = ESPUI.sliderContinuous; document[F("startindex")] = 0; document[F("totalcontrols")] = ESPUI.controlCount; - JsonArray items = document.createNestedArray(F("controls")); - JsonObject titleItem = items.createNestedObject(); + JsonArray items = AllocateJsonArray(document, F("controls")); + JsonObject titleItem = AllocateJsonObject(items); titleItem[F("type")] = (int)UI_TITLE; titleItem[F("label")] = ESPUI.ui_title; } @@ -104,7 +104,7 @@ bool ESPUIclient::SendClientNotification(ClientUpdateType_t value) break; } - DynamicJsonDocument document(ESPUI.jsonUpdateDocumentSize); + AllocateJsonDocument(document, ESPUI.jsonUpdateDocumentSize); FillInHeader(document); if(ClientUpdateType_t::ReloadNeeded == value) { @@ -259,7 +259,7 @@ number this will represent the entire UI. More likely, it will represent a small client will acknowledge receipt by requesting the next chunk. */ uint32_t ESPUIclient::prepareJSONChunk(uint16_t startindex, - DynamicJsonDocument & rootDoc, + JsonDocument & rootDoc, bool InUpdateMode, String FragmentRequestString) { @@ -293,12 +293,15 @@ uint32_t ESPUIclient::prepareJSONChunk(uint16_t startindex, // this is actually a fragment or directed update request // parse the string we got from the UI and try to update that specific // control. - DynamicJsonDocument FragmentRequest(FragmentRequestString.length() * 3); + AllocateJsonDocument(FragmentRequest, FragmentRequestString.length() * 3); +/* + ArduinoJson::detail::sizeofObject(N); if(0 >= FragmentRequest.capacity()) { Serial.println(F("ERROR:prepareJSONChunk:Fragmentation:Could not allocate memory for a fragmentation request. Skipping Response")); break; } +*/ size_t FragmentRequestStartOffset = FragmentRequestString.indexOf("{"); DeserializationError error = deserializeJson(FragmentRequest, FragmentRequestString.substring(FragmentRequestStartOffset)); if(DeserializationError::Ok != error) @@ -393,7 +396,7 @@ uint32_t ESPUIclient::prepareJSONChunk(uint16_t startindex, // Serial.println(String(F("prepareJSONChunk: MaxMarshaledJsonSize: ")) + String(MaxMarshaledJsonSize)); // Serial.println(String(F("prepareJSONChunk: Cur EstimatedUsedMarshaledJsonSize: ")) + String(EstimatedUsedMarshaledJsonSize)); - JsonObject item = items.createNestedObject(); + JsonObject item = AllocateJsonObject(items); elementcount++; uint32_t RemainingSpace = (MaxMarshaledJsonSize - EstimatedUsedMarshaledJsonSize) - 100; // Serial.println(String(F("prepareJSONChunk: RemainingSpace: ")) + String(RemainingSpace)); @@ -418,7 +421,7 @@ uint32_t ESPUIclient::prepareJSONChunk(uint16_t startindex, // Serial.println(String(F("prepareJSONChunk: Control ")) + String(control->id) + F(" is too large to be sent to the browser.")); // Serial.println(String(F("ERROR: prepareJSONChunk: value: ")) + control->value); rootDoc.clear(); - item = items.createNestedObject(); + item = AllocateJsonObject(items); control->MarshalErrorMessage(item); elementcount = 0; } @@ -496,7 +499,7 @@ bool ESPUIclient::SendControlsToClient(uint16_t startidx, ClientUpdateType_t Tra break; } - DynamicJsonDocument document(ESPUI.jsonInitialDocumentSize); + AllocateJsonDocument(document, ESPUI.jsonInitialDocumentSize); FillInHeader(document); document[F("startindex")] = startidx; document[F("totalcontrols")] = uint16_t(-1); // ESPUI.controlCount; @@ -544,7 +547,7 @@ bool ESPUIclient::SendControlsToClient(uint16_t startidx, ClientUpdateType_t Tra return Response; } -bool ESPUIclient::SendJsonDocToWebSocket(DynamicJsonDocument& document) +bool ESPUIclient::SendJsonDocToWebSocket(JsonDocument& document) { bool Response = true; diff --git a/src/ESPUIclient.h b/src/ESPUIclient.h index 444725b..b013f5e 100644 --- a/src/ESPUIclient.h +++ b/src/ESPUIclient.h @@ -43,8 +43,8 @@ protected: // bool NeedsNotification() { return pCurrentFsmState != &fsm_EspuiClient_state_Idle_imp; } bool CanSend(); - void FillInHeader(ArduinoJson::DynamicJsonDocument& document); - uint32_t prepareJSONChunk(uint16_t startindex, DynamicJsonDocument& rootDoc, bool InUpdateMode, String value); + void FillInHeader(ArduinoJson::JsonDocument& document); + uint32_t prepareJSONChunk(uint16_t startindex, JsonDocument& rootDoc, bool InUpdateMode, String value); bool SendControlsToClient(uint16_t startidx, ClientUpdateType_t TransferMode, String FragmentRequest); bool SendClientNotification(ClientUpdateType_t value); @@ -62,6 +62,6 @@ public: bool IsSyncronized(); uint32_t id() { return client->id(); } void SetState(ClientUpdateType_t value); - bool SendJsonDocToWebSocket(ArduinoJson::DynamicJsonDocument& document); + bool SendJsonDocToWebSocket(ArduinoJson::JsonDocument& document); }; diff --git a/src/ESPUIcontrol.cpp b/src/ESPUIcontrol.cpp index a6d8f41..36647bf 100644 --- a/src/ESPUIcontrol.cpp +++ b/src/ESPUIcontrol.cpp @@ -133,7 +133,7 @@ bool Control::MarshalControl(JsonObject & _item, _item[F("offset")] = StartingOffset; _item[F("length")] = ValueLenToSend; _item[F("total")] = value.length(); - item = _item.createNestedObject(F("control")); + AllocateNamedJsonObject(item, _item, F("control")); } item[F("id")] = id;