Skip to content

Commit 157dc08

Browse files
authored
Merge pull request #588 from Alanscut/decimal-test
add bigdecimal/biginteger test case for jackson
2 parents 1fc1784 + 60f77a9 commit 157dc08

File tree

1 file changed

+69
-1
lines changed

1 file changed

+69
-1
lines changed

json-path/src/test/java/com/jayway/jsonpath/JacksonJsonNodeJsonProviderTest.java

Lines changed: 69 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import com.fasterxml.jackson.annotation.JsonCreator;
44
import com.fasterxml.jackson.annotation.JsonProperty;
5+
import com.fasterxml.jackson.databind.DeserializationFeature;
56
import com.fasterxml.jackson.databind.JsonNode;
67
import com.fasterxml.jackson.databind.ObjectMapper;
78
import com.fasterxml.jackson.databind.SerializationFeature;
@@ -14,6 +15,8 @@
1415
import org.junit.Test;
1516

1617
import java.io.IOException;
18+
import java.math.BigDecimal;
19+
import java.math.BigInteger;
1720
import java.util.List;
1821
import java.util.UUID;
1922

@@ -90,7 +93,6 @@ public void longs_are_unwrapped() {
9093
assertThat(unwrapped).isEqualTo(node.asLong());
9194
}
9295

93-
9496
@Test
9597
public void list_of_numbers() {
9698
ArrayNode objs = using(JACKSON_JSON_NODE_CONFIGURATION).parse(JSON_DOCUMENT).read("$.store.book[*].display-price");
@@ -101,6 +103,72 @@ public void list_of_numbers() {
101103
assertThat(objs.get(3).asDouble()).isEqualTo(22.99D);
102104
}
103105

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+
104172
@Test
105173
public void test_type_ref() throws IOException {
106174
TypeRef<List<FooBarBaz<Gen>>> typeRef = new TypeRef<List<FooBarBaz<Gen>>>() {};

0 commit comments

Comments
 (0)