Desearializing a JSON which comes with an empty field

2,043 views
Skip to first unread message

Francisco Javier Martínez

unread,
Mar 30, 2011, 5:46:15 AM3/30/11
to google-gson
Hi,

I'm trying to deserialize a JSON object like this:

public class Example {

private String a;
private int b;
private SubData c;

//more fields, constructor, getters, etc
}

public class SubData{

private String d;
private int e;

//more fields, constructor, getters, etc

}

It works well most cases, but sometimes SubData doesn't brought any
data, I get JSON like this:

{"a":"blabla", b:843849, c:""}

then, GSON fails and I get an exception. Would be possible to fill
with a null the "c" field in the deserialized object?

I'm consuming a webservice that I haven't made, so, I have no option
about changing the JSON that has to be deserialized.

I would appreciate your help

Thank you very much

Joel

unread,
Apr 13, 2011, 4:58:47 AM4/13/11
to google-gson
To me, it appears that the incoming JSON does not match what you
expect. In some cases it is a "string" while others it is a Json
Object. Ideally, the incoming JSON would be consistent so it would be
better if "c":null was passed in instead.

If you are unable to change the incoming JSON then unfortunately you
will have to write a custom deserializer for it.
public class SubDataJsonSerializer implements
JsonDeserializer<SubData> {
public T deserialize(JsonElement json, Type typeOfT,
JsonDeserializationContext context)
throws JsonParseException {
if (json.isJsonPrimitive()) {
// You can add additional check in here to make sure it's an
empty string
return null;
} else {
JsonObject jsonObj = json.getAsJsonObject();
return new SubData(jsonObj.getProperty("d"),
jsonObj.getProperty("e"));
}
}
}

On Mar 30, 2:46 am, Francisco Javier Martínez

Lin Dapeng

unread,
Apr 13, 2011, 10:46:49 PM4/13/11
to googl...@googlegroups.com
Is it because gson default excludes anonymous and local classes? 


--
You received this message because you are subscribed to the Google Groups "google-gson" group.
To post to this group, send email to googl...@googlegroups.com.
To unsubscribe from this group, send email to google-gson...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/google-gson?hl=en.


Joel

unread,
Apr 14, 2011, 11:57:53 AM4/14/11
to google-gson
Gson does exclude local and anonymous classes by default; however this
is not the problem for Francisco's case. What Gson is trying to do is
serialize a String (which maps to a Json Primitive) into a Object
(which maps to a Json Object). The two types are incompatible so Gson
throws an exception since the incoming JSON did not match the spec
(according to the Java Objects defined above).

If you are serializing an object that has an inner class field than
the field will simple be skipped in the JSON output (i.e. it will not
output an empty field). For incoming JSON, if the field is set then
Gson will simply ignore that JSON field and its value.

Hope this helps,
Joel

Lin Dapeng

unread,
Apr 14, 2011, 9:30:00 PM4/14/11
to googl...@googlegroups.com
Hi Joel, is there any reason behind local and anonymous classes are excluded by default? always wonder. Thanks.
--

Regards,
Dapeng

Joel

unread,
Apr 15, 2011, 2:29:21 AM4/15/11
to google-gson
I do not recall the reason for anonymous classes, but an inner class
has an implicit reference on the outer class so it causes a circular
reference if you are using the default Gson serialization. This is
probably something that can be fixed and a bug has been filed for this
feature, bug #196 (http://code.google.com/p/google-gson/issues/detail?
id=196)
Reply all
Reply to author
Forward
0 new messages