|
58 | 58 | import com.fasterxml.jackson.annotation.JsonSubTypes; |
59 | 59 | import com.fasterxml.jackson.annotation.JsonTypeInfo; |
60 | 60 | import com.fasterxml.jackson.annotation.JsonTypeInfo.As; |
| 61 | +import com.fasterxml.jackson.core.JacksonException; |
61 | 62 | import com.fasterxml.jackson.core.JsonParser; |
62 | 63 | import com.fasterxml.jackson.core.JsonProcessingException; |
63 | 64 | import com.fasterxml.jackson.databind.DeserializationContext; |
|
66 | 67 | import com.fasterxml.jackson.databind.JsonNode; |
67 | 68 | import com.fasterxml.jackson.databind.ObjectMapper; |
68 | 69 | import com.fasterxml.jackson.databind.PropertyNamingStrategy; |
| 70 | +import com.fasterxml.jackson.databind.annotation.JsonDeserialize; |
| 71 | +import com.fasterxml.jackson.databind.deser.std.StdDeserializer; |
69 | 72 | import com.fasterxml.jackson.databind.module.SimpleModule; |
70 | 73 | import com.fasterxml.jackson.databind.node.ObjectNode; |
71 | 74 |
|
@@ -109,6 +112,7 @@ void setUp() { |
109 | 112 | mappingContext.getPersistentEntity(ArrayHolder.class); |
110 | 113 | mappingContext.getPersistentEntity(Apple.class); |
111 | 114 | mappingContext.getPersistentEntity(Pear.class); |
| 115 | + mappingContext.getPersistentEntity(WithCustomMappedPrimitiveCollection.class); |
112 | 116 | mappingContext.afterPropertiesSet(); |
113 | 117 |
|
114 | 118 | this.entities = new PersistentEntities(Collections.singleton(mappingContext)); |
@@ -645,6 +649,19 @@ void nestedEntitiesWithReadonlyFieldAreKeptForPut() throws Exception { |
645 | 649 | assertThat(result.inner).isSameAs(inner); |
646 | 650 | } |
647 | 651 |
|
| 652 | + @Test // GH-2261 |
| 653 | + void deserializesCustomCollectionOfPrimitives() throws Exception { |
| 654 | + |
| 655 | + JsonNode node = new ObjectMapper().readTree("{ \"longs\" : [ \"foo:1\", \"bar:2\" ] }"); |
| 656 | + |
| 657 | + WithCustomMappedPrimitiveCollection collection = new WithCustomMappedPrimitiveCollection(); |
| 658 | + collection.longs = Arrays.asList(3L); |
| 659 | + |
| 660 | + WithCustomMappedPrimitiveCollection result = reader.doMerge((ObjectNode) node, collection, new ObjectMapper()); |
| 661 | + |
| 662 | + assertThat(result.longs).isEqualTo(Arrays.asList(1L, 2L)); |
| 663 | + } |
| 664 | + |
648 | 665 | @SuppressWarnings("unchecked") |
649 | 666 | private static <T> T as(Object source, Class<T> type) { |
650 | 667 |
|
@@ -904,4 +921,31 @@ static class Apple extends Fruit { |
904 | 921 | static class Pear extends Fruit { |
905 | 922 | String pear; |
906 | 923 | } |
| 924 | + |
| 925 | + // GH-2261 |
| 926 | + static class WithCustomMappedPrimitiveCollection { |
| 927 | + |
| 928 | + @JsonDeserialize(contentUsing = CustomDeserializer.class) // |
| 929 | + List<Long> longs; |
| 930 | + |
| 931 | + @SuppressWarnings("serial") |
| 932 | + static class CustomDeserializer extends StdDeserializer<Long> { |
| 933 | + |
| 934 | + protected CustomDeserializer() { |
| 935 | + super(Long.class); |
| 936 | + } |
| 937 | + |
| 938 | + /* |
| 939 | + * (non-Javadoc) |
| 940 | + * @see com.fasterxml.jackson.databind.JsonDeserializer#deserialize(com.fasterxml.jackson.core.JsonParser, com.fasterxml.jackson.databind.DeserializationContext) |
| 941 | + */ |
| 942 | + @Override |
| 943 | + public Long deserialize(JsonParser p, DeserializationContext ctxt) throws IOException, JacksonException { |
| 944 | + |
| 945 | + String[] elements = p.getText().split(":"); |
| 946 | + |
| 947 | + return Long.valueOf(elements[elements.length - 1]); |
| 948 | + } |
| 949 | + } |
| 950 | + } |
907 | 951 | } |
0 commit comments