On the server-side, I use com.google.gson.* On the client-side, I use com.google.gwt.core.client.JavaScriptObject; com.google.gwt.json.client.*;
There are two modes to using these APIs. - Use GWT RPC to send Java structures back and forth. In this mode, you are blind to JSON and never need to even refer to any JSON API. - Send JSON explicitly and therefore having to be aware of JSON APIs on both client and server side. What concerns you is the second mode. For example, I am writing a framework which inspects the DAO on the server-side to construct the JSON representing those structures. The GWT client reads the server's JSON representation and data and builds the GWT (or SmartGWT) Ui dynamically and fill them up with data spontaneously. On the server-side, using com.google.gson.*, for example, final JsonObject schema = new JsonObject(); // construct the schema root as hash node. schema.addProperty("name", name); // add key-string value pairs JsonArray elements = new JsonArray(); // create a JSON array schema.add("elements", elements); // add key-child node pairs to hash node. JsonObject elem = new JsonObject(); elements.add(elem); // json obj to jsonarray. Gson gson = new GsonBuilder() .setPrettyPrinting().create(); gson.toJson(schema); // print the JSON representation to output. On client-side, read this article http://code.google.com/webtoolkit/articles/using_gwt_for_json_mashups.html. And then you could use my library by extending JsonRemoteScriptCall.java at http://code.google.com/p/synthfuljava/source/browse/#svn/trunk/gwt/jsElements/org/synthful/gwt/javascript/client. This library works on the premise of jsonp. The principle is, you modify your server-side JSON provider to do envelope the JSON structure as executable javascript: var jsdata = { // json structure } eatJson(jsdata); On client-side: import com.google.gwt.core.client.JavaScriptObject; import com.google.gwt.json.client.*; JsonRemoteScriptCall myJRSC = new JsonRemoteScriptCall() { extends JsonRemoteScriptCall { @Override public void onJsonRSCResponse(JavaScriptObject jso) { if (jso==null) return; this.ignite(); JSONObject json = new JSONObject(jso); // traverse your json tree here. } } What you need to take note is how to trigger reading in the JSON: myJRSC.call( url of json data source, "eatJson"); Notice that the callback onJsonRSCResponse is mapped to eatJson by this utility, so that at the end of the json file, eatJson is executed as a javascript call which actually runs the callback onJsonRSCResponse. Take note that on the client-side use of com.google.gwt.json.client.* APIs, you have to be quite explicit in testing if a node is another node, is an array, is a string, etc: static public String getStringValue(JSONObject json, String key){ if (json == null || key == null ) return null; JSONValue typejsv = json.get(key); if (typejsv!=null){ JSONString typestr = typejsv.isString(); if (typestr!=null) return typestr.stringValue(); } return null; } static public JSONArray getJSONArray(JSONObject json, String key){ if (json == null || key == null ) return null; JSONValue jsv = json.get(key); if (jsv!=null){ return jsv.isArray(); } return null; } static public JSONObject getJSONObject(JSONObject json, String key){ if (json == null || key == null ) return null; JSONValue jsv = json.get(key); if (jsv!=null){ return jsv.isObject(); } return null; } -- You received this message because you are subscribed to the Google Groups "Google Web Toolkit" group. To post to this group, send email to google-web-tool...@googlegroups.com. To unsubscribe from this group, send email to google-web-toolkit+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/google-web-toolkit?hl=en.