Java Code Examples for org.codehaus.jettison.json.JSONObject#isNull()

The following examples show how to use org.codehaus.jettison.json.JSONObject#isNull() . 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: StramWebServices.java    From Bats with Apache License 2.0 6 votes vote down vote up
@POST // not supported by WebAppProxyServlet, can only be called directly
@Path(PATH_PHYSICAL_PLAN_OPERATORS + "/{operatorId:\\d+}/properties")
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public JSONObject setPhysicalOperatorProperties(JSONObject request, @PathParam("operatorId") int operatorId)
{
  init();
  JSONObject response = new JSONObject();
  try {
    @SuppressWarnings("unchecked")
    Iterator<String> keys = request.keys();
    while (keys.hasNext()) {
      String key = keys.next();
      String val = request.isNull(key) ? null : request.getString(key);
      dagManager.setPhysicalOperatorProperty(operatorId, key, val);
    }
  } catch (JSONException ex) {
    LOG.warn("Got JSON Exception: ", ex);
  }
  return response;
}
 
Example 2
Source File: JsonHelper.java    From OSTMap with Apache License 2.0 6 votes vote down vote up
public static String generateCoordinates(String json) {
    try {
        JSONObject obj = new JSONObject(json);
        Double[] longLat = Extractor.extractLocation(obj);

        if (obj.has(KEY_COORDINATES) && obj.isNull(KEY_COORDINATES)) {
            //coordinates is empty
            JSONArray longLatArr = new JSONArray();
            if (longLat != null && longLat[0] != null && longLat[1] != null) {
                longLatArr.put(longLat[0]);
                longLatArr.put(longLat[1]);
                JSONObject coordinates1 = new JSONObject().put(KEY_COORDINATES, longLatArr);
                obj.put(KEY_COORDINATES, coordinates1);
            }
        }
        if (obj.has(KEY_PLACE)) {
            //ccordinates is set, remove place
            obj.remove(KEY_PLACE);
        }
        return obj.toString();
    } catch (JSONException e) {
        log.error("no correct JSON string:" + json);

        return null;
    }
}
 
Example 3
Source File: StramWebServices.java    From attic-apex-core with Apache License 2.0 6 votes vote down vote up
@POST // not supported by WebAppProxyServlet, can only be called directly
@Path(PATH_PHYSICAL_PLAN_OPERATORS + "/{operatorId:\\d+}/properties")
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public JSONObject setPhysicalOperatorProperties(JSONObject request, @PathParam("operatorId") int operatorId)
{
  init();
  JSONObject response = new JSONObject();
  try {
    @SuppressWarnings("unchecked")
    Iterator<String> keys = request.keys();
    while (keys.hasNext()) {
      String key = keys.next();
      String val = request.isNull(key) ? null : request.getString(key);
      dagManager.setPhysicalOperatorProperty(operatorId, key, val);
    }
  } catch (JSONException ex) {
    LOG.warn("Got JSON Exception: ", ex);
  }
  return response;
}
 
Example 4
Source File: Client.java    From batfish with Apache License 2.0 6 votes vote down vote up
/**
 * Loads question from a JSON
 *
 * @param questionText Question JSON Text
 * @param questionSource JSON key of question or file path of JSON
 * @return question loaded as a {@link JSONObject}
 * @throws BatfishException if question does not have instanceName or question cannot be parsed
 */
static JSONObject loadQuestionFromText(String questionText, String questionSource) {
  try {
    JSONObject questionObj = new JSONObject(questionText);
    if (questionObj.has(BfConsts.PROP_INSTANCE) && !questionObj.isNull(BfConsts.PROP_INSTANCE)) {
      JSONObject instanceDataObj = questionObj.getJSONObject(BfConsts.PROP_INSTANCE);
      String instanceDataStr = instanceDataObj.toString();
      InstanceData instanceData =
          BatfishObjectMapper.mapper()
              .readValue(instanceDataStr, new TypeReference<InstanceData>() {});
      validateInstanceData(instanceData);
      return questionObj;
    } else {
      throw new BatfishException(
          String.format("Question in %s has no instance data", questionSource));
    }
  } catch (JSONException | IOException e) {
    throw new BatfishException("Failed to process question", e);
  }
}
 
Example 5
Source File: Main.java    From batfish with Apache License 2.0 6 votes vote down vote up
@VisibleForTesting
static String readQuestionTemplate(Path file, Map<String, String> templates)
    throws JSONException, IOException {
  String questionText = CommonUtil.readFile(file);
  JSONObject questionObj = new JSONObject(questionText);
  if (questionObj.has(BfConsts.PROP_INSTANCE) && !questionObj.isNull(BfConsts.PROP_INSTANCE)) {
    JSONObject instanceDataObj = questionObj.getJSONObject(BfConsts.PROP_INSTANCE);
    String instanceDataStr = instanceDataObj.toString();
    InstanceData instanceData =
        BatfishObjectMapper.mapper().readValue(instanceDataStr, InstanceData.class);
    String name = instanceData.getInstanceName();
    String key = name.toLowerCase();
    if (templates.containsKey(key) && _logger != null) {
      _logger.warnf(
          "Found duplicate template having instance name %s, only the last one in the list of templatedirs will be loaded\n",
          name);
    }

    templates.put(key, questionText);
    return name;
  } else {
    throw new BatfishException(String.format("Question in file:%s has no instance name", file));
  }
}
 
Example 6
Source File: Extractor.java    From OSTMap with Apache License 2.0 4 votes vote down vote up
public static Double[] extractLocation(JSONObject obj) {
    Double longitude = 0.0;
    Double latitude = 0.0;
    try {
        if (!obj.isNull("coordinates")) {
            if (!obj.getJSONObject("coordinates").isNull("coordinates")) {
                JSONArray coords = obj.getJSONObject("coordinates").getJSONArray("coordinates");

                longitude = coords.getDouble(0);
                latitude = coords.getDouble(1);

            }
        } else {
            //System.out.println("In places");
            JSONArray places = obj.getJSONObject("place").getJSONObject("bounding_box").getJSONArray("coordinates").getJSONArray(0);

            if (places.length() > 2) {
                Double topLeftLong = places.getJSONArray(0).getDouble(0);
                Double topLeftLat = places.getJSONArray(0).getDouble(1);

                Double lowerLeftLong = places.getJSONArray(1).getDouble(0);
                Double lowerLeftLat = places.getJSONArray(1).getDouble(1);

                Double topRightLong = places.getJSONArray(2).getDouble(0);
                Double topRightLat = places.getJSONArray(2).getDouble(1);

                Double lowerRightLong = places.getJSONArray(3).getDouble(0);
                Double lowerRightLat = places.getJSONArray(3).getDouble(1);

                longitude = (topLeftLong + lowerLeftLong + topRightLong + lowerRightLong) / 4;
                latitude = (topLeftLat + lowerLeftLat + topRightLat + lowerRightLat) / 4;
            }
        }
        Double[] longLat = {longitude, latitude};
        return longLat;


    } catch (JSONException e) {
        System.err.println("No Correct JSON File");
        e.printStackTrace();
        return null;
    }

}