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

5 Aufrufe
Direkt zur ersten ungelesenen Nachricht

ichess

ungelesen,
18.07.2010, 01:52:3518.07.10
an 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

ungelesen,
19.07.2010, 17:23:3119.07.10
an 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
Allen antworten
Antwort an Autor
Weiterleiten
0 neue Nachrichten