1 package com.fasterxml.jackson.databind.deser; 2 3 import com.fasterxml.jackson.annotation.JsonAlias; 4 import com.fasterxml.jackson.annotation.JsonCreator; 5 import com.fasterxml.jackson.annotation.JsonProperty; 6 import com.fasterxml.jackson.annotation.JsonSubTypes; 7 import com.fasterxml.jackson.annotation.JsonTypeInfo; 8 import com.fasterxml.jackson.databind.BaseMapTest; 9 import com.fasterxml.jackson.databind.MapperFeature; 10 import com.fasterxml.jackson.databind.ObjectMapper; 11 import com.fasterxml.jackson.databind.json.JsonMapper; 12 13 public class PropertyAliasTest extends BaseMapTest 14 { 15 static class AliasBean { 16 @JsonAlias({ "nm", "Name" }) 17 public String name; 18 19 int _xyz; 20 21 int _a; 22 23 @JsonCreator AliasBean(@sonProperty"a") @sonAlias"A") int a)24 public AliasBean(@JsonProperty("a") 25 @JsonAlias("A") int a) { 26 _a = a; 27 } 28 29 @JsonAlias({ "Xyz" }) setXyz(int x)30 public void setXyz(int x) { 31 _xyz = x; 32 } 33 } 34 35 static class AliasBean2378 { 36 String partitionId; 37 String _id; 38 AliasBean2378(boolean bogus, String partId, String userId)39 private AliasBean2378(boolean bogus, String partId, String userId) { 40 partitionId = partId; 41 _id = userId; 42 } 43 44 @JsonCreator create(@sonProperty"partitionId") String partId, @JsonProperty("id") @JsonAlias("userId") String userId)45 public static AliasBean2378 create(@JsonProperty("partitionId") String partId, 46 @JsonProperty("id") @JsonAlias("userId") String userId) { 47 return new AliasBean2378(false, partId, userId); 48 } 49 } 50 51 static class PolyWrapperForAlias { 52 @JsonTypeInfo(use = JsonTypeInfo.Id.NAME, 53 include = JsonTypeInfo.As.WRAPPER_ARRAY) 54 @JsonSubTypes({ 55 @JsonSubTypes.Type(value = AliasBean.class,name = "ab"), 56 }) 57 public Object value; 58 PolyWrapperForAlias()59 protected PolyWrapperForAlias() { } PolyWrapperForAlias(Object v)60 public PolyWrapperForAlias(Object v) { value = v; } 61 } 62 63 // [databind#2669] 64 static class Pojo2669 { 65 @JsonAlias({"nick", "name"}) 66 private String name; 67 getName()68 public String getName() { 69 return name; 70 } 71 setName(String name)72 public void setName(String name) { 73 this.name = name; 74 } 75 } 76 77 /* 78 /********************************************************************** 79 /* Test methods 80 /********************************************************************** 81 */ 82 83 private final ObjectMapper MAPPER = newJsonMapper(); 84 85 // [databind#1029] testSimpleAliases()86 public void testSimpleAliases() throws Exception 87 { 88 AliasBean bean; 89 90 // first, one indicated by field annotation, set via field 91 bean = MAPPER.readValue(aposToQuotes("{'Name':'Foobar','a':3,'xyz':37}"), 92 AliasBean.class); 93 assertEquals("Foobar", bean.name); 94 assertEquals(3, bean._a); 95 assertEquals(37, bean._xyz); 96 97 // then method-bound one 98 bean = MAPPER.readValue(aposToQuotes("{'name':'Foobar','a':3,'Xyz':37}"), 99 AliasBean.class); 100 assertEquals("Foobar", bean.name); 101 assertEquals(3, bean._a); 102 assertEquals(37, bean._xyz); 103 104 // and finally, constructor-backed one 105 bean = MAPPER.readValue(aposToQuotes("{'name':'Foobar','A':3,'xyz':37}"), 106 AliasBean.class); 107 assertEquals("Foobar", bean.name); 108 assertEquals(3, bean._a); 109 assertEquals(37, bean._xyz); 110 } 111 testAliasWithPolymorphic()112 public void testAliasWithPolymorphic() throws Exception 113 { 114 PolyWrapperForAlias value = MAPPER.readValue(aposToQuotes( 115 "{'value': ['ab', {'nm' : 'Bob', 'A' : 17} ] }" 116 ), PolyWrapperForAlias.class); 117 assertNotNull(value.value); 118 AliasBean bean = (AliasBean) value.value; 119 assertEquals("Bob", bean.name); 120 assertEquals(17, bean._a); 121 } 122 123 // [databind#2378] testAliasInFactoryMethod()124 public void testAliasInFactoryMethod() throws Exception 125 { 126 AliasBean2378 bean = MAPPER.readValue(aposToQuotes( 127 "{'partitionId' : 'a', 'userId' : '123'}" 128 ), AliasBean2378.class); 129 assertEquals("a", bean.partitionId); 130 assertEquals("123", bean._id); 131 } 132 133 // [databind#2669] testCaseInsensitiveAliases()134 public void testCaseInsensitiveAliases() throws Exception { 135 136 ObjectMapper mapper = JsonMapper.builder() 137 .enable(MapperFeature.ACCEPT_CASE_INSENSITIVE_PROPERTIES) 138 .build(); 139 140 String text = "{\"name\":\"test\"}"; 141 Pojo2669 pojo = mapper.readValue(text, Pojo2669.class); 142 assertNotNull(pojo); 143 assertEquals("test", pojo.getName()); 144 } 145 } 146