Fixed serializeJson(doc, String) when allocation fails (fixes #1572)

This commit is contained in:
Benoit Blanchon
2021-05-28 09:02:48 +02:00
parent 3b10afd2ab
commit 9bcb409648
4 changed files with 110 additions and 52 deletions

View File

@ -22,10 +22,10 @@ class Writer< ::String, void> {
}
size_t write(uint8_t c) {
ARDUINOJSON_ASSERT(_size < bufferCapacity);
_buffer[_size++] = static_cast<char>(c);
if (_size + 1 >= bufferCapacity)
flush();
if (flush() != 0)
return 0;
_buffer[_size++] = static_cast<char>(c);
return 1;
}
@ -36,14 +36,15 @@ class Writer< ::String, void> {
return n;
}
private:
void flush() {
size_t flush() {
ARDUINOJSON_ASSERT(_size < bufferCapacity);
_buffer[_size] = 0;
*_destination += _buffer;
_size = 0;
if (_destination->concat(_buffer))
_size = 0;
return _size;
}
private:
::String *_destination;
char _buffer[bufferCapacity];
size_t _size;