I have a JSON object which has a key (key1) with the following structure
"key1": {
"key2": "some value1",
"key3": {
"key4": "some value2",
"key5": "some value3"
}
}
Key 3 is dynamic in that it can have the following data as well
"key1": {
"key2": "some value1",
"key3": {
"key4": "some value2",
"key5": "some value3"
"key6": "some value4"
}
}
or
"key1": {
"key2": "some value1",
"key3": {
"key4": "some value2",
}
}
Only these 3 combinations are possible (key 3 having 2 attributes, 3 attributes or just 1 attribute).
With all that in mind, I declared an abstract class (let's say Class Key3) for key3 in Java thinking that key3 can have multiple implementations (each concrete class will have their own fields).
Now there is a POJO composed of class Key3's variable (having a reference variable of Key3)
public Class MYPOJO class{
String variable1;
String variable2;
.
.
.
Key3 variable;
.
.
.
int variable3;
}
Now as the deserialization happens for MYPOJO, it fails as Key3 is abstract and hence can't be instantiated with a desired concrete class (concrete class will be either of the three classes - having 1, 2 or 3 fields). The concrete class can be decided on the basis of the value of key2 or whatever is present in key3 in JSON.
Can anyone suggest me something that can take care of this, instantiate a concrete class and pass it on to the reference variable of abstract class?
Or some better workaround to deal with it?