Question

Eclipse displayed a type safety warning and I tried nearly everything to eradicate it (of course suppressing it would be an option) but unfortunately I hadn't any success.
Do you know how I have to change my code so that there is no type safety warning anymore. Or is @SuppressWarnings("unchecked") the only way?

ArrayList<String> arrayList= (ArrayList<String>) Classname.getArrayList();      
JSONArray obj = new JSONArray();
obj.addAll(arrayList); 

in the last line following type safety warning is displayed:

Type safety: The method addAll(Collection) belongs to the raw type ArrayList. References to generic type ArrayList should be parametrized

JSONArray is fromorg.json.simple.JSONArray. Would you recommend another package?

Was it helpful?

Solution

If you want to work with json go this library, this library has a nice support https://code.google.com/p/google-gson/.

This is an example:

Gson gson = new Gson();
Collection<Integer> ints = Lists.immutableList(1,2,3,4,5);

(Serialization)
String json = gson.toJson(ints); ==> json is [1,2,3,4,5]

Thanks

OTHER TIPS

org.json and org.json.simple JSON parser use raw types of collections underneath. If you're looking for good Generics support try Google Gson. Here's how you would go about serializing your generic ArrayList with Gson:

Gson gson = new Gson();

ArrayList<String> arrayList= (ArrayList<String>) ClassName.getArrayList();

// Serializing to a JSON element node
JsonElement jsonElement = gson.toJsonTree(arrayList);
System.out.println(jsonElement.isJsonArray()); // true

// Or, directly to JSON string
String json = gson.toJson(arrayList);
System.out.println(json);

Here's how you would deserialize the same JSON string with its Generics intact:

Type type = new TypeToken<ArrayList<String>>(){}.getType();
ArrayList<String> arrayList = gson.fromJson(json, type);

The short answer for your question is that you have to suppress it in order for it to go away. The problem is not about what you put using the addAll method, it is because of the JSONArray has no way to guarantee type safety if type is not provided.

JSONArray inherits a non-parametrized ArrayList and the addAll method is defined as:

public boolean addAll(java.util.Collection<? extends E> es)

Without providing the type parameter, E falls back to Object, which makes the addAll method a method that can add a collection that contains ANYTHING on top of the existing collections. Therefore, you can do something like this:

List<Dog> dogs = new ArrayList<Dog>();
dogs.add(new Chiwawa());

List<Car> cars = new ArrayList<Car>();
cars.add(new BMW());

JSONArray jsonArray = new JSONArray();
jsonArray.addAll(dogs);
jsonArray.addAll(cars);

Dogs and Cars are added together to the same JSONArray (ArrayList) and treated as barely Object. If you do something like this, when you retrieve any of the object back, you have no way to tell whether it is a dog or a car. This is why the warning exists.

By using the generic type parameter (e.g. Dog), the addAll definition will be like:

 public boolean addAll(java.util.Collection<? extends Dog> es)

This make sure the parameter can only accept a collection of Dog and Dog's child class. Therefore when you retrieve it from the collection, it is safe to assign the retrieve object to Dog.

The warning is not because of you did something wrong. It is because of the JSONArray inherits a non-parametrized Collection. Feel free to suppress it.

From the documentation for JSONArray, it looks like it extends ArrayList the raw type (it doesn't have a generic type parameter). This is an example of dealing with non-generic legacy code, and the only way to proceed is to make very sure that the JSONArray is expecting to be receiving some Strings and then to suppress the warning.

(The real way to fix it would be to update JSONArray to use generics, but in the meantime, you have to use the raw type.)

Use Gson library to convert ArrayList to JsonArray.

Gson gson = new GsonBuilder().create();
JsonArray myCustomArray = gson.toJsonTree(myCustomList).getAsJsonArray();
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top