Tuesday, May 19, 2015

Re: GWT2.8 JsInterop Java->JS

JSON.stringify will try to serialize the js objects to a json string as best it can (ignoring cycles, and non-json data like methods).

At the risk of sounding snarky, you know that class won't work in JsInterop, right? For a start, its missing its @JsType! ;)

Next, some of these fields can't cleanly be passed between Java and JS - for example JS has no long type (will truncate to double precision, GWT emulates long), and a Java List<?> needs to be a type that exists in JS (either JsArray, or some jsinterop-annotated type that maps to what JS understands). This I'm less sure about, but at least historically boxed types like Boolean were never supported - the primitive type boolean has to be used instead.

Here's a very quick sanity check that I wrote to verify that this is working more or less as expected:
public class SampleEntryPoint implements EntryPoint {
  public void onModuleLoad() {
    MyData data = new MyData("Colin", 123);
    Window.alert(stringify(data));
  }

  public static native String stringify(Object obj) /*-{
    return $wnd.JSON.stringify(obj);
  }-*/;

  @JsType
  public static class MyData {
    public String name;
    public int age;
    public MyData(String name, int age) {
      this.name = name;
      this.age = age;
    }
  }
}

On startup this does indeed output {"name":"Colin","age":123}, though if you remove the @JsType annotation, it just outputs {} since GWT doesnt see any code using the fields, so it optimizes them out.

Live code:

This is however using a version of GWT 2.8 that is a few weeks old, I'll update later today and reverify.

On Tue, May 19, 2015 at 12:11 AM Alex W <alexwhite3000@gmail.com> wrote:
The '_1_g$' indicates you are in super dev mode - does it work correctly when fully compiled to JS (draft or no)? I saw a problem in this area a few weeks ago.

No it doesn't work when it's fully compiled to JS either.

Also, can you post the full definition for MyObject?


class MyObject {
    private String str1;
    private String str2;
    private boolean b1;
    private boolean b2;
    private boolean b3;
    private boolean b4;
    private boolean b5;
    private boolean b6;
    private int size;
    private String str3;
    private Boolean b7;
    private String str4;
    private int numb1;
    private long uid;
    private Boolean b7;
    private List<MyOtherObject> objs;

}

I agree with Alberto though, your printObj doesn't make any sense as is - it is calling the java toString() method on the object rather than serializing to JSON, or do you expect MyObject.toString() to return a json string?

I thought JSON.stringify(...) would stringify the (assumed) JSON object. I'm not really sure what's going on under the hood, but I don't really understand why/how it doesn't.

--
You received this message because you are subscribed to a topic in the Google Groups "Google Web Toolkit" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/google-web-toolkit/ZCayeP1c5c0/unsubscribe.
To unsubscribe from this group and all its topics, 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/d/optout.

--
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/d/optout.

No comments:

Post a Comment