fromJson only accept LinkedList for array of custom objects?

614 views
Skip to first unread message

jul

unread,
Jul 7, 2010, 10:12:49 AM7/7/10
to google-gson
Hi,

I'd like to deserialize a json object in an ArrayList of Restaurant
objects, as follows:

***
ArrayList<Restaurant> restaurantList;
Type listType = new TypeToken<ArrayList<Restaurant>>()
{}.getType();
restaurantList = gson.fromJson( r, listType );
***

but I get a "java.lang.ClassCastException: java.util.LinkedList"
error.

This works when I define a temp LinkedList and copy it to my
ArrayList, as follows:

***
ArrayList<Restaurant> restaurantList;
LinkedList<Restaurant> tmp;

Type listType = new TypeToken<List<Restaurant>>()
{}.getType();
tmp = gson.fromJson( r, listType );

restaurantList = new ArrayList<Restaurant>(tmp);
***

Is that normal? Is fromJson supposed to return only LinkedList?

Thanks

Jul

inde...@gmail.com

unread,
Jul 14, 2010, 2:18:02 PM7/14/10
to google-gson
Could you define restaurantList as List<Restaurant> instead of
ArrayList? Are there any ArrayList specific behavior that you need?

In Gson, we had to pick a List implementation, and we chose
LinkedList.

Inder
Reply all
Reply to author
Forward
0 new messages