I am using the fantastic ArduinoJson library for working with JSON data on my ESP8266. I have been facing issues with random software wdt resets and have been investigating memory leaks wherever I can. I want to trim down my use of Strings and DynamicJsonDocuments as much as possible. So I thought of using automatic variables: char buffers: char[N] and StatitcJsonDocuments instead.
I am using the ArduinoJson assitant service to help me figure out how many bytes I should reserve. The following is one response payload, I am expecting to receive:
// char input[MAX_INPUT_LENGTH];
StaticJsonDocument<128> doc;
DeserializationError error = deserializeJson(doc, input, MAX_INPUT_LENGTH);
if (error) {
Serial.print(F("deserializeJson() failed: "));
Serial.println(error.f_str());
return;
}
int statusCode = doc["statusCode"]; // 200
bool needUpdate = doc["needUpdate"]; // true
const char* message = doc["message"]; // "update required"
JsonObject data = doc["data"];
const char* data_deviceId = data["deviceId"]; // "DLD00005"
const char* data_user_fw_version = data["user_fw_version"]; // "0.0.0"
const char* data_new_fw_version = data["new_fw_version"]; // "0.0.5"
As you can see I am using a char [N] type as the input, which is in fact being copied using strncpy() from the https.getString().c_str() from the response of a previous https.POST() request. My issue is that I do not know how much memory I should reserve for the char[N] array, ie what should be the value of N? The reasons I ask is that I am getting a deserialization() failed: IncompleteInput error, which I'm pretty sure is because the char[N] buffer is not large enough. So my question is if the StaticJsonDocument object is 128 bytes, then how big should I make the char[N] buffer (is there any relation between the sizes)? Or should I just go with an arbitrarily large value like char[512] which I want to avoid to avoid stack overflows?
StaticJsonDocument<128> payload;
char payloadSerial[???];
...
int status = https.POST(payloadSerial);
strncpy(payloadSerial, https.getString().c_str(), sizeof(payloadSerial));
DeserializationError error = deserializeJson(payload, payloadSerial, sizeof(payloadSerial));
if (error) {
Monitor.print(F("DeserializeJson() failed: "));
Monitor.println(error.f_str());
}