Closed GoogleCodeExporter closed 9 years ago
by the way, I tried this under both 1.3/1.4 beta, both threw the same exception
Original comment by hnjch...@gmail.com
on 26 Oct 2009 at 8:51
I am having the same issue as well.
I am using the 1.4 version on Ubuntu.
Is there any solution for this?
Original comment by ekaqu1...@gmail.com
on 14 Mar 2010 at 6:03
Pls refer to the gson's guide for generic class:
Serializing and Deserializing Generic Types
When you call toJson(obj), Gson calls obj.getClass() to get information on the
fields
to serialize. Similarly, you can typically pass MyClass.class object in the
fromJson(json, MyClass.class) method. This works fine if the object is a
non-generic
type. However, if the object is of a generic type, then the Generic type
information
is lost because of Java Type Erasure. Here is an example illustrating the point:
List<String> myStrings = new List<String>();
gson.toJson(myStrings); // Will cause a runtime exception
gson.fromJson(json, myStrings.getClass());
The above call results in a runtime exception because Gson invokes
myStrings.getClass() to get its class information, but this method returns a raw
class, List.class. This means that Gson has no way of knowing that this is a
list of
Strings, and not plain objects.
You can solve this problem by specifying the correct parameterized type for your
generic type. You can do this by using the TypeToken class.
Type listType = new TypeToken<List<String>>() {}.getType();
gson.toJson(myStrings, listType);
gson.fromJson(json, listType);
The idiom used to get listType actually defines an anonymous local inner class
containing a method getType() that returns the fully parameterized type.
Original comment by pvthang...@gmail.com
on 22 Apr 2010 at 3:38
The core problem is that GSON isn't tracking that the type of 'IDT' will be
'Integer' for Employee instances.
Original comment by limpbizkit
on 1 Nov 2010 at 11:10
I've attached a patch that uses type mojo stolen from Guice. In general I think
it's a big leap forward because it'll give GSON better support for type
parameters in fields and type arguments of fields. In particular, this should
work:
class Foo<K, V> {
K k1;
Map<K, V> map;
List<V> list;
}
More interestingly, these should also work:
class WackyHashMap<V, K> extends HashMap<K, V> {...}
class SecondArgCollection<A, B> implements Collection<B> {...}
I'd prefer not to commit this change in time for GSON 1.6 because this change
is destabilizing. It's a large change and we can build upon it by using
TypeToken internally everywhere instead of TypeInfo.
Original comment by limpbizkit
on 2 Nov 2010 at 8:08
Attachments:
Original comment by inder123
on 3 Nov 2010 at 1:47
The attached patch resolves merge conflicts.
Original comment by limpbizkit
on 3 Nov 2010 at 3:12
Attachments:
Issue 241 has been merged into this issue.
Original comment by limpbizkit
on 3 Nov 2010 at 3:16
Issue 40 has been merged into this issue.
Original comment by limpbizkit
on 3 Nov 2010 at 3:20
This issue was closed by revision r707.
Original comment by limpbizkit
on 19 Jan 2011 at 10:24
Original issue reported on code.google.com by
hnjch...@gmail.com
on 26 Oct 2009 at 8:49