Reputation: 309
I use Gson in my android project. And I try to parse object like
{ "field1":"value1", "field2":value2" }
It's invalid json. But I'm not getting an exception. I'm getting valid object with field2 = null
Upvotes: 0
Views: 662
Reputation: 6934
I cannot reproduce that field2
is null
, for me it is value2"
; with the following Gson usage:
new Gson().fromJson("{ \"field1\":\"value1\", \"field2\":value2\" }", CustomClass.class);
Unfortunately due to legacy reasons Gson is 'lenient' by default and therefore accepts JSON which would considered malformed by a strict parser. This is also discussed a bit in this GitHub issue.
As workaround you can obtain the respective TypeAdapter
with Gson.getAdapter
and then call one of its methods to perform the deserialization. See also this related answer about JSON validation with Gson.
Upvotes: 1