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;
}
Aggregations