gregory-halverson / json-simple

Automatically exported from code.google.com/p/json-simple
Apache License 2.0
0 stars 0 forks source link

Less eager JSONParser on streams #56

Open GoogleCodeExporter opened 9 years ago

GoogleCodeExporter commented 9 years ago
Now JSONParser.parse(Reader) reads stream eagerly and throws ParseException if 
a JSON ha ended and another one begins. It would be great if there would be an 
option to return JSON object parsed up to that point instead of throwing an 
exception. After that, more JSONs could be read from the stream.

I guess a small additional logic in
'case Yytoken.TYPE_RIGHT_BRACE' and 'case Yytoken.TYPE_RIGHT_SQUARE'
would do the job. (I plan to make a patch.)

Original issue reported on code.google.com by viktor.t...@gmail.com on 20 Oct 2011 at 12:14

GoogleCodeExporter commented 9 years ago
Any movement on this? Has a patched been created for this yet? I'm just running 
across a case where I want to do this.

Original comment by drizzt...@gmail.com on 15 Nov 2011 at 11:54

GoogleCodeExporter commented 9 years ago
I postponed creating the patch but if you need it I can submit it in one day.

Original comment by viktor.t...@gmail.com on 16 Nov 2011 at 1:16

GoogleCodeExporter commented 9 years ago
I made a work-around. Basically read in data until I reach the end of the JSON 
object from the file, and pass that into the parser. Rinse & repeat.

Would still be a nice to have.

Original comment by drizzt...@gmail.com on 16 Nov 2011 at 6:29

GoogleCodeExporter commented 9 years ago
I tried to do it but it's much harder than I originally thought because of this:
"the old input stream cannot be reused (internal buffer is discarded and lost)."
(comment from the Lexer)
The lexer is reads ahead and when I invoke again "parse" method even on a new 
JSONParser instance, the reader is in a wrong position. Maybe at EOF.
Currently I don't have idea for resolving this.

Original comment by viktor.t...@gmail.com on 17 Nov 2011 at 5:21

GoogleCodeExporter commented 9 years ago
Hi, you can define a higher level protocol if you want to carry multiple JSON 
objects in a single stream. For example, define the stream as the following:
<separator> JSON 1 <separator> JSON 2 ...
Then implement your owner reader which returns JSON stream between separators.

Hope that helps.

Original comment by fangyid...@gmail.com on 17 Nov 2011 at 10:05

GoogleCodeExporter commented 9 years ago
Maybe create a parser and pass it an InputStream/Reader and create a 
PushBackReader around that, which lets you push data back onto the buffer.

Original comment by drizzt...@gmail.com on 17 Nov 2011 at 5:38