blob: 6617c0b33676ff0f7759ab83b3b483c7ec6371c5 (
plain)
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
|
package com.fasterxml.jackson.databind.jsontype;
import java.util.*;
import com.fasterxml.jackson.annotation.JsonTypeInfo;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.*;
public class PolymorphicList1451SerTest extends BaseMapTest
{
@JsonTypeInfo(use = JsonTypeInfo.Id.MINIMAL_CLASS, include = JsonTypeInfo.As.PROPERTY, property = "@class")
public static class A {
public String a;
}
public static class B extends A {
public String b;
}
private final String CLASS_NAME = getClass().getSimpleName();
public void testCollectionWithTypeInfo() throws Exception {
ObjectMapper mapper = new ObjectMapper()
.disable(SerializationFeature.EAGER_SERIALIZER_FETCH)
// .disable(DeserializationFeature.EAGER_DESERIALIZER_FETCH)
;
List<A> input = new ArrayList<A>();
A a = new A();
a.a = "a1";
input.add(a);
B b = new B();
b.b = "b";
b.a = "a2";
input.add(b);
final TypeReference<?> typeRef =
new TypeReference<Collection<A>>(){};
ObjectWriter writer = mapper.writerFor(typeRef);
String result = writer.writeValueAsString(input);
assertEquals(aposToQuotes(
"[{'@class':'."+CLASS_NAME+"$A','a':'a1'},{'@class':'."+CLASS_NAME+"$B','a':'a2','b':'b'}]"
), result);
List<A> output = mapper.readerFor(typeRef)
.readValue(result);
assertEquals(2, output.size());
assertEquals(A.class, output.get(0).getClass());
assertEquals(B.class, output.get(1).getClass());
}
}
|