|
| 1 | +package com.fasterxml.jackson.failing; |
| 2 | + |
| 3 | +import com.fasterxml.jackson.annotation.*; |
| 4 | +import com.fasterxml.jackson.databind.*; |
| 5 | +import com.fasterxml.jackson.databind.annotation.*; |
| 6 | +import com.fasterxml.jackson.databind.util.StdConverter; |
| 7 | + |
| 8 | +// for [databind#795] |
| 9 | +public class ConvertingAbstractSerializer795Test extends BaseMapTest |
| 10 | +{ |
| 11 | + public static abstract class AbstractCustomType { |
| 12 | + final String value; |
| 13 | + public AbstractCustomType(String v) { |
| 14 | + this.value = v; |
| 15 | + } |
| 16 | + } |
| 17 | + |
| 18 | + public static class ConcreteCustomType extends AbstractCustomType { |
| 19 | + public ConcreteCustomType(String v) { |
| 20 | + super(v); |
| 21 | + } |
| 22 | + } |
| 23 | + |
| 24 | + public static class AbstractCustomTypeDeserializationConverter extends StdConverter<String, AbstractCustomType>{ |
| 25 | + |
| 26 | + @Override |
| 27 | + public AbstractCustomType convert(String arg) { |
| 28 | + return new ConcreteCustomType(arg); |
| 29 | + } |
| 30 | + } |
| 31 | + |
| 32 | + public static class AbstractCustomTypeUser { |
| 33 | + @JsonProperty |
| 34 | + @JsonDeserialize(converter = AbstractCustomTypeDeserializationConverter.class) |
| 35 | + private final AbstractCustomType customField; |
| 36 | + |
| 37 | + @JsonCreator AbstractCustomTypeUser(@JsonProperty("customField") |
| 38 | + AbstractCustomType customField) { |
| 39 | + this.customField = customField; |
| 40 | + } |
| 41 | + } |
| 42 | + |
| 43 | + public static class NonAbstractCustomType { |
| 44 | + final String value; |
| 45 | + public NonAbstractCustomType(String v) { |
| 46 | + this.value = v; |
| 47 | + } |
| 48 | + } |
| 49 | + |
| 50 | + |
| 51 | + public static class NonAbstractCustomTypeDeserializationConverter extends StdConverter<String, NonAbstractCustomType>{ |
| 52 | + |
| 53 | + @Override |
| 54 | + public NonAbstractCustomType convert(String arg) { |
| 55 | + return new NonAbstractCustomType(arg); |
| 56 | + } |
| 57 | + } |
| 58 | + |
| 59 | + |
| 60 | + public static class NonAbstractCustomTypeUser { |
| 61 | + @JsonProperty |
| 62 | + @JsonDeserialize(converter = NonAbstractCustomTypeDeserializationConverter.class) |
| 63 | + private final NonAbstractCustomType customField; |
| 64 | + |
| 65 | + @JsonCreator NonAbstractCustomTypeUser(@JsonProperty("customField") NonAbstractCustomType customField) { |
| 66 | + this.customField = customField; |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + private static final ObjectMapper JSON_MAPPER = new ObjectMapper(); |
| 71 | + |
| 72 | + public void testAbstractTypeDeserialization() throws Exception { |
| 73 | + String test="{\"customField\": \"customString\"}"; |
| 74 | + AbstractCustomTypeUser cu = JSON_MAPPER.readValue(test, AbstractCustomTypeUser.class); |
| 75 | + assertNotNull(cu); |
| 76 | + } |
| 77 | + |
| 78 | + public void testNonAbstractDeserialization() throws Exception { |
| 79 | + String test="{\"customField\": \"customString\"}"; |
| 80 | + NonAbstractCustomTypeUser cu = JSON_MAPPER.readValue(test, NonAbstractCustomTypeUser.class); |
| 81 | + assertNotNull(cu); |
| 82 | + } |
| 83 | +} |
0 commit comments