There's an interface, let's say A. I have multiple classes that implements that interface A. Those classes also consists of class variable of type A. So, it's like:
@JsonTypeInfo(use = JsonTypeInfo.Id.Class, include = JsonTypeInfo.As.Property, property = "className")
@JsonSubType({
@Type(value = abstractClass.class, name = "abstractClass"),
@Type(value = subClass1.class, name = "subClass1"),
@Type(value = subClass2.class, name = "subClass2"),
'
'
'
})
interface A {
func1();
func2();
}
abstract class abstractClass implements A {
int abstractvar1;
func1(){//code}
}
class subClass1 extends abstractClass {
int var1;
int var2;
A var3;
A var4;
}
class subClass2 extends abstractClass {
int var1;
int var2;
A var3;
}
class subClass3 extends abstractClass {
float var1;
int var2;
A var3;
}
and more classes defined trying to extend abstractClass..
Constructors, getters and setters are already defined.
Class which consists of all the variables
class Implementor {
int implementorVar1;
String implementorVar2;
A implementorVar3;
int implementorVar4;
}
So, I want to serialize the Implementor class into JSON. I'm using Jackson for the same. So, I added @jsonTypeInfo and @type to interface so that they have a concrete class to work upon. But when I try to serialize the subClasses, only var1 and var2 are serialized which is of int type, and not var3/var4 which are of type A. How can I serialize those variables too?
Json I'm getting if I try to serialize Implementor:
{
"implementorVar1": 1,
"implementorVar2": "hello",
"implementorVar3": {
"className": "subClass2",
"abstractVar1": 45,
},
"implementorVar4": 1000
}
Json I'm expecting:
{
"implementorVar1": 1,
"implementorVar2": "hello",
"implementorVar3": {
"className": "subClass2",
"abstractVar1" : 45,
"var1": 45,
"var2": 56,
"var3": {
"className": "subClass3",
"var1": 2,
"var2": 5,
"var3" : {
"className" : "" ...
}
}
},
"implementorVar4": 1000
}