Incorporated code review comments from r1061.

Made nullSafe() an instance method instead of a static method.
Updated code javadoc to match Guava style.
This commit is contained in:
Inderjeet Singh 2011-12-06 08:18:00 +00:00
parent d5ed0716db
commit 0b734e46e1
2 changed files with 34 additions and 33 deletions

View File

@ -142,46 +142,48 @@ public abstract class TypeAdapter<T> {
* type adapter is required to handle nulls in write and read methods. Here is how this
* is typically done:<br>
* <pre> {@code
Gson gson = new GsonBuilder().registerTypeAdapter(Foo.class,
new TypeAdapter<Foo>() {
public Foo read(JsonReader in) throws IOException {
if (in.peek() == JsonToken.NULL) {
in.nextNull();
return null;
}
// read a Foo from in and return it
}
public void write(JsonWriter out, Foo src) throws IOException {
if (src == null) {
out.nullValue();
return;
}
// write src as JSON to out
}
).create();
*
* Gson gson = new GsonBuilder().registerTypeAdapter(Foo.class,
* new TypeAdapter<Foo>() {
* public Foo read(JsonReader in) throws IOException {
* if (in.peek() == JsonToken.NULL) {
* in.nextNull();
* return null;
* }
* // read a Foo from in and return it
* }
* public void write(JsonWriter out, Foo src) throws IOException {
* if (src == null) {
* out.nullValue();
* return;
* }
* // write src as JSON to out
* }
* }).create();
* }</pre>
* You can avoid this boilerplate handling of nulls by wrapping your type adapter with
* {@link #nullSafe(TypeAdapter)} method. Here is how we will rewrite the above example:
* <pre> {@code
Gson gson = new GsonBuilder().registerTypeAdapter(Foo.class,
TypeAdapter.nullSafe(new TypeAdapter<Foo>() {
public Foo read(JsonReader in) throws IOException {
// read a Foo from in and return it
}
public void write(JsonWriter out, Foo src) throws IOException {
// write src as JSON to out
}
)).create();
*
* Gson gson = new GsonBuilder().registerTypeAdapter(Foo.class,
* new TypeAdapter<Foo>() {
* public Foo read(JsonReader in) throws IOException {
* // read a Foo from in and return it
* }
* public void write(JsonWriter out, Foo src) throws IOException {
* // write src as JSON to out
* }
* }.nullSafe()).create();
* }</pre>
* Note that we didn't need to check for nulls in our type adapter after we used nullSafe.
*/
public static <T> TypeAdapter<T> nullSafe(final TypeAdapter<T> typeAdapter) {
public TypeAdapter<T> nullSafe() {
return new TypeAdapter<T>() {
@Override public void write(JsonWriter out, T value) throws IOException {
if (value == null) {
out.nullValue();
} else {
typeAdapter.write(out, value);
write(out, value);
}
}
@Override public T read(JsonReader reader) throws IOException {
@ -189,7 +191,7 @@ public abstract class TypeAdapter<T> {
reader.nextNull();
return null;
}
return typeAdapter.read(reader);
return read(reader);
}
};
}

View File

@ -173,8 +173,7 @@ public final class StreamingTypeAdaptersTest extends TestCase {
gson.fromJson(json, Truck.class);
fail();
} catch (JsonSyntaxException expected) {}
gson = new GsonBuilder().registerTypeAdapter(
Person.class, TypeAdapter.nullSafe(typeAdapter)).create();
gson = new GsonBuilder().registerTypeAdapter(Person.class, typeAdapter.nullSafe()).create();
assertEquals("{\"horsePower\":1.0,\"passengers\":[null]}", gson.toJson(truck, Truck.class));
truck = gson.fromJson(json, Truck.class);
assertEquals(1.0D, truck.horsePower);