06-01-2009 02:41:46

gwt-json-rpc

Gwt-json-rpc is a simple json-rpc implementation for the Google Web Toolkit (GWT).
Because I started writing this module before GWT contained an JSON encoder, it contains an own JSON encoder and decoder. The decoder is a modified version of the JSONParser from the GWT examples. The encoder is written from scratch.
The main difference to the GWT JSON en-/decoder is that you don't have to use JSONObject, JSONArray, JSONString, rather you can use native Java types like String, int, boolean, Array, HashMap, ArrayList, Vector and so on for encoding and decoding data.

Download

Note that since version 1.0.0 of gwt-json-rpc, GWT 1.5 is required.
Source including test cases:
gwt-json-rpc-1.0.1.zip

The jar file only:
gwt-json-rpc-1.0.1.jar

Changelog

Additional Downloads

A sample project showing the use of gwt-json-rpc with JSON-RPC-Java on the server side:
json-rpc-java-test.zip

Older versions of gwt-json-rpc.

Short instructions for Eclipse

  • Add the jar file to your project.
  • Add <inherits name='net.ffxml.gwt.json.JsonRpc'/> to your module file.
  • Open your Run- or Debug configuration. Open Tab "Classpath" and select "User Entries". Add the jar file.
  • Add the path to the jar file to the compile- and shell-scripts in the root directory of your project.
  • Now the following code snippet should work. It calls the example service from json-rpc.org:
//Create a new JsonRpc instance
JsonRpc jsonRpc = new JsonRpc()
;

//Create a callback handler
AsyncCallback callback = new AsyncCallback() {

  public void onFailure(Throwable caught) {
    label.setText("Error: " + caught);
  };

  public void onSuccess(Object result) {
      //Cast the result to an Integer an display it
      Integer total = (Integer) result;
      label.setText("Total: " + total);
  };
};

//Send the request. In this case the method 'total' is called
//which takes one parameter, an int array, and returns
//
the total of all int values in the array.
jsonRpc.request(
  "http://www.raboof.com/Projects/Jayrock/Demo.ashx",
  "total",
  new Object[] { new int[] {3, 6} },
  callback);


(Due to cross-domain-scripting limitations this example won't work in compiled form on most browsers. Your JSON-RPC Service have to be located on the same domain as your gwt application in real projects.)

Documentation

See the Java docs.