1 package com.fasterxml.jackson.databind.convert; 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; AbstractCustomType(String v)13 public AbstractCustomType(String v) { 14 this.value = v; 15 } 16 } 17 18 public static class ConcreteCustomType extends AbstractCustomType { ConcreteCustomType(String v)19 public ConcreteCustomType(String v) { 20 super(v); 21 } 22 } 23 24 public static class AbstractCustomTypeDeserializationConverter extends StdConverter<String, AbstractCustomType>{ 25 26 @Override convert(String arg)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(@sonProperty"customField") AbstractCustomType cf)38 AbstractCustomTypeUser(@JsonProperty("customField") AbstractCustomType cf) { 39 this.customField = cf; 40 } 41 } 42 43 public static class NonAbstractCustomType { 44 final String value; NonAbstractCustomType(String v)45 public NonAbstractCustomType(String v) { 46 this.value = v; 47 } 48 } 49 50 public static class NonAbstractCustomTypeDeserializationConverter extends StdConverter<String, NonAbstractCustomType>{ 51 52 @Override convert(String arg)53 public NonAbstractCustomType convert(String arg) { 54 return new NonAbstractCustomType(arg); 55 } 56 } 57 58 public static class NonAbstractCustomTypeUser { 59 @JsonProperty 60 @JsonDeserialize(converter = NonAbstractCustomTypeDeserializationConverter.class) 61 private final NonAbstractCustomType customField; 62 NonAbstractCustomTypeUser(@sonProperty"customField") NonAbstractCustomType customField)63 @JsonCreator NonAbstractCustomTypeUser(@JsonProperty("customField") NonAbstractCustomType customField) { 64 this.customField = customField; 65 } 66 } 67 68 /* 69 /********************************************************** 70 /* Test methods 71 /********************************************************** 72 */ 73 74 private static final ObjectMapper JSON_MAPPER = new ObjectMapper(); 75 testAbstractTypeDeserialization()76 public void testAbstractTypeDeserialization() throws Exception { 77 String test="{\"customField\": \"customString\"}"; 78 AbstractCustomTypeUser cu = JSON_MAPPER.readValue(test, AbstractCustomTypeUser.class); 79 assertNotNull(cu); 80 } 81 testNonAbstractDeserialization()82 public void testNonAbstractDeserialization() throws Exception { 83 String test="{\"customField\": \"customString\"}"; 84 NonAbstractCustomTypeUser cu = JSON_MAPPER.readValue(test, NonAbstractCustomTypeUser.class); 85 assertNotNull(cu); 86 } 87 } 88