fangyidong / json-simple

A simple Java toolkit for JSON. You can use json-simple to encode or decode JSON text.
Apache License 2.0
746 stars 338 forks source link

Fix the impossible downcast of toArray(). #125

Open Kui-Liu opened 7 years ago

Kui-Liu commented 7 years ago

This code is casting the result of this.items.toArray() on a list to the String type. This will usually fail by throwing a ClassCastException. The toArray() of almost all collections return an Object[]. They can't really do anything else, since the Collection object has no reference to the declared generic type of the collection.

The correct and efficient way to get a string array from a list is to use list.toArray(new String[list.size()]);. http://findbugs.sourceforge.net/bugDescriptions.html#BC_IMPOSSIBLE_DOWNCAST_OF_TOARRAY