|
| 1 | +package com.fasterxml.jackson.failing; |
| 2 | + |
| 3 | +import java.util.List; |
| 4 | + |
| 5 | +import com.fasterxml.jackson.annotation.*; |
| 6 | +import com.fasterxml.jackson.core.type.TypeReference; |
| 7 | +import com.fasterxml.jackson.databind.*; |
| 8 | +import com.fasterxml.jackson.databind.annotation.JsonDeserialize; |
| 9 | + |
| 10 | +public class BuilderDeserializationTest921 |
| 11 | + extends BaseMapTest |
| 12 | +{ |
| 13 | + public static class MyPOJO { |
| 14 | + public String x; |
| 15 | + public String y; |
| 16 | + |
| 17 | + @JsonCreator |
| 18 | + public MyPOJO(@JsonProperty("x") String x, @JsonProperty("y") String y) { |
| 19 | + this.x = x; |
| 20 | + this.y = y; |
| 21 | + } |
| 22 | + } |
| 23 | + |
| 24 | + @JsonDeserialize(builder = MyGenericPOJO.Builder.class) |
| 25 | + public static class MyGenericPOJO<T> { |
| 26 | + private List<T> data; |
| 27 | + |
| 28 | + private MyGenericPOJO(List<T> d) { |
| 29 | + data = d; |
| 30 | + } |
| 31 | + |
| 32 | + public List<T> getData() { |
| 33 | + return data; |
| 34 | + } |
| 35 | + |
| 36 | + public static class Builder<T> { |
| 37 | + private List<T> data; |
| 38 | + |
| 39 | + public Builder<T> withData(List<T> d) { |
| 40 | + data = d; |
| 41 | + return this; |
| 42 | + } |
| 43 | + |
| 44 | + public MyGenericPOJO<T> build() { |
| 45 | + return new MyGenericPOJO<T>(data); |
| 46 | + } |
| 47 | + } |
| 48 | + } |
| 49 | + |
| 50 | + public static class MyGenericPOJOWithCreator<T> { |
| 51 | + private List<T> data; |
| 52 | + |
| 53 | + private MyGenericPOJOWithCreator(List<T> d) { |
| 54 | + data = d; |
| 55 | + } |
| 56 | + |
| 57 | + @JsonCreator |
| 58 | + public static <T> MyGenericPOJOWithCreator<T> create(@JsonProperty("data") List<T> data) { |
| 59 | + return new MyGenericPOJOWithCreator.Builder<T>().withData(data).build(); |
| 60 | + } |
| 61 | + |
| 62 | + public List<T> getData() { |
| 63 | + return data; |
| 64 | + } |
| 65 | + |
| 66 | + public static class Builder<T> { |
| 67 | + private List<T> data; |
| 68 | + |
| 69 | + public Builder<T> withData(List<T> d) { |
| 70 | + data = d; |
| 71 | + return this; |
| 72 | + } |
| 73 | + |
| 74 | + public MyGenericPOJOWithCreator<T> build() { |
| 75 | + return new MyGenericPOJOWithCreator<T>(data); |
| 76 | + } |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + public void testWithBuilder() throws Exception { |
| 81 | + final ObjectMapper mapper = new ObjectMapper(); |
| 82 | + final String json = aposToQuotes("{ 'data': [ { 'x': 'x', 'y': 'y' } ] }"); |
| 83 | + final MyGenericPOJO<MyPOJO> deserialized = |
| 84 | + mapper.readValue(json, new TypeReference<MyGenericPOJO<MyPOJO>>() {}); |
| 85 | + assertEquals(1, deserialized.data.size()); |
| 86 | + Object ob = deserialized.data.get(0); |
| 87 | + assertNotNull(ob); |
| 88 | + assertEquals(MyPOJO.class, ob.getClass()); |
| 89 | + } |
| 90 | + |
| 91 | + public void testWithCreator() throws Exception { |
| 92 | + final ObjectMapper mapper = new ObjectMapper(); |
| 93 | + final String json = aposToQuotes("{ 'data': [ { 'x': 'x', 'y': 'y' } ] }"); |
| 94 | + final MyGenericPOJOWithCreator<MyPOJO> deserialized = |
| 95 | + mapper.readValue(json, |
| 96 | + new TypeReference<MyGenericPOJOWithCreator<MyPOJO>>() {}); |
| 97 | + assertEquals(1, deserialized.data.size()); |
| 98 | + Object ob = deserialized.data.get(0); |
| 99 | + assertNotNull(ob); |
| 100 | + assertEquals(MyPOJO.class, ob.getClass()); |
| 101 | + } |
| 102 | + } |
0 commit comments