Tuesday, August 20, 2013

Re: Problem decoding complex AutoBean

On Tuesday, August 20, 2013 8:40:47 AM UTC-4, Thomas Broyer wrote:


On Tuesday, August 20, 2013 1:47:58 PM UTC+2, Thad Humphries wrote:
Thank you, Thomas. That worked.

I added the interface

public interface MQueryWrapper {
  MQuery getMquery();
}

In BeanFactory, I changed "AutoBean<MQuery> mquery();" for "AutoBean<MQueryWrapper> mquery();"

My onResponseReceived() now looks like

  AutoBean<MQueryWrapper> bean = 
      AutoBeanCodex.decode(clientFactory.getBeanFactory(), MQueryWrapper.class, 
          response.getText());
  MQuery mQuery = bean.as().getMquery();
  logger.log(Level.INFO, mQuery.getScreenname());
  screenName = mQuery.getScreenname();
  fieldList = mQuery.getFields().getField();
  view.renderFields(fieldList);

Client-side this beats overlay types because it can handle nested objects (so long as I figure out nesting).

Why wouldn't you be able to do it with JSOs?

Ah, thank you for that little programming problem. I was missing something when I previously looked at overlay types. Yes, they can handle nested objects.

Assuming the same JSON structure as above, these two objects manage it:

public class MQueryField extends JavaScriptObject {
  protected MQueryField() {}
  public final native String getTitle() /*-{ return this.title; }-*/;
  public final native String getName() /*-{ return this.name; }-*/;
  public final native String getValue() /*-{ return this.value; }-*/;
}

public class MQueryRoot extends JavaScriptObject {
  protected MQueryRoot() {}
  public final native String getScreenName() /*-{ return this.screenname; }-*/;
  public final native JsArray<MQueryField> getFields() 
      /*-{ return this.fields.field; }-*/;
}

Now reading in that same string:

JSONObject jobj = new JSONObject(JsonUtils.safeEval(response.getText()));
MQueryRoot root = jobj.get("mquery").isObject().getJavaScriptObject().cast();
logger.log(Level.INFO, root.getScreenName());
// prints "Index Card"
logger.log(Level.INFO, "fields["+root.getFields().length()+"]");
// prints "fields[3]"

Hm… I think I prefer AutoBeans, but this is nice to know. 

 
I've not tried it server-side yet, but that looks very handy.

Yup, I'm using it to serialize some data that I output in the host page and load on the client-side at startup, works great and you're sure that your JSON will be understood by the end and don't fear forgetting to update the code on one side and not the other.

Thank you for these examples.
 

--
You received this message because you are subscribed to the Google Groups "Google Web Toolkit" 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 http://groups.google.com/group/google-web-toolkit.
For more options, visit https://groups.google.com/groups/opt_out.

No comments:

Post a Comment