blob: 75fb8af0aaa7784b5770b5afb291872d69a25c04 (
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
54
55
56
57
58
59
60
61
62
|
package com.fasterxml.jackson.databind.deser.creators;
import com.fasterxml.jackson.annotation.*;
import com.fasterxml.jackson.databind.*;
/**
* Test(s) for [Issue#113], problems with polymorphic types, JsonCreator.
*/
public class TestCreatorWithPolymorphic113 extends BaseMapTest
{
@JsonIgnoreProperties(ignoreUnknown = true)
@JsonTypeInfo(use = JsonTypeInfo.Id.CLASS, include = JsonTypeInfo.As.PROPERTY, property = "_class")
@JsonSubTypes({ @JsonSubTypes.Type(Dog.class) })
public static abstract class Animal {
public final static String ID = "id";
private String id;
@JsonCreator
public Animal(@JsonProperty(ID) String id) {
this.id = id;
}
@JsonProperty(ID)
public String getId() {
return id;
}
}
@JsonIgnoreProperties(ignoreUnknown = true)
public static class Dog extends Animal {
@JsonCreator
public Dog(@JsonProperty(ID) String id) {
super(id);
}
}
@JsonIgnoreProperties(ignoreUnknown = true)
public static class AnimalWrapper {
private Animal animal;
@JsonCreator
public AnimalWrapper(@JsonProperty("animal") Animal animal) {
this.animal = animal;
}
public Animal getAnimal() {
return animal;
}
}
public void testSubtypes() throws Exception
{
ObjectMapper mapper = new ObjectMapper();
String id = "nice dogy";
String json = mapper.writeValueAsString(new AnimalWrapper(new Dog(id)));
//System.err.println("JSON = "+json);
AnimalWrapper wrapper = mapper.readValue(json, AnimalWrapper.class);
assertEquals(id, wrapper.getAnimal().getId());
}
}
|