|
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,21 @@ 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 | + var node = new ObjectMapper().readTree(""" |
| 656 | + { "longs" : [ "foo:1", "bar:2" ] } |
| 657 | + """); |
| 658 | + |
| 659 | + var collection = new WithCustomMappedPrimitiveCollection(); |
| 660 | + collection.longs = List.of(3L); |
| 661 | + |
| 662 | + var result = reader.doMerge((ObjectNode) node, collection, new ObjectMapper()); |
| 663 | + |
| 664 | + assertThat(result.longs).isEqualTo(List.of(1L, 2L)); |
| 665 | + } |
| 666 | + |
648 | 667 | @SuppressWarnings("unchecked") |
649 | 668 | private static <T> T as(Object source, Class<T> type) { |
650 | 669 |
|
@@ -900,4 +919,31 @@ static class Apple extends Fruit { |
900 | 919 | static class Pear extends Fruit { |
901 | 920 | String pear; |
902 | 921 | } |
| 922 | + |
| 923 | + // GH-2261 |
| 924 | + static class WithCustomMappedPrimitiveCollection { |
| 925 | + |
| 926 | + @JsonDeserialize(contentUsing = CustomDeserializer.class) // |
| 927 | + List<Long> longs; |
| 928 | + |
| 929 | + @SuppressWarnings("serial") |
| 930 | + static class CustomDeserializer extends StdDeserializer<Long> { |
| 931 | + |
| 932 | + protected CustomDeserializer() { |
| 933 | + super(Long.class); |
| 934 | + } |
| 935 | + |
| 936 | + /* |
| 937 | + * (non-Javadoc) |
| 938 | + * @see com.fasterxml.jackson.databind.JsonDeserializer#deserialize(com.fasterxml.jackson.core.JsonParser, com.fasterxml.jackson.databind.DeserializationContext) |
| 939 | + */ |
| 940 | + @Override |
| 941 | + public Long deserialize(JsonParser p, DeserializationContext ctxt) throws IOException, JacksonException { |
| 942 | + |
| 943 | + var elements = p.getText().split(":"); |
| 944 | + |
| 945 | + return Long.valueOf(elements[elements.length - 1]); |
| 946 | + } |
| 947 | + } |
| 948 | + } |
903 | 949 | } |
0 commit comments