mirror of
https://github.com/HASwitchPlate/openHASP.git
synced 2025-07-28 13:46:36 +00:00
Use heap memory for large JsonDocuments
This commit is contained in:
parent
d6c5f64143
commit
116877099b
@ -90,9 +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;
|
// StaticJsonDocument<1024> map_doc;
|
||||||
// DynamicJsonDocument map_doc(maxsize);
|
size_t maxsize = (128u * ((strlen(payload) / 128) + 1)) + 256;
|
||||||
StaticJsonDocument<1024> map_doc;
|
DynamicJsonDocument map_doc(maxsize);
|
||||||
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
|
||||||
@ -192,9 +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;
|
// StaticJsonDocument<1024> doc;
|
||||||
// DynamicJsonDocument doc(maxsize);
|
size_t maxsize = (128u * ((strlen(payload) / 128) + 1)) + 256;
|
||||||
StaticJsonDocument<1024> doc;
|
DynamicJsonDocument doc(maxsize);
|
||||||
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
|
||||||
@ -1656,15 +1656,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));
|
// StaticJsonDocument<1024> json;
|
||||||
// DynamicJsonDocument json(maxsize);
|
size_t maxsize = (512u + JSON_OBJECT_SIZE(25));
|
||||||
StaticJsonDocument<1024> json;
|
DynamicJsonDocument json(maxsize);
|
||||||
|
|
||||||
// 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
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
/* MIT License - Copyright (c) 2019-2022 Francis Van Roie
|
/* MIT License - Copyright (c) 2019-2023 Francis Van Roie
|
||||||
For full license information read the LICENSE file in the project folder */
|
For full license information read the LICENSE file in the project folder */
|
||||||
|
|
||||||
#include <time.h>
|
#include <time.h>
|
||||||
@ -624,14 +624,14 @@ void dispatch_text_line(const char* payload, uint8_t source)
|
|||||||
{
|
{
|
||||||
|
|
||||||
{
|
{
|
||||||
// size_t maxsize = (128u * ((strlen(payload) / 128) + 1)) + 512;
|
// StaticJsonDocument<1024> doc;
|
||||||
// DynamicJsonDocument json(maxsize);
|
size_t maxsize = (128u * ((strlen(payload) / 128) + 1)) + 512;
|
||||||
StaticJsonDocument<1024> doc;
|
DynamicJsonDocument doc(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(doc, payload);
|
DeserializationError jsonError = deserializeJson(doc, payload);
|
||||||
// json.shrinkToFit();
|
doc.shrinkToFit();
|
||||||
|
|
||||||
if(jsonError) {
|
if(jsonError) {
|
||||||
// dispatch_json_error(TAG_MSGR, jsonError);
|
// dispatch_json_error(TAG_MSGR, jsonError);
|
||||||
@ -654,8 +654,11 @@ void dispatch_text_line(const char* payload, uint8_t source)
|
|||||||
|
|
||||||
void dispatch_parse_json(const char*, const char* payload, uint8_t source)
|
void dispatch_parse_json(const char*, const char* payload, uint8_t source)
|
||||||
{ // Parse an incoming JSON array into individual commands
|
{ // Parse an incoming JSON array into individual commands
|
||||||
StaticJsonDocument<2048> doc;
|
// StaticJsonDocument<2048> doc;
|
||||||
|
size_t maxsize = (128u * ((strlen(payload) / 128) + 1)) + 512;
|
||||||
|
DynamicJsonDocument doc(maxsize);
|
||||||
DeserializationError jsonError = deserializeJson(doc, payload);
|
DeserializationError jsonError = deserializeJson(doc, payload);
|
||||||
|
doc.shrinkToFit();
|
||||||
|
|
||||||
if(jsonError) {
|
if(jsonError) {
|
||||||
dispatch_json_error(TAG_MSGR, jsonError);
|
dispatch_json_error(TAG_MSGR, jsonError);
|
||||||
@ -675,22 +678,21 @@ void dispatch_parse_jsonl(Stream& stream, uint8_t& saved_page_id)
|
|||||||
void dispatch_parse_jsonl(std::istream& stream, uint8_t& saved_page_id)
|
void dispatch_parse_jsonl(std::istream& stream, uint8_t& saved_page_id)
|
||||||
#endif
|
#endif
|
||||||
{
|
{
|
||||||
// uint8_t savedPage = haspPages.get();
|
// StaticJsonDocument<1024> jsonl;
|
||||||
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);
|
||||||
|
jsonl.shrinkToFit();
|
||||||
|
|
||||||
#ifdef ARDUINO
|
#ifdef ARDUINO
|
||||||
stream.setTimeout(25);
|
stream.setTimeout(25);
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
// guiStop();
|
uint16_t line = 1;
|
||||||
while(jsonError == DeserializationError::Ok) {
|
while(jsonError == DeserializationError::Ok) {
|
||||||
hasp_new_object(jsonl.as<JsonObject>(), saved_page_id);
|
hasp_new_object(jsonl.as<JsonObject>(), saved_page_id);
|
||||||
jsonError = deserializeJson(jsonl, stream);
|
jsonError = deserializeJson(jsonl, stream);
|
||||||
line++;
|
line++;
|
||||||
}
|
}
|
||||||
// guiStart();
|
|
||||||
|
|
||||||
/* For debugging purposes */
|
/* For debugging purposes */
|
||||||
if(jsonError == DeserializationError::EmptyInput) {
|
if(jsonError == DeserializationError::EmptyInput) {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user