Skip to content

Commit

Permalink
Fix #682
Browse files Browse the repository at this point in the history
  • Loading branch information
cowtowncoder committed Jan 20, 2015
1 parent ae101c6 commit 00bb11e
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 3 deletions.
5 changes: 4 additions & 1 deletion release-notes/CREDITS
Original file line number Diff line number Diff line change
Expand Up @@ -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)
2 changes: 2 additions & 0 deletions release-notes/VERSION
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,20 @@ public class StdKeySerializer extends StdSerializer<Object>

@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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@ public static JsonSerializer<Object> getStdKeySerializer(SerializationConfig con
|| Number.class.isAssignableFrom(rawKeyType)) {
return DEFAULT_KEY_SERIALIZER;
}
if (rawKeyType == Class.class) {
return (JsonSerializer<Object>) ClassKeySerializer.instance;
}
if (Date.class.isAssignableFrom(rawKeyType)) {
return (JsonSerializer<Object>) DateKeySerializer.instance;
}
Expand Down Expand Up @@ -96,4 +99,15 @@ public void serialize(Calendar value, JsonGenerator jgen, SerializerProvider pro
provider.defaultSerializeDateKey(value.getTimeInMillis(), jgen);
}
}

public static class ClassKeySerializer extends StdSerializer<Class<?>> {
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());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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<Class<?>,Integer> map = new LinkedHashMap<Class<?>,Integer>();
map.put(String.class, 2);
String json = MAPPER.writeValueAsString(map);
assertEquals(aposToQuotes("{'java.lang.String':2}"), json);
}
}

0 comments on commit 00bb11e

Please sign in to comment.