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 from MismatchedInputException cannot getText() for floating-point value

See original GitHub issue

I’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 InputStreams, where here it happens for both Strings and InputStreams.

(Jackson 2.11.0)

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
cowtowncodercommented, Jun 18, 2020

Fixed for 2.11.1.

0reactions
cowtowncodercommented, Jun 19, 2020

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 of parser.getText() before exception is thrown as that (and only that) will ensure that String for getText() is aggregated, retained, even if buffers will not be accessible any more.

Read more comments on GitHub >

github_iconTop 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 >

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