Prefer stack to heap memory for JsonDocuments

This commit is contained in:
fvanroie 2022-09-12 01:11:17 +02:00
parent eccfc6c7a2
commit ba2ed35a85
3 changed files with 28 additions and 33 deletions

View File

@ -90,8 +90,9 @@ void my_msgbox_map_clear(lv_obj_t* obj)
const char** my_map_create(const char* payload) const char** my_map_create(const char* payload)
{ {
// Reserve memory for JsonDocument // Reserve memory for JsonDocument
size_t maxsize = (128u * ((strlen(payload) / 128) + 1)) + 256; // size_t maxsize = (128u * ((strlen(payload) / 128) + 1)) + 256;
DynamicJsonDocument map_doc(maxsize); // DynamicJsonDocument map_doc(maxsize);
StaticJsonDocument<1024> map_doc;
DeserializationError jsonError = deserializeJson(map_doc, payload); DeserializationError jsonError = deserializeJson(map_doc, payload);
if(jsonError) { // Couldn't parse incoming JSON payload if(jsonError) { // Couldn't parse incoming JSON payload
@ -191,8 +192,9 @@ static bool my_line_set_points(lv_obj_t* obj, const char* payload)
// Create new points // Create new points
// Reserve memory for JsonDocument // Reserve memory for JsonDocument
size_t maxsize = (128u * ((strlen(payload) / 128) + 1)) + 256; // size_t maxsize = (128u * ((strlen(payload) / 128) + 1)) + 256;
DynamicJsonDocument doc(maxsize); // DynamicJsonDocument doc(maxsize);
StaticJsonDocument<1024> doc;
DeserializationError jsonError = deserializeJson(doc, payload); DeserializationError jsonError = deserializeJson(doc, payload);
if(jsonError) { // Couldn't parse incoming JSON payload if(jsonError) { // Couldn't parse incoming JSON payload
@ -1640,14 +1642,15 @@ static hasp_attribute_type_t attribute_common_json(lv_obj_t* obj, uint16_t attr_
if(update) { if(update) {
size_t maxsize = (512u + JSON_OBJECT_SIZE(25)); // size_t maxsize = (512u + JSON_OBJECT_SIZE(25));
DynamicJsonDocument json(maxsize); // DynamicJsonDocument json(maxsize);
StaticJsonDocument<1024> json;
// Note: Deserialization can to be (char *) so the objects WILL NOT be copied // Note: Deserialization can to be (char *) so the objects WILL NOT be copied
// this uses less memory since the data is already copied from the mqtt receive buffer and cannot // this uses less memory since the data is already copied from the mqtt receive buffer and cannot
// get overwritten by the send buffer !! // get overwritten by the send buffer !!
DeserializationError jsonError = deserializeJson(json, (char*)payload); DeserializationError jsonError = deserializeJson(json, (char*)payload);
json.shrinkToFit(); // json.shrinkToFit();
if(jsonError == DeserializationError::Ok) { if(jsonError == DeserializationError::Ok) {
// Make sure we have a valid JsonObject to start from // Make sure we have a valid JsonObject to start from

View File

@ -216,14 +216,11 @@ static void dispatch_output(const char* topic, const char* payload)
uint8_t pin = atoi(topic); uint8_t pin = atoi(topic);
if(strlen(payload) > 0) { if(strlen(payload) > 0) {
StaticJsonDocument<128> json;
size_t maxsize = (128u * ((strlen(payload) / 128) + 1)) + 128;
DynamicJsonDocument json(maxsize);
// Note: Deserialization needs to be (const char *) so the objects WILL be copied // Note: Deserialization needs to be (const char *) so the objects WILL be copied
// this uses more memory but otherwise the mqtt receive buffer can get overwritten by the send buffer !! // this uses more memory but otherwise the mqtt receive buffer can get overwritten by the send buffer !!
DeserializationError jsonError = deserializeJson(json, payload); DeserializationError jsonError = deserializeJson(json, payload);
json.shrinkToFit();
if(jsonError) { // Couldn't parse incoming JSON command if(jsonError) { // Couldn't parse incoming JSON command
dispatch_json_error(TAG_MSGR, jsonError); dispatch_json_error(TAG_MSGR, jsonError);
@ -443,8 +440,8 @@ void dispatch_text_line(const char* cmnd, uint8_t source)
// Get or Set a part of the config.json file // Get or Set a part of the config.json file
void dispatch_config(const char* topic, const char* payload, uint8_t source) void dispatch_config(const char* topic, const char* payload, uint8_t source)
{ {
DynamicJsonDocument doc(128 * 3); StaticJsonDocument<384> doc;
char buffer[128 * 3]; char buffer[384];
JsonObject settings; JsonObject settings;
bool update; bool update;
@ -603,13 +600,14 @@ void dispatch_parse_json(const char*, const char* payload, uint8_t source)
strPayload.remove(strPayload.length() - 2, 2); strPayload.remove(strPayload.length() - 2, 2);
strPayload.concat("]"); strPayload.concat("]");
}*/ }*/
size_t maxsize = (128u * ((strlen(payload) / 128) + 1)) + 512; // size_t maxsize = (128u * ((strlen(payload) / 128) + 1)) + 512;
DynamicJsonDocument json(maxsize); // DynamicJsonDocument json(maxsize);
StaticJsonDocument<1024> json;
// Note: Deserialization needs to be (const char *) so the objects WILL be copied // Note: Deserialization needs to be (const char *) so the objects WILL be copied
// this uses more memory but otherwise the mqtt receive buffer can get overwritten by the send buffer !! // this uses more memory but otherwise the mqtt receive buffer can get overwritten by the send buffer !!
DeserializationError jsonError = deserializeJson(json, payload); DeserializationError jsonError = deserializeJson(json, payload);
json.shrinkToFit(); // json.shrinkToFit();
if(jsonError) { // Couldn't parse incoming JSON command if(jsonError) { // Couldn't parse incoming JSON command
dispatch_json_error(TAG_MSGR, jsonError); dispatch_json_error(TAG_MSGR, jsonError);
@ -652,7 +650,7 @@ void dispatch_parse_jsonl(std::istream& stream, uint8_t& saved_page_id)
{ {
// uint8_t savedPage = haspPages.get(); // uint8_t savedPage = haspPages.get();
uint16_t line = 1; uint16_t line = 1;
DynamicJsonDocument jsonl(MQTT_MAX_PACKET_SIZE / 2 + 128); StaticJsonDocument<1024> jsonl;
DeserializationError jsonError = deserializeJson(jsonl, stream); DeserializationError jsonError = deserializeJson(jsonl, stream);
#ifdef ARDUINO #ifdef ARDUINO
@ -815,13 +813,12 @@ void dispatch_page(const char*, const char* payload, uint8_t source)
uint8_t pageid = Parser::haspPayloadToPageid(payload); uint8_t pageid = Parser::haspPayloadToPageid(payload);
if(pageid == 0) { if(pageid == 0) {
size_t maxsize = (128u * ((strlen(payload) / 128) + 1)) + 128; StaticJsonDocument<128> json;
DynamicJsonDocument json(maxsize);
// Note: Deserialization needs to be (const char *) so the objects WILL be copied // Note: Deserialization needs to be (const char *) so the objects WILL be copied
// this uses more memory but otherwise the mqtt receive buffer can get overwritten by the send buffer !! // this uses more memory but otherwise the mqtt receive buffer can get overwritten by the send buffer !!
DeserializationError jsonError = deserializeJson(json, payload); DeserializationError jsonError = deserializeJson(json, payload);
json.shrinkToFit(); // json.shrinkToFit();
if(!jsonError && json.is<JsonObject>()) { // Only JsonObject is valid if(!jsonError && json.is<JsonObject>()) { // Only JsonObject is valid
JsonVariant prop; JsonVariant prop;
@ -884,14 +881,12 @@ void dispatch_moodlight(const char* topic, const char* payload, uint8_t source)
{ {
// Set the current state // Set the current state
if(strlen(payload) != 0) { if(strlen(payload) != 0) {
StaticJsonDocument<128> json;
size_t maxsize = (128u * ((strlen(payload) / 128) + 1)) + 128;
DynamicJsonDocument json(maxsize);
// Note: Deserialization needs to be (const char *) so the objects WILL be copied // Note: Deserialization needs to be (const char *) so the objects WILL be copied
// this uses more memory but otherwise the mqtt receive buffer can get overwritten by the send buffer !! // this uses more memory but otherwise the mqtt receive buffer can get overwritten by the send buffer !!
DeserializationError jsonError = deserializeJson(json, payload); DeserializationError jsonError = deserializeJson(json, payload);
json.shrinkToFit(); // json.shrinkToFit();
if(jsonError) { // Couldn't parse incoming JSON command if(jsonError) { // Couldn't parse incoming JSON command
dispatch_json_error(TAG_MSGR, jsonError); dispatch_json_error(TAG_MSGR, jsonError);
@ -944,13 +939,12 @@ void dispatch_backlight(const char*, const char* payload, uint8_t source)
// Set the current state // Set the current state
if(strlen(payload) != 0) { if(strlen(payload) != 0) {
size_t maxsize = (128u * ((strlen(payload) / 128) + 1)) + 128; StaticJsonDocument<128> json;
DynamicJsonDocument json(maxsize);
// Note: Deserialization needs to be (const char *) so the objects WILL be copied // Note: Deserialization needs to be (const char *) so the objects WILL be copied
// this uses more memory but otherwise the mqtt receive buffer can get overwritten by the send buffer !! // this uses more memory but otherwise the mqtt receive buffer can get overwritten by the send buffer !!
DeserializationError jsonError = deserializeJson(json, payload); DeserializationError jsonError = deserializeJson(json, payload);
json.shrinkToFit(); // json.shrinkToFit();
if(jsonError) { // Couldn't parse incoming payload as json if(jsonError) { // Couldn't parse incoming payload as json
if(Parser::is_only_digits(payload)) { if(Parser::is_only_digits(payload)) {
@ -1012,13 +1006,12 @@ void dispatch_web_update(const char*, const char* espOtaUrl, uint8_t source)
void dispatch_antiburn(const char*, const char* payload, uint8_t source) void dispatch_antiburn(const char*, const char* payload, uint8_t source)
{ {
if(strlen(payload) >= 0) { if(strlen(payload) >= 0) {
size_t maxsize = (128u * ((strlen(payload) / 128) + 1)) + 128; StaticJsonDocument<128> json;
DynamicJsonDocument json(maxsize);
// Note: Deserialization needs to be (const char *) so the objects WILL be copied // Note: Deserialization needs to be (const char *) so the objects WILL be copied
// this uses more memory but otherwise the mqtt receive buffer can get overwritten by the send buffer !! // this uses more memory but otherwise the mqtt receive buffer can get overwritten by the send buffer !!
DeserializationError jsonError = deserializeJson(json, payload); DeserializationError jsonError = deserializeJson(json, payload);
json.shrinkToFit(); // json.shrinkToFit();
int32_t count = 30; int32_t count = 30;
uint32_t period = 1000; uint32_t period = 1000;
bool state = false; bool state = false;

View File

@ -79,7 +79,7 @@ extern const uint8_t rootca_crt_bundle_start[] asm("_binary_data_cert_x509_crt_b
static WiFiClientSecure secureClient; static WiFiClientSecure secureClient;
std::string otaUrl = "http://ota.netwize.be"; std::string otaUrl = "http://ota.netwize.be";
uint16_t arduinoOtaPort = HASP_ARDUINOOTA_PORT; uint16_t arduinoOtaPort = HASP_ARDUINOOTA_PORT;
int8_t otaPrecentageComplete = -1; int8_t otaPrecentageComplete = -1;
bool otaUpdateCheck() bool otaUpdateCheck()
@ -98,7 +98,7 @@ bool otaUpdateCheck()
return false; return false;
} }
DynamicJsonDocument updateJson(1024); StaticJsonDocument<1024> updateJson;
DeserializationError jsonError = deserializeJson(updateJson, updateClient.getString()); DeserializationError jsonError = deserializeJson(updateJson, updateClient.getString());
updateClient.end(); updateClient.end();
@ -199,7 +199,6 @@ void otaEverySecond(void)
} }
#endif // HASP_USE_ARDUINOOTA #endif // HASP_USE_ARDUINOOTA
void otaSetup(void) void otaSetup(void)
{ {
#if ESP_ARDUINO_VERSION_MAJOR >= 2 #if ESP_ARDUINO_VERSION_MAJOR >= 2