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

The following examples show how to use javax.json.JsonNumber#isIntegral() . 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: 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 2
Source File: PropertyGroup.java    From FHIR with Apache License 2.0 5 votes vote down vote up
/**
 * Converts the specified JsonValue into the appropriate java.lang.* type.
 * @param jsonValue the JsonValue instance to be converted
 * @return an instance of Boolean, Integer, String, PropertyGroup, or List<Object>
 * @throws Exception 
 */
public static Object convertJsonValue(JsonValue jsonValue) throws Exception {
    Object result = null;
    switch (jsonValue.getValueType()) {
    case ARRAY:
        Object[] objArray = convertJsonArray((JsonArray)jsonValue);
        result = Arrays.asList(objArray);
        break;
    case OBJECT:
        result = new PropertyGroup((JsonObject) jsonValue);
        break;
    case STRING:
        result = FHIRUtilities.decode(((JsonString) jsonValue).getString());
        break;
    case NUMBER:
        JsonNumber jsonNumber = (JsonNumber) jsonValue;
        if (jsonNumber.isIntegral()) {
            result = Integer.valueOf(jsonNumber.intValue());
        } else {
            result = Double.valueOf(jsonNumber.doubleValue());
        }
        break;
    case TRUE:
        result = Boolean.TRUE;
        break;
    case FALSE:
        result = Boolean.FALSE;
        break;
    default:
        throw new IllegalStateException("Unexpected JSON value type: " + jsonValue.getValueType().name());
    }
    return result;
}
 
Example 3
Source File: VertxJson.java    From quarkus with Apache License 2.0 5 votes vote down vote up
public static void copy(JsonArray array, javax.json.JsonArray origin) {
    for (int i = 0; i < origin.size(); i++) {
        JsonValue value = origin.get(i);
        JsonValue.ValueType kind = value.getValueType();
        switch (kind) {
            case STRING:
                array.add(origin.getString(i));
                break;
            case TRUE:
                array.add(true);
                break;
            case FALSE:
                array.add(false);
                break;
            case NULL:
                array.addNull();
                break;
            case NUMBER:
                JsonNumber number = origin.getJsonNumber(i);
                if (number.isIntegral()) {
                    array.add(number.longValue());
                } else {
                    array.add(number.doubleValue());
                }
                break;
            case ARRAY:
                JsonArray newArray = new JsonArray();
                copy(newArray, origin.getJsonArray(i));
                array.add(newArray);
                break;
            case OBJECT:
                JsonObject newObject = new JsonObject();
                copy(newObject, origin.getJsonObject(i));
                array.add(newObject);
                break;
            default:
                throw new IllegalArgumentException("Unknown JSON Value " + kind);
        }
    }
}
 
Example 4
Source File: ProviderInjectionEndpoint.java    From microprofile-jwt-auth with Apache License 2.0 5 votes vote down vote up
@GET
@Path("/verifyInjectedCustomInteger")
@Produces(MediaType.APPLICATION_JSON)
public JsonObject verifyInjectedCustomInteger(@QueryParam("value") Long value) {
    boolean pass = false;
    String msg;
    // iat
    Object test = customInteger.get();
    System.out.printf("+++ verifyInjectedCustomInteger, JsonNumber.class.CL: %s\n",
        JsonNumber.class.getClassLoader());
    System.out.printf("+++ customInteger.CL: %s\n",
        test.getClass().getClassLoader());
    Class[] ifaces = test.getClass().getInterfaces();
    for(Class iface : ifaces) {
        System.out.printf("%s: %s\n", iface, iface.getClassLoader());
    }
    JsonNumber customValue = JsonNumber.class.cast(test);
    if(customValue == null || customValue.isIntegral() == false) {
        msg = "customInteger value is null or not integral, FAIL";
    }
    else if(customValue.longValueExact() == value) {
        msg = "customInteger PASS";
        pass = true;
    }
    else {
        msg = String.format("customInteger: %d != %d", customValue, value);
    }
    JsonObject result = Json.createObjectBuilder()
        .add("pass", pass)
        .add("msg", msg)
        .build();
    return result;
}
 
Example 5
Source File: ClaimValueInjectionEndpoint.java    From microprofile-jwt-auth with Apache License 2.0 5 votes vote down vote up
@GET
@Path("/verifyInjectedCustomInteger")
@Produces(MediaType.APPLICATION_JSON)
public JsonObject verifyInjectedCustomInteger(@QueryParam("value") Long value) {
    boolean pass = false;
    String msg;
    Object test = customInteger.getValue();
    System.out.printf("+++ verifyInjectedCustomInteger, JsonNumber.class.CL: %s\n",
        JsonNumber.class.getClassLoader());
    System.out.printf("+++ customInteger.CL: %s\n",
        test.getClass().getClassLoader());
    Class[] ifaces = test.getClass().getInterfaces();
    for(Class iface : ifaces) {
        System.out.printf("%s: %s\n", iface, iface.getClassLoader());
    }
    JsonNumber customValue = JsonNumber.class.cast(test);
    if(customValue == null || customValue.isIntegral() == false) {
        msg = "customInteger value is null or not integral, FAIL";
    }
    else if(customValue.longValueExact() == value) {
        msg = "customInteger PASS";
        pass = true;
    }
    else {
        msg = String.format("customInteger: %d != %d", customValue, value);
    }
    JsonObject result = Json.createObjectBuilder()
        .add("pass", pass)
        .add("msg", msg)
        .build();
    return result;
}
 
Example 6
Source File: JsonUnmarshaller.java    From karaf-decanter with Apache License 2.0 5 votes vote down vote up
private Object unmarshalAttribute(JsonValue value) {
    if (value instanceof JsonNumber) {
        JsonNumber num = (JsonNumber)value;
        return num.isIntegral() ? num.longValue() : num.bigDecimalValue();
    } else if (value instanceof JsonString) {
        return ((JsonString)value).getString();
    } else if (value instanceof JsonObject) {
        return build((JsonObject)value);
    } else if (value instanceof JsonArray) {
        return build((JsonArray)value);
    } else {
        return null;
    }
}
 
Example 7
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 );
                }
            }
        }
    }
}