From 0b734e46e180a452e4bff45878ac60441be6ff74 Mon Sep 17 00:00:00 2001 From: Inderjeet Singh Date: Tue, 6 Dec 2011 08:18:00 +0000 Subject: [PATCH] Incorporated code review comments from r1061. Made nullSafe() an instance method instead of a static method. Updated code javadoc to match Guava style. --- .../java/com/google/gson/TypeAdapter.java | 64 ++++++++++--------- .../functional/StreamingTypeAdaptersTest.java | 3 +- 2 files changed, 34 insertions(+), 33 deletions(-) diff --git a/gson/src/main/java/com/google/gson/TypeAdapter.java b/gson/src/main/java/com/google/gson/TypeAdapter.java index f2ad9923..e70707e5 100644 --- a/gson/src/main/java/com/google/gson/TypeAdapter.java +++ b/gson/src/main/java/com/google/gson/TypeAdapter.java @@ -141,47 +141,49 @@ public abstract class TypeAdapter { * This wrapper method is used to make a type adapter null tolerant. In general, a * type adapter is required to handle nulls in write and read methods. Here is how this * is typically done:
- *
{@code
-   Gson gson = new GsonBuilder().registerTypeAdapter(Foo.class,
-    new TypeAdapter() {
-      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();
+   * 
   {@code
+   *
+   * Gson gson = new GsonBuilder().registerTypeAdapter(Foo.class,
+   *   new TypeAdapter() {
+   *     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();
    * }
* 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: - *
{@code
-   Gson gson = new GsonBuilder().registerTypeAdapter(Foo.class,
-    TypeAdapter.nullSafe(new TypeAdapter() {
-      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();
+   * 
   {@code
+   *
+   * Gson gson = new GsonBuilder().registerTypeAdapter(Foo.class,
+   *   new TypeAdapter() {
+   *     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();
    * }
* Note that we didn't need to check for nulls in our type adapter after we used nullSafe. */ - public static TypeAdapter nullSafe(final TypeAdapter typeAdapter) { + public TypeAdapter nullSafe() { return new TypeAdapter() { @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 { reader.nextNull(); return null; } - return typeAdapter.read(reader); + return read(reader); } }; } diff --git a/gson/src/test/java/com/google/gson/functional/StreamingTypeAdaptersTest.java b/gson/src/test/java/com/google/gson/functional/StreamingTypeAdaptersTest.java index 2f0830be..ca2bb924 100644 --- a/gson/src/test/java/com/google/gson/functional/StreamingTypeAdaptersTest.java +++ b/gson/src/test/java/com/google/gson/functional/StreamingTypeAdaptersTest.java @@ -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);