From f077bbb97d7bec43ad1f5f095b4d328b5370bcc0 Mon Sep 17 00:00:00 2001 From: Kevin Conaway Date: Wed, 2 Mar 2016 15:49:04 -0500 Subject: [PATCH] #200 Add support for BigInteger to JsonSmartMappingProvider --- .../spi/mapper/JsonSmartMappingProvider.java | 14 ++++++++++++++ .../test/java/com/jayway/jsonpath/MapperTest.java | 10 ++++++++++ 2 files changed, 24 insertions(+) diff --git a/json-path/src/main/java/com/jayway/jsonpath/spi/mapper/JsonSmartMappingProvider.java b/json-path/src/main/java/com/jayway/jsonpath/spi/mapper/JsonSmartMappingProvider.java index 6ba6ee45..5d402080 100644 --- a/json-path/src/main/java/com/jayway/jsonpath/spi/mapper/JsonSmartMappingProvider.java +++ b/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 { public DoubleReader() { super(null); @@ -209,6 +212,17 @@ public class JsonSmartMappingProvider implements MappingProvider { return new BigDecimal(src.toString()); } } + private static class BigIntegerReader extends JsonReaderI { + 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 { public DateReader() { super(null); diff --git a/json-path/src/test/java/com/jayway/jsonpath/MapperTest.java b/json-path/src/test/java/com/jayway/jsonpath/MapperTest.java index 023ccc68..3407a88d 100644 --- a/json-path/src/test/java/com/jayway/jsonpath/MapperTest.java +++ b/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)); + } }