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