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?
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 community
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.