diff --git a/src/AutoConnectAux.cpp b/src/AutoConnectAux.cpp index 5449e40..c84371d 100644 --- a/src/AutoConnectAux.cpp +++ b/src/AutoConnectAux.cpp @@ -774,19 +774,26 @@ size_t AutoConnectAux::saveElement(Stream& out, std::vector const& names size_t size_n = 0; // Calculate JSON buffer size - if (amount == 0) + if (amount == 0) { bufferSize += JSON_OBJECT_SIZE(4); + bufferSize += sizeof(AUTOCONNECT_JSON_KEY_TITLE) + _title.length() + sizeof(AUTOCONNECT_JSON_KEY_URI) + _uriStr.length() + sizeof(AUTOCONNECT_JSON_KEY_MENU) + sizeof("false") + sizeof(AUTOCONNECT_JSON_KEY_ELEMENT); + } if (amount != 1) bufferSize += JSON_ARRAY_SIZE(amount); - for (String name : names) - for (AutoConnectElement& elm : _addonElm) - if (elm.name.equalsIgnoreCase(name)) { - bufferSize += elm.getObjectSize(); - break; - } + for (AutoConnectElement& elmEach : _addonElm) { + AutoConnectElement* elm = &elmEach; + if (amount > 0) { + String& elmName = elm->name; + auto aim = std::find_if(names.begin(), names.end(), [&](const String& n) { return n.equalsIgnoreCase(elmName); }); + if (aim == names.end()) + continue; + } + bufferSize += elm->getObjectSize(); + } // Round up to 16 boundary - bufferSize = bufferSize > 0 ? ((bufferSize + 16) & (~0xf)) : bufferSize; + bufferSize = bufferSize > 0 ? ((bufferSize + 128) & (~0xf)) : bufferSize; + AC_DBG("JSON buffer size:%d\n", bufferSize); // Serialization if (bufferSize > 0) { diff --git a/src/AutoConnectElementJsonImpl.h b/src/AutoConnectElementJsonImpl.h index fd03913..65f260f 100644 --- a/src/AutoConnectElementJsonImpl.h +++ b/src/AutoConnectElementJsonImpl.h @@ -257,9 +257,9 @@ void AutoConnectInputJson::serialize(JsonObject& json) { * @return An object size for JsonBuffer. */ size_t AutoConnectRadioJson::getObjectSize() const { - size_t size =AutoConnectElementJson::getObjectSize() + JSON_OBJECT_SIZE(3) + _values.size() * JSON_OBJECT_SIZE(1) + JSON_ARRAY_SIZE(1); - size += sizeof(AUTOCONNECT_JSON_KEY_LABEL) + label.length() + sizeof(AUTOCONNECT_JSON_KEY_ARRANGE) + sizeof(AUTOCONNECT_JSON_VALUE_HORIZONTAL) + sizeof(AUTOCONNECT_JSON_KEY_CHECKED); - for (String _value : _values) + size_t size = AutoConnectElementJson::getObjectSize() + JSON_OBJECT_SIZE(2) + JSON_ARRAY_SIZE(_values.size()); + size += sizeof(AUTOCONNECT_JSON_KEY_LABEL) + label.length() + sizeof(AUTOCONNECT_JSON_KEY_ARRANGE) + sizeof(AUTOCONNECT_JSON_VALUE_HORIZONTAL) + sizeof(AUTOCONNECT_JSON_KEY_CHECKED) + 2; + for (const String& _value : _values) size += _value.length(); return size; } @@ -310,7 +310,7 @@ void AutoConnectRadioJson::serialize(JsonObject& json) { json[F(AUTOCONNECT_JSON_KEY_TYPE)] = String(F(AUTOCONNECT_JSON_TYPE_ACRADIO)); json[F(AUTOCONNECT_JSON_KEY_LABEL)] = label; ArduinoJsonArray values = json.createNestedArray(F(AUTOCONNECT_JSON_KEY_VALUE)); - for (String v : _values) + for (const String& v : _values) values.add(v); switch (order) { case AC_Horizontal: @@ -329,9 +329,9 @@ void AutoConnectRadioJson::serialize(JsonObject& json) { * @return An object size for JsonBuffer. */ size_t AutoConnectSelectJson::getObjectSize() const { - size_t size = AutoConnectElementJson::getObjectSize() + JSON_OBJECT_SIZE(4) + _options.size() * JSON_OBJECT_SIZE(1) + JSON_ARRAY_SIZE(1); - size += sizeof(AUTOCONNECT_JSON_KEY_LABEL) + label.length(); - for (String _option : _options) + size_t size = AutoConnectElementJson::getObjectSize() + JSON_OBJECT_SIZE(3) + JSON_ARRAY_SIZE(_options.size()); + size += sizeof(AUTOCONNECT_JSON_KEY_LABEL) + label.length() + sizeof(AUTOCONNECT_JSON_KEY_SELECTED) + 2; + for (const String& _option : _options) size += _option.length(); return size; }