Overwrite string values (non-const char *) of the same length instead of allocating them into the Json doc?
Is your feature request related to a problem? Please describe.
I use a DynamicJsonDocument to frequently store c strings (not const) and these strings are always of the same length. If I use doc["key"] = value;
repeatedly, the document is overflowed after a while (I am aware of the information in "I found a memory leak in the library!")
Describe the solution you'd like
Would it be possible to overwrite c strings of the same length (or even strings that are shorter) at the same key instead of allocating them into the Json document?
Describe alternatives you've considered
If I do this:
JsonVariant test = doc["key"];
// Make sure the object at 'key' is already present and it's value is equal or longer than the new value
if(!test.isNull() && strlen(test.as<const char *>() >= the lengh of the new value) {
const char * ptr = test.as<const char *>();
strcpy((char *) ptr, the addess of the new value, the length of the new value);
} else {
// Do a regular insertion into the Json document
doc["key"] = the new value;
}
This perfectly works (my c strings are dynamically created and get out of scope, so I can not use the 'const char *' feature: they need to be copied into the Json document).
Additional context
Although I am not sure, a #define
like is used here might optionally add such a feature? But maybe it is impossible to add this to the monotonic allocator?