1 package com.fasterxml.jackson.databind.deser; 2 3 import com.fasterxml.jackson.annotation.*; 4 import com.fasterxml.jackson.annotation.JsonAutoDetect.Visibility; 5 import com.fasterxml.jackson.databind.*; 6 import com.fasterxml.jackson.databind.annotation.*; 7 8 /** 9 * Unit tests for verifying that field-backed properties can also be 10 * deserialized as well as setter-accessible properties. 11 */ 12 public class TestFieldDeserialization 13 extends BaseMapTest 14 { 15 /* 16 /********************************************************** 17 /* Annotated helper classes 18 /********************************************************** 19 */ 20 21 static class SimpleFieldBean 22 { 23 public int x, y; 24 25 // not auto-detectable, not public 26 int z; 27 28 // ignored, not detectable either 29 @JsonIgnore public int a; 30 } 31 32 static class SimpleFieldBean2 33 { 34 @JsonDeserialize String[] values; 35 } 36 37 @JsonAutoDetect(fieldVisibility=Visibility.NONE) 38 static class NoAutoDetectBean 39 { 40 // not auto-detectable any more 41 public int z; 42 43 @JsonProperty("z") 44 public int _z; 45 } 46 47 // Let's test invalid bean too 48 static class DupFieldBean 49 { 50 public int z; 51 52 @JsonProperty("z") 53 public int _z; 54 } 55 56 public static class DupFieldBean2 57 { 58 @JsonProperty("foo") 59 public int _z; 60 61 @JsonDeserialize 62 private int foo; 63 } 64 65 public static class OkDupFieldBean 66 extends SimpleFieldBean 67 { 68 @JsonProperty("x") 69 protected int myX = 10; 70 71 @SuppressWarnings("hiding") 72 public int y = 11; 73 } 74 75 abstract static class Abstract { } 76 static class Concrete extends Abstract 77 { 78 String value; 79 Concrete(String v)80 public Concrete(String v) { value = v; } 81 } 82 83 static class AbstractWrapper { 84 @JsonDeserialize(as=Concrete.class) 85 public Abstract value; 86 } 87 88 /* 89 /********************************************************** 90 /* Main tests 91 /********************************************************** 92 */ 93 testSimpleAutoDetect()94 public void testSimpleAutoDetect() throws Exception 95 { 96 ObjectMapper m = new ObjectMapper(); 97 SimpleFieldBean result = m.readValue("{ \"x\" : -13 }", 98 SimpleFieldBean.class); 99 assertEquals(-13, result.x); 100 assertEquals(0, result.y); 101 } 102 testSimpleAnnotation()103 public void testSimpleAnnotation() throws Exception 104 { 105 ObjectMapper m = new ObjectMapper(); 106 SimpleFieldBean2 bean = m.readValue("{ \"values\" : [ \"x\", \"y\" ] }", 107 SimpleFieldBean2.class); 108 String[] values = bean.values; 109 assertNotNull(values); 110 assertEquals(2, values.length); 111 assertEquals("x", values[0]); 112 assertEquals("y", values[1]); 113 } 114 testNoAutoDetect()115 public void testNoAutoDetect() throws Exception 116 { 117 ObjectMapper m = new ObjectMapper(); 118 NoAutoDetectBean bean = m.readValue("{ \"z\" : 7 }", 119 NoAutoDetectBean.class); 120 assertEquals(7, bean._z); 121 } 122 testTypeAnnotation()123 public void testTypeAnnotation() throws Exception 124 { 125 ObjectMapper m = new ObjectMapper(); 126 AbstractWrapper w = m.readValue("{ \"value\" : \"abc\" }", 127 AbstractWrapper.class); 128 Abstract bean = w.value; 129 assertNotNull(bean); 130 assertEquals(Concrete.class, bean.getClass()); 131 assertEquals("abc", ((Concrete)bean).value); 132 } 133 testFailureDueToDups()134 public void testFailureDueToDups() throws Exception 135 { 136 try { 137 writeAndMap(new ObjectMapper(), new DupFieldBean()); 138 } catch (JsonMappingException e) { 139 verifyException(e, "Multiple fields representing property"); 140 } 141 } 142 testFailureDueToDups2()143 public void testFailureDueToDups2() throws Exception 144 { 145 try { 146 writeAndMap(new ObjectMapper(), new DupFieldBean2()); 147 } catch (JsonMappingException e) { 148 verifyException(e, "Multiple fields representing property"); 149 } 150 } 151 152 // For [JACKSON-226], acceptable field overrides testOkFieldOverride()153 public void testOkFieldOverride() throws Exception 154 { 155 ObjectMapper m = new ObjectMapper(); 156 OkDupFieldBean result = m.readValue("{ \"x\" : 1, \"y\" : 2 }", 157 OkDupFieldBean.class); 158 assertEquals(1, result.myX); 159 assertEquals(2, result.y); 160 } 161 } 162