Skip to content

Commit

Permalink
Merge pull request google#681 from lcoote/nullSafeRuntimeType
Browse files Browse the repository at this point in the history
fix google#680 - make RuntimeTypeAdapterFactory null-safe
  • Loading branch information
swankjesse committed Aug 6, 2015
2 parents 77e31ed + 1867457 commit f0bf15e
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 f0bf15e

Please sign in to comment.