|
1 | 1 | package com.fasterxml.jackson.databind.node;
|
2 | 2 |
|
| 3 | +import java.math.BigDecimal; |
| 4 | + |
3 | 5 | import org.junit.jupiter.api.Test;
|
4 | 6 |
|
5 | 7 | import com.fasterxml.jackson.databind.*;
|
@@ -156,4 +158,56 @@ public void testWriteSortedProperties() throws Exception
|
156 | 158 | /* Other features
|
157 | 159 | /**********************************************************************
|
158 | 160 | */
|
| 161 | + |
| 162 | + // [databind#4801] USE_BIG_DECIMAL_FOR_FLOATS |
| 163 | + @Test |
| 164 | + public void testBigDecimalForJsonNodeFeature() throws Exception { |
| 165 | + final String JSON = "0.1234567890123456789012345678912345"; // Precision-sensitive |
| 166 | + |
| 167 | + BigDecimal expectedBigDecimal = new BigDecimal("0.1234567890123456789012345678912345"); // Full precision |
| 168 | + BigDecimal expectedDoubleLossy = new BigDecimal("0.12345678901234568"); // Precision loss |
| 169 | + |
| 170 | + ObjectMapper mapper; |
| 171 | + |
| 172 | + // Case 1: Both enabled → Should use BigDecimal |
| 173 | + mapper = JsonMapper.builder() |
| 174 | + .enable(DeserializationFeature.USE_BIG_DECIMAL_FOR_FLOATS) |
| 175 | + .enable(JsonNodeFeature.USE_BIG_DECIMAL_FOR_FLOATS) |
| 176 | + .build(); |
| 177 | + assertEquals(expectedBigDecimal, mapper.readTree(JSON).decimalValue()); |
| 178 | + |
| 179 | + // Case 2: Global enabled, JsonNodeFeature disabled → Should use Double (truncated decimal) |
| 180 | + mapper = JsonMapper.builder() |
| 181 | + .enable(DeserializationFeature.USE_BIG_DECIMAL_FOR_FLOATS) |
| 182 | + .disable(JsonNodeFeature.USE_BIG_DECIMAL_FOR_FLOATS) |
| 183 | + .build(); |
| 184 | + assertEquals(expectedDoubleLossy, mapper.readTree(JSON).decimalValue()); |
| 185 | + |
| 186 | + // Case 3: Global enabled, JsonNodeFeature undefined → Should use BigDecimal (default to global) |
| 187 | + mapper = JsonMapper.builder() |
| 188 | + .enable(DeserializationFeature.USE_BIG_DECIMAL_FOR_FLOATS) |
| 189 | + .build(); |
| 190 | + assertEquals(expectedBigDecimal, mapper.readTree(JSON).decimalValue()); |
| 191 | + |
| 192 | + // Case 4: Global disabled, JsonNodeFeature enabled → Should use BigDecimal |
| 193 | + mapper = JsonMapper.builder() |
| 194 | + .disable(DeserializationFeature.USE_BIG_DECIMAL_FOR_FLOATS) |
| 195 | + .enable(JsonNodeFeature.USE_BIG_DECIMAL_FOR_FLOATS) |
| 196 | + .build(); |
| 197 | + assertEquals(expectedBigDecimal, mapper.readTree(JSON).decimalValue()); |
| 198 | + |
| 199 | + // Case 5: Both disabled → Should use Double (truncated decimal) |
| 200 | + mapper = JsonMapper.builder() |
| 201 | + .disable(DeserializationFeature.USE_BIG_DECIMAL_FOR_FLOATS) |
| 202 | + .disable(JsonNodeFeature.USE_BIG_DECIMAL_FOR_FLOATS) |
| 203 | + .build(); |
| 204 | + assertEquals(expectedDoubleLossy, mapper.readTree(JSON).decimalValue()); |
| 205 | + |
| 206 | + // Case 6: Global disabled, JsonNodeFeature undefined → Should use Double (default to global, truncated decimal) |
| 207 | + mapper = JsonMapper.builder() |
| 208 | + .disable(DeserializationFeature.USE_BIG_DECIMAL_FOR_FLOATS) |
| 209 | + .build(); |
| 210 | + assertEquals(expectedDoubleLossy, mapper.readTree(JSON).decimalValue()); |
| 211 | + } |
| 212 | + |
159 | 213 | }
|
0 commit comments