Integer value will always deserialize to be java.lang.Double
4,006 views
Skip to first unread message
Luis Ashurei
unread,
Dec 27, 2011, 1:49:55 AM12/27/11
Reply to author
Sign in to reply to author
Forward
Sign in to forward
Delete
You do not have permission to delete messages in this group
Copy link
Report message
Show original message
Either email addresses are anonymous for this group or you need the view member email addresses permission to view the original message
to google-gson
The client code looks like:
public class MyTest {
class Example {
Object p;
}
public static void main(final String args[]) {
Gson gson = new Gson();
Example a = gson.fromJson("{\"p\":1}", Example.class);
System.out.println(a.p.getClass().getName()); //
java.lang.Double
}
}
The gson source code looks like:
com.google.gson.internal.bind.ObjectTypeAdapter.read(JsonReader in)
case NUMBER:
return in.nextDouble();
It's not so good, is it?
Should JsonToken add enum like INTEGER, FLOAT etc. ?
Jesse Wilson
unread,
Dec 27, 2011, 11:40:08 AM12/27/11
Reply to author
Sign in to reply to author
Forward
Sign in to forward
Delete
You do not have permission to delete messages in this group
Copy link
Report message
Show original message
Either email addresses are anonymous for this group or you need the view member email addresses permission to view the original message
to googl...@googlegroups.com
If an Integer is what you want, declare the type of Example's 'p' field to be Integer or int.
Luis Ashurei
unread,
Dec 27, 2011, 8:00:13 PM12/27/11
Reply to author
Sign in to reply to author
Forward
Sign in to forward
Delete
You do not have permission to delete messages in this group
Copy link
Report message
Show original message
Either email addresses are anonymous for this group or you need the view member email addresses permission to view the original message
to google-gson
I know i can explicit declare the type to Integer to get
java.lang.Integer.
But dynamic declare is useful in some cases, such as reflection,
dynamic object etc.
Inderjeet Singh
unread,
Dec 31, 2011, 3:07:37 AM12/31/11
Reply to author
Sign in to reply to author
Forward
Sign in to forward
Delete
You do not have permission to delete messages in this group
Copy link
Report message
Show original message
Either email addresses are anonymous for this group or you need the view member email addresses permission to view the original message
to googl...@googlegroups.com
For performance reason, Gson no longer tries to figure out the minimum type that can retain the precision. (Earlier versions of Gson used to do so). It just uses double. I would argue that you should rethink the design of your model class Example, or register a type-adapter for it.
HTH
Inder
h.schenk
unread,
Jan 27, 2012, 11:47:52 AM1/27/12
Reply to author
Sign in to reply to author
Forward
Sign in to forward
Delete
You do not have permission to delete messages in this group
Copy link
Report message
Show original message
Either email addresses are anonymous for this group or you need the view member email addresses permission to view the original message
to google-gson
Can you say me, which is the latest version which tries to figure out
the correct type?
Jesse Wilson
unread,
Feb 6, 2012, 10:23:04 PM2/6/12
Reply to author
Sign in to reply to author
Forward
Sign in to forward
Delete
You do not have permission to delete messages in this group
Copy link
Report message
Show original message
Either email addresses are anonymous for this group or you need the view member email addresses permission to view the original message