|
| 1 | +package com.fasterxml.jackson.databind.convert; |
| 2 | + |
| 3 | +import java.util.Collections; |
| 4 | +import java.util.Map; |
| 5 | + |
| 6 | +import org.junit.jupiter.api.Test; |
| 7 | + |
| 8 | +import com.fasterxml.jackson.databind.*; |
| 9 | +import com.fasterxml.jackson.databind.module.SimpleModule; |
| 10 | +import com.fasterxml.jackson.databind.module.SimpleSerializers; |
| 11 | +import com.fasterxml.jackson.databind.ser.std.StdDelegatingSerializer; |
| 12 | +import com.fasterxml.jackson.databind.testutil.DatabindTestUtil; |
| 13 | +import com.fasterxml.jackson.databind.util.StdConverter; |
| 14 | + |
| 15 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 16 | + |
| 17 | +public class MapConversion4878Test extends DatabindTestUtil |
| 18 | +{ |
| 19 | + // [databind#4878] |
| 20 | + static class MapWrapper4878 { |
| 21 | + final Map<String, Object> value; |
| 22 | + |
| 23 | + MapWrapper4878(Map<String, Object> value) { |
| 24 | + this.value = value; |
| 25 | + } |
| 26 | + } |
| 27 | + |
| 28 | + static class WrapperConverter4878 extends StdConverter<MapWrapper4878, Object> { |
| 29 | + @Override |
| 30 | + public Object convert(MapWrapper4878 value) { |
| 31 | + return value.value; |
| 32 | + } |
| 33 | + } |
| 34 | + |
| 35 | + @SuppressWarnings("serial") |
| 36 | + static class Serializers4878 extends SimpleSerializers { |
| 37 | + @Override |
| 38 | + public JsonSerializer<?> findSerializer(SerializationConfig config, JavaType type, BeanDescription beanDesc) { |
| 39 | + Class<?> rawClass = type.getRawClass(); |
| 40 | + if (MapWrapper4878.class.isAssignableFrom(rawClass)) { |
| 41 | + return new StdDelegatingSerializer(new WrapperConverter4878()); |
| 42 | + } |
| 43 | + return super.findSerializer(config, type, beanDesc); |
| 44 | + } |
| 45 | + } |
| 46 | + |
| 47 | + // [databind#4878] |
| 48 | + @Test |
| 49 | + public void testMapConverter() throws Exception |
| 50 | + { |
| 51 | + SimpleModule sm = new SimpleModule(); |
| 52 | + sm.setSerializers(new Serializers4878()); |
| 53 | + final ObjectMapper mapper = jsonMapperBuilder() |
| 54 | + .addModule(sm) |
| 55 | + .build(); |
| 56 | + String json = mapper.writeValueAsString(new MapWrapper4878(Collections.singletonMap("a", 1))); |
| 57 | + assertEquals(a2q("{'a':1}"), json); |
| 58 | + } |
| 59 | +} |
0 commit comments