Skip to content

Commit

Permalink
Fix #745
Browse files Browse the repository at this point in the history
  • Loading branch information
John Meyer committed Mar 30, 2015
1 parent 6c5826c commit 7c72020
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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")
Expand Down Expand Up @@ -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<AnnotatedMethod> 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
Expand Down Expand Up @@ -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<EnumWithCreator,String> value = mapper.readValue("{\"enumA\":\"value\"}",
new TypeReference<EnumMap<EnumWithCreator,String>>() {});
assertEquals("value", value.get(EnumWithCreator.A));
}
}

0 comments on commit 7c72020

Please sign in to comment.