When deserializing the JSON String containing an array of numbers to a Java object, it populates the corresponding generic collection with numbers which are transformed into a smaller type. For example, the ArrayList<Long> gets populated with Integers if the value can fit in the Integer type. This is apparant when looping through the List<Long> and a class cast exception is thrown.
Here is an example:
Bean:
private long id;
private List<Long> sessionIds = new ArrayList<Long>();
Sample JSON:
String json = "{\"id\":12,\"sessionIds\":[1955]}";
//ClassCastException when looping: java.lang.Integer found
for (Long sessionId: bean.getSessionIds()){
System.out.println ("sessionId: "+ sessionId);
}
The expected behavior should be that the generic type gets respected and data gets transformed based on it.
Logged In: YES
user_id=1192026
Originator: NO
I'm still deciding how to solve this issue. The need to transform data with generics may be 'generic enough' (no pun intended) to be included in ezmorph instead of json-lib, but that would create a fork on ezmorph (to handle jdk 1.5) and a bigger problem on handling json-lib's dependencies, which ultimately may update pom.xml with profiles. I'll continue with it once I find a way to organize the deps and source dirs.
Logged In: YES
user_id=1192026
Originator: NO
There's been some advances in this FR. It is now possible to use collections and generics with the current HEAD.