Java Code Examples for com.google.gwt.json.client.JSONObject#get()

The following examples show how to use com.google.gwt.json.client.JSONObject#get() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: WaitForBuildResultCommand.java    From appinventor-extensions with Apache License 2.0 6 votes vote down vote up
private static String extractFormName(RpcResult result) {
  String extraString = result.getExtra();
  if (extraString != null) {
    JSONValue extraJSONValue = JSONParser.parseStrict(extraString);
    JSONObject extraJSONObject = extraJSONValue.isObject();
    if (extraJSONObject != null) {
      JSONValue formNameJSONValue = extraJSONObject.get("formName");
      if (formNameJSONValue != null) {
        JSONString formNameJSONString = formNameJSONValue.isString();
        if (formNameJSONString != null) {
          return formNameJSONString.stringValue();
        }
      }
    }
  }
  return "Screen1";
}
 
Example 2
Source File: GadgetUserPrefs.java    From swellrt with Apache License 2.0 6 votes vote down vote up
/**
 * Extracts default preference values from the gadget metadata JSON object
 * returned from GGS.
 *
 * @param prefs the preference JSON object received from GGS.
 */
public void parseDefaultValues(JSONObject prefs) {
  if (prefs != null) {
    for (String pref : prefs.keySet()) {
      if (!has(pref)) {
        JSONObject prefJson = prefs.get(pref).isObject();
        if (prefJson != null) {
          JSONValue value = prefJson.get("default");
          if ((value != null) && (value.isString() != null)) {
            put(pref, value.isString().stringValue());
            log("Gadget pref '" + pref + "' = '" + get(pref) + "'");
          }
        } else {
          log("Invalid pref '" + pref + "' value in Gadget metadata.");
        }
      }
    }
  }
}
 
Example 3
Source File: GadgetUserPrefs.java    From incubator-retired-wave with Apache License 2.0 6 votes vote down vote up
/**
 * Extracts default preference values from the gadget metadata JSON object
 * returned from GGS.
 *
 * @param prefs the preference JSON object received from GGS.
 */
public void parseDefaultValues(JSONObject prefs) {
  if (prefs != null) {
    for (String pref : prefs.keySet()) {
      if (!has(pref)) {
        JSONObject prefJson = prefs.get(pref).isObject();
        if (prefJson != null) {
          JSONValue value = prefJson.get("default");
          if ((value != null) && (value.isString() != null)) {
            put(pref, value.isString().stringValue());
            log("Gadget pref '" + pref + "' = '" + get(pref) + "'");
          }
        } else {
          log("Invalid pref '" + pref + "' value in Gadget metadata.");
        }
      }
    }
  }
}
 
Example 4
Source File: UploadResponse.java    From core with GNU Lesser General Public License v2.1 6 votes vote down vote up
private String extractFailure(final JSONObject response) {
    String failure = "n/a";
    JSONValue failureValue = response.get(FAILURE_DESCRIPTION);
    if (failureValue.isString() != null) {
        failure = failureValue.isString().stringValue();
    } else if (failureValue.isObject() != null) {
        JSONObject failureObject = failureValue.isObject();
        for (String key : failureObject.keySet()) {
            if (key.contains("failure") && failureObject.get(key).isString() != null) {
                failure = failureObject.get(key).isString().stringValue();
                break;
            }
        }
    }
    return failure;
}
 
Example 5
Source File: GadgetMetadata.java    From swellrt with Apache License 2.0 5 votes vote down vote up
/**
 * Helper function to extract a string value from given JSON object.
 *
 * @param json JSON object to extract the value from.
 * @param key key of the value to extract.
 * @return the string object extracted from JSON (can be null if the value
 *         does not exist or is invalid.
 */
private static String getJsonStringValue(JSONObject json, String key) {
  JSONValue value = json.get(key);
  JSONString string = (value == null) ? null : value.isString();
  if (string != null) {
    return string.stringValue();
  } else {
    return null;
  }
}
 
Example 6
Source File: GadgetMetadata.java    From swellrt with Apache License 2.0 5 votes vote down vote up
/**
 * Helper function to extract a long value from given JSON object.
 *
 * @param json JSON object to extract the value from.
 * @param key key of the value to extract.
 * @return the Long object extracted from JSON (can be null if the value does
 *         not exist or is invalid.
 */
private static Long getJsonLongValue(JSONObject json, String key) {
  JSONValue value = json.get(key);
  JSONNumber number = (value == null) ? null : value.isNumber();
  if (number != null) {
    return Math.round(number.doubleValue());
  } else {
    return null;
  }
}
 
Example 7
Source File: GadgetMetadata.java    From swellrt with Apache License 2.0 5 votes vote down vote up
/**
 * Helper function to extract a boolean value from given JSON object.
 *
 * @param json JSON object to extract the value from.
 * @param key key of the value to extract.
 * @return the Boolean object extracted from JSON (can be null if the value
 *         does not exist or is invalid.
 */
private static Boolean getJsonBooleanValue(JSONObject json, String key) {
  JSONValue value = json.get(key);
  JSONBoolean bool = (value == null) ? null : value.isBoolean();
  if (bool != null) {
    return bool.booleanValue();
  } else {
    return null;
  }
}
 
Example 8
Source File: GadgetMetadata.java    From incubator-retired-wave with Apache License 2.0 5 votes vote down vote up
/**
 * Helper function to extract a string value from given JSON object.
 *
 * @param json JSON object to extract the value from.
 * @param key key of the value to extract.
 * @return the string object extracted from JSON (can be null if the value
 *         does not exist or is invalid.
 */
private static String getJsonStringValue(JSONObject json, String key) {
  JSONValue value = json.get(key);
  JSONString string = (value == null) ? null : value.isString();
  if (string != null) {
    return string.stringValue();
  } else {
    return null;
  }
}
 
Example 9
Source File: GadgetMetadata.java    From incubator-retired-wave with Apache License 2.0 5 votes vote down vote up
/**
 * Helper function to extract a long value from given JSON object.
 *
 * @param json JSON object to extract the value from.
 * @param key key of the value to extract.
 * @return the Long object extracted from JSON (can be null if the value does
 *         not exist or is invalid.
 */
private static Long getJsonLongValue(JSONObject json, String key) {
  JSONValue value = json.get(key);
  JSONNumber number = (value == null) ? null : value.isNumber();
  if (number != null) {
    return Math.round(number.doubleValue());
  } else {
    return null;
  }
}
 
Example 10
Source File: GadgetMetadata.java    From incubator-retired-wave with Apache License 2.0 5 votes vote down vote up
/**
 * Helper function to extract a boolean value from given JSON object.
 *
 * @param json JSON object to extract the value from.
 * @param key key of the value to extract.
 * @return the Boolean object extracted from JSON (can be null if the value
 *         does not exist or is invalid.
 */
private static Boolean getJsonBooleanValue(JSONObject json, String key) {
  JSONValue value = json.get(key);
  JSONBoolean bool = (value == null) ? null : value.isBoolean();
  if (bool != null) {
    return bool.booleanValue();
  } else {
    return null;
  }
}
 
Example 11
Source File: SubsetJSONPropertyEditor.java    From appinventor-extensions with Apache License 2.0 4 votes vote down vote up
private void loadComponents(JSONObject jsonObj) {
  // TODO: Review JSON format. There has to be a better way to store and retrieve this info.
  JSONObject shownComponents = jsonObj.get("shownComponentTypes").isObject();
  JSONObject shownComponentBlocks = jsonObj.get("shownBlockTypes").isObject().get("ComponentBlocks").isObject();
  for (int i = 0; i < componentTree.getItemCount(); ++i) {
    TreeItem componentCatItem = componentTree.getItem(i);
    CheckBox componentCatCb = (CheckBox)componentCatItem.getWidget();
    String catName = componentCatCb.getName().toUpperCase();
    if (shownComponents.containsKey(catName)) {
      JSONArray jsonComponentCat = shownComponents.get(catName).isArray();
      if (jsonComponentCat.size() > 0) {
        componentCatCb.setValue(true, false);
        HashMap<String, String> jsonComponentHash = new HashMap<String, String>();
        for (int j = 0; j < jsonComponentCat.size(); ++j) {
          JSONValue jsonComponentHashCat = jsonComponentCat.get(j);
          if (jsonComponentHashCat != null) {
            jsonComponentHash.put(jsonComponentHashCat.isObject().get("type").isString().stringValue(), "type");
          }
        }
        for (int j = 0; j < componentCatItem.getChildCount(); ++j) {
          TreeItem componentItem = componentCatItem.getChild(j);
          CheckBox componentCb = (CheckBox) componentItem.getWidget();
          if (jsonComponentHash.get(componentCb.getName()) != null) {
            componentCb.setValue(true, false);
            JSONArray jsonComponentBlockProps = shownComponentBlocks.get(componentCb.getName()).isArray();
            HashMap<String, String> componentPropHash = new HashMap<String, String>();
            for (int k = 0; k < jsonComponentBlockProps.size(); ++k) {
              JSONObject jsonComponentBlockType = jsonComponentBlockProps.get(k).isObject();
              String componentBlockType = jsonComponentBlockType.get("type").isString().stringValue();
              if ("component_set_get".equals(componentBlockType)) {
                componentPropHash.put(jsonComponentBlockType.get("mutatorNameToValue").isObject().get("property_name").isString().stringValue(), "PROP");
              } else if ("component_event".equals(componentBlockType)) {
                JSONValue mutatorValue = jsonComponentBlockType.get("mutatorNameToValue");
                JSONValue event_name = mutatorValue.isObject().get("event_name");
                componentPropHash.put(event_name.isString().stringValue(), "EVENT");
              } else if ("component_method".equals(componentBlockType)) {
                componentPropHash.put(jsonComponentBlockType.get("mutatorNameToValue").isObject().get("method_name").isString().stringValue(), "METHOD");
              }
            }
            for (int k = 0; k < componentItem.getChildCount(); ++k) {
              TreeItem componentPropItem = componentItem.getChild(k);
              CheckBox componentPropCb = (CheckBox) componentPropItem.getWidget();
              if (componentPropHash.get(componentPropCb.getText()) != null) {
                componentPropCb.setValue(true, false);
              } else {
                componentPropCb.setValue(false, false);
              }
            }

          } else {
            componentCb.setValue(false, false);
            toggleChildren(componentItem, false);
          }
        }
      } else {
        componentCatCb.setValue(false, false);
        toggleChildren(componentCatItem, false);
      }
    } else {
      componentCatCb.setValue(false, false);
      toggleChildren(componentCatItem, false);
    }
  }
}
 
Example 12
Source File: JsonTokenizer.java    From proarc with GNU General Public License v3.0 4 votes vote down vote up
public static String getString(JSONObject json, String name) {
    JSONValue jsonVal = json.get(name);
    JSONString jsonString = (jsonVal == null) ? null : jsonVal.isString();
    return (jsonString == null) ? null : jsonString.stringValue();
}
 
Example 13
Source File: GadgetMetadata.java    From swellrt with Apache License 2.0 4 votes vote down vote up
/**
 * Parses JSON object to extract metadata information.
 *
 * @param gadget JSON object returned in the gadget field from the server.
 */
public void parse(JSONObject gadget) {
  iframeUrl = getJsonStringValue(gadget, "iframeUrl");
  // Get rid of the protocol as we should rely on the browser to do the right
  // thing with "//" as the beginning of the url.
  // By doing this it also works around an issue in shindig where it shouldn't
  // put "//" in front of "http://" see:
  // https://issues.apache.org/jira/browse/SHINDIG-1460?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=12928110#action_12928110
  // This work around may no longer be needed on the next version of shindig
  // Added null check on iframeUrl.Issue #166:Client shiny on broken gadget.
  if(iframeUrl != null) {
    iframeUrl = iframeUrl.replaceFirst("^//http[s]?://", "//");
  }
  url = getJsonStringValue(gadget, "url");
  moduleId = getJsonStringValue(gadget, "moduleId");
  title = getJsonStringValue(gadget, "title");
  titleUrl = getJsonStringValue(gadget, "titleUrl");
  directoryTitle = getJsonStringValue(gadget, "directoryTitle");
  thumbnail = getJsonStringValue(gadget, "thumbnail");
  screenshot = getJsonStringValue(gadget, "screenshot");
  author = getJsonStringValue(gadget, "author");
  authorEmail = getJsonStringValue(gadget, "authorEmail");
  height = getJsonLongValue(gadget, "height");
  width = getJsonLongValue(gadget, "width");
  scrolling = getJsonBooleanValue(gadget, "scrolling");
  // Added null check on gadget.get("userPrefs").Issue #166:Client shiny on broken gadget.
  if(gadget.get("userPrefs") != null) {
    userPrefs.parseDefaultValues(gadget.get("userPrefs").isObject());
  }
  JSONObject gadgetViews = getJsonObjectValue(gadget, "views");
  if (gadgetViews != null) {
    View lastView = null;
    for (String viewName : gadgetViews.keySet()) {
      JSONObject viewJson = gadgetViews.get(viewName).isObject();
      View v = new View();
      v.type = viewName;
      v.preferredHeight = getJsonLongValue(viewJson, "preferredHeight");
      v.preferredWidth = getJsonLongValue(viewJson, "preferredWidth");
      views.put(v.type, v);
      lastView = v;
    }
  }
}
 
Example 14
Source File: GadgetMetadata.java    From incubator-retired-wave with Apache License 2.0 4 votes vote down vote up
/**
 * Parses JSON object to extract metadata information.
 *
 * @param gadget JSON object returned in the gadget field from the server.
 */
public void parse(JSONObject gadget) {
  iframeUrl = getJsonStringValue(gadget, "iframeUrl");
  // Get rid of the protocol as we should rely on the browser to do the right
  // thing with "//" as the beginning of the url.
  // By doing this it also works around an issue in shindig where it shouldn't
  // put "//" in front of "http://" see:
  // https://issues.apache.org/jira/browse/SHINDIG-1460?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=12928110#action_12928110
  // This work around may no longer be needed on the next version of shindig
  // Added null check on iframeUrl.Issue #166:Client shiny on broken gadget.
  if(iframeUrl != null) {
    iframeUrl = iframeUrl.replaceFirst("^//http[s]?://", "//");
  }
  url = getJsonStringValue(gadget, "url");
  moduleId = getJsonStringValue(gadget, "moduleId");
  title = getJsonStringValue(gadget, "title");
  titleUrl = getJsonStringValue(gadget, "titleUrl");
  directoryTitle = getJsonStringValue(gadget, "directoryTitle");
  thumbnail = getJsonStringValue(gadget, "thumbnail");
  screenshot = getJsonStringValue(gadget, "screenshot");
  author = getJsonStringValue(gadget, "author");
  authorEmail = getJsonStringValue(gadget, "authorEmail");
  height = getJsonLongValue(gadget, "height");
  width = getJsonLongValue(gadget, "width");
  scrolling = getJsonBooleanValue(gadget, "scrolling");
  // Added null check on gadget.get("userPrefs").Issue #166:Client shiny on broken gadget.
  if(gadget.get("userPrefs") != null) {
    userPrefs.parseDefaultValues(gadget.get("userPrefs").isObject());
  }
  JSONObject gadgetViews = getJsonObjectValue(gadget, "views");
  if (gadgetViews != null) {
    View lastView = null;
    for (String viewName : gadgetViews.keySet()) {
      JSONObject viewJson = gadgetViews.get(viewName).isObject();
      View v = new View();
      v.type = viewName;
      v.preferredHeight = getJsonLongValue(viewJson, "preferredHeight");
      v.preferredWidth = getJsonLongValue(viewJson, "preferredWidth");
      views.put(v.type, v);
      lastView = v;
    }
  }
}
 
Example 15
Source File: GadgetMetadata.java    From swellrt with Apache License 2.0 2 votes vote down vote up
/**
 * Helper function to extract a JSONObject from a JSONValue if it exists.
 *
 * @param json JSON object to extract the value from.
 * @param key key of the value to extract.
 * @return the JSONObject if it exists.
 */
private static JSONObject getJsonObjectValue(JSONObject json, String key) {
  JSONValue value = json.get(key);
  return (value != null) ? value.isObject() : null;
}
 
Example 16
Source File: GadgetMetadata.java    From incubator-retired-wave with Apache License 2.0 2 votes vote down vote up
/**
 * Helper function to extract a JSONObject from a JSONValue if it exists.
 *
 * @param json JSON object to extract the value from.
 * @param key key of the value to extract.
 * @return the JSONObject if it exists.
 */
private static JSONObject getJsonObjectValue(JSONObject json, String key) {
  JSONValue value = json.get(key);
  return (value != null) ? value.isObject() : null;
}