|
| 1 | +package com.fasterxml.jackson.failing; |
| 2 | + |
| 3 | +import java.util.*; |
| 4 | + |
| 5 | +import com.fasterxml.jackson.annotation.JsonCreator; |
| 6 | +import com.fasterxml.jackson.databind.*; |
| 7 | + |
| 8 | +// For [databind#936], losing parametric type information it seems |
| 9 | +public class PolymorphicListTest936 extends BaseMapTest |
| 10 | +{ |
| 11 | + // note: would prefer using CharSequence, but while abstract, that's deserialized |
| 12 | + // just fine as ... String |
| 13 | + static class StringyList<T extends java.io.Serializable> implements Collection<T> { |
| 14 | + private Collection<T> _stuff; |
| 15 | + |
| 16 | + @JsonCreator |
| 17 | + public StringyList(Collection<T> src) { |
| 18 | + _stuff = new ArrayList<T>(src); |
| 19 | + } |
| 20 | + |
| 21 | + public StringyList() { |
| 22 | + _stuff = new ArrayList<T>(); |
| 23 | + } |
| 24 | + |
| 25 | + @Override |
| 26 | + public boolean add(T arg) { |
| 27 | + return _stuff.add(arg); |
| 28 | + } |
| 29 | + |
| 30 | + @Override |
| 31 | + public boolean addAll(Collection<? extends T> args) { |
| 32 | + return _stuff.addAll(args); |
| 33 | + } |
| 34 | + |
| 35 | + @Override |
| 36 | + public void clear() { |
| 37 | + _stuff.clear(); |
| 38 | + } |
| 39 | + |
| 40 | + @Override |
| 41 | + public boolean contains(Object arg) { |
| 42 | + return _stuff.contains(arg); |
| 43 | + } |
| 44 | + |
| 45 | + @Override |
| 46 | + public boolean containsAll(Collection<?> args) { |
| 47 | + return _stuff.containsAll(args); |
| 48 | + } |
| 49 | + |
| 50 | + @Override |
| 51 | + public boolean isEmpty() { |
| 52 | + return _stuff.isEmpty(); |
| 53 | + } |
| 54 | + |
| 55 | + @Override |
| 56 | + public Iterator<T> iterator() { |
| 57 | + return _stuff.iterator(); |
| 58 | + } |
| 59 | + |
| 60 | + @Override |
| 61 | + public boolean remove(Object arg) { |
| 62 | + return _stuff.remove(arg); |
| 63 | + } |
| 64 | + |
| 65 | + @Override |
| 66 | + public boolean removeAll(Collection<?> args) { |
| 67 | + return _stuff.removeAll(args); |
| 68 | + } |
| 69 | + |
| 70 | + @Override |
| 71 | + public boolean retainAll(Collection<?> args) { |
| 72 | + return _stuff.retainAll(args); |
| 73 | + } |
| 74 | + |
| 75 | + @Override |
| 76 | + public int size() { |
| 77 | + return _stuff.size(); |
| 78 | + } |
| 79 | + |
| 80 | + @Override |
| 81 | + public Object[] toArray() { |
| 82 | + return _stuff.toArray(); |
| 83 | + } |
| 84 | + |
| 85 | + @Override |
| 86 | + public <X> X[] toArray(X[] arg) { |
| 87 | + return _stuff.toArray(arg); |
| 88 | + } |
| 89 | + } |
| 90 | + |
| 91 | + private final ObjectMapper MAPPER = new ObjectMapper(); |
| 92 | + |
| 93 | + public void testPolymorphicWithOverride() throws Exception |
| 94 | + { |
| 95 | + JavaType type = MAPPER.getTypeFactory().constructCollectionType(StringyList.class, String.class); |
| 96 | + |
| 97 | + StringyList<String> list = new StringyList<String>(); |
| 98 | + list.add("value 1"); |
| 99 | + list.add("value 2"); |
| 100 | + |
| 101 | + String serialized = MAPPER.writeValueAsString(list); |
| 102 | + System.out.println(serialized); |
| 103 | + |
| 104 | + StringyList<String> deserialized = MAPPER.readValue(serialized, type); |
| 105 | + System.out.println(deserialized); |
| 106 | + |
| 107 | + } |
| 108 | +} |
0 commit comments