• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 package com.fasterxml.jackson.databind.deser.creators;
2 
3 import java.util.List;
4 import java.util.Map;
5 
6 import com.fasterxml.jackson.annotation.*;
7 
8 import com.fasterxml.jackson.databind.*;
9 import com.fasterxml.jackson.databind.introspect.AnnotatedMember;
10 import com.fasterxml.jackson.databind.introspect.AnnotatedParameter;
11 import com.fasterxml.jackson.databind.introspect.JacksonAnnotationIntrospector;
12 
13 // Misc Creator tests, part 3
14 public class TestCreators3 extends BaseMapTest
15 {
16     static final class Foo {
17 
18         @JsonProperty("foo")
19         protected Map<Integer, Bar> foo;
20         @JsonProperty("anumber")
21         protected long anumber;
22 
Foo()23         public Foo() {
24             anumber = 0;
25         }
26 
getFoo()27         public Map<Integer, Bar> getFoo() {
28             return foo;
29         }
30 
getAnumber()31         public long getAnumber() {
32             return anumber;
33         }
34     }
35 
36     static final class Bar {
37 
38         private final long p;
39         private final List<String> stuff;
40 
41         @JsonCreator
Bar(@sonProperty"p") long p, @JsonProperty("stuff") List<String> stuff)42         public Bar(@JsonProperty("p") long p, @JsonProperty("stuff") List<String> stuff) {
43             this.p = p;
44             this.stuff = stuff;
45         }
46 
47         @JsonProperty("s")
getStuff()48         public List<String> getStuff() {
49             return stuff;
50         }
51 
52         @JsonProperty("stuff")
getStuffDeprecated()53         private List<String> getStuffDeprecated() {
54             return stuff;
55         }
56 
getP()57         public long getP() {
58             return p;
59         }
60     }
61 
62     // [databind#421]
63 
64     static class MultiCtor
65     {
66         protected String _a, _b;
67 
MultiCtor()68         private MultiCtor() { }
MultiCtor(String a, String b, Boolean c)69         private MultiCtor(String a, String b, Boolean c) {
70             if (c == null) {
71                 throw new RuntimeException("Wrong factory!");
72             }
73             _a = a;
74             _b = b;
75         }
76 
77         @JsonCreator
factory(@sonProperty"a") String a, @JsonProperty("b") String b)78         static MultiCtor factory(@JsonProperty("a") String a, @JsonProperty("b") String b) {
79             return new MultiCtor(a, b, Boolean.TRUE);
80         }
81     }
82 
83     @SuppressWarnings("serial")
84     static class MyParamIntrospector extends JacksonAnnotationIntrospector
85     {
86         @Override
findImplicitPropertyName(AnnotatedMember param)87         public String findImplicitPropertyName(AnnotatedMember param) {
88             if (param instanceof AnnotatedParameter) {
89                 AnnotatedParameter ap = (AnnotatedParameter) param;
90                 switch (ap.getIndex()) {
91                 case 0: return "a";
92                 case 1: return "b";
93                 case 2: return "c";
94                 default:
95                     return "param"+ap.getIndex();
96                 }
97             }
98             return super.findImplicitPropertyName(param);
99         }
100     }
101 
102     // [databind#1853]
103     public static class Product1853 {
104         String name;
105 
106         public Object other, errors;
107 
108         @JsonCreator(mode = JsonCreator.Mode.PROPERTIES)
Product1853(@sonProperty"name") String name)109         public Product1853(@JsonProperty("name") String name) {
110             this.name = "PROP:" + name;
111         }
112 
113         @JsonCreator(mode = JsonCreator.Mode.DELEGATING)
from(String name)114         public static Product1853 from(String name){
115             return new Product1853(false, "DELEG:"+name);
116         }
117 
Product1853(boolean bogus, String name)118         private Product1853(boolean bogus, String name) {
119             this.name = name;
120         }
121 
122         @JsonValue
getName()123         public String getName(){
124             return name;
125         }
126     }
127 
128     /*
129     /**********************************************************
130     /* Test methods
131     /**********************************************************
132      */
133 
134     private final ObjectMapper MAPPER = newJsonMapper();
135 
testCreator541()136     public void testCreator541() throws Exception
137     {
138         ObjectMapper mapper = jsonMapperBuilder()
139                 .disable(
140                 MapperFeature.AUTO_DETECT_CREATORS,
141                 MapperFeature.AUTO_DETECT_FIELDS,
142                 MapperFeature.AUTO_DETECT_GETTERS,
143                 MapperFeature.AUTO_DETECT_IS_GETTERS,
144                 MapperFeature.AUTO_DETECT_SETTERS,
145                 MapperFeature.USE_GETTERS_AS_SETTERS)
146                 .disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES)
147                 .disable(SerializationFeature.FAIL_ON_EMPTY_BEANS)
148                 .serializationInclusion(JsonInclude.Include.NON_NULL)
149         .build();
150 
151         final String JSON = "{\n"
152                 + "    \"foo\": {\n"
153                 + "        \"0\": {\n"
154                 + "            \"p\": 0,\n"
155                 + "            \"stuff\": [\n"
156                 + "              \"a\", \"b\" \n"
157                 + "            ]   \n"
158                 + "        },\n"
159                 + "        \"1\": {\n"
160                 + "            \"p\": 1000,\n"
161                 + "            \"stuff\": [\n"
162                 + "              \"c\", \"d\" \n"
163                 + "            ]   \n"
164                 + "        },\n"
165                 + "        \"2\": {\n"
166                 + "            \"p\": 2000,\n"
167                 + "            \"stuff\": [\n"
168                 + "            ]   \n"
169                 + "        }\n"
170                 + "    },\n"
171                 + "    \"anumber\": 25385874\n"
172                 + "}";
173 
174         Foo obj = mapper.readValue(JSON, Foo.class);
175         assertNotNull(obj);
176         assertNotNull(obj.foo);
177         assertEquals(3, obj.foo.size());
178         assertEquals(25385874L, obj.getAnumber());
179     }
180 
181     // [databind#421]
testMultiCtor421()182     public void testMultiCtor421() throws Exception
183     {
184         final ObjectMapper mapper = newJsonMapper();
185         mapper.setAnnotationIntrospector(new MyParamIntrospector());
186 
187         MultiCtor bean = mapper.readValue(aposToQuotes("{'a':'123','b':'foo'}"), MultiCtor.class);
188         assertNotNull(bean);
189         assertEquals("123", bean._a);
190         assertEquals("foo", bean._b);
191     }
192 
193     // [databind#1853]
testSerialization()194     public void testSerialization() throws Exception {
195         assertEquals(quote("testProduct"),
196                 MAPPER.writeValueAsString(new Product1853(false, "testProduct")));
197     }
198 
testDeserializationFromObject()199     public void testDeserializationFromObject() throws Exception {
200         final String EXAMPLE_DATA = "{\"name\":\"dummy\",\"other\":{},\"errors\":{}}";
201         assertEquals("PROP:dummy", MAPPER.readValue(EXAMPLE_DATA, Product1853.class).getName());
202     }
203 
testDeserializationFromString()204     public void testDeserializationFromString() throws Exception {
205         assertEquals("DELEG:testProduct",
206                 MAPPER.readValue(quote("testProduct"), Product1853.class).getName());
207     }
208 }
209 
210