using fromJson(String, Class) when class has primitive classes

5 views
Skip to first unread message

ichess

unread,
Jul 18, 2010, 1:52:35 AM7/18/10
to google-gson
Hello All, I hope you can help me

I'm having a class with an "Integer" field :

public class foo
{
Integer bar;
}

and I'm sending a JSON string over an HTML request that converst to
this class.

now, If "bar" is declared as int, everything works as expected.
however, if "bar" is declared as Integer, this does not work.
I want to declare "bar" as integer since in the other direction, when
using toJson(), I want to save up the message
size and don't send "bar" in case it is null. if "bar" is int, it is
always sent...

Thank you for your help :)
Ran B
IChess - chess Web site
http://www.ichess.co.il

inde...@gmail.com

unread,
Jul 19, 2010, 5:23:31 PM7/19/10
to google-gson
The following test passes in Gson.

public void testDeserializePrimitiveWrapperAsObjectField() {
String json = "{i:10}";
ClassWithIntegerField target = gson.fromJson(json,
ClassWithIntegerField.class);
assertEquals(10, target.i.intValue());
}

private static class ClassWithIntegerField {
Integer i;
}

Can you check your code again?
Thanks
Inder
Reply all
Reply to author
Forward
0 new messages