How to use autobean for converting json to java class in GWT -




i have class person in gwt , have sent instance of person servlet converted using gson server client. in client side seems can't use gson. read in forums seems best way using autobeans convert json object person again.

however in autobeans can use interface. appreciate if can me write it.

a json example server , want convert person class again:

{"name":"aaa","family":"fff","username":"uuu","age":20,"phones":[{"id":0,"phonenumber":"0911111"}],"relatives":[null]}

public class person implements serializable {    private string name;    private string family;    private string username;    private int age;    private list<phone> phones;    private list<person> relatives;     public person() {    }     public person(string name, string family, string username, int age, list<phone> phones, list<person> relatives) {       this.name = name;       this.family = family;       this.username = username;       this.age = age;       this.phones = phones;       this.relatives = new arraylist<person>();       this.relatives = relatives;    }     public void addphone(phone p) {       phones.add(p);    }     public string getname() {       return this.name;    }     public string getfamily() {       return this.family;    }     public int getage() {       return this.age;    }     public string getusername() {       return this.username;    }     public list<phone> getnumbers() {       return this.phones;    }     public list<person> getrelatives() {       return this.relatives;    }     public string getallnumbers() {       return phone.convertphonestotext(phones);    }     public static person findperson(list<person> personlist, string username) {       // .....    }     public static list<person> converttexttopersons(list<person> personlist, string personstext) {       // .....    }     public string convertpersonstotext() {       // ....    } } 

yep, commented tobika other answer indicates autobeans requires interface. autobeans feets better if use on both sides, client , server side , define models interfaces.

if want use class models, can use gwt jackson pretty similar autobeans uses models, binding json model (like other server side libraries; jackson, gson, etc): https://github.com/nmorel/gwt-jackson

public static interface personmapper extends objectmapper<person> {}  @override public void onmoduleload() {     personmapper mapper = gwt.create(personmapper.class);      string json = mapper.write(new person("john", "doe"));     gwt.log( json ); // > {"firstname":"john","lastname":"doe"}      person person = mapper.read(json);     gwt.log(person.getfirstname() + " " + person.getlastname()); } 

alternatively, can use plain gwt jsinterop. has many limitations limitation, pretty option. favorite option if can avoid inheritance in dtos. has big advantage of being super lightweight (actually 0 overhead mapping overhead , 0 code overhead uses native parsing , no copies, accesing directly parsed json object). limitations: cannot use inheritance, "broken type system" (all x instanceof somedtotype returns true dtos of type object wich makes sense because using parsed json), cannot use collections native arrays (but java8 stream should not problem, whatever want start stream.of(arr)), , double , boolean boxed types supported (not supported fancy type date or biginteger, not supported long/long...).

@jstype(isnative=true, package=global, name="object") final class person {     // can use getter/setter class final dto adds no value     public string firstname; public string lastname; public phome[] numbers;     // can add helper methods, don't forget skip serialization!     public final @jsoverlay @jsonignore list<phone> getnumberlist() {         return stream.of(numbers).collect(collectors.tolist());     } }  @jstype(isnative=true, package=global, name="object) final class phone {     public string number; }  @jsmethod(namespace = "json") public static native <t> t parse(string text);  @override public void onmoduleload() {     person person = parse("{\"firstname\":\"john\",\"lastname\":\"doe\"}");     gwt.log(person.firstname + " " + person.lastname); } 

these simple , limited dtos more dto scheme type. has big advantage, dtos works out of box of server side parsers. jackson , gson encode , parse without configuration.





wiki

Comments

Popular posts from this blog

firebase - How to wait value in Ionic 2 -

Jenkins: find build number for git commit -

python - Minimize function with Scipy minimize -