JsonParser from MismatchedInputException cannot getText() for floating-point value
See original GitHub issueI’ve stumbled upon another 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 a number to something that expects a boolean:
try {
objectMapper.readValue("{ \"value\": 1.0 }", 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_FLOAT
while I would expect parser.getText()
to return "1.0"
.
Related with https://github.com/FasterXML/jackson-databind/issues/2635. However that one was specifically for InputStream
s, where here it happens for both String
s and InputStream
s.
(Jackson 2.11.0)
Issue Analytics
- State:
- Created 3 years ago
- Comments:5 (3 by maintainers)
Top Results From Across the Web
Security update for jackson-databind, jackson-dataformats ...
UNWRAP_SINGLE_VALUE_ARRAYS' don't support 'Map' type field + JsonParser from MismatchedInputException cannot getText() for floating-point ...
Read more >When parsing JSON with java, how to getText() bounded by a ...
throws IOException { String nextAttr = jsonParser. nextFieldName(); if ("X-TIKA:content". equals(nextAttr)) { return jsonParser. getText(); } / ...
Read more >mismatchedinputexception: cannot deserialize value of type ...
FasterXML/jackson-databindJsonParser from MismatchedInputException cannot getText() for floating-point value#2770. Created over 2 years ago.
Read more >Jackson JSON Java Parser API Example Tutorial - DigitalOcean
Jackson JSON Parser API provides easy way to convert JSON to POJO Object and supports easy conversion to Map from JSON data. Jackson...
Read more >Class JsonParser - Adobe Developer
VALUE_NUMBER_INT and it can not be used as a Java long primitive type due to its magnitude ... Method similar to getText() ,...
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 Free
Top 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
Fixed for 2.11.1.
Thanks, makes sense. Code refactoring for 2.12 had actually resolved that for 2.12 branch, but I fixed it for 2.11. The general problem is tricky, however: there is no real cure for the fact that parser will get closed, buffers recycled. One thing that could help would be use of
DeserializationProblemHandler
, as that will get called before exception is throw, before parser gets called. Until then I can fix specific cases to force calling ofparser.getText()
before exception is thrown as that (and only that) will ensure thatString
forgetText()
is aggregated, retained, even if buffers will not be accessible any more.