FileDocCategorySizeDatePackage
JSON.javaAPI DocExample6866Mon Aug 27 20:12:16 BST 2007com.google.gwt.sample.json.client

JSON

public class JSON extends Object
Class that acts as a client to a JSON service. Currently, this client just requests a text which contains a JSON encoding of a search result set from yahoo. We use a text file to demonstrate how the pieces work without tripping on cross-site scripting issues. If you would like to make this a more dynamic example, you can associate a servlet with this example and simply have it hit the yahoo service and return the results.

Fields Summary
private static final String
DEFAULT_SEARCH_URL
private static final String
SEARCH_BUTTON_DEFAULT_TEXT
private static final String
SEARCH_BUTTON_WAITING_TEXT
private com.google.gwt.user.client.ui.Tree
jsonTree
private com.google.gwt.user.client.ui.Button
searchButton
Constructors Summary
Methods Summary
private voidaddChildren(com.google.gwt.user.client.ui.TreeItem treeItem, com.google.gwt.json.client.JSONValue jsonValue)

    JSONArray jsonArray;
    JSONObject jsonObject;
    JSONString jsonString;

    if ((jsonArray = jsonValue.isArray()) != null) {
      for (int i = 0; i < jsonArray.size(); ++i) {
        TreeItem child = treeItem.addItem(getChildText("["
            + Integer.toString(i) + "]"));
        addChildren(child, jsonArray.get(i));
      }
    } else if ((jsonObject = jsonValue.isObject()) != null) {
      Set keys = jsonObject.keySet();
      for (Iterator iter = keys.iterator(); iter.hasNext();) {
        String key = (String) iter.next();
        TreeItem child = treeItem.addItem(getChildText(key));
        addChildren(child, jsonObject.get(key));
      }
    } else if ((jsonString = jsonValue.isString()) != null) {
      // Use stringValue instead of toString() because we don't want escaping
      treeItem.addItem(jsonString.stringValue());
    } else {
      // JSONBoolean, JSONNumber, and JSONNull work well with toString().
      treeItem.addItem(getChildText(jsonValue.toString()));
    }
  
private voiddisplayError(java.lang.String responseText)

    jsonTree.removeItems();
    jsonTree.setVisible(true);
    TreeItem treeItem = jsonTree.addItem("Failed to parse JSON response");
    treeItem.addItem(responseText);
    treeItem.setStyleName("JSON-JSONResponseObject");
    treeItem.setState(true);
  
private voiddisplayJSONObject(com.google.gwt.json.client.JSONValue jsonValue)

    jsonTree.removeItems();
    jsonTree.setVisible(true);
    TreeItem treeItem = jsonTree.addItem("JSON Response");
    addChildren(treeItem, jsonValue);
    treeItem.setStyleName("JSON-JSONResponseObject");
    treeItem.setState(true);
  
private voiddoFetchURL()

    searchButton.setText(SEARCH_BUTTON_WAITING_TEXT);
    if (!HTTPRequest.asyncGet(DEFAULT_SEARCH_URL, new JSONResponseTextHandler())) {
      // Reset the caption.
      //
      searchButton.setText(SEARCH_BUTTON_DEFAULT_TEXT);
    }
  
private java.lang.StringgetChildText(java.lang.String text)

    return "<span style='white-space:normal'>" + text + "</span>";
  
private voidinitializeMainForm()
Initialize the main form's layout and content.

    searchButton.setStyleName("JSON-SearchButton");
    searchButton.setText(SEARCH_BUTTON_DEFAULT_TEXT);
    searchButton.addClickListener(new SearchButtonClickListener());

    // Avoids showing an "empty" cell
    jsonTree.setVisible(false);

    // Find out where the host page wants the button.
    //
    RootPanel searchButtonSlot = RootPanel.get("search");
    if (searchButtonSlot == null) {
      Window.alert("Please define a container element whose id is 'search'");
      return;
    }

    // Find out where the host page wants the tree view.
    //
    RootPanel treeViewSlot = RootPanel.get("tree");
    if (treeViewSlot == null) {
      Window.alert("Please define a container element whose id is 'tree'");
      return;
    }

    // Add both widgets.
    //
    searchButtonSlot.add(searchButton);
    treeViewSlot.add(jsonTree);
  
public voidonModuleLoad()
Entry point for this simple application. Currently, we build the application's form and wait for events.


                     
     
    initializeMainForm();