Merge pull request #772 from chalup/nullsafe-jsonadapter

Add nullsafe option to JsonAdapter annotation
This commit is contained in:
Jesse Wilson 2016-01-18 11:56:14 -05:00
commit 57b08bbc31
3 changed files with 53 additions and 8 deletions

View File

@ -51,18 +51,20 @@ public final class JsonAdapterAnnotationTypeAdapterFactory implements TypeAdapte
static TypeAdapter<?> getTypeAdapter(ConstructorConstructor constructorConstructor, Gson gson, static TypeAdapter<?> getTypeAdapter(ConstructorConstructor constructorConstructor, Gson gson,
TypeToken<?> fieldType, JsonAdapter annotation) { TypeToken<?> fieldType, JsonAdapter annotation) {
Class<?> value = annotation.value(); Class<?> value = annotation.value();
final TypeAdapter<?> typeAdapter;
if (TypeAdapter.class.isAssignableFrom(value)) { if (TypeAdapter.class.isAssignableFrom(value)) {
Class<TypeAdapter<?>> typeAdapter = (Class<TypeAdapter<?>>) value; Class<TypeAdapter<?>> typeAdapterClass = (Class<TypeAdapter<?>>) value;
return constructorConstructor.get(TypeToken.get(typeAdapter)).construct(); typeAdapter = constructorConstructor.get(TypeToken.get(typeAdapterClass)).construct();
} } else if (TypeAdapterFactory.class.isAssignableFrom(value)) {
if (TypeAdapterFactory.class.isAssignableFrom(value)) { Class<TypeAdapterFactory> typeAdapterFactory = (Class<TypeAdapterFactory>) value;
Class<TypeAdapterFactory> typeAdapterFactory = (Class<TypeAdapterFactory>) value; typeAdapter = constructorConstructor.get(TypeToken.get(typeAdapterFactory))
return constructorConstructor.get(TypeToken.get(typeAdapterFactory))
.construct() .construct()
.create(gson, fieldType); .create(gson, fieldType);
} else {
throw new IllegalArgumentException(
"@JsonAdapter value must be TypeAdapter or TypeAdapterFactory reference.");
} }
throw new IllegalArgumentException( return typeAdapter.nullSafe();
"@JsonAdapter value must be TypeAdapter or TypeAdapterFactory reference.");
} }
} }

View File

@ -133,6 +133,12 @@ public final class JsonAdapterAnnotationOnClassesTest extends TestCase {
assertFalse(json.contains("jsonAdapter")); assertFalse(json.contains("jsonAdapter"));
} }
public void testNullSafeObjectFromJson() {
Gson gson = new Gson();
NullableClass fromJson = gson.fromJson("null", NullableClass.class);
assertNull(fromJson);
}
@JsonAdapter(A.JsonAdapter.class) @JsonAdapter(A.JsonAdapter.class)
private static class A { private static class A {
final String value; final String value;
@ -215,6 +221,23 @@ public final class JsonAdapterAnnotationOnClassesTest extends TestCase {
} }
} }
@JsonAdapter(value = NullableClassJsonAdapter.class)
private static class NullableClass {
}
private static class NullableClassJsonAdapter extends TypeAdapter<NullableClass> {
@Override
public void write(JsonWriter out, NullableClass value) throws IOException {
out.value("nullable");
}
@Override
public NullableClass read(JsonReader in) throws IOException {
in.nextString();
return new NullableClass();
}
}
@JsonAdapter(FooJsonAdapter.class) @JsonAdapter(FooJsonAdapter.class)
private static enum Foo { BAR, BAZ } private static enum Foo { BAR, BAZ }
private static class FooJsonAdapter extends TypeAdapter<Foo> { private static class FooJsonAdapter extends TypeAdapter<Foo> {

View File

@ -200,4 +200,24 @@ public final class JsonAdapterAnnotationOnFieldsTest extends TestCase {
this.part2 = part2; this.part2 = part2;
} }
} }
public void testJsonAdapterWrappedInNullSafeAsRequested() {
Gson gson = new Gson();
String fromJson = "{'part':null}";
GadgetWithOptionalPart gadget = gson.fromJson(fromJson, GadgetWithOptionalPart.class);
assertNull(gadget.part);
String toJson = gson.toJson(gadget);
assertFalse(toJson.contains("PartJsonFieldAnnotationAdapter"));
}
private static final class GadgetWithOptionalPart {
@JsonAdapter(value = PartJsonFieldAnnotationAdapter.class)
final Part part;
private GadgetWithOptionalPart(Part part) {
this.part = part;
}
}
} }