From 2143b0f2f81df075c507af8bb5a397b9ce801cdb Mon Sep 17 00:00:00 2001 From: Hieromon Ikasamo Date: Fri, 3 May 2019 20:35:11 +0900 Subject: [PATCH] mproved calculation of JSON buffer size --- src/AutoConnect.cpp | 20 -------------------- src/AutoConnectAux.cpp | 7 ++++--- src/AutoConnectElementJsonImpl.h | 31 ++++++++++++++++--------------- 3 files changed, 20 insertions(+), 38 deletions(-) diff --git a/src/AutoConnect.cpp b/src/AutoConnect.cpp index 4ab45b8..5cafa1e 100644 --- a/src/AutoConnect.cpp +++ b/src/AutoConnect.cpp @@ -789,26 +789,6 @@ bool AutoConnect::_classifyHandle(HTTPMethod method, String uri) { _portalAccessPeriod = millis(); AC_DBG("Host:%s,URI:%s", _webServer->hostHeader().c_str(), uri.c_str()); - // When handleClient calls RequestHandler, the parsed http argument - // remains the previous request. - // If the current request argument contains AutoConnectElement, it is - // the form data of the AutoConnectAux page and with this timing save - // the value of each element. - // if (_webServer->hasArg(String(F(AUTOCONNECT_AUXURI_PARAM)))) { - // _auxUri = _webServer->arg(AUTOCONNECT_AUXURI_PARAM); - // _auxUri.replace("/", "/"); - // AutoConnectAux* aux = _aux.get(); - // while (aux) { - // if (aux->_uriStr == _auxUri) { - // // Save the value owned by each element contained in the POST body - // // of a current HTTP request to AutoConnectElements. - // aux->_storeElements(_webServer.get()); - // break; - // } - // aux = aux->_next.get(); - // } - // } - // Here, classify requested uri if (uri == _uri) { AC_DBG_DUMB(",already allocated\n"); diff --git a/src/AutoConnectAux.cpp b/src/AutoConnectAux.cpp index c84371d..691d5d4 100644 --- a/src/AutoConnectAux.cpp +++ b/src/AutoConnectAux.cpp @@ -776,9 +776,10 @@ size_t AutoConnectAux::saveElement(Stream& out, std::vector const& names // Calculate JSON buffer size 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); + bufferSize += sizeof(AUTOCONNECT_JSON_KEY_TITLE) + _title.length() + 1 + sizeof(AUTOCONNECT_JSON_KEY_URI) + _uriStr.length() + 1 + sizeof(AUTOCONNECT_JSON_KEY_MENU) + sizeof(AUTOCONNECT_JSON_KEY_ELEMENT); + bufferSize += JSON_ARRAY_SIZE(_addonElm.size()); } - if (amount != 1) + else bufferSize += JSON_ARRAY_SIZE(amount); for (AutoConnectElement& elmEach : _addonElm) { @@ -792,7 +793,7 @@ size_t AutoConnectAux::saveElement(Stream& out, std::vector const& names bufferSize += elm->getObjectSize(); } // Round up to 16 boundary - bufferSize = bufferSize > 0 ? ((bufferSize + 128) & (~0xf)) : bufferSize; + bufferSize = bufferSize > 0 ? ((bufferSize + 16) & (~0xf)) : bufferSize; AC_DBG("JSON buffer size:%d\n", bufferSize); // Serialization diff --git a/src/AutoConnectElementJsonImpl.h b/src/AutoConnectElementJsonImpl.h index 65f260f..dca6f6b 100644 --- a/src/AutoConnectElementJsonImpl.h +++ b/src/AutoConnectElementJsonImpl.h @@ -17,8 +17,9 @@ * @return An object size for JsonBuffer. */ size_t AutoConnectElementJson::getObjectSize() const { - size_t size = JSON_OBJECT_SIZE(3) + sizeof(AUTOCONNECT_JSON_KEY_TYPE) + sizeof(AUTOCONNECT_JSON_KEY_NAME) + sizeof(AUTOCONNECT_JSON_KEY_VALUE) + 10; - size += name.length() + value.length(); + size_t size = JSON_OBJECT_SIZE(3); + size += sizeof(AUTOCONNECT_JSON_KEY_NAME) + sizeof(AUTOCONNECT_JSON_KEY_TYPE) + sizeof(AUTOCONNECT_JSON_KEY_VALUE) + sizeof(AUTOCONNECT_JSON_TYPE_ACELEMENT); + size += name.length() + 1 + value.length() + 1; return size; } @@ -72,7 +73,7 @@ void AutoConnectElementJson::_setMember(const JsonObject& json) { */ size_t AutoConnectButtonJson::getObjectSize() const { size_t size = AutoConnectElementJson::getObjectSize() + JSON_OBJECT_SIZE(1); - size += sizeof(AUTOCONNECT_JSON_KEY_ACTION) + action.length(); + size += sizeof(AUTOCONNECT_JSON_KEY_ACTION) + action.length() + 1; return size; } @@ -110,7 +111,7 @@ void AutoConnectButtonJson::serialize(JsonObject& json) { */ size_t AutoConnectCheckboxJson::getObjectSize() const { size_t size = AutoConnectElementJson::getObjectSize() + JSON_OBJECT_SIZE(2); - size += sizeof(AUTOCONNECT_JSON_KEY_LABEL) + label.length() + sizeof(AUTOCONNECT_JSON_KEY_CHECKED); + size += sizeof(AUTOCONNECT_JSON_KEY_LABEL) + label.length() + 1 + sizeof(AUTOCONNECT_JSON_KEY_CHECKED); return size; } @@ -152,7 +153,7 @@ void AutoConnectCheckboxJson::serialize(JsonObject& json) { */ size_t AutoConnectFileJson::getObjectSize() const { size_t size = AutoConnectElementJson::getObjectSize() + JSON_OBJECT_SIZE(2); - size += sizeof(AUTOCONNECT_JSON_KEY_LABEL) + label.length() + sizeof(AUTOCONNECT_JSON_KEY_STORE) + sizeof(AUTOCONNECT_JSON_VALUE_FS); + size += sizeof(AUTOCONNECT_JSON_KEY_LABEL) + label.length() + 1 + sizeof(AUTOCONNECT_JSON_KEY_STORE) + sizeof(AUTOCONNECT_JSON_VALUE_EXTERNAL); return size; } @@ -214,7 +215,7 @@ void AutoConnectFileJson::serialize(JsonObject& json) { */ size_t AutoConnectInputJson::getObjectSize() const { size_t size = AutoConnectElementJson::getObjectSize() + JSON_OBJECT_SIZE(3); - size += sizeof(AUTOCONNECT_JSON_KEY_LABEL) + label.length() + sizeof(AUTOCONNECT_JSON_KEY_PATTERN) + pattern.length() + sizeof(AUTOCONNECT_JSON_KEY_PLACEHOLDER) + placeholder.length(); + size += sizeof(AUTOCONNECT_JSON_KEY_LABEL) + label.length() + 1 + sizeof(AUTOCONNECT_JSON_KEY_PATTERN) + pattern.length() + 1 + sizeof(AUTOCONNECT_JSON_KEY_PLACEHOLDER) + placeholder.length() + 1; return size; } @@ -257,10 +258,10 @@ void AutoConnectInputJson::serialize(JsonObject& json) { * @return An object size for JsonBuffer. */ size_t AutoConnectRadioJson::getObjectSize() const { - 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; + size_t size = AutoConnectElementJson::getObjectSize() + JSON_OBJECT_SIZE(3) + JSON_ARRAY_SIZE(_values.size()); + size += sizeof(AUTOCONNECT_JSON_KEY_LABEL) + label.length() + 1 + sizeof(AUTOCONNECT_JSON_KEY_ARRANGE) + sizeof(AUTOCONNECT_JSON_VALUE_HORIZONTAL) + sizeof(AUTOCONNECT_JSON_KEY_CHECKED); for (const String& _value : _values) - size += _value.length(); + size += _value.length() + 1; return size; } @@ -329,10 +330,10 @@ void AutoConnectRadioJson::serialize(JsonObject& json) { * @return An object size for JsonBuffer. */ size_t AutoConnectSelectJson::getObjectSize() const { - 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; + size_t size = AutoConnectElementJson::getObjectSize() + JSON_OBJECT_SIZE(2) + JSON_ARRAY_SIZE(_options.size()); + size += sizeof(AUTOCONNECT_JSON_KEY_LABEL) + label.length() + 1 + sizeof(AUTOCONNECT_JSON_KEY_SELECTED); for (const String& _option : _options) - size += _option.length(); + size += _option.length() + 1; return size; } @@ -381,8 +382,8 @@ void AutoConnectSelectJson::serialize(JsonObject& json) { * @return An object size for JsonBuffer. */ size_t AutoConnectSubmitJson::getObjectSize() const { - size_t size = AutoConnectElementJson::getObjectSize() + JSON_OBJECT_SIZE(1); - size += sizeof(AUTOCONNECT_JSON_KEY_URI) + uri.length(); + size_t size = AutoConnectElementJson::getObjectSize(); + size += sizeof(AUTOCONNECT_JSON_KEY_URI) + uri.length() + 1; return size; } @@ -420,7 +421,7 @@ void AutoConnectSubmitJson::serialize(JsonObject& json) { */ size_t AutoConnectTextJson::getObjectSize() const { size_t size = AutoConnectElementJson::getObjectSize() + JSON_OBJECT_SIZE(2); - size += sizeof(AUTOCONNECT_JSON_KEY_STYLE) + style.length() + sizeof(AUTOCONNECT_JSON_KEY_FORMAT) + format.length(); + size += sizeof(AUTOCONNECT_JSON_KEY_STYLE) + style.length() + 1 + sizeof(AUTOCONNECT_JSON_KEY_FORMAT) + format.length() + 1; return size; }