|
7 | 7 | import com.fasterxml.jackson.core.JsonGenerator;
|
8 | 8 | import com.fasterxml.jackson.core.type.TypeReference;
|
9 | 9 | import com.fasterxml.jackson.databind.*;
|
| 10 | +import com.fasterxml.jackson.databind.annotation.JsonDeserialize; |
10 | 11 | import com.fasterxml.jackson.databind.annotation.JsonSerialize;
|
11 | 12 | import com.fasterxml.jackson.databind.jsontype.TypeResolverBuilder;
|
12 | 13 | import com.fasterxml.jackson.databind.module.SimpleModule;
|
| 14 | +import com.fasterxml.jackson.databind.module.SimpleSerializers; |
| 15 | +import org.junit.Test; |
13 | 16 |
|
14 | 17 | public class TestKeySerializers extends BaseMapTest
|
15 | 18 | {
|
@@ -141,6 +144,37 @@ public void testCustomForEnum() throws IOException
|
141 | 144 | assertEquals("{\"stuff\":{\"xxxB\":\"bar\"}}", json);
|
142 | 145 | }
|
143 | 146 |
|
| 147 | + @JsonSerialize(keyUsing = ABCKeySerializer.class) |
| 148 | + public static enum ABCMixin { } |
| 149 | + |
| 150 | + public static enum Outer { |
| 151 | + inner; |
| 152 | + } |
| 153 | + |
| 154 | + public void testCustomEnumInnerMapKey() { |
| 155 | + Map<Outer, Object> outerMap = new HashMap<Outer, Object>(); |
| 156 | + Map<ABC, Map<String, String>> map = new EnumMap<ABC, Map<String, String>>(ABC.class); |
| 157 | + Map<String, String> innerMap = new HashMap<String, String>(); |
| 158 | + innerMap.put("one", "1"); |
| 159 | + map.put(ABC.A, innerMap); |
| 160 | + outerMap.put(Outer.inner, map); |
| 161 | + final ObjectMapper mapper = new ObjectMapper(); |
| 162 | + SimpleModule mod = new SimpleModule("test") { |
| 163 | + @Override |
| 164 | + public void setupModule(SetupContext context) { |
| 165 | + context.setMixInAnnotations(ABC.class, ABCMixin.class); |
| 166 | + SimpleSerializers keySerializers = new SimpleSerializers(); |
| 167 | + keySerializers.addSerializer(ABC.class, new ABCKeySerializer()); |
| 168 | + context.addKeySerializers(keySerializers); |
| 169 | + } |
| 170 | + }; |
| 171 | + mapper.registerModule(mod); |
| 172 | + JsonNode tree = mapper.convertValue(outerMap, JsonNode.class); |
| 173 | + JsonNode innerNode = tree.get("inner"); |
| 174 | + String key = innerNode.fieldNames().next(); |
| 175 | + assertEquals("xxxA", key); |
| 176 | + } |
| 177 | + |
144 | 178 | // [databind#838]
|
145 | 179 | public void testUnWrappedMapWithDefaultType() throws Exception{
|
146 | 180 | final ObjectMapper mapper = new ObjectMapper();
|
|
0 commit comments