eredisg
eredisg

Reputation: 23

Unexpected end-of-input: expected close marker

I am attempting to parse JSON using the Jackson v2.9.9 Streaming API and I get the follow stack trace:

com.fasterxml.jackson.core.io.JsonEOFException: Unexpected end-of-input: expected close marker for Object (start marker at [Source: (StringReader); line: 1, column: 1])
 at [Source: (StringReader); line: 1, column: 8001]
        at com.fasterxml.jackson.core.base.ParserMinimalBase._reportInvalidEOF(ParserMinimalBase.java:618)
        at com.fasterxml.jackson.core.base.ParserBase._handleEOF(ParserBase.java:485)
        at com.fasterxml.jackson.core.base.ParserBase._eofAsNextChar(ParserBase.java:497)
        at com.fasterxml.jackson.core.json.ReaderBasedJsonParser._skipWSOrEnd(ReaderBasedJsonParser.java:2340)
        at com.fasterxml.jackson.core.json.ReaderBasedJsonParser.nextToken(ReaderBasedJsonParser.java:656)

I have downgraded Jackson down to 2.8 and I still receive the error, verified that the input JSON is valid, and do null checks on the code.

JSON:

{"name":"name", "score":100, "output":[],"images":[],"description":"This is a description"}

Code:

try {
    JsonFactory factory = new JsonFactory();
    String jsonString = "{\"name\":\"name\", \"score\":100, \"output\":[],\"images\":[],\"description\":\"This is a description\"}";
    JsonParser parser = factory.createParser(jsonString);
    JsonToken nextToken = parser.nextToken();
    while (nextToken != null) {

        JsonToken token = parser.getCurrentToken();

        if (parser.getCurrentName() != null) {
            System.out.println("Current Token 1: " + parser.getCurrentName());
        }

        if (token.equals(JsonToken.FIELD_NAME)) {
            String fieldName = parser.getCurrentName();
            parser.nextToken();
            if (parser.getCurrentName() != null) {
                System.out.println("Current Token 2: " + parser.getCurrentName());
            }
            while (parser.nextToken() != JsonToken.END_OBJECT) {
                parser.nextToken();
                if (parser.getCurrentName().equals("name")) {
                    System.out.println("Name: " + parser.getValueAsString());
                }
            }

        }
        parser.close();
    }
} catch (IOException ex) {
    Logger.getLogger(NewIssueExporter.class.getName()).log(Level.SEVERE, null, ex);
}

It errors out on line 4:

while(nextToken != null) {

I expect for all the debug messages to print, but they do not after it errors.

Upvotes: 0

Views: 6655

Answers (1)

Michał Ziober
Michał Ziober

Reputation: 38690

When you parse valid JSON you need to think about it like about state machine. You found name - it means: next will be value, etc. You can simplify your code to:

JsonFactory factory = new JsonFactory();
String jsonString = "{\"name\":\"name\", \"score\":100, \"output\":[1,2],\"images\":[\"image1\",\"image2\"],\"description\":\"This is a description\"}";
JsonParser parser = factory.createParser(jsonString);
JsonToken token;
while ((token = parser.nextToken()) != JsonToken.END_OBJECT) {
    if (token == JsonToken.FIELD_NAME) {
        // Found name, lets read value
        System.out.print(parser.getCurrentName() + " = ");
        token = parser.nextToken();
        if (token == JsonToken.VALUE_STRING) {
            System.out.println(parser.getValueAsString());
        } else if (token == JsonToken.VALUE_NUMBER_INT) {
            System.out.println(parser.getValueAsInt());
        } else if (token == JsonToken.START_ARRAY) {
            // Found start array, lets read it all
            System.out.print("[");
            while (parser.nextToken() != JsonToken.END_ARRAY) {
                System.out.print(parser.getText() + ", ");
            }
            System.out.println("]");
        }
    }
}
parser.close();

See also:

Upvotes: 1

Related Questions