|
| 1 | +package com.fasterxml.jackson.databind.deser; |
| 2 | + |
| 3 | +import com.fasterxml.jackson.core.JsonFactory; |
| 4 | +import com.fasterxml.jackson.core.StreamReadConstraints; |
| 5 | +import com.fasterxml.jackson.databind.BaseMapTest; |
| 6 | +import com.fasterxml.jackson.databind.JsonMappingException; |
| 7 | +import com.fasterxml.jackson.databind.ObjectMapper; |
| 8 | +import com.fasterxml.jackson.databind.json.JsonMapper; |
| 9 | + |
| 10 | +import java.math.BigDecimal; |
| 11 | +import java.math.BigInteger; |
| 12 | + |
| 13 | +public class TestBigNumbers extends BaseMapTest |
| 14 | +{ |
| 15 | + static class BigDecimalWrapper { |
| 16 | + BigDecimal number; |
| 17 | + |
| 18 | + public BigDecimalWrapper() {} |
| 19 | + |
| 20 | + public BigDecimalWrapper(BigDecimal number) { |
| 21 | + this.number = number; |
| 22 | + } |
| 23 | + |
| 24 | + public void setNumber(BigDecimal number) { |
| 25 | + this.number = number; |
| 26 | + } |
| 27 | + } |
| 28 | + |
| 29 | + static class BigIntegerWrapper { |
| 30 | + BigInteger number; |
| 31 | + |
| 32 | + public BigIntegerWrapper() {} |
| 33 | + |
| 34 | + public BigIntegerWrapper(BigInteger number) { |
| 35 | + this.number = number; |
| 36 | + } |
| 37 | + |
| 38 | + public void setNumber(BigInteger number) { |
| 39 | + this.number = number; |
| 40 | + } |
| 41 | + } |
| 42 | + |
| 43 | + /* |
| 44 | + /********************************************************** |
| 45 | + /* Tests |
| 46 | + /********************************************************** |
| 47 | + */ |
| 48 | + |
| 49 | + private final ObjectMapper MAPPER = newJsonMapper(); |
| 50 | + |
| 51 | + private final ObjectMapper newJsonMapperWithUnlimitedNumberSizeSupport() { |
| 52 | + JsonFactory jsonFactory = JsonFactory.builder() |
| 53 | + .streamReadConstraints(StreamReadConstraints.builder().maxNumberLength(Integer.MAX_VALUE).build()) |
| 54 | + .build(); |
| 55 | + return JsonMapper.builder(jsonFactory).build(); |
| 56 | + } |
| 57 | + |
| 58 | + public void testDouble() throws Exception |
| 59 | + { |
| 60 | + try { |
| 61 | + MAPPER.readValue(generateJson("d"), DoubleWrapper.class); |
| 62 | + } catch (JsonMappingException jsonMappingException) { |
| 63 | + assertTrue("unexpected exception message: " + jsonMappingException.getMessage(), |
| 64 | + jsonMappingException.getMessage().startsWith("Malformed numeric value ([number with 1200 characters])")); |
| 65 | + } |
| 66 | + } |
| 67 | + |
| 68 | + public void testDoubleUnlimited() throws Exception |
| 69 | + { |
| 70 | + DoubleWrapper dw = |
| 71 | + newJsonMapperWithUnlimitedNumberSizeSupport().readValue(generateJson("d"), DoubleWrapper.class); |
| 72 | + assertNotNull(dw); |
| 73 | + } |
| 74 | + |
| 75 | + public void testBigDecimal() throws Exception |
| 76 | + { |
| 77 | + try { |
| 78 | + MAPPER.readValue(generateJson("number"), BigDecimalWrapper.class); |
| 79 | + } catch (JsonMappingException jsonMappingException) { |
| 80 | + assertTrue("unexpected exception message: " + jsonMappingException.getMessage(), |
| 81 | + jsonMappingException.getMessage().startsWith("Malformed numeric value ([number with 1200 characters])")); |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + public void testBigDecimalUnlimited() throws Exception |
| 86 | + { |
| 87 | + BigDecimalWrapper bdw = |
| 88 | + newJsonMapperWithUnlimitedNumberSizeSupport() |
| 89 | + .readValue(generateJson("number"), BigDecimalWrapper.class); |
| 90 | + assertNotNull(bdw); |
| 91 | + } |
| 92 | + |
| 93 | + public void testBigInteger() throws Exception |
| 94 | + { |
| 95 | + try { |
| 96 | + MAPPER.readValue(generateJson("number"), BigIntegerWrapper.class); |
| 97 | + } catch (JsonMappingException jsonMappingException) { |
| 98 | + assertTrue("unexpected exception message: " + jsonMappingException.getMessage(), |
| 99 | + jsonMappingException.getMessage().startsWith("Malformed numeric value ([number with 1200 characters])")); |
| 100 | + } |
| 101 | + } |
| 102 | + |
| 103 | + public void testBigIntegerUnlimited() throws Exception |
| 104 | + { |
| 105 | + BigIntegerWrapper bdw = |
| 106 | + newJsonMapperWithUnlimitedNumberSizeSupport() |
| 107 | + .readValue(generateJson("number"), BigIntegerWrapper.class); |
| 108 | + assertNotNull(bdw); |
| 109 | + } |
| 110 | + |
| 111 | + private String generateJson(final String fieldName) { |
| 112 | + final int len = 1200; |
| 113 | + final StringBuilder sb = new StringBuilder(); |
| 114 | + sb.append("{\"") |
| 115 | + .append(fieldName) |
| 116 | + .append("\": "); |
| 117 | + for (int i = 0; i < len; i++) { |
| 118 | + sb.append(1); |
| 119 | + } |
| 120 | + sb.append("}"); |
| 121 | + return sb.toString(); |
| 122 | + } |
| 123 | +} |
0 commit comments