I have the following abstract class:
@JsonIgnoreProperties(ignoreUnknown = true)
@JsonTypeInfo(use = JsonTypeInfo.Id.DEDUCTION)
@JsonSubTypes({
@JsonSubTypes.Type(A.class),
@JsonSubTypes.Type(B.class),
@JsonSubTypes.Type(C.class)
})
public abstract class ABC {}
The class has 3 representations:
public class A extends ABC {
int attribute1;
String attribute2;
// getters and setters follow
}
public class B extends ABC {
int attribute3;
// getters and setters follow
}
public class C extends ABC {
int attribute4;
String attribute5;
String attribute6;
// getters and setters follow
}
I want to able able to serialize and deserialize the different representations into an escaped JSON, for example: if an object of A is as the following:
{
"attribute1" : 1,
"attribute2" : "string"
}
I want to be able to serialize it as :
"{\"attribute1\":1,\"attribute2\":\"string\"}"
The API for this implementation should not change from the default Jackson API, i.e.,
when I want to serialize an ABC object, it would be as the following:
ObjectMapper mapper = // init a mapper
ABC obj = // init an ABC Object
mapper.writeValueAsString(obj); // would return the escaped Json.
When I want to deserialize an ABC object, it would be as the following:
ObjectMapper mapper = // init a mapper
String json = // json representing an ABC subclass
mapper.readValue(json, ABC.class); // would return the ABC object
The solution should support having the ABC as its own standalone object/json and also when the ABC is an attribute in other class.