From 97d00f49309dec6cc92b248a7f08408bd6aeb3e1 Mon Sep 17 00:00:00 2001 From: Joel Leitch Date: Wed, 13 Apr 2011 16:14:54 +0000 Subject: [PATCH] Add test to verify issue 309. --- .../com/google/gson/functional/MapTest.java | 26 +++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/gson/src/test/java/com/google/gson/functional/MapTest.java b/gson/src/test/java/com/google/gson/functional/MapTest.java index ca77d0b3..16bd779e 100755 --- a/gson/src/test/java/com/google/gson/functional/MapTest.java +++ b/gson/src/test/java/com/google/gson/functional/MapTest.java @@ -19,10 +19,13 @@ package com.google.gson.functional; import com.google.gson.Gson; import com.google.gson.GsonBuilder; import com.google.gson.InstanceCreator; +import com.google.gson.JsonArray; import com.google.gson.JsonElement; +import com.google.gson.JsonPrimitive; import com.google.gson.JsonSerializationContext; import com.google.gson.JsonSerializer; import com.google.gson.common.TestTypes; +import com.google.gson.internal.$Gson$Types; import com.google.gson.reflect.TypeToken; import junit.framework.TestCase; @@ -200,6 +203,29 @@ public class MapTest extends TestCase { assertEquals("2", map.get("b")); } + public void testCustomSerializerForSpecificMapType() { + Type type = $Gson$Types.newParameterizedTypeWithOwner( + null, Map.class, String.class, Long.class); + Gson gson = new GsonBuilder() + .registerTypeAdapter(type, new JsonSerializer>() { + public JsonElement serialize(Map src, Type typeOfSrc, + JsonSerializationContext context) { + JsonArray array = new JsonArray(); + for (long value : src.values()) { + array.add(new JsonPrimitive(value)); + } + return array; + } + }).create(); + + Map src = new LinkedHashMap(); + src.put("one", 1L); + src.put("two", 2L); + src.put("three", 3L); + + assertEquals("[1,2,3]", gson.toJson(src, type)); + } + /** * Created in response to http://code.google.com/p/google-gson/issues/detail?id=99 */