|
| 1 | +package com.fasterxml.jackson.databind.creators; |
| 2 | + |
| 3 | +import java.util.*; |
| 4 | + |
| 5 | +import com.fasterxml.jackson.annotation.JsonCreator; |
| 6 | +import com.fasterxml.jackson.annotation.JsonInclude; |
| 7 | +import com.fasterxml.jackson.annotation.JsonProperty; |
| 8 | + |
| 9 | +import com.fasterxml.jackson.databind.*; |
| 10 | + |
| 11 | +public class TestCreators541 extends BaseMapTest |
| 12 | +{ |
| 13 | + static final class Foo { |
| 14 | + |
| 15 | + @JsonProperty("foo") |
| 16 | + private Map<Integer, Bar> foo; |
| 17 | + @JsonProperty("anumber") |
| 18 | + private long anumber; |
| 19 | + |
| 20 | + public Foo() { |
| 21 | + anumber = 0; |
| 22 | + } |
| 23 | + |
| 24 | + public Map<Integer, Bar> getFoo() { |
| 25 | + return foo; |
| 26 | + } |
| 27 | + |
| 28 | + public long getAnumber() { |
| 29 | + return anumber; |
| 30 | + } |
| 31 | + } |
| 32 | + |
| 33 | + static final class Bar { |
| 34 | + |
| 35 | + private final long p; |
| 36 | + private final List<String> stuff; |
| 37 | + |
| 38 | + @JsonCreator |
| 39 | + public Bar(@JsonProperty("p") long p, @JsonProperty("stuff") List<String> stuff) { |
| 40 | + this.p = p; |
| 41 | + this.stuff = stuff; |
| 42 | + } |
| 43 | + |
| 44 | + @JsonProperty("s") |
| 45 | + public List<String> getStuff() { |
| 46 | + return stuff; |
| 47 | + } |
| 48 | + |
| 49 | + @JsonProperty("stuff") |
| 50 | + private List<String> getStuffDeprecated() { |
| 51 | + return stuff; |
| 52 | + } |
| 53 | + |
| 54 | + public long getP() { |
| 55 | + return p; |
| 56 | + } |
| 57 | + } |
| 58 | + /* |
| 59 | + /********************************************************** |
| 60 | + /* Test methods |
| 61 | + /********************************************************** |
| 62 | + */ |
| 63 | + |
| 64 | + public void testCreator541() throws Exception |
| 65 | + { |
| 66 | + ObjectMapper mapper = new ObjectMapper(); |
| 67 | + |
| 68 | + mapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES); |
| 69 | + mapper.disable(SerializationFeature.FAIL_ON_EMPTY_BEANS); |
| 70 | + mapper.disable( |
| 71 | + MapperFeature.AUTO_DETECT_CREATORS, |
| 72 | + MapperFeature.AUTO_DETECT_FIELDS, |
| 73 | + MapperFeature.AUTO_DETECT_GETTERS, |
| 74 | + MapperFeature.AUTO_DETECT_IS_GETTERS, |
| 75 | + MapperFeature.AUTO_DETECT_SETTERS, |
| 76 | + MapperFeature.USE_GETTERS_AS_SETTERS |
| 77 | + ); |
| 78 | + mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL); |
| 79 | + |
| 80 | + final String JSON = "{\n" |
| 81 | + + " \"foo\": {\n" |
| 82 | + + " \"0\": {\n" |
| 83 | + + " \"p\": 0,\n" |
| 84 | + + " \"stuff\": [\n" |
| 85 | + + " \"a\", \"b\" \n" |
| 86 | + + " ] \n" |
| 87 | + + " },\n" |
| 88 | + + " \"1\": {\n" |
| 89 | + + " \"p\": 1000,\n" |
| 90 | + + " \"stuff\": [\n" |
| 91 | + + " \"c\", \"d\" \n" |
| 92 | + + " ] \n" |
| 93 | + + " },\n" |
| 94 | + + " \"2\": {\n" |
| 95 | + + " \"p\": 2000,\n" |
| 96 | + + " \"stuff\": [\n" |
| 97 | + + " ] \n" |
| 98 | + + " }\n" |
| 99 | + + " },\n" |
| 100 | + + " \"anumber\": 25385874\n" |
| 101 | + + "}"; |
| 102 | + |
| 103 | + Foo obj = mapper.readValue(JSON, Foo.class); |
| 104 | + assertNotNull(obj); |
| 105 | + assertNotNull(obj.foo); |
| 106 | + assertEquals(3, obj.foo.size()); |
| 107 | + assertEquals(25385874L, obj.getAnumber()); |
| 108 | + } |
| 109 | +} |
0 commit comments