Jersey/JAXB bind different JSON format to a single type of java object -


suppose have 2 different types of json defined below , need map both them same type of java object.

type a

{ "startdate": "2013-05-10", "enddate": "2013-05-19", "value": "1.5" } 

type b

{ "startdate": "2013-05-10", "enddate": "2013-05-19", "data": "1.5" } 

i want use jersey/jackson map both of them single java class:

@xmlrootelement public class datapoint{      public string startdate;     public string enddate;     public string value; //what do here??? } 

essentially data , value should considered equivalent. conversion datapoint done automatically right using jersey/jackson, how customize use case? using jersey client apis.

you need have 2 setters in pojo maps json.

public class testpojo {   string startdate;   string enddate;   string content;  //default constructor.  //getter , setter startdate , enddate       public void setvalue(string content)     {         this.content = content;     }       public void setdata(string content)     {         this.content = content;     }     } 

my test:

        string s = "{\"startdate\": \"2013-05-10\",\"enddate\": \"2013-05-19\",\"value\": \"value1.5\"}";         objectmapper mapper = new objectmapper();         mapper.configure(deserializationconfig.feature.fail_on_unknown_properties, false);         testpojo pojo = mapper.readvalue(s, testpojo.class);         system.out.println(pojo);         s = "{\"startdate\": \"2013-05-10\",\"enddate\": \"2013-05-19\",\"data\": \"data1.5\"}";          mapper.configure(deserializationconfig.feature.fail_on_unknown_properties, false);         pojo = mapper.readvalue(s, testpojo.class);         system.out.println(pojo); 

output:

testpojo [startdate=2013-05-10, enddate=2013-05-19, content=value1.5] testpojo [startdate=2013-05-10, enddate=2013-05-19, content=data1.5] 

Comments

Popular posts from this blog

python - How to create a legend for 3D bar in matplotlib? -

java - Multi-Label Document Classification -

php - Dynamic url re-writing using htaccess -