From cdd5d80b85155c21c61f888fb3e70c65e72edef7 Mon Sep 17 00:00:00 2001 From: Jesse Wilson Date: Fri, 9 Sep 2011 08:17:20 +0000 Subject: [PATCH] Support writing maps with non-string keys --- .../bind/GsonCompatibleMapTypeAdapter.java | 15 ++++++--------- .../gson/functional/CircularReferenceTest.java | 12 ++++++------ 2 files changed, 12 insertions(+), 15 deletions(-) diff --git a/gson/src/main/java/com/google/gson/internal/bind/GsonCompatibleMapTypeAdapter.java b/gson/src/main/java/com/google/gson/internal/bind/GsonCompatibleMapTypeAdapter.java index 62e4ca29..f0567380 100644 --- a/gson/src/main/java/com/google/gson/internal/bind/GsonCompatibleMapTypeAdapter.java +++ b/gson/src/main/java/com/google/gson/internal/bind/GsonCompatibleMapTypeAdapter.java @@ -31,7 +31,7 @@ import java.util.Map; /** * Adapt a map whose keys are any type. */ -public final class GsonCompatibleMapTypeAdapter extends TypeAdapter> { +public final class GsonCompatibleMapTypeAdapter extends TypeAdapter> { public static final Factory FACTORY = new Factory() { public TypeAdapter create(MiniGson context, TypeToken typeToken) { Type type = typeToken.getType(); @@ -71,7 +71,7 @@ public final class GsonCompatibleMapTypeAdapter extends TypeAdapter read(JsonReader reader) throws IOException { + public Map read(JsonReader reader) throws IOException { if (reader.peek() == JsonToken.NULL) { reader.nextNull(); // TODO: does this belong here? return null; @@ -82,24 +82,21 @@ public final class GsonCompatibleMapTypeAdapter extends TypeAdapter map) throws IOException { + public void write(JsonWriter writer, Map map) throws IOException { if (map == null) { writer.nullValue(); // TODO: better policy here? return; } writer.beginObject(); - for (Map.Entry entry : map.entrySet()) { - String key = entry.getKey(); - if (key == null) { - key = "null"; - } + for (Map.Entry entry : map.entrySet()) { + String key = String.valueOf(entry.getKey()); writer.name(key); valueTypeAdapter.write(writer, entry.getValue()); } diff --git a/gson/src/test/java/com/google/gson/functional/CircularReferenceTest.java b/gson/src/test/java/com/google/gson/functional/CircularReferenceTest.java index f4a04e42..f1367eb4 100644 --- a/gson/src/test/java/com/google/gson/functional/CircularReferenceTest.java +++ b/gson/src/test/java/com/google/gson/functional/CircularReferenceTest.java @@ -52,8 +52,8 @@ public class CircularReferenceTest extends TestCase { try { gson.toJson(a); fail("Circular types should not get printed!"); - } catch (IllegalStateException expected) { - assertTrue(expected.getMessage().contains("children")); + } catch (IllegalStateException expected) { + assertTrue(expected.getMessage().contains("children")); } } @@ -74,7 +74,7 @@ public class CircularReferenceTest extends TestCase { try { gson.toJson(objA); fail("Circular reference to self can not be serialized!"); - } catch (IllegalStateException expected) { + } catch (IllegalStateException expected) { assertTrue(expected.getMessage().contains("children")); } } @@ -89,12 +89,12 @@ public class CircularReferenceTest extends TestCase { obj.addProperty("property", "value"); obj.add("child", context.serialize(src.child)); return obj; - } + } }).create(); try { gson.toJson(obj); fail("Circular reference to self can not be serialized!"); - } catch (IllegalStateException expected) { + } catch (IllegalStateException expected) { assertTrue(expected.getMessage().contains("Offending")); } } @@ -119,7 +119,7 @@ public class CircularReferenceTest extends TestCase { private static class ContainsReferenceToSelfType { Collection children = new ArrayList(); } - + private static class ClassWithSelfReference { ClassWithSelfReference child; }