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.
When calling
valueToTreeon a mapper with enabledUSE_BIG_DECIMAL_FOR_FLOATS, it produces NumberFormatException while trying to convert NaN to BigDecimal.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.FLOATandNumberType.DOUBLEare tested for NaN/Infinity before callinggetDecimalValue(). If positive, USE_BIG_DECIMAL_FOR_FLOATS should be ignored.