Java Code Examples for javax.json.JsonNumber#intValue()

The following examples show how to use javax.json.JsonNumber#intValue() . 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: ResultAnalyzer.java    From aceql-http with GNU Lesser General Public License v2.1 5 votes vote down vote up
/**
    * Returns the int value for a name
    *
    * @param name
    * @return the value
    */
   public int getIntvalue(String name) {
if (name == null) {
    throw new NullPointerException("name is null!");
}

if (isInvalidJsonStream()) {
    return -1;
}

try {
    JsonReader reader = Json.createReader(new StringReader(jsonResult));
    JsonStructure jsonst = reader.read();

    JsonObject object = (JsonObject) jsonst;
    JsonNumber value = (JsonNumber) object.get(name);

    if (value == null) {
	return -1;
    }

    return value.intValue();
} catch (Exception e) {
    this.parseException = e;
    return -1;
}
   }
 
Example 2
Source File: ResultAnalyzer.java    From aceql-http with GNU Lesser General Public License v2.1 5 votes vote down vote up
/**
    * Returns the error_type in case of failure
    *
    * @return the error_type in case of failure, -1 if no error
    */
   public int getErrorType() {

if (isInvalidJsonStream()) {
    return 0;
}

try {
    JsonReader reader = Json.createReader(new StringReader(jsonResult));
    JsonStructure jsonst = reader.read();

    JsonObject object = (JsonObject) jsonst;
    JsonString status = (JsonString) object.get("status");

    if (status == null) {
	return -1;
    }

    JsonNumber errorType = (JsonNumber) object.get("error_type");

    if (errorType == null) {
	return -1;
    } else {
	return errorType.intValue();
    }
} catch (Exception e) {
    this.parseException = e;
    return -1;
}

   }
 
Example 3
Source File: JsonUtil.java    From activemq-artemis with Apache License 2.0 4 votes vote down vote up
public static Object convertJsonValue(Object jsonValue, Class desiredType) {
   if (jsonValue instanceof JsonNumber) {
      JsonNumber number = (JsonNumber) jsonValue;

      if (desiredType == null || desiredType == Long.class || desiredType == Long.TYPE) {
         return number.longValue();
      } else if (desiredType == Integer.class || desiredType == Integer.TYPE) {
         return number.intValue();
      } else if (desiredType == Double.class || desiredType == Double.TYPE) {
         return number.doubleValue();
      } else {
         return number.longValue();
      }
   } else if (jsonValue instanceof JsonString) {
      return ((JsonString) jsonValue).getString();
   } else if (jsonValue instanceof JsonValue) {
      if (jsonValue == JsonValue.TRUE) {
         return true;
      } else if (jsonValue == JsonValue.FALSE) {
         return false;
      } else {
         return jsonValue.toString();
      }
   } else if (jsonValue instanceof Number) {
      Number jsonNumber = (Number) jsonValue;
      if (desiredType == Integer.TYPE || desiredType == Integer.class) {
         return jsonNumber.intValue();
      } else if (desiredType == Long.TYPE || desiredType == Long.class) {
         return jsonNumber.longValue();
      } else if (desiredType == Double.TYPE || desiredType == Double.class) {
         return jsonNumber.doubleValue();
      } else if (desiredType == Short.TYPE || desiredType == Short.class) {
         return jsonNumber.shortValue();
      } else {
         return jsonValue;
      }
   } else if (jsonValue instanceof Object[]) {
      Object[] array = (Object[]) jsonValue;
      Object[] result;
      if (desiredType != null) {
         result = (Object[]) Array.newInstance(desiredType, array.length);
      } else {
         result = array;
      }
      for (int i = 0; i < array.length; i++) {
         result[i] = convertJsonValue(array[i], desiredType);
      }
      return result;
   } else {
      return jsonValue;
   }
}