Tuesday, May 19, 2015

Re: GWT2.8 JsInterop Java->JS

It uses @JsType @JSExport and also implements Serializable. I already mentioned I was using those so I didn't put that in when I sanitized the code since we are not allowed to pastie actual work code. As a user in js, I would expect that all those types would have js analogs in js under their respective java.lang namespace. There are various Big Integer js libraries on github for example.

Looking at Ray Cromwell's slides I noticed there is a (vapour) reference to a Js.js(...) class that might do what I want. Apparently, it doesn't exist. Oops. At the risk of sounding snarky, GWT is a poorly run project, consistently overpromising and underdelivering.

On Wednesday, May 20, 2015 at 12:18:41 AM UTC+10, Colin Alworth wrote:
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 <alexwh...@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 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