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

The following examples show how to use javax.json.JsonNumber#intValueExact() . 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: JsonNumberReader.java    From smallrye-graphql with Apache License 2.0 6 votes vote down vote up
private Object read(Location location, JsonNumber value, Class<?> rawType) {
    if (byte.class.equals(rawType) || Byte.class.equals(rawType))
        return (byte) readIntBetween(location, value, Byte.MIN_VALUE, Byte.MAX_VALUE);
    if (char.class.equals(rawType) || Character.class.equals(rawType))
        return (char) readIntBetween(location, value, Character.MIN_VALUE, Character.MAX_VALUE);
    if (short.class.equals(rawType) || Short.class.equals(rawType))
        return (short) readIntBetween(location, value, Short.MIN_VALUE, Short.MAX_VALUE);
    if (int.class.equals(rawType) || Integer.class.equals(rawType))
        return value.intValueExact();
    if (long.class.equals(rawType) || Long.class.equals(rawType))
        return value.longValueExact();
    if (float.class.equals(rawType) || Float.class.equals(rawType))
        return (float) value.doubleValue();
    if (double.class.equals(rawType) || Double.class.equals(rawType))
        return value.doubleValue();
    if (BigInteger.class.equals(rawType))
        return value.bigIntegerValueExact();
    if (BigDecimal.class.equals(rawType))
        return value.bigDecimalValue();

    throw new GraphQlClientValueException(location, value);
}
 
Example 2
Source File: RequestCaseJson.java    From tcases with MIT License 5 votes vote down vote up
/**
 * Returns the DataValue represented by the given number object.
 */
private static DataValue<?> asIntegerValue( RequestCaseContext context, JsonNumber json, String format)
  {
  return
    "int64".equals( format)
    ? new LongValue( json.longValueExact())
    : new IntegerValue( json.intValueExact());
  }
 
Example 3
Source File: Message.java    From diirt with MIT License 5 votes vote down vote up
/**
 * Un-marshals an integer, or throws an exception if it's not able to.
 *
 * @param jObject the JSON object
 * @param name the attribute name where the integer is stored
 * @return the message integer
 * @throws MessageDecodeException if the field is missing or of the wrong type
 */
static int intMandatory(JsonObject jObject, String name) throws MessageDecodeException {
    try {
        JsonNumber jsonNumber = jObject.getJsonNumber(name);
        if (jsonNumber == null) {
            throw MessageDecodeException.missingMandatoryAttribute(jObject, name);
        } else {
            return jsonNumber.intValueExact();
        }
    } catch (ClassCastException | ArithmeticException e) {
        throw MessageDecodeException.wrongAttributeType(jObject, name, "integer");
    }
}
 
Example 4
Source File: Message.java    From diirt with MIT License 5 votes vote down vote up
/**
 * Un-marshals an integer, or returns the default value if it's not able to.
 *
 * @param jObject the JSON object
 * @param name the attribute name where the integer is stored
 * @param defaultValue the value to use if no attribute is found
 * @return the message integer
 * @throws MessageDecodeException if the field is of the wrong type
 */
static int intOptional(JsonObject jObject, String name, int defaultValue) throws MessageDecodeException {
    try {
        JsonNumber jsonNumber = jObject.getJsonNumber(name);
        if (jsonNumber == null) {
            return defaultValue;
        } else {
            return jsonNumber.intValueExact();
        }
    } catch (ClassCastException | ArithmeticException e) {
        throw MessageDecodeException.wrongAttributeType(jObject, name, "integer");
    }
}
 
Example 5
Source File: JsonNumberReader.java    From smallrye-graphql with Apache License 2.0 4 votes vote down vote up
private int readIntBetween(Location location, JsonNumber value, int minValue, int maxValue) {
    int intValue = value.intValueExact();
    check(location, value, intValue >= minValue);
    check(location, value, intValue <= maxValue);
    return intValue;
}