From 7c72020b51ff44ffc79049812a3505409baf063c Mon Sep 17 00:00:00 2001 From: John Meyer Date: Mon, 30 Mar 2015 17:32:43 -0400 Subject: [PATCH] Fix #745 --- .../databind/deser/std/EnumDeserializer.java | 7 +++- .../deser/TestEnumDeserialization.java | 42 ++++++++++++++++++- 2 files changed, 47 insertions(+), 2 deletions(-) diff --git a/src/main/java/com/fasterxml/jackson/databind/deser/std/EnumDeserializer.java b/src/main/java/com/fasterxml/jackson/databind/deser/std/EnumDeserializer.java index c478a4d578..e9e7667422 100644 --- a/src/main/java/com/fasterxml/jackson/databind/deser/std/EnumDeserializer.java +++ b/src/main/java/com/fasterxml/jackson/databind/deser/std/EnumDeserializer.java @@ -218,7 +218,12 @@ public Object deserialize(JsonParser p, DeserializationContext ctxt) throws IOEx if (_deser != null) { value = _deser.deserialize(p, ctxt); } else { - value = p.getValueAsString(); + JsonToken curr = p.getCurrentToken(); + if (curr == JsonToken.VALUE_STRING || curr == JsonToken.FIELD_NAME) { + value = p.getText(); + } else { + value = p.getValueAsString(); + } } try { return _factory.invoke(_valueClass, value); diff --git a/src/test/java/com/fasterxml/jackson/databind/deser/TestEnumDeserialization.java b/src/test/java/com/fasterxml/jackson/databind/deser/TestEnumDeserialization.java index e553ec5167..cafce0db90 100644 --- a/src/test/java/com/fasterxml/jackson/databind/deser/TestEnumDeserialization.java +++ b/src/test/java/com/fasterxml/jackson/databind/deser/TestEnumDeserialization.java @@ -2,6 +2,7 @@ import java.io.IOException; import java.math.BigDecimal; +import java.util.Collection; import java.util.EnumMap; import java.util.EnumSet; import java.util.Map; @@ -12,7 +13,9 @@ import com.fasterxml.jackson.core.type.TypeReference; import com.fasterxml.jackson.databind.*; import com.fasterxml.jackson.databind.annotation.JsonDeserialize; +import com.fasterxml.jackson.databind.deser.std.EnumDeserializer; import com.fasterxml.jackson.databind.deser.std.StdDeserializer; +import com.fasterxml.jackson.databind.introspect.AnnotatedMethod; import com.fasterxml.jackson.databind.module.SimpleModule; @SuppressWarnings("serial") @@ -123,7 +126,34 @@ protected enum TestEnum324 throw new RuntimeException("Foobar!"); } } - + + // [Issue#745] + static class DelegatingDeserializers extends Deserializers.Base + { + @Override + public JsonDeserializer findEnumDeserializer(final Class type, final DeserializationConfig config, final BeanDescription beanDesc) throws JsonMappingException { + final Collection factoryMethods = beanDesc.getFactoryMethods(); + if (factoryMethods != null) { + for (AnnotatedMethod am : factoryMethods) { + final JsonCreator creator = am.getAnnotation(JsonCreator.class); + if (creator != null) { + return EnumDeserializer.deserializerForCreator(config, type, am); + } + } + } + return null; + } + } + + // [Issue#745] + static class DelegatingDeserializersModule extends SimpleModule + { + @Override + public void setupModule(final SetupContext context) { + context.addDeserializers(new DelegatingDeserializers()); + } + } + /* /********************************************************** /* Tests @@ -424,4 +454,14 @@ public void testIndexAsString() throws Exception en = MAPPER.readValue(quote("1"), TestEnum.class); assertSame(TestEnum.values()[1], en); } + + // [Issue#745] + public void testDeserializerForCreatorWithEnumMaps() throws Exception + { + final ObjectMapper mapper = new ObjectMapper(); + mapper.registerModule(new DelegatingDeserializersModule()); + EnumMap value = mapper.readValue("{\"enumA\":\"value\"}", + new TypeReference>() {}); + assertEquals("value", value.get(EnumWithCreator.A)); + } }