Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

issue-1770 - big number not returned as DecimalNode #4191

Merged
merged 4 commits into from
Nov 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -755,12 +755,11 @@ protected final JsonNode _fromFloat(JsonParser p, DeserializationContext ctxt,
return _fromBigDecimal(ctxt, nodeFactory, p.getDecimalValue());
}
if (ctxt.isEnabled(DeserializationFeature.USE_BIG_DECIMAL_FOR_FLOATS)) {
// 20-May-2016, tatu: As per [databind#1028], need to be careful
// (note: JDK 1.8 would have `Double.isFinite()`)
if (p.isNaN()) {
return nodeFactory.numberNode(p.getDoubleValue());
try {
return _fromBigDecimal(ctxt, nodeFactory, p.getDecimalValue());
} catch (NumberFormatException nfe) {
// fall through - BigDecimal does not support values like NaN
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think this makes sense -- this is an error condition and we should report an error shouldn't we?
(existing code was wrong already, I think)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually, let me re-think -- should probably merge this as suggested but file a follow-up issue to consider making it fail as expected.

And possibly do that with configurable JsonNodeFeature setting.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Created #4194 as follow-up

}
return _fromBigDecimal(ctxt, nodeFactory, p.getDecimalValue());
}
if (nt == JsonParser.NumberType.FLOAT) {
return nodeFactory.numberNode(p.getFloatValue());
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package com.fasterxml.jackson.databind;
cowtowncoder marked this conversation as resolved.
Show resolved Hide resolved

import com.fasterxml.jackson.core.JsonFactory;
import com.fasterxml.jackson.core.json.JsonReadFeature;
import com.fasterxml.jackson.databind.json.JsonMapper;

import java.math.BigDecimal;

/**
* Basic tests for {@link JsonNode} implementations that
* contain numeric values.
*/
public class NumberNodes1770Test extends BaseMapTest
{
private final ObjectMapper MAPPER = newJsonMapper();

// For to [databind#1770] (broken due to fix for #1028): `JsonNodeDeserializer`
// would coerce ok but does `parser.isNaN()` check which ends up parsing
// as Double, gets `POSITIVE_INFINITY` and returns `true`: this results in
// `DoubleNode` being used even tho `BigDecimal` could fit the number.
public void testBigDecimalCoercion() throws Exception
{
final String value = "7976931348623157e309";
final JsonNode jsonNode = MAPPER.reader()
.with(DeserializationFeature.USE_BIG_DECIMAL_FOR_FLOATS)
.readTree(value);
assertTrue("Expected DecimalNode, got: "+jsonNode.getClass().getName()+": "+jsonNode, jsonNode.isBigDecimal());
assertEquals(new BigDecimal(value), jsonNode.decimalValue());
}

public void testBigDecimalCoercionInf() throws Exception
{
final String value = "+INF";
JsonFactory factory = JsonFactory.builder()
.enable(JsonReadFeature.ALLOW_NON_NUMERIC_NUMBERS)
.build();
final JsonNode jsonNode = new JsonMapper(factory).reader()
.with(DeserializationFeature.USE_BIG_DECIMAL_FOR_FLOATS)
.readTree(value);
assertTrue("Expected DoubleNode, got: "+jsonNode.getClass().getName()+": "+jsonNode, jsonNode.isDouble());
assertEquals(Double.POSITIVE_INFINITY, jsonNode.doubleValue());
}
}

This file was deleted.