squix78 / json-streaming-parser

Arduino library for parsing potentially huge json streams on devices with scarce memory
MIT License
205 stars 88 forks source link

Parser not serially reusable #34

Open MitchBradley opened 7 months ago

MitchBradley commented 7 months ago

I have an asynchronous listener thread that receives JSON documents. After one is complete (endDocument is called), the next one is ignored because the parser is in STATE_DONE. I tried to do parser.reset() inside endDocument(), but that does not work because of this code

void JsonStreamingParser::endDocument() {
    myListener->endDocument();
    state = STATE_DONE;
  }

which sets STATE_DONE after endDocument, thus undoing the effect of parser.reset().

I have worked around the problem by setting a flag in endDocument and, when that flag is set, calling parser.reset() in the read loop.

It seems to me that it would be better to set STATE_DONE before calling endDocument(). I cannot think of any existing use that would break, considering that any existing code that calls parser.reset() would not work with the current order.