Thursday, May 30, 2013

Using nested arrays of complex types in GWT

Java's code:

public class Person {
   private String name;
   private int[] luckyDays;

   public String getName() {
      return name;
   }

   public void setName(String name) {
      this.name = name;
   }

   public int[] getLuckyDays() {
      return luckyDays;
   }

   public void setLuckyDays(int[] luckyDays) {
      this.luckyDays = luckyDays;
   }

}


public class Family {
   private Person[] persons;

   public Person[] getPersons() {
      return persons;
   }

   public void setPersons(Person[] persons) {
      this.persons = persons;
   }

}

in GWT will be (only getters for deserializer):


import com.google.gwt.core.client.JavaScriptObject;
import com.google.gwt.core.client.JsArray;

public class PersonClient extends JavaScriptObject {

   protected PersonClient() {}

   
   public final native String getName() /*-{
      return this.name;

   }-*/;

   public final native JsArrayNumber
getLuckyDays() /*-{
      return this.
luckyDays;
   }-*/;


}

import com.google.gwt.core.client.JavaScriptObject;
import com.google.gwt.core.client.JsArray;

public class FamilyClient extends JavaScriptObject {


   protected FamilyClient() {} 


   public final native JsArray<PersonClient> getPersons() /*-{
      return this.
persons
   }-*/;


}

Example how to use it:


JsArrayString names = Properties.createArray().cast();
s.push("Foo");

Properties properties = Properties.create();
properties.setNumber("from", from.getValue().getTime());
properties.setNumber("to", to.getValue().getTime());
properties.set("names", s);


GQuery.getJSON("/getPersonsByNames.json", $$(params), new Function() {

   @Override
   public void f() {



      JavaScriptObject response = (JavaScriptObject) this.getDataObject();
     
FamilyClient family = response.cast();

      for (int i = 0; i <
family.getPersons().length(); i++) {
         
PersonClient person = family.getPersons().get(i);
         ...
      }
   }
}




No comments :

Post a Comment