Skip to content

Ignore USE_BIG_DECIMAL_FOR_FLOATS for NaN/Infinity #1028

@lightoze

Description

@lightoze

When calling valueToTree on a mapper with enabled USE_BIG_DECIMAL_FOR_FLOATS, it produces NumberFormatException while trying to convert NaN to BigDecimal.

ObjectMapper m = new ObjectMapper();
m.enable(DeserializationFeature.USE_BIG_DECIMAL_FOR_FLOATS);
Object o = new Object() {
    private double x = Double.NaN;

    public double getX() {
        return x;
    }

    public void setX(double x) {
        this.x = x;
    }
};
JsonNode tree = m.valueToTree(o);
System.out.println(tree);

In my data I need BigDecimal in some places where exact precision is required. Because Jackson does not automatically use BigDecimal for values which will not fit into Double (like it does for integers), I have to enable USE_BIG_DECIMAL_FOR_FLOATS, but still have to use doubles (which can be NaN) in other parts of the data structure.

I suggest that tokens with NumberType.FLOAT and NumberType.DOUBLE are tested for NaN/Infinity before calling getDecimalValue(). If positive, USE_BIG_DECIMAL_FOR_FLOATS should be ignored.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions