Skip to main content

Reading Continuous Streams

The following parsers can be configured to read continuous streams of objects from the same input stream:

JsonParser UonParser MsgPackParser

The JsonParser and UonParser classes can read continuous streams by using the Parser.Builder.unbuffered() setting. This prevents the parsers from using an internal buffer that would read past the end of the currently parsed POJO.

Examples
// If you're calling parse on the same input multiple times, use a session instead of the parser directly.
ReaderParserSession session = JsonParser.create().unbuffered().build().createSession();
Object pojo;
Reader reader;

reader = new StringReader("{foo:'bar'}{baz:'qux'}");
pojo = session.parse(reader, JsonMap.class); // {foo:'bar'}
pojo = session.parse(reader, JsonMap.class); // {baz:'qux'}

reader = new StringReader("[123][456]");
pojo = session.parse(reader, int[].class); // [123]
pojo = session.parse(reader, int[].class); // [456]

Note that this isn't perfect in all cases since you can't combine two JSON numbers into a single reader (e.g. "123" + "456" = "123456").

note

For obvious reasons, do not use the following properties when reading continuous streams:

JsonParser.BuildervalidateEnd()UonParser.BuildervalidateEnd()Parser.BuildervalidateEnd()
note

The MsgPackParser class doesn't use any internal buffering to begin with, so it can be used with continuous streams without any special properties.