Skip to content

Commit 9ced6e6

Browse files
committed
Add test wrt #3651
1 parent 9534a97 commit 9ced6e6

File tree

2 files changed

+26
-2
lines changed

2 files changed

+26
-2
lines changed

src/main/java/com/fasterxml/jackson/databind/node/JsonNodeFactory.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -278,7 +278,6 @@ public ValueNode numberNode(BigDecimal v)
278278
if (v == null) {
279279
return nullNode();
280280
}
281-
282281
/*
283282
* If the user wants the exact representation of this big decimal,
284283
* return the value directly

src/test/java/com/fasterxml/jackson/databind/node/JsonNodeFactoryTest.java

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,9 @@
99

1010
public class JsonNodeFactoryTest extends NodeTestBase
1111
{
12-
private final ObjectMapper MAPPER = objectMapper();
12+
private final ObjectMapper MAPPER = JsonMapper.builder()
13+
.enable(DeserializationFeature.USE_BIG_DECIMAL_FOR_FLOATS)
14+
.build();
1315

1416
static class SortingNodeFactory extends JsonNodeFactory {
1517
private static final long serialVersionUID = 1L;
@@ -72,4 +74,27 @@ public void testSortingObjectNode() throws Exception
7274
assertEquals(BIGGER_OUTPUT,
7375
MAPPER.writeValueAsString(mapper.readTree(BIGGER_INPUT)));
7476
}
77+
78+
// 06-Nov-2022, tatu: Wasn't being tests, oddly enough
79+
public void testBigDecimalNormalization() throws Exception
80+
{
81+
final BigDecimal NON_NORMALIZED = new BigDecimal("12.5000");
82+
final BigDecimal NORMALIZED = NON_NORMALIZED.stripTrailingZeros();
83+
84+
// By default, 2.x WILL normalize
85+
JsonNode n1 = MAPPER.readTree(String.valueOf(NON_NORMALIZED));
86+
assertEquals(NORMALIZED, n1.decimalValue());
87+
88+
// But can change
89+
JsonNodeFactory nf = JsonNodeFactory.withExactBigDecimals(true);
90+
JsonNode n2 = nf.numberNode(NON_NORMALIZED);
91+
assertEquals(NON_NORMALIZED, n2.decimalValue());
92+
93+
ObjectMapper nonNormMapper = JsonMapper.builder()
94+
.enable(DeserializationFeature.USE_BIG_DECIMAL_FOR_FLOATS)
95+
.nodeFactory(nf)
96+
.build();
97+
JsonNode n3 = nonNormMapper.readTree(String.valueOf(NON_NORMALIZED));
98+
assertEquals(NON_NORMALIZED, n3.decimalValue());
99+
}
75100
}

0 commit comments

Comments
 (0)