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)
{
// Reserve memory for JsonDocument
size_t maxsize = (128u * ((strlen(payload) / 128) + 1)) + 256;
DynamicJsonDocument map_doc(maxsize);
// size_t maxsize = (128u * ((strlen(payload) / 128) + 1)) + 256;
// DynamicJsonDocument map_doc(maxsize);
StaticJsonDocument<1024> map_doc;
DeserializationError jsonError = deserializeJson(map_doc, 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
// Reserve memory for JsonDocument
size_t maxsize = (128u * ((strlen(payload) / 128) + 1)) + 256;
DynamicJsonDocument doc(maxsize);
// size_t maxsize = (128u * ((strlen(payload) / 128) + 1)) + 256;
// DynamicJsonDocument doc(maxsize);
StaticJsonDocument<1024> doc;
DeserializationError jsonError = deserializeJson(doc, 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) {
size_t maxsize = (512u + JSON_OBJECT_SIZE(25));
DynamicJsonDocument json(maxsize);
// size_t maxsize = (512u + JSON_OBJECT_SIZE(25));
// DynamicJsonDocument json(maxsize);
StaticJsonDocument<1024> json;
// 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
// get overwritten by the send buffer !!
DeserializationError jsonError = deserializeJson(json, (char*)payload);
json.shrinkToFit();
// json.shrinkToFit();
if(jsonError == DeserializationError::Ok) {
// 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);
if(strlen(payload) > 0) {
size_t maxsize = (128u * ((strlen(payload) / 128) + 1)) + 128;
DynamicJsonDocument json(maxsize);
StaticJsonDocument<128> json;
// 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 !!
DeserializationError jsonError = deserializeJson(json, payload);
json.shrinkToFit();
if(jsonError) { // Couldn't parse incoming JSON command
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
void dispatch_config(const char* topic, const char* payload, uint8_t source)
{
DynamicJsonDocument doc(128 * 3);
char buffer[128 * 3];
StaticJsonDocument<384> doc;
char buffer[384];
JsonObject settings;
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.concat("]");
}*/
size_t maxsize = (128u * ((strlen(payload) / 128) + 1)) + 512;
DynamicJsonDocument json(maxsize);
// size_t maxsize = (128u * ((strlen(payload) / 128) + 1)) + 512;
// DynamicJsonDocument json(maxsize);
StaticJsonDocument<1024> json;
// 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 !!
DeserializationError jsonError = deserializeJson(json, payload);
json.shrinkToFit();
// json.shrinkToFit();
if(jsonError) { // Couldn't parse incoming JSON command
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();
uint16_t line = 1;
DynamicJsonDocument jsonl(MQTT_MAX_PACKET_SIZE / 2 + 128);
StaticJsonDocument<1024> jsonl;
DeserializationError jsonError = deserializeJson(jsonl, stream);
#ifdef ARDUINO
@ -815,13 +813,12 @@ void dispatch_page(const char*, const char* payload, uint8_t source)
uint8_t pageid = Parser::haspPayloadToPageid(payload);
if(pageid == 0) {
size_t maxsize = (128u * ((strlen(payload) / 128) + 1)) + 128;
DynamicJsonDocument json(maxsize);
StaticJsonDocument<128> json;
// 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 !!
DeserializationError jsonError = deserializeJson(json, payload);
json.shrinkToFit();
// json.shrinkToFit();
if(!jsonError && json.is<JsonObject>()) { // Only JsonObject is valid
JsonVariant prop;
@ -884,14 +881,12 @@ void dispatch_moodlight(const char* topic, const char* payload, uint8_t source)
{
// Set the current state
if(strlen(payload) != 0) {
size_t maxsize = (128u * ((strlen(payload) / 128) + 1)) + 128;
DynamicJsonDocument json(maxsize);
StaticJsonDocument<128> json;
// 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 !!
DeserializationError jsonError = deserializeJson(json, payload);
json.shrinkToFit();
// json.shrinkToFit();
if(jsonError) { // Couldn't parse incoming JSON command
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
if(strlen(payload) != 0) {
size_t maxsize = (128u * ((strlen(payload) / 128) + 1)) + 128;
DynamicJsonDocument json(maxsize);
StaticJsonDocument<128> json;
// 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 !!
DeserializationError jsonError = deserializeJson(json, payload);
json.shrinkToFit();
// json.shrinkToFit();
if(jsonError) { // Couldn't parse incoming payload as json
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)
{
if(strlen(payload) >= 0) {
size_t maxsize = (128u * ((strlen(payload) / 128) + 1)) + 128;
DynamicJsonDocument json(maxsize);
StaticJsonDocument<128> json;
// 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 !!
DeserializationError jsonError = deserializeJson(json, payload);
json.shrinkToFit();
// json.shrinkToFit();
int32_t count = 30;
uint32_t period = 1000;
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;
std::string otaUrl = "http://ota.netwize.be";
uint16_t arduinoOtaPort = HASP_ARDUINOOTA_PORT;
uint16_t arduinoOtaPort = HASP_ARDUINOOTA_PORT;
int8_t otaPrecentageComplete = -1;
bool otaUpdateCheck()
@ -98,7 +98,7 @@ bool otaUpdateCheck()
return false;
}
DynamicJsonDocument updateJson(1024);
StaticJsonDocument<1024> updateJson;
DeserializationError jsonError = deserializeJson(updateJson, updateClient.getString());
updateClient.end();
@ -199,7 +199,6 @@ void otaEverySecond(void)
}
#endif // HASP_USE_ARDUINOOTA
void otaSetup(void)
{
#if ESP_ARDUINO_VERSION_MAJOR >= 2