Skip to content

"Unexpected end-of-input" for JsonParser.readValueAs() (in 3.0) #5344

@philsttr

Description

@philsttr

Search before asking

  • I searched in the issues and found nothing similar.

Describe the bug

In Jackson 3.0.0, parsing a simple JSON Object, such as the following:

{
    "key" : "value"
}

as a Map<String, String> via parser.readValueAs(new TypeReference<Map<String, String>>() { })

fails with the following exception:

tools.jackson.databind.exc.MismatchedInputException: Unexpected end-of-input when trying read value of type `java.util.LinkedHashMap<java.lang.String,java.lang.String>`
 at [Source: (String)"{
    "key" : "value"
}
"; line: 1, column: 0]

	at tools.jackson.databind.exc.MismatchedInputException.from(MismatchedInputException.java:63)
	at tools.jackson.databind.DeserializationContext.reportInputMismatch(DeserializationContext.java:1822)
	at tools.jackson.databind.DeserializationContext.handleUnexpectedToken(DeserializationContext.java:1618)
	at tools.jackson.databind.DeserializationContext.handleUnexpectedToken(DeserializationContext.java:1567)
	at tools.jackson.databind.deser.jdk.MapDeserializer.deserialize(MapDeserializer.java:439)
	at tools.jackson.databind.deser.jdk.MapDeserializer.deserialize(MapDeserializer.java:30)
	at tools.jackson.databind.DeserializationContext._readValue(DeserializationContext.java:403)
	at tools.jackson.databind.DeserializationContext.readValue(DeserializationContext.java:391)
	at tools.jackson.databind.DeserializationContext.readValue(DeserializationContext.java:372)
	at tools.jackson.core.base.ParserMinimalBase.readValueAs(ParserMinimalBase.java:815)
	at Jackson3ParserTest.testJackson3Parser(Jackson3ParserTest.java:26)

It works with Jackson 2.20.0

Version Information

Does not work with Jackson 3.0.0
Works with Jackson 2.20.0

Reproduction

I have attached a simple project (jackson-parser-test.zip) with unit tests for Jackson 2 (works) and Jackson 3 (does not work).

The unit test that fails for Jackson 3 looks like this:

import org.junit.jupiter.api.Test;
import tools.jackson.core.JsonParser;
import tools.jackson.core.StreamReadFeature;
import tools.jackson.core.type.TypeReference;
import tools.jackson.databind.json.JsonMapper;

import java.util.Map;

import static org.junit.jupiter.api.Assertions.assertEquals;

public class Jackson3ParserTest {

    @Test
    void testJackson3Parser() {
        String jsonText = """
                {
                    "key" : "value"
                }
                """;

        JsonMapper jsonMapper = JsonMapper.builder()
                .enable(StreamReadFeature.INCLUDE_SOURCE_IN_LOCATION)
                .build();

        try (JsonParser parser = jsonMapper.createParser(jsonText)) {
            Map<String, String> result = parser.readValueAs(new TypeReference<Map<String, String>>() { });
            assertEquals("value", result.get("key"));
        }
    }
}

Expected behavior

Expect the call to parser.readValueAs(new TypeReference<Map<String, String>>() { }) to not throw an exception, and return a Map<String, String> object populated with the correct entries.

Additional context

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    3.0Issue planned for initial 3.0 release

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions