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:
parent
d5ed0716db
commit
0b734e46e1
@ -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
|
* type adapter is required to handle nulls in write and read methods. Here is how this
|
||||||
* is typically done:<br>
|
* is typically done:<br>
|
||||||
* <pre> {@code
|
* <pre> {@code
|
||||||
Gson gson = new GsonBuilder().registerTypeAdapter(Foo.class,
|
*
|
||||||
new TypeAdapter<Foo>() {
|
* Gson gson = new GsonBuilder().registerTypeAdapter(Foo.class,
|
||||||
public Foo read(JsonReader in) throws IOException {
|
* new TypeAdapter<Foo>() {
|
||||||
if (in.peek() == JsonToken.NULL) {
|
* public Foo read(JsonReader in) throws IOException {
|
||||||
in.nextNull();
|
* if (in.peek() == JsonToken.NULL) {
|
||||||
return null;
|
* in.nextNull();
|
||||||
}
|
* return null;
|
||||||
// read a Foo from in and return it
|
* }
|
||||||
}
|
* // read a Foo from in and return it
|
||||||
public void write(JsonWriter out, Foo src) throws IOException {
|
* }
|
||||||
if (src == null) {
|
* public void write(JsonWriter out, Foo src) throws IOException {
|
||||||
out.nullValue();
|
* if (src == null) {
|
||||||
return;
|
* out.nullValue();
|
||||||
}
|
* return;
|
||||||
// write src as JSON to out
|
* }
|
||||||
}
|
* // write src as JSON to out
|
||||||
).create();
|
* }
|
||||||
|
* }).create();
|
||||||
* }</pre>
|
* }</pre>
|
||||||
* You can avoid this boilerplate handling of nulls by wrapping your type adapter with
|
* 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:
|
* {@link #nullSafe(TypeAdapter)} method. Here is how we will rewrite the above example:
|
||||||
* <pre> {@code
|
* <pre> {@code
|
||||||
Gson gson = new GsonBuilder().registerTypeAdapter(Foo.class,
|
*
|
||||||
TypeAdapter.nullSafe(new TypeAdapter<Foo>() {
|
* Gson gson = new GsonBuilder().registerTypeAdapter(Foo.class,
|
||||||
public Foo read(JsonReader in) throws IOException {
|
* new TypeAdapter<Foo>() {
|
||||||
// read a Foo from in and return it
|
* 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
|
* public void write(JsonWriter out, Foo src) throws IOException {
|
||||||
}
|
* // write src as JSON to out
|
||||||
)).create();
|
* }
|
||||||
|
* }.nullSafe()).create();
|
||||||
* }</pre>
|
* }</pre>
|
||||||
* Note that we didn't need to check for nulls in our type adapter after we used nullSafe.
|
* 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>() {
|
return new TypeAdapter<T>() {
|
||||||
@Override public void write(JsonWriter out, T value) throws IOException {
|
@Override public void write(JsonWriter out, T value) throws IOException {
|
||||||
if (value == null) {
|
if (value == null) {
|
||||||
out.nullValue();
|
out.nullValue();
|
||||||
} else {
|
} else {
|
||||||
typeAdapter.write(out, value);
|
write(out, value);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@Override public T read(JsonReader reader) throws IOException {
|
@Override public T read(JsonReader reader) throws IOException {
|
||||||
@ -189,7 +191,7 @@ public abstract class TypeAdapter<T> {
|
|||||||
reader.nextNull();
|
reader.nextNull();
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
return typeAdapter.read(reader);
|
return read(reader);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
@ -173,8 +173,7 @@ public final class StreamingTypeAdaptersTest extends TestCase {
|
|||||||
gson.fromJson(json, Truck.class);
|
gson.fromJson(json, Truck.class);
|
||||||
fail();
|
fail();
|
||||||
} catch (JsonSyntaxException expected) {}
|
} catch (JsonSyntaxException expected) {}
|
||||||
gson = new GsonBuilder().registerTypeAdapter(
|
gson = new GsonBuilder().registerTypeAdapter(Person.class, typeAdapter.nullSafe()).create();
|
||||||
Person.class, TypeAdapter.nullSafe(typeAdapter)).create();
|
|
||||||
assertEquals("{\"horsePower\":1.0,\"passengers\":[null]}", gson.toJson(truck, Truck.class));
|
assertEquals("{\"horsePower\":1.0,\"passengers\":[null]}", gson.toJson(truck, Truck.class));
|
||||||
truck = gson.fromJson(json, Truck.class);
|
truck = gson.fromJson(json, Truck.class);
|
||||||
assertEquals(1.0D, truck.horsePower);
|
assertEquals(1.0D, truck.horsePower);
|
||||||
|
Loading…
Reference in New Issue
Block a user