Section 8.9. Using the JSON Module


8.9. Using the JSON Module

Our application's module inherits the GWT's JSON module. Our GwtAjax.java class imports classes that are part of this API. If you do not include the associated module in your own module XML file, as in:

 <inherits name="com.google.gwt.json.JSON"/> 

then the GWT compiler will complain, for example, that it cannot resolve the following import statements.

 import com.google.gwt.json.client.JSONObject; import com.google.gwt.json.client.JSONParser; import com.google.gwt.json.client.JSONValue; import com.google.gwt.json.client.JSONException; 

We are using these classes because the client-side code, which will eventually be JavaScript, will handle a JSON return value from the server. A simplified version of the return value looks like this:

 ("server":"Apache","date":"November 8, 2006","browser":"Firefox"} 

The client-side code pulls the server, date, and browser-related information out of this object and displays them in the TextBoxes. As mentioned in a previous note, evaluating JSON formats as program code can open up your application to malicious insertions of JavaScript (I hope this phrase does not evolve into another acronym: MIJ!)

Therefore, the client application uses a regular expression to filter the textual server return value before the string is evaluated as JavaScript code.

For example, if the server sends back code such as alert('Gotcha!'),then the following code piece will actually end up displaying that browser window and message:

 JSONValue jval = JSONParser.parse(responseText); 

Here is the GwtAjax.java code for accomplishing these filtering and formatting actions:

 //The regular expression is an instance variable private final static String regexCheck =  "^\\{\"(server|browser|date)\":\".+\"\\,\"(server|browser|date)\":"+  "\".+\"\\,\"(server|browser|date)\":\".+\"\\}$"; //..Later on in the code try {    if (! checkResponseValue(responseText))        throw new JSONException("Return value was invalid.");    JSONValue jval = JSONParser.parse(responseText);    JSONObject jobj = jval.isObject();     if (jobj != null){         //Fill in the server info         servBox.setText(jobj.get("server").isString().         stringValue());         //Fill in the date info         timeBox.setText(jobj.get("date").isString().         stringValue());         //Fill in the browser info         browserBox.setText(jobj.get("browser").isString().         stringValue());         //Refresh the status message         status.showStatus(true,         myConstants.JsonMsg(),"green");     } } catch (JSONException je)  {//display Exception message} //The method that uses the regular expression private boolean checkResponseValue(String val) {   return val.matches(regexCheck); } 

This code parses the return value and pulls out the string values, using objects from GWT's JSON classes.

The regular expression String is quite difficult to look at; therefore, here is a narrative description of what the expression is designed to accomplish.

The String the expression checks for begins with "{" and double quotations, then continues with either the words server, browser, or date, followed by ending quotations and a colon (":"). In the pattern, these characters are followed by one or more characters in double quotations and a comma. The expression repeats the latter pattern twice until the String concludes with end doouble quotes and a "}" character.

NOTE

What if we wanted to handle return values formatted as XML instead of JSON? We can do that! We just need to import another module in our XXX.gwt.XML file. This module is named com.google.gwt.xml.XML. Inherit it the same way that your module inherited the JSON moduleby adding an inherits element to your own module. Then you use the GWT API classes in the com.google.gwt.xml.client package to handle the HTTP responses in XML format.




Google Web Toolkit for Ajax
Google Web Toolkit GWT Java AJAX Programming: A step-by-step to Google Web Toolkit for creating Ajax applications fast
ISBN: 1847191002
EAN: 2147483647
Year: 2006
Pages: 29

Similar book on Amazon

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net