|
2 | 2 |
|
3 | 3 | import java.io.IOException;
|
4 | 4 | import java.math.BigDecimal;
|
| 5 | +import java.util.Collection; |
5 | 6 | import java.util.EnumMap;
|
6 | 7 | import java.util.EnumSet;
|
7 | 8 | import java.util.Map;
|
|
12 | 13 | import com.fasterxml.jackson.core.type.TypeReference;
|
13 | 14 | import com.fasterxml.jackson.databind.*;
|
14 | 15 | import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
|
| 16 | +import com.fasterxml.jackson.databind.deser.std.EnumDeserializer; |
15 | 17 | import com.fasterxml.jackson.databind.deser.std.StdDeserializer;
|
| 18 | +import com.fasterxml.jackson.databind.introspect.AnnotatedMethod; |
16 | 19 | import com.fasterxml.jackson.databind.module.SimpleModule;
|
17 | 20 |
|
18 | 21 | @SuppressWarnings("serial")
|
@@ -123,7 +126,34 @@ protected enum TestEnum324
|
123 | 126 | throw new RuntimeException("Foobar!");
|
124 | 127 | }
|
125 | 128 | }
|
126 |
| - |
| 129 | + |
| 130 | + // [Issue#745] |
| 131 | + static class DelegatingDeserializers extends Deserializers.Base |
| 132 | + { |
| 133 | + @Override |
| 134 | + public JsonDeserializer<?> findEnumDeserializer(final Class<?> type, final DeserializationConfig config, final BeanDescription beanDesc) throws JsonMappingException { |
| 135 | + final Collection<AnnotatedMethod> factoryMethods = beanDesc.getFactoryMethods(); |
| 136 | + if (factoryMethods != null) { |
| 137 | + for (AnnotatedMethod am : factoryMethods) { |
| 138 | + final JsonCreator creator = am.getAnnotation(JsonCreator.class); |
| 139 | + if (creator != null) { |
| 140 | + return EnumDeserializer.deserializerForCreator(config, type, am); |
| 141 | + } |
| 142 | + } |
| 143 | + } |
| 144 | + return null; |
| 145 | + } |
| 146 | + } |
| 147 | + |
| 148 | + // [Issue#745] |
| 149 | + static class DelegatingDeserializersModule extends SimpleModule |
| 150 | + { |
| 151 | + @Override |
| 152 | + public void setupModule(final SetupContext context) { |
| 153 | + context.addDeserializers(new DelegatingDeserializers()); |
| 154 | + } |
| 155 | + } |
| 156 | + |
127 | 157 | /*
|
128 | 158 | /**********************************************************
|
129 | 159 | /* Tests
|
@@ -424,4 +454,14 @@ public void testIndexAsString() throws Exception
|
424 | 454 | en = MAPPER.readValue(quote("1"), TestEnum.class);
|
425 | 455 | assertSame(TestEnum.values()[1], en);
|
426 | 456 | }
|
| 457 | + |
| 458 | + // [Issue#745] |
| 459 | + public void testDeserializerForCreatorWithEnumMaps() throws Exception |
| 460 | + { |
| 461 | + final ObjectMapper mapper = new ObjectMapper(); |
| 462 | + mapper.registerModule(new DelegatingDeserializersModule()); |
| 463 | + EnumMap<EnumWithCreator,String> value = mapper.readValue("{\"enumA\":\"value\"}", |
| 464 | + new TypeReference<EnumMap<EnumWithCreator,String>>() {}); |
| 465 | + assertEquals("value", value.get(EnumWithCreator.A)); |
| 466 | + } |
427 | 467 | }
|
0 commit comments