From 1ab73ffd21d8c08bbe734154921a936e4a8cb091 Mon Sep 17 00:00:00 2001 From: Inderjeet Singh Date: Fri, 26 Feb 2016 09:25:23 -0800 Subject: [PATCH] incorporated code review feedback by eliminating the stringified type adapter. --- .../regression/JsonAdapterNullSafeTest.java | 53 +++---------------- 1 file changed, 6 insertions(+), 47 deletions(-) diff --git a/gson/src/test/java/com/google/gson/regression/JsonAdapterNullSafeTest.java b/gson/src/test/java/com/google/gson/regression/JsonAdapterNullSafeTest.java index 3b332940..30a6775c 100644 --- a/gson/src/test/java/com/google/gson/regression/JsonAdapterNullSafeTest.java +++ b/gson/src/test/java/com/google/gson/regression/JsonAdapterNullSafeTest.java @@ -15,54 +15,37 @@ */ package com.google.gson.regression; -import java.io.IOException; -import java.util.Objects; - import com.google.gson.Gson; -import com.google.gson.JsonElement; -import com.google.gson.JsonParser; -import com.google.gson.JsonPrimitive; -import com.google.gson.JsonSyntaxException; import com.google.gson.TypeAdapter; import com.google.gson.TypeAdapterFactory; import com.google.gson.annotations.JsonAdapter; import com.google.gson.reflect.TypeToken; -import com.google.gson.stream.JsonReader; -import com.google.gson.stream.JsonWriter; import junit.framework.TestCase; public class JsonAdapterNullSafeTest extends TestCase { - // The recursiveCall in {@link Device.JsonAdapterFactory} is the source of this bug - // because it returns a null type adapter. - private final Gson gson = new Gson(); public void testNullSafeBugSerialize() throws Exception { - Device device = new Device("ec57803e", 2); + Device device = new Device("ec57803e"); gson.toJson(device); } public void testNullSafeBugDeserialize() throws Exception { - String json = "\"{\\\"id\\\":\\\"ec57803e2\\\",\\\"category\\\":2}\""; - Device device = gson.fromJson(json, Device.class); + Device device = gson.fromJson("{'id':'ec57803e2'}", Device.class); assertEquals("ec57803e2", device.id); - assertEquals(2, device.category); } @JsonAdapter(Device.JsonAdapterFactory.class) private static final class Device { String id; - int category; - Device(String id, int category) { + Device(String id) { this.id = id; - this.category = category; } - /** - * Write the value as a String, not JSON. - */ static final class JsonAdapterFactory implements TypeAdapterFactory { + // The recursiveCall in {@link Device.JsonAdapterFactory} is the source of this bug + // because we use it to return a null type adapter on a recursive call. private static final ThreadLocal recursiveCall = new ThreadLocal(); @SuppressWarnings({"unchecked", "rawtypes"}) @@ -72,31 +55,7 @@ public class JsonAdapterNullSafeTest extends TestCase { return null; } recursiveCall.set(Boolean.TRUE); - final TypeAdapter delegate = (TypeAdapter) gson.getDelegateAdapter(this, type); - return (TypeAdapter) new TypeAdapter() { - @Override public void write(JsonWriter out, Device value) throws IOException { - delegate.write(out, value); - } - @Override public Device read(JsonReader in) throws IOException { - String json = in.nextString(); - JsonParser parser = new JsonParser(); - JsonElement root = parseString(parser, json, null); - return root == null ? null : delegate.fromJsonTree(root); - } - private JsonElement parseString(JsonParser parser, String json, String prevJson) - throws IOException { // called recursively - JsonElement root = parser.parse(json); - if (root instanceof JsonPrimitive) { - prevJson = json; - json = root.getAsString(); - if (Objects.equals(json, prevJson)) { - throw new JsonSyntaxException("Unexpected Json: " + json); - } - return parseString(parser, json, prevJson); - } - return root; - } - }; + return (TypeAdapter) gson.getDelegateAdapter(this, type); } } }