JsonArray.getvaluesAs does not take String.class parameter as parameter
I am using javax.Json.JsonArray
object.
method: javax.Json.JsonArray.getvaluesAs(Class<T>)
returns JsonArray
as List. So I want to do List
in Set
. this is what i am doing:
Set values= new HashSet(myjsonArray.getValuesAs(null));
This works great, but I know it myjsonArray
only contains String
. so I really want:
Set<String> values= new HashSet(myjsonArray.getValuesAs(String.class));
For some reason, for the above code, I am getting a compiler error:
The generic method getValuesAs(Class<T>) of type JsonArray is not applicable for the arguments (Class<String>). The inferred type String is not a valid substitute for the bounded parameter <T extends JsonValue>
I can create mine Set<String>
built using loops, but I try to avoid it, so is there an easy fix for the error above?
The getValuesAs () method only supports classes that extend JsonValue. In this case, you can use JsonString.class, which then just passes the string.
Check this out ( http://docs.oracle.com/javaee/7/api/javax/json/JsonArray.html#getValuesAs-java.lang.Class- ) and you could see T extends JsonValue
I have used this library a long time ago. From what I remember, you can only pass the class to the getValuesAs () method, which extends from JsonValue. This is why the line doesn't work. You will have to try another method. Sorry, I don't remember which method exactly.
As Nine said the getValuesAs () method only supports classes that extend JsonValue "
Here's my work (and the only one possible) for this problem:
private static List<String> toList(JsonArray json){
List<String> list = new ArrayList<String>();
if (json != null){
for (JsonString elm: json.getValuesAs(JsonString.class)){
list.add(elm.getString());
}
}
return list;
}