Skip to content

Commit

Permalink
Centralize JSON config sizing
Browse files Browse the repository at this point in the history
  • Loading branch information
lbussy committed Apr 1, 2020
1 parent e42e7ba commit e488d3d
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 8 deletions.
4 changes: 2 additions & 2 deletions src/jsonconfig.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ SOFTWARE. */

const char *filename = "/config.json";
Config config;
const size_t capacityDeserial = 3*JSON_OBJECT_SIZE(2) + 4*JSON_OBJECT_SIZE(3) + JSON_OBJECT_SIZE(8) + 690;
const size_t capacitySerial = 3*JSON_OBJECT_SIZE(2) + 4*JSON_OBJECT_SIZE(3) + JSON_OBJECT_SIZE(8);
extern const size_t capacityDeserial = 3*JSON_OBJECT_SIZE(2) + 4*JSON_OBJECT_SIZE(3) + JSON_OBJECT_SIZE(8) + 690;
extern const size_t capacitySerial = 3*JSON_OBJECT_SIZE(2) + 4*JSON_OBJECT_SIZE(3) + JSON_OBJECT_SIZE(8);

bool deleteConfig() {
if (!SPIFFS.begin()) {
Expand Down
9 changes: 3 additions & 6 deletions src/webserver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -153,8 +153,7 @@ void setJsonHandlers() {
Log.verbose(F("Serving /config/." CR));

// Serialize configuration
const size_t capacity = 3*JSON_OBJECT_SIZE(2) + 4*JSON_OBJECT_SIZE(3) + JSON_OBJECT_SIZE(8);
DynamicJsonDocument doc(capacity);
DynamicJsonDocument doc(capacitySerial);

// Create an object at the root
JsonObject root = doc.to<JsonObject>();
Expand Down Expand Up @@ -318,8 +317,7 @@ void setSettingsAliases() {
server.on("/config/apply/", HTTP_POST, [] (AsyncWebServerRequest *request) { // Process JSON POST configuration changes
Log.verbose(F("Processing post to /config/apply/." CR));
String input = request->arg(F("plain"));
const size_t capacity = 3*JSON_OBJECT_SIZE(2) + 4*JSON_OBJECT_SIZE(3) + JSON_OBJECT_SIZE(8) + 690;
DynamicJsonDocument doc(capacity);
DynamicJsonDocument doc(capacityDeserial);
DeserializationError err = deserializeJson(doc, input);
if (!err) {
bool updated = false;
Expand Down Expand Up @@ -458,8 +456,7 @@ void setSettingsAliases() {
}, NULL, [](AsyncWebServerRequest *request, uint8_t *data, size_t len, size_t index, size_t total){
request->addInterestingHeader("Accept: application/json");
Log.verbose(F("Processing /apply/." CR));
const size_t capacity = 3*JSON_OBJECT_SIZE(2) + 4*JSON_OBJECT_SIZE(3) + JSON_OBJECT_SIZE(8) + 690;
DynamicJsonDocument doc(capacity);
DynamicJsonDocument doc(capacityDeserial);

DeserializationError error = deserializeJson(doc, (const char*)data);
if (error) {
Expand Down
2 changes: 2 additions & 0 deletions src/webserver.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,5 +49,7 @@ void stopWebServer();
extern struct Config config;
extern struct ThatVersion thatVersion;
extern struct Bubbles bubbles;
extern const size_t capacityDeserial;
extern const size_t capacitySerial;

#endif // _WEBSERVER_H

0 comments on commit e488d3d

Please sign in to comment.