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

The following examples show how to use javax.json.JsonNumber#doubleValue() . 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: RawClaimTypeProducer.java    From smallrye-jwt with Apache License 2.0 6 votes vote down vote up
@Produces
@Claim("")
Double getClaimAsDouble(InjectionPoint ip) {
    CDILogging.log.getClaimAsDouble(ip);
    if (currentToken == null) {
        return null;
    }

    String name = getName(ip);
    Optional<Object> optValue = currentToken.claim(name);
    Double returnValue = null;
    if (optValue.isPresent()) {
        Object value = optValue.get();
        if (value instanceof JsonNumber) {
            JsonNumber jsonValue = (JsonNumber) value;
            returnValue = jsonValue.doubleValue();
        } else {
            returnValue = Double.parseDouble(value.toString());
        }
    }
    return returnValue;
}
 
Example 3
Source File: JwtClaimsBuilderImpl.java    From smallrye-jwt with Apache License 2.0 6 votes vote down vote up
private static Object convertJsonValue(JsonValue jsonValue) {
    if (jsonValue instanceof JsonString) {
        String jsonString = jsonValue.toString();
        return jsonString.toString().substring(1, jsonString.length() - 1);
    } else if (jsonValue instanceof JsonNumber) {
        JsonNumber jsonNumber = (JsonNumber) jsonValue;
        if (jsonNumber.isIntegral()) {
            return jsonNumber.longValue();
        } else {
            return jsonNumber.doubleValue();
        }
    } else if (jsonValue == JsonValue.TRUE) {
        return true;
    } else if (jsonValue == JsonValue.FALSE) {
        return false;
    } else {
        return null;
    }
}
 
Example 4
Source File: SolrEntityIndexerMixin.java    From attic-polygene-java with Apache License 2.0 4 votes vote down vote up
private void indexJson( SolrInputDocument input, Object object )
{
    if( object instanceof JsonArray )
    {
        JsonArray array = (JsonArray) object;
        for( int i = 0; i < array.size(); i++ )
        {
            indexJson( input, array.get( i ) );
        }
    }
    else
    {
        JsonObject jsonObject = (JsonObject) object;
        for( String name : jsonObject.keySet() )
        {
            JsonValue jsonValue = jsonObject.get( name );
            if( jsonValue.getValueType() == JsonValue.ValueType.OBJECT
                || jsonValue.getValueType() == JsonValue.ValueType.ARRAY )
            {
                indexJson( input, jsonValue );
            }
            else
            {
                SchemaField field = indexedFields.get( name );
                if( field != null )
                {
                    Object value;
                    switch( jsonValue.getValueType() )
                    {
                        case NULL:
                            value = null;
                            break;
                        case STRING:
                            value = ( (JsonString) jsonValue ).getString();
                            break;
                        case NUMBER:
                            JsonNumber jsonNumber = (JsonNumber) jsonValue;
                            value = jsonNumber.isIntegral() ? jsonNumber.longValue() : jsonNumber.doubleValue();
                            break;
                        case TRUE:
                            value = Boolean.TRUE;
                            break;
                        case FALSE:
                            value = Boolean.FALSE;
                            break;
                        default:
                            value = jsonValue.toString();
                    }
                    input.addField( name, value );
                }
            }
        }
    }
}
 
Example 5
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;
   }
}