Skip to content

Commit

Permalink
Fix #1322
Browse files Browse the repository at this point in the history
  • Loading branch information
cowtowncoder committed Aug 16, 2016
1 parent 958c824 commit 2936ba5
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 27 deletions.
5 changes: 5 additions & 0 deletions release-notes/VERSION
Expand Up @@ -4,6 +4,11 @@ Project: jackson-databind
=== Releases ===
------------------------------------------------------------------------

2.7.7 (not yet released)

#1322: EnumMap keys not using enum's `@JsonProperty` values unlike Enum values
(reported by MichaelChambers@github)

2.7.6 (23-Jul-2016)

#1215: Problem with type specialization for Maps with `@JsonDeserialize(as=subtype)`
Expand Down
Expand Up @@ -7,6 +7,7 @@
import com.fasterxml.jackson.core.*;
import com.fasterxml.jackson.databind.*;
import com.fasterxml.jackson.databind.ser.impl.PropertySerializerMap;
import com.fasterxml.jackson.databind.util.EnumValues;

@SuppressWarnings("serial")
public class StdKeySerializers
Expand Down Expand Up @@ -64,6 +65,7 @@ public static JsonSerializer<Object> getStdKeySerializer(SerializationConfig con
*
* @since 2.7
*/
@SuppressWarnings("unchecked")
public static JsonSerializer<Object> getFallbackKeySerializer(SerializationConfig config,
Class<?> rawKeyType)
{
Expand All @@ -78,7 +80,8 @@ public static JsonSerializer<Object> getFallbackKeySerializer(SerializationConfi
return new Dynamic();
}
if (rawKeyType.isEnum()) {
return new Default(Default.TYPE_ENUM, rawKeyType);
return EnumKeySerializer.construct(rawKeyType,
EnumValues.constructFromName(config, (Class<Enum<?>>) rawKeyType));
}
}
return DEFAULT_KEY_SERIALIZER;
Expand Down Expand Up @@ -205,4 +208,37 @@ public void serialize(Object value, JsonGenerator g, SerializerProvider provider
g.writeFieldName((String) value);
}
}

/**
* Specialized instance to use for Enum keys, as per [databind#1322]
*
* @since 2.8
*/
public static class EnumKeySerializer extends StdSerializer<Object>
{
protected final EnumValues _values;

protected EnumKeySerializer(Class<?> enumType, EnumValues values) {
super(enumType, false);
_values = values;
}

public static EnumKeySerializer construct(Class<?> enumType,
EnumValues enumValues)
{
return new EnumKeySerializer(enumType, enumValues);
}

@Override
public void serialize(Object value, JsonGenerator g, SerializerProvider serializers)
throws IOException
{
if (serializers.isEnabled(SerializationFeature.WRITE_ENUMS_USING_TO_STRING)) {
g.writeFieldName(value.toString());
return;
}
Enum<?> en = (Enum<?>) value;
g.writeFieldName(_values.serializedValueFor(en));
}
}
}
Expand Up @@ -432,6 +432,14 @@ public void testEnumsWithJsonPropertyInSet() throws Exception
assertEquals("[\"aleph\"]",
MAPPER.writeValueAsString(EnumSet.of(EnumWithJsonProperty.A)));
}

// [databind#1322]
public void testEnumsWithJsonPropertyAsKey() throws Exception
{
EnumMap<EnumWithJsonProperty,String> input = new EnumMap<EnumWithJsonProperty,String>(EnumWithJsonProperty.class);
input.put(EnumWithJsonProperty.A, "b");
assertEquals("{\"aleph\":\"b\"}", MAPPER.writeValueAsString(input));
}
}

// [JACKSON-757], non-inner enum
Expand Down

This file was deleted.

0 comments on commit 2936ba5

Please sign in to comment.