0

I have a JSON like

{
   "age" : "35",
   "otherdetails":{
   "address" : "xyx",
    "pincode" : "978897"
    }
}

I have a base class to covert JSON TO JAVA

 class Base
 {
     int age;
     OtherDetails otherdetails;
 }

also i have an interface for otherdetails and a class implementing the interface

interface OtherDetails
class AddressDetails extends OtherDetails
{
   String address;
   String pincode;
}

How can i use gson or jackson to convert the json so that it picksup AddressDetails class when converting thee otherdetails attribute?

1 Answers1

0

I was able to find the answer in Deserialize JSON with Jackson into Polymorphic Types - A Complete Example is giving me a compile error

I tried similar kind of thing and it worked

  • The solution was to add a new attribute in my json like classname in which i populate the correct classname eg AdressDetail. Then at the interface level i had to add annotation with the classname attribute – tushar singhal Jul 08 '19 at 12:41