Skip to content

Confusing exception for DoubleNode to Integer conversion in Jackson 3 #5309

@caalador

Description

@caalador

Search before asking

  • I searched in the issues and found nothing similar.

Describe the bug

When having a DoubleNode with decimal value doing a ObjectMapper.treeToValue(doubleNode, Integer.class) in 2.x 2.718 results in 2 but in 3.x the fractional part is checked which fails the conversion to Integer.

The returned exception for the conversion in 3.x is:
tools.jackson.core.exc.InputCoercionException: Numeric value (2.718) out of range of int (-2147483648 - 2147483647)

This was confusing in that the value is easily inside the range and the issue is actually that the value has a fractional part marking the canConvertToInt as false due to it not being able to convertToExactIntegral.

To lessen the confusion of this conversion error it would be good if the exception would say clearly that it failed due to the fractional value being present and not because the value is out of range.

Version Information

3.0.0-rc9

Reproduction

@Test
public void double_to_int_conversion_failure {
    ObjectMapper mapper = new ObjectMapper();

    JsonNode jsonNode = mapper.valueToTree(2.718);

    mapper.treeToValue(jsonNode, Integer.class);
}

Expected behavior

The exception should highlight the actual reason for the failure.

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