Search in sources :

Example 1 with ValueType

use of javax.json.JsonValue.ValueType in project Payara by payara.

the class CompositeUtil method processJsonArray.

/**
 * Turns a JsonArray into an array or a list of the unboxed type it contains
 * @param locale
 * @param param0 The type of the array or list to create i.e. java.lang.String[]
 * @param array
 * @return
 * @throws JsonException
 */
private Object processJsonArray(Locale locale, Type param0, JsonArray array) throws JsonException {
    Type type;
    boolean isArray = false;
    if (ParameterizedType.class.isAssignableFrom(param0.getClass())) {
        type = ((ParameterizedType) param0).getActualTypeArguments()[0];
    } else {
        isArray = ((Class<?>) param0).isArray();
        type = ((Class<?>) param0).getComponentType();
    }
    // TODO: We either have a List<T> or T[]. While this works, perhaps we should only support List<T>. It's cleaner.
    Object values = isArray ? Array.newInstance((Class<?>) type, array.size()) : new ArrayList();
    for (int i = 0; i < array.size(); i++) {
        JsonValue element = array.get(i);
        if (JsonObject.class.isAssignableFrom(element.getClass())) {
            if (isArray) {
                Array.set(values, i, unmarshallClass(locale, (Class) type, (JsonObject) element));
            } else {
                ((List) values).add(unmarshallClass(locale, (Class) type, (JsonObject) element));
            }
        } else {
            if (isArray) {
                ValueType jsonvalue = element.getValueType();
                switch(jsonvalue) {
                    case STRING:
                        Array.set(values, i, element.toString());
                        break;
                    case NUMBER:
                        Array.set(values, i, ((JsonNumber) element).numberValue());
                        break;
                    case NULL:
                        Array.set(values, i, null);
                        break;
                    case TRUE:
                        Array.set(values, i, true);
                        break;
                    case FALSE:
                        Array.set(values, i, false);
                        break;
                    case ARRAY:
                        Array.set(values, i, processJsonArray(locale, param0, (JsonArray) element));
                        break;
                    default:
                        // Should never get here
                        throw new JsonException("Json Element " + element + "not valid valuetype: " + jsonvalue);
                }
            } else {
                ((List) values).add(element);
            }
        }
    }
    return values;
}
Also used : JsonException(javax.json.JsonException) ValueType(javax.json.JsonValue.ValueType) ArrayList(java.util.ArrayList) JsonValue(javax.json.JsonValue) JsonObject(javax.json.JsonObject) ParameterizedType(java.lang.reflect.ParameterizedType) JsonArray(javax.json.JsonArray) ValueType(javax.json.JsonValue.ValueType) Type(java.lang.reflect.Type) ParameterizedType(java.lang.reflect.ParameterizedType) JsonObject(javax.json.JsonObject) List(java.util.List) ArrayList(java.util.ArrayList)

Aggregations

ParameterizedType (java.lang.reflect.ParameterizedType)1 Type (java.lang.reflect.Type)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 JsonArray (javax.json.JsonArray)1 JsonException (javax.json.JsonException)1 JsonObject (javax.json.JsonObject)1 JsonValue (javax.json.JsonValue)1 ValueType (javax.json.JsonValue.ValueType)1