StaticJsonDocument with a variable to set the size

Hello

is there any way to create a StaticJsonDocument using a variable as a size:

Example:

String myJson = "{\"hello\":\"world\"}"; //(the size can change)
StaticJsonDocument<myJson.length()> jsonDocument;

I get the following error:

exit status 1
call to non-constexpr function 'unsigned int String::length() const'

I'm using a <ArduinoJson.h> library (6.18.4)

thanks

I just looked at the documentation:

"In older versions, DynamicJsonDocument was able to grow if needed. Starting with version 6.7.0, DynamicJsonDocument has a fixed capacity, just like StaticJsonDocument. This change allows better performance, smaller code, and no heap fragmentation."

I think that means 'No, there is no way to set the size of a JsonDocument to a run-time calculated value.'

Not exactly optimal but I guess a work around could be to include a bunch of if statements and allocate a different "static" size depending on the length of the string.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.