Skip to content

Commit

Permalink
fix google#680 - make RuntimeTypeAdapterFactory null-safe
Browse files Browse the repository at this point in the history
  • Loading branch information
lcoote committed Aug 5, 2015
1 parent 77e31ed commit 1867457
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,6 @@ public <R> TypeAdapter<R> create(Gson gson, TypeToken<R> type) {
}
Streams.write(clone, out);
}
};
}.nullSafe();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,25 @@ public void testSerializeCollidingTypeFieldName() {
}
}

public void testSerializeWrappedNullValue() {
TypeAdapterFactory billingAdapter = RuntimeTypeAdapterFactory.of(BillingInstrument.class)
.registerSubtype(CreditCard.class)
.registerSubtype(BankTransfer.class);
Gson gson = new GsonBuilder()
.registerTypeAdapterFactory(billingAdapter)
.create();
String serialized = gson.toJson(new BillingInstrumentWrapper(null), BillingInstrumentWrapper.class);
BillingInstrumentWrapper deserialized = gson.fromJson(serialized, BillingInstrumentWrapper.class);
assertNull(deserialized.instrument);
}

static class BillingInstrumentWrapper {
BillingInstrument instrument;
BillingInstrumentWrapper(BillingInstrument instrument) {
this.instrument = instrument;
}
}

static class BillingInstrument {
private final String ownerName;
BillingInstrument(String ownerName) {
Expand Down

0 comments on commit 1867457

Please sign in to comment.