|
| 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 | + |
| 23 | + public Foo() { |
| 24 | + anumber = 0; |
| 25 | + } |
| 26 | + |
| 27 | + public Map<Integer, Bar> getFoo() { |
| 28 | + return foo; |
| 29 | + } |
| 30 | + |
| 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 |
| 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") |
| 48 | + public List<String> getStuff() { |
| 49 | + return stuff; |
| 50 | + } |
| 51 | + |
| 52 | + @JsonProperty("stuff") |
| 53 | + private List<String> getStuffDeprecated() { |
| 54 | + return stuff; |
| 55 | + } |
| 56 | + |
| 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 | + |
| 68 | + private MultiCtor() { } |
| 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 |
| 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 |
| 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 Product { |
| 104 | + String name; |
| 105 | + |
| 106 | + public Object other, errors; |
| 107 | + |
| 108 | + @JsonCreator(mode = JsonCreator.Mode.PROPERTIES) |
| 109 | + public Product(@JsonProperty("name") String name) { |
| 110 | + this.name = "PROP:" + name; |
| 111 | + } |
| 112 | + |
| 113 | + @JsonCreator(mode = JsonCreator.Mode.DELEGATING) |
| 114 | + public static Product from(String name){ |
| 115 | + return new Product(false, "DELEG:"+name); |
| 116 | + } |
| 117 | + |
| 118 | + private Product(boolean bogus, String name) { |
| 119 | + this.name = name; |
| 120 | + } |
| 121 | + |
| 122 | + @JsonValue |
| 123 | + public String getName(){ |
| 124 | + return name; |
| 125 | + } |
| 126 | + } |
| 127 | + |
| 128 | + /* |
| 129 | + /********************************************************** |
| 130 | + /* Test methods |
| 131 | + /********************************************************** |
| 132 | + */ |
| 133 | + |
| 134 | + private final ObjectMapper MAPPER = newObjectMapper(); |
| 135 | + |
| 136 | + public void testCreator541() throws Exception |
| 137 | + { |
| 138 | + ObjectMapper mapper = new ObjectMapper(); |
| 139 | + mapper.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 | + ); |
| 147 | + mapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES); |
| 148 | + mapper.disable(SerializationFeature.FAIL_ON_EMPTY_BEANS); |
| 149 | + mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL); |
| 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] |
| 182 | + public void testMultiCtor421() throws Exception |
| 183 | + { |
| 184 | + final ObjectMapper mapper = newObjectMapper(); |
| 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] |
| 194 | + public void testSerialization() throws Exception { |
| 195 | + assertEquals(quote("testProduct"), |
| 196 | + MAPPER.writeValueAsString(new Product(false, "testProduct"))); |
| 197 | + } |
| 198 | + |
| 199 | + public void testDeserializationFromObject() throws Exception { |
| 200 | + final String EXAMPLE_DATA = "{\"name\":\"dummy\",\"other\":{},\"errors\":{}}"; |
| 201 | + assertEquals("PROP:dummy", MAPPER.readValue(EXAMPLE_DATA, Product.class).getName()); |
| 202 | + } |
| 203 | + |
| 204 | + public void testDeserializationFromString() throws Exception { |
| 205 | + assertEquals("DELEG:testProduct", |
| 206 | + MAPPER.readValue(quote("testProduct"), Product.class).getName()); |
| 207 | + } |
| 208 | +} |
| 209 | + |
0 commit comments