yunussasmaz / google-gson

Automatically exported from code.google.com/p/google-gson
0 stars 0 forks source link

Could not deserialize JsonPrimitive to int #235

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
Gson gson = new Gson();

gson.fromJson("1", int.class) works ok,
gson.fromJson(new JsonPrimitive(1), int.class) throws ClassCastException,

The latter method calls cast method of Class, while the former one does not.
int.class.cast(new Integer(1)) does not work on my Java6SE JVM.

I'm using Gson 1.5 on Java6SE

Original issue reported on code.google.com by adi...@gmail.com on 13 Sep 2010 at 6:53

GoogleCodeExporter commented 9 years ago
It's definitely an inconsistency in GSON.

Gson.fromJson(JsonElement, Class) calls Class.cast() but fromJson(String, 
Class) does not.

It suggests there are two bugs:
1. fromJson(String, Class) should call Class.cast() before returning its result
2. In both methods we should canonicalize int.class to Integer.class, and 
similarly for the other primitive types.

Original comment by limpbizkit on 31 Oct 2010 at 2:52

GoogleCodeExporter commented 9 years ago
Fixed in r623.

Original comment by limpbizkit on 1 Nov 2010 at 10:38