incorporated code review feedback by eliminating the stringified type adapter.

This commit is contained in:
Inderjeet Singh 2016-02-26 09:25:23 -08:00
parent 1fa43821e8
commit 1ab73ffd21

View File

@ -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<Boolean> recursiveCall = new ThreadLocal<Boolean>();
@SuppressWarnings({"unchecked", "rawtypes"})
@ -72,31 +55,7 @@ public class JsonAdapterNullSafeTest extends TestCase {
return null;
}
recursiveCall.set(Boolean.TRUE);
final TypeAdapter<Device> delegate = (TypeAdapter) gson.getDelegateAdapter(this, type);
return (TypeAdapter) new TypeAdapter<Device>() {
@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);
}
}
}