Menu

#44 Generics handling when deserializing to Java object

open
aalmiray
5
2007-09-09
2007-09-09
mansari
No

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.

Discussion

  • aalmiray

    aalmiray - 2007-09-09
    • assigned_to: nobody --> aalmiray
     
  • aalmiray

    aalmiray - 2007-09-24

    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.

     
  • aalmiray

    aalmiray - 2007-12-01

    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.

     

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.