2
2
3
3
import com .fasterxml .jackson .annotation .JsonCreator ;
4
4
import com .fasterxml .jackson .annotation .JsonProperty ;
5
+ import com .fasterxml .jackson .databind .DeserializationFeature ;
5
6
import com .fasterxml .jackson .databind .JsonNode ;
6
7
import com .fasterxml .jackson .databind .ObjectMapper ;
7
8
import com .fasterxml .jackson .databind .SerializationFeature ;
14
15
import org .junit .Test ;
15
16
16
17
import java .io .IOException ;
18
+ import java .math .BigDecimal ;
19
+ import java .math .BigInteger ;
17
20
import java .util .List ;
18
21
import java .util .UUID ;
19
22
@@ -90,7 +93,6 @@ public void longs_are_unwrapped() {
90
93
assertThat (unwrapped ).isEqualTo (node .asLong ());
91
94
}
92
95
93
-
94
96
@ Test
95
97
public void list_of_numbers () {
96
98
ArrayNode objs = using (JACKSON_JSON_NODE_CONFIGURATION ).parse (JSON_DOCUMENT ).read ("$.store.book[*].display-price" );
@@ -101,6 +103,72 @@ public void list_of_numbers() {
101
103
assertThat (objs .get (3 ).asDouble ()).isEqualTo (22.99D );
102
104
}
103
105
106
+ ObjectMapper objectMapperDecimal = new ObjectMapper ().configure (DeserializationFeature .USE_BIG_DECIMAL_FOR_FLOATS , true );
107
+ Configuration JACKSON_JSON_NODE_CONFIGURATION_DECIMAL = Configuration
108
+ .builder ()
109
+ .mappingProvider (new JacksonMappingProvider ())
110
+ .jsonProvider (new JacksonJsonNodeJsonProvider (objectMapperDecimal ))
111
+ .build ();
112
+
113
+ @ Test
114
+ public void bigdecimals_are_unwrapped () {
115
+ final BigDecimal bd = BigDecimal .valueOf (Long .MAX_VALUE ).add (BigDecimal .valueOf (10.5 ));
116
+ final String json = "{\" bd-property\" : " + bd .toString () + "}" ;
117
+
118
+ JsonNode node = using (JACKSON_JSON_NODE_CONFIGURATION_DECIMAL ).parse (json ).read ("$.bd-property" );
119
+ BigDecimal val = using (JACKSON_JSON_NODE_CONFIGURATION_DECIMAL ).parse (json ).read ("$.bd-property" , BigDecimal .class );
120
+
121
+ assertThat (node .isBigDecimal ()).isTrue ();
122
+ assertThat (val ).isEqualTo (bd );
123
+ assertThat (val ).isEqualTo (node .decimalValue ());
124
+ }
125
+
126
+ @ Test
127
+ public void small_bigdecimals_are_unwrapped () {
128
+ final BigDecimal bd = BigDecimal .valueOf (10.5 );
129
+ final String json = "{\" bd-property\" : " + bd .toString () + "}" ;
130
+
131
+ JsonNode node = using (JACKSON_JSON_NODE_CONFIGURATION_DECIMAL ).parse (json ).read ("$.bd-property" );
132
+ BigDecimal val = using (JACKSON_JSON_NODE_CONFIGURATION_DECIMAL ).parse (json ).read ("$.bd-property" , BigDecimal .class );
133
+
134
+ assertThat (node .isBigDecimal ()).isTrue ();
135
+ assertThat (val ).isEqualTo (bd );
136
+ assertThat (val ).isEqualTo (node .decimalValue ());
137
+ }
138
+
139
+ ObjectMapper objectMapperBigInteger = new ObjectMapper ().configure (DeserializationFeature .USE_BIG_INTEGER_FOR_INTS , true );
140
+ Configuration JACKSON_JSON_NODE_CONFIGURATION_Big_Integer = Configuration
141
+ .builder ()
142
+ .mappingProvider (new JacksonMappingProvider ())
143
+ .jsonProvider (new JacksonJsonNodeJsonProvider (objectMapperBigInteger ))
144
+ .build ();
145
+
146
+ @ Test
147
+ public void bigintegers_are_unwrapped () {
148
+ final BigInteger bi = BigInteger .valueOf (Long .MAX_VALUE ).add (BigInteger .TEN );
149
+ final String json = "{\" bi-property\" : " + bi .toString () + "}" ;
150
+
151
+ JsonNode node = using (JACKSON_JSON_NODE_CONFIGURATION_Big_Integer ).parse (json ).read ("$.bi-property" );
152
+ BigInteger val = using (JACKSON_JSON_NODE_CONFIGURATION_Big_Integer ).parse (json ).read ("$.bi-property" , BigInteger .class );
153
+
154
+ assertThat (node .isBigInteger ()).isTrue ();
155
+ assertThat (val ).isEqualTo (bi );
156
+ assertThat (val ).isEqualTo (node .bigIntegerValue ());
157
+ }
158
+
159
+ @ Test
160
+ public void small_bigintegers_are_unwrapped () {
161
+ final BigInteger bi = BigInteger .valueOf (Long .MAX_VALUE );
162
+ final String json = "{\" bi-property\" : " + bi .toString () + "}" ;
163
+
164
+ JsonNode node = using (JACKSON_JSON_NODE_CONFIGURATION_Big_Integer ).parse (json ).read ("$.bi-property" );
165
+ BigInteger val = using (JACKSON_JSON_NODE_CONFIGURATION_Big_Integer ).parse (json ).read ("$.bi-property" , BigInteger .class );
166
+
167
+ assertThat (node .isBigInteger ()).isTrue ();
168
+ assertThat (val ).isEqualTo (bi );
169
+ assertThat (val ).isEqualTo (node .bigIntegerValue ());
170
+ }
171
+
104
172
@ Test
105
173
public void test_type_ref () throws IOException {
106
174
TypeRef <List <FooBarBaz <Gen >>> typeRef = new TypeRef <List <FooBarBaz <Gen >>>() {};
0 commit comments