Browse Source

Add BooleanReader to allow boolean primitive to be read

Resolves #280
pull/285/head
odlp 8 years ago
parent
commit
ba3e1196dc
  1. 15
      json-path/src/main/java/com/jayway/jsonpath/spi/mapper/JsonSmartMappingProvider.java
  2. 6
      json-path/src/test/java/com/jayway/jsonpath/MapperTest.java

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

@ -44,6 +44,7 @@ public class JsonSmartMappingProvider implements MappingProvider {
DEFAULT.registerReader(String.class, new StringReader());
DEFAULT.registerReader(Date.class, new DateReader());
DEFAULT.registerReader(BigInteger.class, new BigIntegerReader());
DEFAULT.registerReader(boolean.class, new BooleanReader());
}
@ -245,4 +246,18 @@ public class JsonSmartMappingProvider implements MappingProvider {
throw new MappingException("can not map a " + src.getClass() + " to " + Date.class.getName());
}
}
private static class BooleanReader extends JsonReaderI<Boolean> {
public BooleanReader() {
super(null);
}
public Boolean convert(Object src) {
if(src == null){
return null;
}
if (Boolean.class.isAssignableFrom(src.getClass())) {
return (Boolean) src;
}
throw new MappingException("can not map a " + src.getClass() + " to " + Boolean.class.getName());
}
}
}

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

@ -52,4 +52,10 @@ public class MapperTest extends BaseTest {
public void a_String_can_be_converted_to_a_BigDecimal() {
assertThat(parse("{\"val\": \"1.5\"}").read("val", BigDecimal.class)).isEqualTo(BigDecimal.valueOf(1.5d));
}
@Test
public void a_Boolean_can_be_converted_to_a_primitive_boolean() {
assertThat(parse("{\"val\": true}").read("val", boolean.class)).isTrue();
assertThat(parse("{\"val\": false}").read("val", boolean.class)).isFalse();
}
}

Loading…
Cancel
Save