mirror of
https://github.com/fankes/moshi.git
synced 2025-10-20 00:19:21 +08:00
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:
@@ -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());
|
||||||
|
@@ -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() {
|
||||||
|
Reference in New Issue
Block a user