Merge pull request #863 from nykolaslima/explicit-null-in-json

handle explicit null values in JSON
This commit is contained in:
inder123 2016-05-23 23:17:54 -07:00
commit 2360cfa05c
2 changed files with 9 additions and 2 deletions

View File

@ -262,8 +262,8 @@ public class ProtoTypeAdapter
String jsonFieldName =
getCustSerializedName(fieldDescriptor.getOptions(), fieldDescriptor.getName());
if (jsonObject.has(jsonFieldName)) {
JsonElement jsonElement = jsonObject.get(jsonFieldName);
JsonElement jsonElement = jsonObject.get(jsonFieldName);
if (jsonElement != null && !jsonElement.isJsonNull()) {
// Do not reuse jsonFieldName here, it might have a custom value
Object fieldValue;
if (fieldDescriptor.getType() == ENUM_TYPE) {

View File

@ -66,4 +66,11 @@ public class ProtosWithPrimitiveTypesTest extends TestCase {
assertEquals("foo", proto.getMsg());
assertEquals(3, proto.getCount());
}
public void testDeserializeWithExplicitNullValue() {
SimpleProto proto = gson.fromJson("{msg:'foo',count:null}", SimpleProto.class);
assertEquals("foo", proto.getMsg());
assertEquals(0, proto.getCount());
}
}