JSON

From Documentation
Revision as of 08:42, 23 August 2011 by Alicelin (talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)


The data of a widget event (Event.data) is serialized to a string (so-called marshal) by JSON, when the event is sent back to the server. ZK Update Engine will unmarshal it back to a map. If an entry of the data is an array, it will be converted to a list[1].

The map of data can be retrieve by the use of AuRequest.getData().

For example, assume we fire an event at the client as follows.

wgt.fire('onFly', {x: 10, y: 20});

Then, we can retrieve and process it at the server as follows:

public class Fly extends AbstractComponet {
  static {
    addClientEvent(Fly.class, "onFly", CE_IMPORTANT); //assume it is an important event
  }

  public void service(org.zkoss.zk.au.AuRequest request, boolean everError) {
    String cmd = request.getCommand();
    if (cmd.equals("onFly")) {
      Map data = request.getData();
      int x = ((Integer)data.get("x")).intValue();
      int y = ((Integer)data.get("y")).intValue();
      //do whatever you want
    } else {
      super.service(request, everError);
    }
  }
}

Notice that

  • AuRequests provides a collection of utilities to convert it to int, long and boolean.
  • An integer number is converted to an instance of Integer if it is not overflow (i.e., less than Integer.MAX_VALUE). Otherwise, Long is assumed.
  • A decimal number (with . or e) is converted to an instance of Double.

If the data is not a map, it can be retrieved with the empty key:

Types in JavaScript Codes in Java
wgt.fire("onFly", "sky"); String sky = (String)request.getData().get("");
wgt.fire("onFly", 123); Integer val = (Integer)request.getData().get("");
wgt.fire("onFly", ["sky", 123]); List data = (List)request.getData().get("");

String sky = (String)data.get(0); Integer val = (Integer)data.get(1);

wgt.fire("onFly", {left:'10px', top:20px', more:[1, 2]}); Map data = request.getData();

String left = (String)data.get("left"); String top = (String)data.get("top"); List more = (List)data.get("more"); Integer v1 = (Integer)more.get(0);


Map data = request.getData();
String left = (String)data.get("left");
String top = (String)data.get("left");

For custom data types, you can implement toJSON (at the client) to convert a JavaScript object to a string in custom way.

MyClass.prototype.toJSON = function (key) { //key usually meaningless
   return this.uuid;
};

In addition to the default handling, You can add a custom AU request service to a component by calling Component.setAuService(AuService).


  1. More precisely, they are converted to JSONObject (a map) and JSONObject (a list)

Version History

Last Update : 2011/08/23


Version Date Content
     



Last Update : 2011/08/23

Copyright © Potix Corporation. This article is licensed under GNU Free Documentation License.