json-streaming-parser icon indicating copy to clipboard operation
json-streaming-parser copied to clipboard

Parser not serially reusable

Open MitchBradley opened this issue 2 years ago • 0 comments

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.

MitchBradley avatar Jan 09 '24 00:01 MitchBradley