• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Generics + Gson != Fun

 
author
Posts: 4335
39
jQuery Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm trying to create a utility method that calls Gson to deserialize a List of generic objects... Let's say they are "Widget" in a concrete example. Aka, the JSON is of the form: [{...},{...}] where {...} represents a serialized Widget object.

Below are two methods that both compile just fine. I've pulled out of a lot of extra businessy stuff, so apologize in advance if I pulled out so much it doesn't work!



The first method works as expected, and I get back a List of Widgets. Great if I am only working with Widgets, but as it turns out I need to support hundreds of different objects. Hence generics.

The second method appears to compile/run without issue, in fact I can even call toString() on the List and it prints the contents. The problem is that it is actually a List of LinkedTreeMap, not a List of Widgets. As soon as you call a Widget method like... result.get(0).getWidgetName(), it throws a ClassCastException.

While I understand this problem is related to type erasure, I'm wondering if anyone has a solution. I came across numerous posts using 'the Google' where people tried to get around this but had difficulty. I found one example where you can pass Widget[].class instead of Widget.class and then load the elements into an Array, but it feels hacky and I don't want the client to pass Widget[].class, I want them to pass Widget.class!

Any ideas?

 
Sheriff
Posts: 11604
178
Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Scott Selikoff wrote:Any ideas?


I have never used this library, but after some googling I found this SO question which suggests to create a parametrized wrapper class which you can then use in the fromJson call.

So this wrapper class would look likeAnd then you should be able to use this getUsingGeneric methodI don't have tested it myself, so I don't know if it works. But if it does, it's a very nice and clean solution in my opinion.

Hope it helps!
Kind regards,
Roel
 
Scott Selikoff
author
Posts: 4335
39
jQuery Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Roel, I'll give it a shot! I tried a few different options although without much luck. Creating a class to perform a deserialization seems like overkill, but it may be the only way!
 
Roel De Nijs
Sheriff
Posts: 11604
178
Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Scott Selikoff wrote:Thanks Roel, I'll give it a shot! I tried a few different options although without much luck. Creating a class to perform a deserialization seems like overkill, but it may be the only way!


Fingers crossed! I have just created a test case myself and it seems to work for me And I would be happy to create this (fairly simple) class to get such a clean and nice solution to deserialize a generic list.

Here is my test caseOutput:
100 value1 true
200 value2 false
300 value3 true
 
Scott Selikoff
author
Posts: 4335
39
jQuery Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Roel! I tested it out and it does indeed work. I was hoping for a solution that didn't require a class but its the only way to get over the loss of information during type erasure, so glad it works. I set it up as an inner class so the client isn't even aware it's being used. Thanks again!
 
Roel De Nijs
Sheriff
Posts: 11604
178
Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Scott Selikoff wrote:I tested it out and it does indeed work. I was hoping for a solution that didn't require a class but its the only way to get over the loss of information during type erasure, so glad it works. I set it up as an inner class so the client isn't even aware it's being used.


Glad to hear it's working flawlessly! And I think an inner class is indeed the best option for this kind of "helper" class

As a side note: I don't agree with the title of this topic. I had fun writing this class (and its test case)
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic