From 00bb11edc3581f42e1f3c78a047538c8ea9eb0d9 Mon Sep 17 00:00:00 2001 From: Tatu Saloranta Date: Mon, 19 Jan 2015 22:14:14 -0800 Subject: [PATCH] Fix #682 --- release-notes/CREDITS | 5 ++++- release-notes/VERSION | 2 ++ .../jackson/databind/ser/std/StdKeySerializer.java | 13 +++++++++++-- .../databind/ser/std/StdKeySerializers.java | 14 ++++++++++++++ .../jackson/databind/ser/TestMapSerialization.java | 8 ++++++++ 5 files changed, 39 insertions(+), 3 deletions(-) diff --git a/release-notes/CREDITS b/release-notes/CREDITS index 63f25cdba4..655d66f2a7 100644 --- a/release-notes/CREDITS +++ b/release-notes/CREDITS @@ -197,6 +197,9 @@ Alexandre Santana Campelo (alexqi200@github): (2.5.1) Zoltan Farkas (zolyfarkas@github) - * Reported #674: Spring CGLIB proxies not handled as intended (2.5.1) + +Ludevik@github: + * Reported #682: Class-valued Map keys not serialized properly + (2.5.1) diff --git a/release-notes/VERSION b/release-notes/VERSION index bec9a9b99d..6aec33ea83 100644 --- a/release-notes/VERSION +++ b/release-notes/VERSION @@ -13,6 +13,8 @@ Project: jackson-databind (contributed by Alexandre S-C) #674: Spring CGLIB proxies not handled as intended (reported by Zoltan F) +#682: Class-valued Map keys not serialized properly + (reported by Ludevik@github) 2.5.0 (01-Jan-2015) diff --git a/src/main/java/com/fasterxml/jackson/databind/ser/std/StdKeySerializer.java b/src/main/java/com/fasterxml/jackson/databind/ser/std/StdKeySerializer.java index 089c257f83..56fbbe112c 100644 --- a/src/main/java/com/fasterxml/jackson/databind/ser/std/StdKeySerializer.java +++ b/src/main/java/com/fasterxml/jackson/databind/ser/std/StdKeySerializer.java @@ -23,11 +23,20 @@ public class StdKeySerializer extends StdSerializer @Override public void serialize(Object value, JsonGenerator jgen, SerializerProvider provider) throws IOException { - if (value instanceof Date) { + String str; + Class cls = value.getClass(); + + if (cls == String.class) { + str = (String) value; + } else if (Date.class.isAssignableFrom(cls)) { provider.defaultSerializeDateKey((Date) value, jgen); + return; + } else if (cls == Class.class) { + str = ((Class) value).getName(); } else { - jgen.writeFieldName(value.toString()); + str = value.toString(); } + jgen.writeFieldName(str); } @Override diff --git a/src/main/java/com/fasterxml/jackson/databind/ser/std/StdKeySerializers.java b/src/main/java/com/fasterxml/jackson/databind/ser/std/StdKeySerializers.java index 515c08fc63..2cb9bbdf3d 100644 --- a/src/main/java/com/fasterxml/jackson/databind/ser/std/StdKeySerializers.java +++ b/src/main/java/com/fasterxml/jackson/databind/ser/std/StdKeySerializers.java @@ -37,6 +37,9 @@ public static JsonSerializer getStdKeySerializer(SerializationConfig con || Number.class.isAssignableFrom(rawKeyType)) { return DEFAULT_KEY_SERIALIZER; } + if (rawKeyType == Class.class) { + return (JsonSerializer) ClassKeySerializer.instance; + } if (Date.class.isAssignableFrom(rawKeyType)) { return (JsonSerializer) DateKeySerializer.instance; } @@ -96,4 +99,15 @@ public void serialize(Calendar value, JsonGenerator jgen, SerializerProvider pro provider.defaultSerializeDateKey(value.getTimeInMillis(), jgen); } } + + public static class ClassKeySerializer extends StdSerializer> { + protected final static JsonSerializer instance = new ClassKeySerializer(); + + public ClassKeySerializer() { super(Class.class, false); } + + @Override + public void serialize(Class value, JsonGenerator jgen, SerializerProvider provider) throws IOException, JsonGenerationException { + jgen.writeFieldName(value.getName()); + } + } } diff --git a/src/test/java/com/fasterxml/jackson/databind/ser/TestMapSerialization.java b/src/test/java/com/fasterxml/jackson/databind/ser/TestMapSerialization.java index a9c36e456e..16fc24569f 100644 --- a/src/test/java/com/fasterxml/jackson/databind/ser/TestMapSerialization.java +++ b/src/test/java/com/fasterxml/jackson/databind/ser/TestMapSerialization.java @@ -247,4 +247,12 @@ public void testNonNullValueMapViaProp() throws IOException .add("c", "bar")); assertEquals(aposToQuotes("{'stuff':{'a':'foo','c':'bar'}}"), json); } + + public void testClassKey() throws IOException + { + Map,Integer> map = new LinkedHashMap,Integer>(); + map.put(String.class, 2); + String json = MAPPER.writeValueAsString(map); + assertEquals(aposToQuotes("{'java.lang.String':2}"), json); + } }