Parsing a sequence of JSON values that are not enclosed in a JSON array
Joy is described as being:
Fully compliant with the latest specification of Jakarta JSON Processing API.
In the latest specification, the documentation for JsonParser says:
JsonParser can be used to parse sequence of JSON values that are not enclosed in a JSON array, e.g. { } { }. The following code demonstrates how to parse such sequence.
JsonParser parser = Json.createParser(...); while (parser.hasNext) { parser.next(); // advance parser state JsonValue value = parser.getValue(); }
The "following code" there is not quite syntactically correct, but i think the intent is clear.
However, the code in the documentation (my version), run on the input in the documentation, using Joy 2.1.0, does not work - it throws an exception:
jakarta.json.stream.JsonParsingException: [line=1,column=5,offset=4] Unexpected char '{' was found.
at org.leadpony.joy.core.BasicJsonParser.newUnexpectedCharException(BasicJsonParser.java:778)
at org.leadpony.joy.core.BasicJsonParser$State$2.accepts(BasicJsonParser.java:820)
at org.leadpony.joy.core.BasicJsonParser.hasNext(BasicJsonParser.java:97)
I have tried this with Glassfish Jaka 1.1.6 (not the latest), and it fails in much the same way.
Should it be possible to parse multiple top-level JSON values like this?
If so, is this a bug in Joy?
If not, is this a bug in the API documentation?
I have the same issue in both joy and Glassfish. My code produces a stream of JSON objects, which are consumed by a server using the JsonParser. The only workaround is to put the objects in a JSON array, but that is against some other standard that is defined for the object stream.
Any response on the above questions?
I just had a peek at the BasicJsonParser.java code. Parsing a series of JSON objects seems not be implemented.
The parser state machine starts in the state INITIAL, parses the first object (working through several substates), and transitions to the state FINISHED. From this state the exception is thrown.
To support a sequence of objects, the parser should transition to a NEXTOBJECT state (and parse the next object), rather than FINISHED.
I patched the file BasicJsonParser.java in my local devtool and commented out the transition to FINISHED:
private enum State {
INITIAL() {
@Override
boolean accepts(int c, BasicJsonParser parser) {
return c >= 0;
}
@Override
Event process(int c, BasicJsonParser parser) {
// parser.setState(FINISHED);
return parser.processValue(c);
}
},
So the parser remains in the INITIAL state, and subsequent Json values can be parsed. I successfully tested this against an object stream from my server.
If the fix is so simple, can it be rolled into a next release soon?