I am working in Java with Jackson.
I have an abstract class Base.class, which looks as follows:
@JsonDeserialize(as = Child1.class)
public abstract class Base {
@JsonProperty("field1")
private Boolean field1;
@JsonProperty("field2")
private Long field2;
//getters, setters
}
And a bunch of child classes that extend the abstract class, that look like this:
public class Child1 extends Base {
@JsonProperty("field3")
private Boolean field3;
@JsonProperty("field4")
private Long field4;
//getters, setters
}
When I deserialise the following JSON, it works perfectly well:
Base base = mapper.readValue(new File("src/test/resources/testerTemplate/example.json"), Base.class)
example.json :
{
"field1":true,
"field2":123456,
"field3":false,
"field4":123456,
}
The issue is that this limits this structure to one-to-one relationship between base and child classes - to deserialise into other child classes I would need to change the annotation every single time. I have tried other annotations but has not been able to work it out. How do I do this?