Merge pull request #80 from serj-lotutovici/sl/dont_fail_on_null_values

ObjectJsonAdapter should not fail on null values.
This commit is contained in:
Jesse Wilson
2015-09-17 07:06:02 -04:00
2 changed files with 14 additions and 0 deletions

View File

@@ -239,6 +239,9 @@ final class StandardJsonAdapters {
case BOOLEAN: case BOOLEAN:
return reader.nextBoolean(); return reader.nextBoolean();
case NULL:
return reader.nextNull();
default: default:
throw new IllegalStateException("Expected a value but was " + reader.peek() throw new IllegalStateException("Expected a value but was " + reader.peek()
+ " at path " + reader.getPath()); + " at path " + reader.getPath());

View File

@@ -84,6 +84,17 @@ public final class ObjectAdapterTest {
assertThat(adapter.fromJson("[0, 1]")).isEqualTo(Arrays.asList(0d, 1d)); assertThat(adapter.fromJson("[0, 1]")).isEqualTo(Arrays.asList(0d, 1d));
} }
@Test public void fromJsonDoesNotFailOnNullValues() throws Exception {
Map<Object, Object> emptyDelivery = new LinkedHashMap<>();
emptyDelivery.put("address", null);
emptyDelivery.put("items", null);
Moshi moshi = new Moshi.Builder().build();
JsonAdapter<Object> adapter = moshi.adapter(Object.class);
assertThat(adapter.fromJson("{\"address\":null, \"items\":null}"))
.isEqualTo(emptyDelivery);
}
@Test public void toJsonCoercesRuntimeTypeForCollections() throws Exception { @Test public void toJsonCoercesRuntimeTypeForCollections() throws Exception {
Collection<String> collection = new AbstractCollection<String>() { Collection<String> collection = new AbstractCollection<String>() {
@Override public Iterator<String> iterator() { @Override public Iterator<String> iterator() {