question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

JsonParser cannot getText() for input stream on MismatchedInputException

See original GitHub issue

I’ve stumbled upon a situation in which I’m not able to get the textual representation of the current token.

I have the following class:

public class Test {
    public Boolean value;
    public Boolean getValue() { return value; }
    public void setValue(Boolean value) { this.value = value; }
}

And the following object mapper:

ObjectMapper objectMapper = new ObjectMapper();
objectMapper.disable(MapperFeature.ALLOW_COERCION_OF_SCALARS);

I’m trying to deserialize something that would result in a MismatchedInputException. I’m providing an integer to something that should be a boolean:

try {
    objectMapper.readValue("{ \"value\": 1 }", Test.class);
} catch (MismatchedInputException exception) {
    JsonParser parser = (JsonParser) exception.getProcessor();
    System.out.println(parser.currentToken());
    System.out.println(parser.getText());
} catch (Exception exception) {}

This results in:

VALUE_NUMBER_INT
1

as expected.

What’s the use case for this you might ask? Whenever the clients of my service send me something incorrect I want to be able to give them detailed information about what went wrong. For example, “I was expecting a boolean but you gave me 1, which is an integer.”.

Now, if I use an input stream instead of a string, I can no longer access the text and get a null instead:

objectMapper.readValue(new ByteArrayInputStream("{ \"value\": 1 }".getBytes()), Test.class);
VALUE_NUMBER_INT

On Jackson 2.10.2.

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:6 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
cowtowncodercommented, Mar 8, 2020

So, the actual fix is for:

https://github.com/FasterXML/jackson-core/issues/606

and takes care of this problem. Will go in 2.11.0.

0reactions
cowtowncodercommented, Jun 16, 2020

@joca-bt Please file another one, easier to track – you can refer to this issue as background (to avoid some explanation).

And yes, it is quite possible there are other cases where this won’t work as it is unfortunately not something that can be solved in general fashion (to retain content, it must be accessed before input is auto-closed).

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to get the underlying String from a JsonParser (Jackson ...
I cannot pull it out of the InputStream because it doesn't support resetting and the creation of the JsonParser consumes it. Is there...
Read more >
Security update for jackson-databind, jackson-dataformats ...
handleMissingInstantiator()' throws 'MismatchedInputException' for ... on serialization + JsonParser cannot getText() for input stream on ...
Read more >
DeserializationContext.wrongTokenException - Java - Tabnine
Method for finding a deserializer for root-level value. weirdStringException. Helper method for constructing exception to indicate that input JSON String was ...
Read more >
mismatchedinputexception: cannot deserialize value of type ...
By default Jackson creates instance of any class using default constructor and setter / getter method. Since your bean is immutable i.e. no...
Read more >
Jackson - Quick Guide - Tutorialspoint
JsonParser reads the data whereas JsonGenerator writes the data. ... 102, JsonNode readTree(InputStream in) - Method to deserialize JSON content as tree ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found