diff --git a/extras/src/main/java/com/google/gson/graph/GraphTypeAdapterFactory.java b/extras/src/main/java/com/google/gson/graph/GraphTypeAdapterFactory.java index 11f32f52..5aaa9e25 100644 --- a/extras/src/main/java/com/google/gson/graph/GraphTypeAdapterFactory.java +++ b/extras/src/main/java/com/google/gson/graph/GraphTypeAdapterFactory.java @@ -17,14 +17,18 @@ package com.google.gson.graph; import com.google.gson.Gson; +import com.google.gson.JsonElement; import com.google.gson.TypeAdapter; import com.google.gson.TypeAdapterFactory; +import com.google.gson.internal.bind.JsonTreeReader; import com.google.gson.reflect.TypeToken; import com.google.gson.stream.JsonReader; +import com.google.gson.stream.JsonToken; import com.google.gson.stream.JsonWriter; import java.io.IOException; import java.lang.reflect.Type; import java.util.Arrays; +import java.util.HashMap; import java.util.HashSet; import java.util.IdentityHashMap; import java.util.LinkedList; @@ -55,6 +59,7 @@ public final class GraphTypeAdapterFactory implements TypeAdapterFactory { } final TypeAdapter typeAdapter = gson.getNextAdapter(this, type); + final TypeAdapter elementAdapter = gson.getAdapter(JsonElement.class); return new TypeAdapter() { @Override public void write(JsonWriter out, T value) throws IOException { if (value == null) { @@ -63,62 +68,141 @@ public final class GraphTypeAdapterFactory implements TypeAdapterFactory { } Graph graph = graphThreadLocal.get(); + boolean writeEntireGraph = false; - // this is the top-level object in the graph; write the whole graph recursively if (graph == null) { - graph = new Graph(); - graphThreadLocal.set(graph); - Element element = new Element(value, graph.elements.size() + 1, typeAdapter); - graph.elements.put(value, element); + writeEntireGraph = true; + graph = new Graph(new IdentityHashMap>()); + } + + Element element = (Element) graph.map.get(value); + if (element == null) { + element = new Element(value, graph.nextName(), typeAdapter, null); + graph.map.put(value, element); graph.queue.add(element); + } - out.beginObject(); - Element current; - while ((current = graph.queue.poll()) != null) { - out.name(current.getName()); - current.write(out); + if (writeEntireGraph) { + graphThreadLocal.set(graph); + try { + out.beginObject(); + Element current; + while ((current = graph.queue.poll()) != null) { + out.name(current.id); + current.write(out); + } + out.endObject(); + } finally { + graphThreadLocal.remove(); } - out.endObject(); - graphThreadLocal.remove(); - - // this is an element nested in the graph; just reference it by ID } else { - Element element = graph.elements.get(value); - if (element == null) { - element = new Element(value, graph.elements.size() + 1, typeAdapter); - graph.elements.put(value, element); - graph.queue.add(element); - } - out.value(element.getName()); + out.value(element.id); } } @Override public T read(JsonReader in) throws IOException { - // TODO: - return null; + if (in.peek() == JsonToken.NULL) { + in.nextNull(); + return null; + } + + String currentName = null; + Graph graph = graphThreadLocal.get(); + boolean readEntireGraph = false; + + if (graph == null) { + graph = new Graph(new HashMap>()); + readEntireGraph = true; + + // read the entire tree into memory + in.beginObject(); + while (in.hasNext()) { + String name = in.nextName(); + if (currentName == null) { + currentName = name; + } + JsonElement element = elementAdapter.read(in); + graph.map.put(name, new Element(null, name, typeAdapter, element)); + } + in.endObject(); + } else { + currentName = in.nextString(); + } + + if (readEntireGraph) { + graphThreadLocal.set(graph); + } + try { + Element element = (Element) graph.map.get(currentName); + if (element.value == null) { + element.typeAdapter = typeAdapter; + element.read(); + } + return element.value; + } finally { + if (readEntireGraph) { + graphThreadLocal.remove(); + } + } } }; } static class Graph { - private final Map elements = new IdentityHashMap(); + /** + * The graph elements. On serialization keys are objects (using an identity + * hash map) and on deserialization keys are the string names (using a + * standard hash map). + */ + private final Map> map; private final Queue queue = new LinkedList(); + + private Graph(Map> map) { + this.map = map; + } + + /** + * Returns a unique name for an element to be inserted into the graph. + */ + public String nextName() { + return "0x" + Integer.toHexString(map.size() + 1); + } } static class Element { - private final T value; - private final int id; - private final TypeAdapter typeAdapter; - Element(T value, int id, TypeAdapter typeAdapter) { + private final String id; + private T value; + private TypeAdapter typeAdapter; + private final JsonElement element; + private boolean reading = false; + + Element(T value, String id, TypeAdapter typeAdapter, JsonElement element) { this.value = value; this.id = id; this.typeAdapter = typeAdapter; + this.element = element; } - private String getName() { - return "0x" + Integer.toHexString(id); - } + private void write(JsonWriter out) throws IOException { typeAdapter.write(out, value); } + + private void read() throws IOException { + if (reading) { + // TODO: this currently fails because we don't have the instance we want yet + System.out.println("ALREADY READING " + id); + return; + } + reading = true; + try { + // TODO: use TypeAdapter.fromJsonTree() when that's public + value = typeAdapter.read(new JsonTreeReader(element)); + if (value == null) { + throw new IllegalStateException("non-null value deserialized to null: " + element); + } + } finally { + reading = false; + } + } } } diff --git a/extras/src/main/java/com/google/gson/typeadapters/RuntimeTypeAdapterFactory.java b/extras/src/main/java/com/google/gson/typeadapters/RuntimeTypeAdapterFactory.java index 201441f7..81b8b00d 100644 --- a/extras/src/main/java/com/google/gson/typeadapters/RuntimeTypeAdapterFactory.java +++ b/extras/src/main/java/com/google/gson/typeadapters/RuntimeTypeAdapterFactory.java @@ -25,7 +25,7 @@ import com.google.gson.JsonPrimitive; import com.google.gson.TypeAdapter; import com.google.gson.TypeAdapterFactory; import com.google.gson.internal.Streams; -import com.google.gson.internal.bind.JsonElementWriter; +import com.google.gson.internal.bind.JsonTreeWriter; import com.google.gson.internal.bind.JsonTreeReader; import com.google.gson.reflect.TypeToken; import com.google.gson.stream.JsonReader; @@ -252,7 +252,7 @@ public final class RuntimeTypeAdapterFactory implements TypeAdapterFactory { // TODO: remove this when TypeAdapter.toJsonTree() is public private JsonElement toJsonTree(TypeAdapter delegate, T value) { try { - JsonElementWriter jsonWriter = new JsonElementWriter(); + JsonTreeWriter jsonWriter = new JsonTreeWriter(); jsonWriter.setLenient(true); delegate.write(jsonWriter, value); return jsonWriter.get(); diff --git a/extras/src/test/java/com/google/gson/graph/GraphTypeAdapterFactoryTest.java b/extras/src/test/java/com/google/gson/graph/GraphTypeAdapterFactoryTest.java index 7a95494a..0a2bdc22 100644 --- a/extras/src/test/java/com/google/gson/graph/GraphTypeAdapterFactoryTest.java +++ b/extras/src/test/java/com/google/gson/graph/GraphTypeAdapterFactoryTest.java @@ -21,7 +21,7 @@ import com.google.gson.GsonBuilder; import junit.framework.TestCase; public final class GraphTypeAdapterFactoryTest extends TestCase { - public void testBasicCycle() { + public void testSerialization() { Roshambo rock = new Roshambo("ROCK"); Roshambo scissors = new Roshambo("SCISSORS"); Roshambo paper = new Roshambo("PAPER"); @@ -39,6 +39,24 @@ public final class GraphTypeAdapterFactoryTest extends TestCase { gson.toJson(rock).replace('\"', '\'')); } + public void testDeserialization() { + String json = "{'0x1':{'name':'ROCK','beats':'0x2'}," + + "'0x2':{'name':'SCISSORS','beats':'0x3'}," + + "'0x3':{'name':'PAPER','beats':'0x1'}}"; + + Gson gson = new GsonBuilder() + .registerTypeAdapterFactory(GraphTypeAdapterFactory.of(Roshambo.class)) + .create(); + + Roshambo rock = gson.fromJson(json, Roshambo.class); + assertEquals("ROCK", rock.name); + Roshambo scissors = rock.beats; + assertEquals("SCISSORS", scissors.name); + Roshambo paper = scissors.beats; + assertEquals("PAPER", paper.name); + assertSame(rock, paper.beats); // TODO: currently fails + } + static class Roshambo { String name; Roshambo beats; diff --git a/gson/src/main/java/com/google/gson/Gson.java b/gson/src/main/java/com/google/gson/Gson.java index 2a9eebdb..09f3365e 100644 --- a/gson/src/main/java/com/google/gson/Gson.java +++ b/gson/src/main/java/com/google/gson/Gson.java @@ -26,7 +26,7 @@ import com.google.gson.internal.bind.BigIntegerTypeAdapter; import com.google.gson.internal.bind.CollectionTypeAdapterFactory; import com.google.gson.internal.bind.DateTypeAdapter; import com.google.gson.internal.bind.JsonTreeReader; -import com.google.gson.internal.bind.JsonElementWriter; +import com.google.gson.internal.bind.JsonTreeWriter; import com.google.gson.internal.bind.MapTypeAdapterFactory; import com.google.gson.internal.bind.ObjectTypeAdapter; import com.google.gson.internal.bind.ReflectiveTypeAdapterFactory; @@ -437,7 +437,7 @@ public final class Gson { * @since 1.4 */ public JsonElement toJsonTree(Object src, Type typeOfSrc) { - JsonElementWriter writer = new JsonElementWriter(); + JsonTreeWriter writer = new JsonTreeWriter(); toJson(src, typeOfSrc, writer); return writer.get(); } diff --git a/gson/src/main/java/com/google/gson/TypeAdapter.java b/gson/src/main/java/com/google/gson/TypeAdapter.java index 4ca939b5..2f05641d 100644 --- a/gson/src/main/java/com/google/gson/TypeAdapter.java +++ b/gson/src/main/java/com/google/gson/TypeAdapter.java @@ -16,9 +16,8 @@ package com.google.gson; -import com.google.gson.internal.bind.JsonElementWriter; +import com.google.gson.internal.bind.JsonTreeWriter; import com.google.gson.internal.bind.JsonTreeReader; -import com.google.gson.reflect.TypeToken; import com.google.gson.stream.JsonReader; import com.google.gson.stream.JsonToken; import com.google.gson.stream.JsonWriter; @@ -224,7 +223,7 @@ public abstract class TypeAdapter { */ /*public*/ final JsonElement toJsonTree(T value) { try { - JsonElementWriter jsonWriter = new JsonElementWriter(); + JsonTreeWriter jsonWriter = new JsonTreeWriter(); jsonWriter.setLenient(true); write(jsonWriter, value); return jsonWriter.get(); diff --git a/gson/src/main/java/com/google/gson/internal/bind/JsonElementWriter.java b/gson/src/main/java/com/google/gson/internal/bind/JsonTreeWriter.java similarity index 98% rename from gson/src/main/java/com/google/gson/internal/bind/JsonElementWriter.java rename to gson/src/main/java/com/google/gson/internal/bind/JsonTreeWriter.java index 3ef172db..5f9f0395 100644 --- a/gson/src/main/java/com/google/gson/internal/bind/JsonElementWriter.java +++ b/gson/src/main/java/com/google/gson/internal/bind/JsonTreeWriter.java @@ -30,7 +30,7 @@ import java.util.List; /** * This writer creates a JsonElement. */ -public final class JsonElementWriter extends JsonWriter { +public final class JsonTreeWriter extends JsonWriter { private static final Writer UNWRITABLE_WRITER = new Writer() { @Override public void write(char[] buffer, int offset, int counter) { throw new AssertionError(); @@ -54,7 +54,7 @@ public final class JsonElementWriter extends JsonWriter { /** the JSON element constructed by this writer. */ private JsonElement product = JsonNull.INSTANCE; // TODO: is this really what we want?; - public JsonElementWriter() { + public JsonTreeWriter() { super(UNWRITABLE_WRITER); } diff --git a/gson/src/main/java/com/google/gson/internal/bind/MapTypeAdapterFactory.java b/gson/src/main/java/com/google/gson/internal/bind/MapTypeAdapterFactory.java index 27be1d70..b64c5524 100644 --- a/gson/src/main/java/com/google/gson/internal/bind/MapTypeAdapterFactory.java +++ b/gson/src/main/java/com/google/gson/internal/bind/MapTypeAdapterFactory.java @@ -266,7 +266,7 @@ public final class MapTypeAdapterFactory implements TypeAdapterFactory { // TODO: remove this when TypeAdapter.toJsonTree() is public private static JsonElement toJsonTree(TypeAdapter typeAdapter, T value) { try { - JsonElementWriter jsonWriter = new JsonElementWriter(); + JsonTreeWriter jsonWriter = new JsonTreeWriter(); jsonWriter.setLenient(true); typeAdapter.write(jsonWriter, value); return jsonWriter.get(); diff --git a/gson/src/main/java/com/google/gson/internal/bind/ReflectiveTypeAdapterFactory.java b/gson/src/main/java/com/google/gson/internal/bind/ReflectiveTypeAdapterFactory.java index 42f5a438..b95e6552 100644 --- a/gson/src/main/java/com/google/gson/internal/bind/ReflectiveTypeAdapterFactory.java +++ b/gson/src/main/java/com/google/gson/internal/bind/ReflectiveTypeAdapterFactory.java @@ -32,7 +32,6 @@ import com.google.gson.stream.JsonReader; import com.google.gson.stream.JsonToken; import com.google.gson.stream.JsonWriter; import java.io.IOException; -import java.lang.reflect.AccessibleObject; import java.lang.reflect.Field; import java.lang.reflect.Type; import java.util.LinkedHashMap; diff --git a/gson/src/test/java/com/google/gson/internal/bind/JsonElementWriterTest.java b/gson/src/test/java/com/google/gson/internal/bind/JsonTreeWriterTest.java similarity index 85% rename from gson/src/test/java/com/google/gson/internal/bind/JsonElementWriterTest.java rename to gson/src/test/java/com/google/gson/internal/bind/JsonTreeWriterTest.java index e1ee3abf..6ec7ec23 100644 --- a/gson/src/test/java/com/google/gson/internal/bind/JsonElementWriterTest.java +++ b/gson/src/test/java/com/google/gson/internal/bind/JsonTreeWriterTest.java @@ -20,9 +20,9 @@ import com.google.gson.JsonNull; import java.io.IOException; import junit.framework.TestCase; -public final class JsonElementWriterTest extends TestCase { +public final class JsonTreeWriterTest extends TestCase { public void testArray() throws IOException { - JsonElementWriter writer = new JsonElementWriter(); + JsonTreeWriter writer = new JsonTreeWriter(); writer.beginArray(); writer.value(1); writer.value(2); @@ -32,7 +32,7 @@ public final class JsonElementWriterTest extends TestCase { } public void testNestedArray() throws IOException { - JsonElementWriter writer = new JsonElementWriter(); + JsonTreeWriter writer = new JsonTreeWriter(); writer.beginArray(); writer.beginArray(); writer.endArray(); @@ -45,7 +45,7 @@ public final class JsonElementWriterTest extends TestCase { } public void testObject() throws IOException { - JsonElementWriter writer = new JsonElementWriter(); + JsonTreeWriter writer = new JsonTreeWriter(); writer.beginObject(); writer.name("A").value(1); writer.name("B").value(2); @@ -54,7 +54,7 @@ public final class JsonElementWriterTest extends TestCase { } public void testNestedObject() throws IOException { - JsonElementWriter writer = new JsonElementWriter(); + JsonTreeWriter writer = new JsonTreeWriter(); writer.beginObject(); writer.name("A"); writer.beginObject(); @@ -70,7 +70,7 @@ public final class JsonElementWriterTest extends TestCase { } public void testWriteAfterClose() throws Exception { - JsonElementWriter writer = new JsonElementWriter(); + JsonTreeWriter writer = new JsonTreeWriter(); writer.setLenient(true); writer.beginArray(); writer.value("A"); @@ -84,7 +84,7 @@ public final class JsonElementWriterTest extends TestCase { } public void testPrematureClose() throws Exception { - JsonElementWriter writer = new JsonElementWriter(); + JsonTreeWriter writer = new JsonTreeWriter(); writer.setLenient(true); writer.beginArray(); try { @@ -95,7 +95,7 @@ public final class JsonElementWriterTest extends TestCase { } public void testSerializeNullsFalse() throws IOException { - JsonElementWriter writer = new JsonElementWriter(); + JsonTreeWriter writer = new JsonTreeWriter(); writer.setSerializeNulls(false); writer.beginObject(); writer.name("A"); @@ -105,7 +105,7 @@ public final class JsonElementWriterTest extends TestCase { } public void testSerializeNullsTrue() throws IOException { - JsonElementWriter writer = new JsonElementWriter(); + JsonTreeWriter writer = new JsonTreeWriter(); writer.setSerializeNulls(true); writer.beginObject(); writer.name("A"); @@ -115,12 +115,12 @@ public final class JsonElementWriterTest extends TestCase { } public void testEmptyWriter() { - JsonElementWriter writer = new JsonElementWriter(); + JsonTreeWriter writer = new JsonTreeWriter(); assertEquals(JsonNull.INSTANCE, writer.get()); } public void testLenientNansAndInfinities() throws IOException { - JsonElementWriter writer = new JsonElementWriter(); + JsonTreeWriter writer = new JsonTreeWriter(); writer.setLenient(true); writer.beginArray(); writer.value(Double.NaN); @@ -131,7 +131,7 @@ public final class JsonElementWriterTest extends TestCase { } public void testStrictNansAndInfinities() throws IOException { - JsonElementWriter writer = new JsonElementWriter(); + JsonTreeWriter writer = new JsonTreeWriter(); writer.setLenient(false); writer.beginArray(); try { @@ -152,7 +152,7 @@ public final class JsonElementWriterTest extends TestCase { } public void testStrictBoxedNansAndInfinities() throws IOException { - JsonElementWriter writer = new JsonElementWriter(); + JsonTreeWriter writer = new JsonTreeWriter(); writer.setLenient(false); writer.beginArray(); try {