JsonParser cannot getText() for input stream on MismatchedInputException
See original GitHub issueI’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:
- Created 4 years ago
- Comments:6 (4 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
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.
@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).