Simple java library to deserialise json to a java object using getters/setter and not properties(GSON) -


i working on rest based application , need convert json object class. problem class third party library , not have access source file. further properties in java object not match getter/setters in class(does not follow java bean convention). example if try deserialize following json simpleobject nulls instead of actual data.

json input:

{    name: 'test',    id: '1' } 

java class deserialize to

public class sampleobject {     private string myname;     private long myid;      public long getid() {         return this.myid;     }      public string getname() {         return this.myname;     }     .. // setters } 

test class

public class testgson {    public static void main(final string[] args) {     final gson gson = new gson();     final sampleobject smapleobject = gson.fromjson("{"             + "name:'test',"             + "id: 1"             + "}", sampleobject.class);     system.out.println(smapleobject.getname());     system.out.println(smapleobject.getid());   } } 

result:

null null 

expected:

test 1 

the problem gson uses properties deserialize json whereas requirement use getter , setters.the class has getter , setters properties , should not have properties not have one.

is there other simple json library can use achieve without modifying existing class(cannot add annotations source not available). had @ jackson seems add bit complexity such simple task , team mates not happy it.

jackson can exact thing , syntax same.

e.g.:

string json = "{\"name\": \"test\",\"id\": \"1\"}"; objectmapper mapper = new objectmapper(); sampleobject sample = mapper.readvalue(json, sampleobject.class); system.out.println(sample.getname()); 

pojo class :

class sampleobject {     private string myname;     private long myid;      public long getid() {         return this.myid;     }      public string getname() {         return this.myname;     }      public void setname(string myname) {         this.myname = myname;     }      public void setid(long myid) {         this.myid = myid;     }  } 

Comments

Popular posts from this blog

c# - Validate object ID from GET to POST -

node.js - Custom Model Validator SailsJS -

php - Find a regex to take part of Email -