diff --git a/extras/src/main/java/com/google/gson/graph/GraphAdapterBuilder.java b/extras/src/main/java/com/google/gson/graph/GraphAdapterBuilder.java index 236cbe98..976fe832 100644 --- a/extras/src/main/java/com/google/gson/graph/GraphAdapterBuilder.java +++ b/extras/src/main/java/com/google/gson/graph/GraphAdapterBuilder.java @@ -291,7 +291,7 @@ public final class GraphAdapterBuilder { void read(Graph graph) throws IOException { if (graph.nextCreate != null) { - throw new IllegalStateException("Unexpected recursive call to read()"); + throw new IllegalStateException("Unexpected recursive call to read() for " + id); } graph.nextCreate = this; value = typeAdapter.fromJsonTree(element); diff --git a/extras/src/test/java/com/google/gson/graph/GraphAdapterBuilderTest.java b/extras/src/test/java/com/google/gson/graph/GraphAdapterBuilderTest.java index a6babf42..9b69e03e 100644 --- a/extras/src/test/java/com/google/gson/graph/GraphAdapterBuilderTest.java +++ b/extras/src/test/java/com/google/gson/graph/GraphAdapterBuilderTest.java @@ -18,6 +18,11 @@ package com.google.gson.graph; import com.google.gson.Gson; import com.google.gson.GsonBuilder; +import com.google.gson.reflect.TypeToken; +import java.lang.reflect.Type; +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; import junit.framework.TestCase; public final class GraphAdapterBuilderTest extends TestCase { @@ -38,7 +43,7 @@ public final class GraphAdapterBuilderTest extends TestCase { assertEquals("{'0x1':{'name':'ROCK','beats':'0x2'}," + "'0x2':{'name':'SCISSORS','beats':'0x3'}," + "'0x3':{'name':'PAPER','beats':'0x1'}}", - gson.toJson(rock).replace('\"', '\'')); + gson.toJson(rock).replace('"', '\'')); } public void testDeserialization() { @@ -72,7 +77,7 @@ public final class GraphAdapterBuilderTest extends TestCase { Gson gson = gsonBuilder.create(); assertEquals("{'0x1':{'name':'SUICIDE','beats':'0x1'}}", - gson.toJson(suicide).replace('\"', '\'')); + gson.toJson(suicide).replace('"', '\'')); } public void testDeserializationDirectSelfReference() { @@ -89,6 +94,42 @@ public final class GraphAdapterBuilderTest extends TestCase { assertSame(suicide, suicide.beats); } + public void testSerializeListOfLists() { + Type listOfListsType = new TypeToken>>() {}.getType(); + Type listOfAnyType = new TypeToken>() {}.getType(); + + List> listOfLists = new ArrayList>(); + listOfLists.add(listOfLists); + listOfLists.add(new ArrayList()); + + GsonBuilder gsonBuilder = new GsonBuilder(); + new GraphAdapterBuilder() + .addType(listOfListsType) + .addType(listOfAnyType) + .registerOn(gsonBuilder); + Gson gson = gsonBuilder.create(); + + String json = gson.toJson(listOfLists, listOfListsType); + assertEquals("{'0x1':['0x1','0x2'],'0x2':[]}", json.replace('"', '\'')); + } + + public void testDeserializeListOfLists() { + Type listOfAnyType = new TypeToken>() {}.getType(); + Type listOfListsType = new TypeToken>>() {}.getType(); + + GsonBuilder gsonBuilder = new GsonBuilder(); + new GraphAdapterBuilder() + .addType(listOfListsType) + .addType(listOfAnyType) + .registerOn(gsonBuilder); + Gson gson = gsonBuilder.create(); + + List> listOfLists = gson.fromJson("{'0x1':['0x1','0x2'],'0x2':[]}", listOfListsType); + assertEquals(2, listOfLists.size()); + assertSame(listOfLists, listOfLists.get(0)); + assertEquals(Collections.emptyList(), listOfLists.get(1)); + } + static class Roshambo { String name; Roshambo beats;