|
| 1 | +package com.fasterxml.jackson.databind; |
| 2 | + |
| 3 | +import com.fasterxml.jackson.core.JsonFactory; |
| 4 | +import com.fasterxml.jackson.core.json.JsonReadFeature; |
| 5 | +import com.fasterxml.jackson.databind.json.JsonMapper; |
| 6 | + |
| 7 | +import java.math.BigDecimal; |
| 8 | + |
| 9 | +/** |
| 10 | + * Basic tests for {@link JsonNode} implementations that |
| 11 | + * contain numeric values. |
| 12 | + */ |
| 13 | +public class NumberNodes1770Test extends BaseMapTest |
| 14 | +{ |
| 15 | + private final ObjectMapper MAPPER = newJsonMapper(); |
| 16 | + |
| 17 | + // For to [databind#1770] (broken due to fix for #1028): `JsonNodeDeserializer` |
| 18 | + // would coerce ok but does `parser.isNaN()` check which ends up parsing |
| 19 | + // as Double, gets `POSITIVE_INFINITY` and returns `true`: this results in |
| 20 | + // `DoubleNode` being used even tho `BigDecimal` could fit the number. |
| 21 | + public void testBigDecimalCoercion() throws Exception |
| 22 | + { |
| 23 | + final String value = "7976931348623157e309"; |
| 24 | + final JsonNode jsonNode = MAPPER.reader() |
| 25 | + .with(DeserializationFeature.USE_BIG_DECIMAL_FOR_FLOATS) |
| 26 | + .readTree(value); |
| 27 | + assertTrue("Expected DecimalNode, got: "+jsonNode.getClass().getName()+": "+jsonNode, jsonNode.isBigDecimal()); |
| 28 | + assertEquals(new BigDecimal(value), jsonNode.decimalValue()); |
| 29 | + } |
| 30 | + |
| 31 | + public void testBigDecimalCoercionInf() throws Exception |
| 32 | + { |
| 33 | + final String value = "+INF"; |
| 34 | + JsonFactory factory = JsonFactory.builder() |
| 35 | + .enable(JsonReadFeature.ALLOW_NON_NUMERIC_NUMBERS) |
| 36 | + .build(); |
| 37 | + final JsonNode jsonNode = new JsonMapper(factory).reader() |
| 38 | + .with(DeserializationFeature.USE_BIG_DECIMAL_FOR_FLOATS) |
| 39 | + .readTree(value); |
| 40 | + assertTrue("Expected DoubleNode, got: "+jsonNode.getClass().getName()+": "+jsonNode, jsonNode.isDouble()); |
| 41 | + assertEquals(Double.POSITIVE_INFINITY, jsonNode.doubleValue()); |
| 42 | + } |
| 43 | +} |
0 commit comments