Thursday, September 22, 2016

JSON to Client Side GWT JavaScriptObject

I'm looking for help in converting a JSON to an instance of a POJO with embedded objects, on the GWT client side. I'm partially successful but unable to convert every piece of the JSON I have.


I have removed all but one working piece of code here to narrow down to the problem area. Specifically, my question is why the following method returns null?


public final native JsonArray<NameNumberJson> getServerStatusCodeMap() /*-{
    // debugger shows serverStatusCodeMap is valid below
    this.serverStatusCodeMap;   }-*/;


The JSON is valid (given below) and I have provided all the relevant code in this post. I would greatly appreciate any help.


Here's the JSON:

{    "idOfSavedObject": "AVc-CQ4OI-SZQb7PIPb_",    "serverStatusCodeMap": [      {        "name": "AVc-CTACI-SZQb7PIPcO",        "value": 202      },      {        "name": "AVc-CTACI-SZQb7PIPcP",        "value": 100      }    ]  }

Its JS overlay class is

public class ServerResponseJson extends JavaScriptObject {        protected ServerResponseJson() {}        public static final native ServerResponseJson build(String json) /*-{          return eval('(' + json + ')');      }-*/;        public final native String getIdOfSavedObject() /*-{          return this.idOfSavedObject;      }-*/;        public final native JsonArray<NameNumberJson> getServerStatusCodeMap() /*-{          this.serverStatusCodeMap;      }-*/;  }

I debugged and found the method below has the right values

public final native JsonArray<NameNumberJson> getServerStatusCodeMap() /*-{      this.serverStatusCodeMap; // this attribute has the right value, the caller of this method gets a null  }-*/;

Screen clip showing the right values for serverStatusCodeMap


JsonArray.java

public class JsonArray<E extends JavaScriptObject> extends JavaScriptObject {      protected JsonArray() {}        public final native int length() /*-{          return this.length;      }-*/;        public final native E get(int i) /*-{          return this[i];      }-*/;  }

the NameNumberJson.java

public class NameNumberJson extends JavaScriptObject {      protected NameNumberJson() {}        public static final native NameNumberJson buildNameNumberJson(String json) /*-{          return eval('(' + json + ')');      }-*/;        // Return the whole JSON array, as is      public static final native JsonArray<NameNumberJson> getNameNumberJsons(String json) /*-{          return eval('(' + json + ')');      }-*/;        public final native String getName() /*-{          return this.name;      }-*/;        public final native int getValue() /*-{          return this.value;      }-*/;  }

--
You received this message because you are subscribed to the Google Groups "GWT Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to google-web-toolkit+unsubscribe@googlegroups.com.
To post to this group, send email to google-web-toolkit@googlegroups.com.
Visit this group at https://groups.google.com/group/google-web-toolkit.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment