JSON"

From Documentation
m (Created page with '{{ZKClient-sideReferencePageHeader}} =Version History= {{LastUpdated}} {| border='1px' | width="100%" ! Version !! Date !! Content |- |   |   |   |} {{ZKClient-s…')
 
Line 1: Line 1:
 
{{ZKClient-sideReferencePageHeader}}
 
{{ZKClient-sideReferencePageHeader}}
 +
 +
The data of a widget event (<javadoc method="data" directory="jsdoc">zk.Event</javadoc>) is serialized to a string (so-called marshal) by [http://www.json.org/js.html 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<ref>More precisely, they are converted to <javadoc>org.zkoss.json.JSONObject</javadoc> (a map) and <javadoc>org.zkoss.json.JSONObject</javadoc> (a list)</ref>.
 +
 +
The map of data can be retrieve by use of <javadoc method="getData()">org.zkoss.zk.au.AuRequest</javadoc>.
 +
 +
For example, assume we fire an event at the client as follows.
 +
 +
<source lang="javascript">
 +
wgt.fire('onFly', {x: 10, y: 20});
 +
</source>
 +
 +
Then, we can retrieve and process it at the server as follows:
 +
 +
<source lang="java">
 +
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);
 +
    }
 +
  }
 +
}
 +
</source>
 +
 +
Notice that
 +
* <javadoc>org.zkoss.zk.au.AuRequests</javadoc> provides a collection of utilities to convert it to int, long and boolean.
 +
* A 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:
 +
 +
{|border="1" cellspacing="0" width="100%"
 +
|+
 +
|-
 +
! 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);
 +
|}
 +
 +
 +
<source lang="java">
 +
Map data = request.getData();
 +
String left = (String)data.get("left");
 +
String top = (String)data.get("left");
 +
</source>
 +
 +
For custom data types, you can implement <code>toJSON</code> (at the client) to convert a JavaScript object to a string in custom way.
 +
 +
<source lang="javascript">
 +
MyClass.prototype.toJSON = function (key) { //key usually meaningless
 +
  return this.uuid;
 +
};
 +
</source>
 +
 +
In addition to the default handling, You can add a custom AU request service to a component by calling <javadoc method="setAuService(org.zkoss.zk.au.AuService)" type="interface">org.zkoss.zk.ui.Component</javadoc>.
 +
 +
<blockquote>
 +
----
 +
<references/>
 +
</blockquote>
  
 
=Version History=
 
=Version History=

Revision as of 03:50, 20 December 2010


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 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.
  • A 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 : 2010/12/20


Version Date Content
     



Last Update : 2010/12/20

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