I'm trying to effectively allocate a doc's size, based on the size of the file saved in flash of ESP8266. Is there a way?
For example: file.size()
X 1.5
Arduino Stack Exchange is a question and answer site for developers of open-source hardware and software that is compatible with Arduino. It only takes a minute to sign up.
Sign up to join this communityI'm trying to effectively allocate a doc's size, based on the size of the file saved in flash of ESP8266. Is there a way?
For example: file.size()
X 1.5
There is unfortunately no way to predict the size of the JsonDocument
from the file size alone.
As a workaround, I suggest that you allocate a very large memory pool and shrink it after deserialization; like so:
DynamicJsonDocument doc(ESP.getMaxAllocHeap());
deserializeJson(doc, file);
doc.shrinkToFit();
Indeed, this program consumes more memory than strictly required, but only for a fraction of a second.
See also:
StaticJsonDocument
is not an option.
Jun 22, 2022 at 7:51