I use jackson to deserialize an java object LivingBeing which has Animal class in it. So far I was dierctly passing object of Animal.
class LivingBeing {
@JsonProperty('animal')
Animal animal;
}
class Animal {
@JsonProperty('color')
String color;
}
But now, we have extended Animal class with classes Cat, Dog.
class Cat extends Animal {
@JsonProperty('cat_specific_feature')
String catFeature;
}
class Dog extends Animal {
@JsonProperty('dog_specific_feature')
String dogFeature;
@JsonProperty('dog_special_power')
String dogPower;
}
Example jsons:
livingbeing: {animal :{color :”black”}}
livingbeing: {animal :{color :”black”, cat_specific_feature :”someCatFeature”}}
livingbeing: {animal :{color :”black”, dog_specific_feature :”someDogFeature”, dog_special_power:”power”}}
I do not already know what kind of object will be coming to living being. My only idea is to use some extra flag in LivingBeing like flag: Cat,Dog etc as enum, but dont feel its a good design. Currently, if(livingbeing.getanimal() instanceOf Cat) is false because livingbeing knows only Animal type. Note: Cat and Dog are representing diff use cases. And I can't put 'feature' in Animal. This is only an example code structure that represents different use cases . Constructor overloading for Animal is not possible due to function erasure. How can I deserialize the LivingBeing?