|
1 | | -package com.fasterxml.jackson.databind.seq; |
| 1 | +package com.fasterxml.jackson.failing; |
2 | 2 |
|
3 | 3 | import java.io.IOException; |
4 | 4 | import java.util.HashMap; |
5 | 5 | import java.util.Map; |
6 | 6 |
|
7 | 7 | import org.junit.Assert; |
8 | | -import org.junit.Test; |
9 | 8 |
|
10 | | -import com.fasterxml.jackson.annotation.JsonAutoDetect.Visibility; |
11 | | -import com.fasterxml.jackson.annotation.PropertyAccessor; |
12 | 9 | import com.fasterxml.jackson.core.JsonGenerator; |
13 | 10 | import com.fasterxml.jackson.core.JsonProcessingException; |
| 11 | +import com.fasterxml.jackson.core.type.TypeReference; |
14 | 12 | import com.fasterxml.jackson.databind.*; |
15 | | -import com.fasterxml.jackson.databind.SerializerProvider; |
16 | 13 | import com.fasterxml.jackson.databind.module.SimpleModule; |
17 | | -import com.fasterxml.jackson.databind.type.MapType; |
18 | 14 |
|
19 | 15 | // for [databind#827] |
20 | | -public class PolyMapWriterTest extends BaseMapTest |
| 16 | +public class PolyMapWriter827Test extends BaseMapTest |
21 | 17 | { |
22 | 18 | static class CustomKey { |
23 | 19 | String a; |
24 | 20 | int b; |
25 | 21 |
|
26 | 22 | public String toString() { return "BAD-KEY"; } |
27 | 23 | } |
28 | | - |
| 24 | + |
29 | 25 | public class CustomKeySerializer extends JsonSerializer<CustomKey> { |
30 | 26 | @Override |
31 | 27 | public void serialize(CustomKey key, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) throws IOException, JsonProcessingException { |
32 | 28 | jsonGenerator.writeFieldName(key.a + "," + key.b); |
33 | 29 | } |
34 | 30 | } |
35 | | - |
36 | | - @Test |
37 | | - public void testPolyCustomKeySerializer() throws Exception { |
| 31 | + |
| 32 | + public void testPolyCustomKeySerializer() throws Exception |
| 33 | + { |
38 | 34 | ObjectMapper mapper = new ObjectMapper(); |
39 | | - mapper.setVisibility(PropertyAccessor.ALL, Visibility.NONE); |
40 | | - mapper.setVisibility(PropertyAccessor.FIELD, Visibility.ANY); |
41 | 35 | mapper.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL); |
42 | | - |
43 | | - SimpleModule module = new SimpleModule("keySerializerModule"); |
44 | | - module.addKeySerializer(CustomKey.class, new CustomKeySerializer()); |
45 | | - mapper.registerModule(module); |
| 36 | + |
| 37 | + mapper.registerModule(new SimpleModule("keySerializerModule") |
| 38 | + .addKeySerializer(CustomKey.class, new CustomKeySerializer())); |
46 | 39 |
|
47 | 40 | Map<CustomKey, String> map = new HashMap<CustomKey, String>(); |
48 | 41 | CustomKey key = new CustomKey(); |
49 | 42 | key.a = "foo"; |
50 | 43 | key.b = 1; |
51 | 44 | map.put(key, "bar"); |
52 | 45 |
|
53 | | - final MapType type = mapper.getTypeFactory().constructMapType( |
54 | | - Map.class, CustomKey.class, String.class); |
55 | | - final ObjectWriter writer = mapper.writerFor(type); |
| 46 | + final ObjectWriter writer = mapper.writerFor(new TypeReference<Map<CustomKey,String>>() { }); |
56 | 47 | String json = writer.writeValueAsString(map); |
57 | | - |
58 | 48 | Assert.assertEquals("[\"java.util.HashMap\",{\"foo,1\":\"bar\"}]", json); |
59 | 49 | } |
60 | | - |
61 | 50 | } |
0 commit comments