null values

95 views
Skip to first unread message

archmisha

unread,
Oct 28, 2008, 5:08:06 PM10/28/08
to google-gson
I am having an issue with null values for example:
{id:''}
for class
public class A
{
private Long id;
}

I would like id to be null after from Json() but what i get is a
NumebrFormatException

I tried to set a custom Deserializer but it seems that gson dont use
it.

Here is my code:
GsonBuilder gb = new GsonBuilder();
// allow null values for Long (id)
gb.registerTypeAdapter(Long.class, new
JsonDeserializer<Long>()
{
public Long deserialize(JsonElement jsonElement, Type
type, JsonDeserializationContext jsonDeserializationContext) throws
JsonParseException
{
System.out.println("ttt");
String str =
jsonElement.getAsJsonPrimitive().getAsString();
if (str.length() == 0)
return null;
else
return Long.parseLong(str);
}
});
Gson g = gb.create();
A a = g.fromJson(json, A.class);

Any help would be appriciated

inde...@gmail.com

unread,
Oct 29, 2008, 9:32:00 PM10/29/08
to google-gson
I filed a bug on this. See http://groups.google.com/group/google-gson/browse_thread/thread/8418964267ace8f1

If this is valid issue, we will fix it in our next release.

Thanks for the bug report
Inder

inde...@gmail.com

unread,
Oct 29, 2008, 9:49:47 PM10/29/08
to google-gson
I wrote tests in r280 http://code.google.com/p/google-gson/source/detail?r=280
that serialize and deserialize null fields that are wrapped
primitive types (Long). The tests pass so I am closing this bug. Can
you post a specific test that fails?

Thanks
Inder

archmisha

unread,
Oct 30, 2008, 10:06:47 AM10/30/08
to google-gson
Thank you for your reply Inger.
I will post my test case as soon as i get home.

What i didnt understand though is: your wrappers that deserialize null
fields are outside of the package?
Like i tried to write custom type adapters?
Because for the code i wrote above the system out dont print out -
meaning the type adapter doesnt take effect...

Thank you

On Oct 30, 3:49 am, "inder...@gmail.com" <inder...@gmail.com> wrote:
> I wrote tests in r280http://code.google.com/p/google-gson/source/detail?r=280
> Inger

inde...@gmail.com

unread,
Oct 30, 2008, 10:55:04 AM10/30/08
to google-gson
I am not sure what you mean, but I did not write any custom type
adapter for my class. I dont need to, since Gson has inbuilt type
adapters for primitive types.

Jacob Tomaw

unread,
Oct 30, 2008, 10:58:34 AM10/30/08
to googl...@googlegroups.com
Inder,

I think he is asking why his custom adapter for Long was not called.  Setting an adapter for Long seems very odd and broad to me, but there might be a use case for it.  Can one not replace the built in adapters?

Jacob
--
Jacob Tomaw
tfl:The Flatiron Life (http://tomaw.com)
Follow me on Twitter! (http://twitter.com/JacobTomaw)

Inderjeet Singh

unread,
Oct 30, 2008, 11:15:37 AM10/30/08
to googl...@googlegroups.com
I see. The user supplied custom adapters should take precedence over
Gson's internal type adapters, so this is surprising.
I will write a test for this as well.
Thanks
Inder

inde...@gmail.com

unread,
Oct 30, 2008, 2:32:59 PM10/30/08
to google-gson
I wrote a test for overriding the custom type adapter for a Long in
r281 http://code.google.com/p/google-gson/source/detail?r=281
This bug does indeed exist, and the culprit is navigateClassFields
method of ObjectNavigator. That method does not invoke a custom type
adapter for primitive types or strings. I reopened the bug
http://code.google.com/p/google-gson/issues/detail?id=68

The fix is fairly trivial, but I am hesitant to introduce the fix. I
dont really see a compelling use-case for overriding type adapters for
primitive types.
archmisha: can you explain why this will be a good thing ?

Thanks
Inder

archmisha

unread,
Oct 31, 2008, 8:37:10 AM10/31/08
to google-gson
Suppose i want to override the boolean type - to add default value in
case empty string is introduced.
In my case the builtin adapter doesnt cope with empty string and i am
getting NumberFormatException: For input string: ""

test case:
public static void main(String[] args)
{
String json = "{\"rows\": [{\"id\":\"\",\"name\":\"New
Field1\",\"type\":\"String\",\"list_visible\":\"true\"},{\"id\":\"\",
\"name\":\"New Field2\",\"type\":\"Number\",\"list_visible\":\"\"}]}";
try
{
System.out.println(json);

GsonBuilder gb = new GsonBuilder();

// allow null values for long (id)
gb.registerTypeAdapter(Long.class, new
JsonDeserializer<Long>()
{
public Long deserialize(JsonElement jsonElement, Type
type, JsonDeserializationContext jsonDeserializationContext) throws
JsonParseException
{
System.out.println("ttt"); // Never printed !!!
String str =
jsonElement.getAsJsonPrimitive().getAsString();
if (str.length() == 0)
return null;
else
return Long.parseLong(str);
}
});

// allow default value of false
gb.registerTypeAdapter(Boolean.class, new
JsonDeserializer<Boolean>()
{
public Boolean deserialize(JsonElement jsonElement,
Type type, JsonDeserializationContext jsonDeserializationContext)
throws JsonParseException
{
String str =
jsonElement.getAsJsonPrimitive().getAsString();
if (str.length() == 0)
return false;
else
return Boolean.parseBoolean(str);
}
});

Gson g = gb.create();
FieldsGrid fieldsGrid = g.fromJson(json,
FieldsGrid.class);
}
catch (Exception e)
{
e.printStackTrace(); //To change body of catch statement
use File | Settings | File Templates.
}
}

private static class FieldsGridRowInstanceCreator implements
InstanceCreator<FieldsGridRowInstanceCreator>
{
public FieldsGridRowInstanceCreator createInstance(Type type)
{
return null; //To change body of implemented methods use
File | Settings | File Templates.
}
}

On Oct 30, 8:32 pm, "inder...@gmail.com" <inder...@gmail.com> wrote:
> I wrote a test for overriding the custom type adapter for a Long in
> r281http://code.google.com/p/google-gson/source/detail?r=281
> This bug does indeed exist, and the culprit is navigateClassFields
> method of ObjectNavigator. That method does not invoke a custom type
> adapter for primitive types or strings.  I reopened the bughttp://code.google.com/p/google-gson/issues/detail?id=68

joel....@gmail.com

unread,
Nov 29, 2008, 7:36:39 PM11/29/08
to google-gson
Sorry for jumping into this thread so late. I did a major overhaul on
how primitives are handled in version 1.2.3. The functionality that
you are looking for is now supported. This was added in r296. Here
is a test that has been added to show that overriding the default
primitive handling using custom type adapters is now supported:
http://code.google.com/p/google-gson/source/diff?spec=svn296&r=296&format=side&path=/trunk/gson/src/test/java/com/google/gson/functional/PrimitiveTest.java

Happy coding,
Joel
Reply all
Reply to author
Forward
0 new messages