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
-
1I would think it very much depends on what the content is. If you have a lot of string data then it can correlate quite well. If you have a lot of numeric data then I would imagine the internal representation could be a lot smaller than the textual file representation of the same data.Majenko– Majenko2022年06月21日 11:20:01 +00:00Commented Jun 21, 2022 at 11:20
-
@Majenko Since some parameter files are stored on flash (which also can be change in future), one size's can be 256, and other is 1250. My goal is not to allocate max of allguyd– guyd2022年06月21日 11:22:42 +00:00Commented Jun 21, 2022 at 11:22
1 Answer 1
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:
-
Is only for DynamicJsonDocument?guyd– guyd2022年06月21日 11:50:35 +00:00Commented Jun 21, 2022 at 11:50
-
1
-
1
-
2@guyd, you asked how to determine the document size at run time, so
StaticJsonDocument
is not an option.Benoit Blanchon– Benoit Blanchon2022年06月22日 07:51:08 +00:00Commented Jun 22, 2022 at 7:51