Fixed losing elements in saveElement with ArduinoJson V6. Issue #64

pull/69/head
Hieromon Ikasamo 6 years ago
parent 9d19f2a367
commit 4c7a079a70
  1. 21
      src/AutoConnectAux.cpp
  2. 14
      src/AutoConnectElementJsonImpl.h

@ -774,19 +774,26 @@ size_t AutoConnectAux::saveElement(Stream& out, std::vector<String> const& names
size_t size_n = 0; size_t size_n = 0;
// Calculate JSON buffer size // Calculate JSON buffer size
if (amount == 0) if (amount == 0) {
bufferSize += JSON_OBJECT_SIZE(4); 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) if (amount != 1)
bufferSize += JSON_ARRAY_SIZE(amount); bufferSize += JSON_ARRAY_SIZE(amount);
for (String name : names) for (AutoConnectElement& elmEach : _addonElm) {
for (AutoConnectElement& elm : _addonElm) AutoConnectElement* elm = &elmEach;
if (elm.name.equalsIgnoreCase(name)) { if (amount > 0) {
bufferSize += elm.getObjectSize(); String& elmName = elm->name;
break; 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 // 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 // Serialization
if (bufferSize > 0) { if (bufferSize > 0) {

@ -257,9 +257,9 @@ void AutoConnectInputJson::serialize(JsonObject& json) {
* @return An object size for JsonBuffer. * @return An object size for JsonBuffer.
*/ */
size_t AutoConnectRadioJson::getObjectSize() const { 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_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); 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 (String _value : _values) for (const String& _value : _values)
size += _value.length(); size += _value.length();
return size; 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_TYPE)] = String(F(AUTOCONNECT_JSON_TYPE_ACRADIO));
json[F(AUTOCONNECT_JSON_KEY_LABEL)] = label; json[F(AUTOCONNECT_JSON_KEY_LABEL)] = label;
ArduinoJsonArray values = json.createNestedArray(F(AUTOCONNECT_JSON_KEY_VALUE)); ArduinoJsonArray values = json.createNestedArray(F(AUTOCONNECT_JSON_KEY_VALUE));
for (String v : _values) for (const String& v : _values)
values.add(v); values.add(v);
switch (order) { switch (order) {
case AC_Horizontal: case AC_Horizontal:
@ -329,9 +329,9 @@ void AutoConnectRadioJson::serialize(JsonObject& json) {
* @return An object size for JsonBuffer. * @return An object size for JsonBuffer.
*/ */
size_t AutoConnectSelectJson::getObjectSize() const { 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_t size = AutoConnectElementJson::getObjectSize() + JSON_OBJECT_SIZE(3) + JSON_ARRAY_SIZE(_options.size());
size += sizeof(AUTOCONNECT_JSON_KEY_LABEL) + label.length(); size += sizeof(AUTOCONNECT_JSON_KEY_LABEL) + label.length() + sizeof(AUTOCONNECT_JSON_KEY_SELECTED) + 2;
for (String _option : _options) for (const String& _option : _options)
size += _option.length(); size += _option.length();
return size; return size;
} }

Loading…
Cancel
Save