Skip to content

Commit fd4eb1b

Browse files
committed
Fix #601
1 parent 6b201f9 commit fd4eb1b

File tree

5 files changed

+20
-8
lines changed

5 files changed

+20
-8
lines changed

release-notes/CREDITS

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,8 @@ Chris Cleveland:
139139
Benson Margulies:
140140
* Reported #467: Unwanted POJO's embedded in tree via serialization to tree
141141
(2.4.0)
142+
* Reported #601: ClassCastException for a custom serializer for enum key in `EnumMap`
143+
(2.4.4)
142144

143145
Steve Sanbeg: (sanbeg@github)
144146
* Contributed #482: Make date parsing error behavior consistent with JDK

release-notes/VERSION

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ Project: jackson-databind
1717
(reported by Jerbell@github)
1818
#592: Wrong `TokenBuffer` delegate deserialization using `@JsonCreator`
1919
(reported by Eugene L)
20+
#601: ClassCastException for a custom serializer for enum key in `EnumMap`
21+
(reported by Benson M)
2022
#604: `Map` deserializers not being cached, causing performance problems
2123
- Minor fix to `EnumSerializer` regarding detection "serialize using index"
2224
- Minor fix to number serializers, to call proper callback for schema generation

src/main/java/com/fasterxml/jackson/databind/ser/BasicSerializerFactory.java

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -706,9 +706,8 @@ protected JsonSerializer<?> buildMapSerializer(SerializationConfig config,
706706
if (ser != null) { break; }
707707
}
708708
if (ser == null) {
709-
/* 05-Nov-2014, tatu: As per [databind#601], may be easier NOT to use specialized
710-
* serializers, if custom serialization of keys is needed.
711-
*/
709+
// 08-Nov-2014, tatu: As per [databind#601], better just use default Map serializer
710+
/*
712711
if (EnumMap.class.isAssignableFrom(type.getRawClass())
713712
&& ((keySerializer == null) || ClassUtil.isJacksonStdImpl(keySerializer))) {
714713
JavaType keyType = type.getKeyType();
@@ -722,11 +721,11 @@ protected JsonSerializer<?> buildMapSerializer(SerializationConfig config,
722721
ser = new EnumMapSerializer(type.getContentType(), staticTyping, enums,
723722
elementTypeSerializer, elementValueSerializer);
724723
} else {
725-
Object filterId = findFilterId(config, beanDesc);
726-
ser = MapSerializer.construct(config.getAnnotationIntrospector().findPropertiesToIgnore(beanDesc.getClassInfo()),
724+
*/
725+
Object filterId = findFilterId(config, beanDesc);
726+
ser = MapSerializer.construct(config.getAnnotationIntrospector().findPropertiesToIgnore(beanDesc.getClassInfo()),
727727
type, staticTyping, elementTypeSerializer,
728728
keySerializer, elementValueSerializer, filterId);
729-
}
730729
}
731730
// [Issue#120]: Allow post-processing
732731
if (_factoryConfig.hasSerializerModifiers()) {

src/main/java/com/fasterxml/jackson/databind/ser/std/EnumMapSerializer.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,11 @@
2525
* Specialized serializer for {@link EnumMap}s. Somewhat tricky to
2626
* implement because actual Enum value type may not be available;
2727
* and if not, it can only be gotten from actual instance.
28+
*
29+
* @deprecated Since 2.4.4; standard {@link MapSerializer} works better.
2830
*/
2931
@JacksonStdImpl
32+
@Deprecated
3033
public class EnumMapSerializer
3134
extends ContainerSerializer<EnumMap<? extends Enum<?>, ?>>
3235
implements ContextualSerializer

src/test/java/com/fasterxml/jackson/databind/ser/TestEnumSerialization.java

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -192,13 +192,11 @@ public void testSubclassedEnums() throws Exception
192192
assertEquals("\"B\"", mapper.writeValueAsString(EnumWithSubClass.B));
193193
}
194194

195-
// [JACKSON-193]
196195
public void testEnumsWithJsonValue() throws Exception
197196
{
198197
assertEquals("\"bar\"", mapper.writeValueAsString(EnumWithJsonValue.B));
199198
}
200199

201-
// also, for [JACKSON-193], needs to work via mix-ins
202200
public void testEnumsWithJsonValueUsingMixin() throws Exception
203201
{
204202
// can't share, as new mix-ins are added
@@ -207,6 +205,14 @@ public void testEnumsWithJsonValueUsingMixin() throws Exception
207205
assertEquals("\"b\"", m.writeValueAsString(TestEnum.B));
208206
}
209207

208+
// [databind#601]
209+
public void testEnumsWithJsonValueInMap() throws Exception
210+
{
211+
EnumMap<EnumWithJsonValue,String> input = new EnumMap<EnumWithJsonValue,String>(EnumWithJsonValue.class);
212+
input.put(EnumWithJsonValue.B, "x");
213+
assertEquals("{\""+EnumWithJsonValue.B.toString()+"\":\"x\"}", mapper.writeValueAsString(input));
214+
}
215+
210216
/**
211217
* Test for ensuring that @JsonSerializable is used with Enum types as well
212218
* as with any other types.

0 commit comments

Comments
 (0)