DynamicJsonBuffer::clear() should reset the allocation size too
Created by: ewaldc
Hi Benoit, Apologies for filing a new question for this really cool library. Pre-5.11, I used StaticJsonBufferBase inside in my class library. This gave me full control of the allocation of the buffer space (stack, heap, flash), the scope in relation to the class objects, ability to deal with JSON strings of dynamic length (unknown at compile time) and the performance of StaticJsonBuffer (a single flat buffer space) etc. A major disadvantage is that I have to get the length of the buffer right (no auto extension) using estimation code and now, since 5.11, the reality that it longer works. Hence, I followed your suggestion to use DynamicJsonBuffer(bufferSize) instead. Recoding was easy, the clear() function allows me to free the memory when the buffer is no longer needed ahead of the disposal of an instantiated class object, performance difference is small, but.. I was unable to solve the scope issue :-(.
The original class definition (shortened):
class OpenHab { protected: char *_sitemapBuffer; StaticJsonBufferBase *_sitemapJsonBufferPtr; JsonVariant _siteMap; }
and code
_sitemapBuffer = (char *) malloc (size); // allocate the buffer
StaticJsonBufferBase sitemapJsonBuffer = StaticJsonBufferBase(_sitemapBuffer, size);
_sitemapJsonBufferPtr = &sitemapJsonBuffer; // save buffer just in case
_siteMap = sitemapJsonBuffer.parse(sitemapJson, 20); // increase nestingLimit !
The new class definition (shortened):
class OpenHab { protected: DynamicJsonBuffer _sitemapJsonBuffer; JsonVariant _siteMap; }
and parsing code
DynamicJsonBuffer _sitemapJsonBuffer(size);
_siteMap = _sitemapJsonBuffer.parse(sitemapJson, 20); // increase nestingLimit !
The problem is that _sitemapJsonBuffer seems to go out of scope at the end of the function containing the parsing code. Adding a class initializer e.g. _sitemapJsonBuffer(1) did not solve it either.
I am sure you answered this once before, but I searched for an hour and could not find it.
Platform: NodeMCU 1.0/ESP 12E with ESP8266 board manager 2.3.0 Library: ArduinoJson 5.11.1 IDE: Arduino IDE 18.3
Thanks in advance, Ewald