Thanks to all for your help.

I have read your suggestions and also found Bruce Johnson's
presentation (http://www.youtube.com/watch?
v=vv2MnqP8Bmk&feature=PlayList&p=968FABA11F2EDEA1&index=1) helpful.

I tested Java to JavaScript communication on a simple application that
went about  passing map to javascript and back as follows:
* instantiated HashMap
* converted it to JSON representation and assign it to a window's
variable
* call JSNI method that returns that variable as JsArray<GwtStock>

That all works fine, but code surely does not look pretty for that
conversion must be programmed per hand and
the resulting JsArray object is read-only.

If I could raise some more questions on that matter:
- If I pass a map directly to JSNI method it is being converted by
GWT. Maybe there is a way to use GWT representation in javascript code
and somehow returning it back also?
- If not, are there APIs provided to convert Map, List, etc in JSON so
that there is no need to write it per hand?
- And last, but very puzzling for me. I started this passing-around-
map thing with two frames originally and that did not obviously work
but if the same code is put under one application passing and
returning Map<Long,GwtStock> to/from JSNI method works without hitch
(code appended). My explanation is that since it runs all in one
application GWT compiler makes very smart guesses which are impossible
with two applications to make (2 frames), but could also be that I am
completely misleading.

Many thanks,
Denis

---MapTest.java---
public class MapTest implements EntryPoint {

        private HTML html = new HTML();
        private Map<Long, GwtStock> stockMap = new HashMap<Long, GwtStock>();

        public void onModuleLoad() {

                populateMap();
                convertMapToJSON();
                setUpJavaScriptApis(this);

                html.setHTML(getMapJsForDisplay());
                RootPanel.get("display").add(html);

        }

        public String getMapJsForDisplay() {
                Map<Long, GwtStock> stockMapJs = getMapJs();
                Iterator<Long> iterator = stockMapJs.keySet().iterator();
                StringBuffer sb = new StringBuffer();
                sb.append("=== MapJs ===<br/>");
                sb.append("keySet(): " + stockMapJs.keySet() + "<br/>");
                sb.append("---Elements---<br/>");
                while (iterator.hasNext()) {
                        sb.append(stockMapJs.get(iterator.next()) + "<br/>");
                }
                sb.append("<br/>");
                sb.append("===stockJson===<br/>");
                JsArray<GwtStockJson> stockJson = getStockJson();
                for (int i = 0; i < stockJson.length(); i++) {
                        sb.append(stockJson.get(i).getId() + ":" + stockJson.get
(i).toStringJson()
                                + "<br/>");
                }
                return sb.toString();
        }

        public void populateMap() {
                stockMap.put(1l, new GwtStock(1l, "s1", 111));
                stockMap.put(2l, new GwtStock(21, "s2", 222));
                stockMap.put(3l, new GwtStock(31, "s2", 333));
        }

        public void convertMapToJSON() {
                Iterator<Long> iterator = stockMap.keySet().iterator();
                while (iterator.hasNext()) {
                        GwtStock gwtStock = stockMap.get(iterator.next());
                        addJson(gwtStock.getId(), gwtStock.getSymbol(), 
gwtStock.getPrice
());
                }
        }

        public native void addJson(Long id, String symbol, double price) /*-{
                if (!$wnd.stockJson) $wnd.stockJson = new Array();
                $wnd.stockJson.push({"id":id, "symbol": symbol, "price": 
price});
        }-*/;

        public native JsArray<GwtStockJson> getStockJson() /*-{
                return $wnd.stockJson;
        }-*/;

        public Map<Long, GwtStock> getMap() {
                return stockMap;
        }

        public native Map<Long, GwtStock> getMapJs() /*-{
                return $wnd.__getMapJs();
        }-*/;

        public native void setUpJavaScriptApis(MapTest x) /*-{
                $wnd.__getMapJs = function() {
                        return [email protected]::getMap()();
                };
        }-*/;

}

---GwtStock.java---

public class GwtStock implements Serializable {

        private long id;
        private String symbol;
        private double price;

        public GwtStock() {
        }

        public GwtStock(long id, String symbol, double price) {
                this.id = id;
                this.symbol = symbol;
                this.price = price;
        }

        public long getId() {
                return id;
        }

        public String getSymbol() {
                return symbol;
        }

        public double getPrice() {
                return price;
        }

        @Override
        public String toString() {
                return id + ";" + symbol + ";" + price;
        }

}

---GwtStockJson---
package de.test.gwt.client;

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

public class GwtStockJson extends JavaScriptObject {

        protected GwtStockJson() {
        }

        //long cannot be returned from jsni method
        public final native Long getId() /*-{
                return this.id;
        }-*/;

        public final native String getSymbol() /*-{
                return this.symbol;
        }-*/;

        public final native double getPrice() /*-{
                return this.price;
        }-*/;

        public final  String toStringJson() {
                return getId() +";"+ getSymbol() +";"+ getPrice();
        }
}





On 16 Feb., 23:24, Shawn Brown <[email protected]> wrote:
> > On the other hand, I tried printing out the return value of
> > $wnd.__getMapJs() and that turns to be a huge JavaScript Object and I
> > have no idea how to iterate through it. So an example will help
> > tremendously.
>
> {"data":[{"post":0,"fax":"","idCustomerType":1,"idLanguage":0,"tel":"","street":"","inboxSize":4194304
> ,"idCountry":0,"idAgency":1,"contact":"","isActive":1,"id":286,"email":"","name":"UVI","connectionSpeed"
> :131072,"contactMobile":"","activeFilters":1}],"total":1}
>
> I'd use the getters and setters if you have them in a bean someplace.
>
> import com.google.gwt.core.client.JavaScriptObject;
> import com.google.gwt.json.client.JSONArray;
> import com.google.gwt.json.client.JSONObject;
> import com.google.gwt.json.client.JSONValue;
>
> For example (see the api to handle an array):
>
> JSONObject dataHolder;
>
> public String handleData(JavaScriptObject jsData) {
> Customer = new Customer();//sample bean with typical getters and setters
>         dataHolder = new JSONObject(jsData);
>        
> setPost((int)dataHolder.get("data").isObject().get("post").isNumber().doubleValue());
> setEmail(dataHolder.get("data").isObject().get("email").isString().toString());
>
> etc etc.
>
> }
>
> If you are getting back json as a String and not JavaScriptObject,
> then you'll have to parse it into a JavaScriptObject first.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Google Web Toolkit" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to 
[email protected]
For more options, visit this group at 
http://groups.google.com/group/Google-Web-Toolkit?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to