Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JsonArray.getvaluesAs doesn't take String.class as parameter

Tags:

java

json

arrays

I'm using javax.Json.JsonArray object.

method: javax.Json.JsonArray.getvaluesAs(Class<T>) returns the JsonArray as List. So what I want to do is to get the List into a Set. this is what I do:

Set values= new HashSet(myjsonArray.getValuesAs(null));

This works fine, but I know that myjsonArray only contains String. so what I really want to do is :

Set<String> values= new HashSet(myjsonArray.getValuesAs(String.class));

For some reason for the above code I get 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 get my Set<String> built using loops, but I try to avoid that, so is there an easy fix for above error?

like image 244
nafas Avatar asked Sep 23 '14 08:09

nafas


1 Answers

The getValuesAs() method only supports classes that extends JsonValue. In this case you can use JsonString.class which then casts simply to a String.

Check this (http://docs.oracle.com/javaee/7/api/javax/json/JsonArray.html#getValuesAs-java.lang.Class-) and you could see that T extends JsonValue

like image 190
Nine Avatar answered Oct 19 '22 11:10

Nine