Behavior when parsing from stream with multiple JSON objects? #3411
Replies: 2 comments 8 replies
-
Could this help? https://json.nlohmann.me/features/parsing/json_lines/ |
Beta Was this translation helpful? Give feedback.
-
I'm guessing that the parser will throw the exception after it has begun reading the next value after the first one, which means I'd have to push that text back onto the stream before the next time I try to parse. Right? It looks like parse, accept and operator>> do blocking reads until they hit an error or an EOF (right? If so, would be good to add to the doc.) If I use the parser_callback_t hook, I can detect when the end of the top-level value is received, and could even grab the value of the top-level JSON object. but the bool "discard this item" return value can't tell the parser "stop right there"; it'll keep parsing into the next object, triggering that exception and requiring me to push back again. Since the parser can unambiguously detect the end of the top-level value before it starts reading the next value (right?), could there be a new option to have the parser read one value and then return, leaving subsequent data on the stream ready to be read? (I doubt you want to add yet another optional parameter to the parse() function, but I don't know how else you'd do this...) |
Beta Was this translation helpful? Give feedback.
-
I'm interested in using this library to parse a TCP stream containing multiple LF-terminated JSON objects ("ndjson"). Scanning the issues database, it looks like this is possible, e.g.:
However, the documentation doesn't mention this at all:
Does the streaming parser read a single JSON object/array/item and then stop reading, allowing a subsequent parse to work on a subsequent object/array/item?
Beta Was this translation helpful? Give feedback.
All reactions