Browse Source

#200 Add support for BigInteger to JsonSmartMappingProvider

pull/201/head
Kevin Conaway 9 years ago
parent
commit
f077bbb97d
  1. 14
      json-path/src/main/java/com/jayway/jsonpath/spi/mapper/JsonSmartMappingProvider.java
  2. 10
      json-path/src/test/java/com/jayway/jsonpath/MapperTest.java

14
json-path/src/main/java/com/jayway/jsonpath/spi/mapper/JsonSmartMappingProvider.java

@ -21,6 +21,7 @@ import net.minidev.json.writer.JsonReader;
import net.minidev.json.writer.JsonReaderI;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.text.DateFormat;
import java.text.ParseException;
import java.util.Date;
@ -42,6 +43,7 @@ public class JsonSmartMappingProvider implements MappingProvider {
DEFAULT.registerReader(BigDecimal.class, new BigDecimalReader());
DEFAULT.registerReader(String.class, new StringReader());
DEFAULT.registerReader(Date.class, new DateReader());
DEFAULT.registerReader(BigInteger.class, new BigIntegerReader());
}
@ -150,6 +152,7 @@ public class JsonSmartMappingProvider implements MappingProvider {
throw new MappingException("can not map a " + src.getClass() + " to " + Long.class.getName());
}
}
private static class DoubleReader extends JsonReaderI<Double> {
public DoubleReader() {
super(null);
@ -209,6 +212,17 @@ public class JsonSmartMappingProvider implements MappingProvider {
return new BigDecimal(src.toString());
}
}
private static class BigIntegerReader extends JsonReaderI<BigInteger> {
public BigIntegerReader() {
super(null);
}
public BigInteger convert(Object src) {
if(src == null){
return null;
}
return new BigInteger(src.toString());
}
}
private static class DateReader extends JsonReaderI<Date> {
public DateReader() {
super(null);

10
json-path/src/test/java/com/jayway/jsonpath/MapperTest.java

@ -2,6 +2,8 @@ package com.jayway.jsonpath;
import org.junit.Test;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.Date;
import static com.jayway.jsonpath.JsonPath.parse;
@ -41,5 +43,13 @@ public class MapperTest extends BaseTest {
assertThat(parse("{\"val\": "+now.getTime()+"}").read("val", Date.class)).isEqualTo(now);
}
@Test
public void a_String_can_be_converted_to_a_BigInteger() {
assertThat(parse("{\"val\": \"1\"}").read("val", BigInteger.class)).isEqualTo(BigInteger.valueOf(1));
}
@Test
public void a_String_can_be_converted_to_a_BigDecimal() {
assertThat(parse("{\"val\": \"1.5\"}").read("val", BigDecimal.class)).isEqualTo(BigDecimal.valueOf(1.5d));
}
}

Loading…
Cancel
Save