From 2928b3ef978a619a5433a6f3c9209e2968497904 Mon Sep 17 00:00:00 2001 From: Nykolas Lima Date: Mon, 23 May 2016 16:44:30 -0300 Subject: [PATCH] handle explicit null values in JSON --- .../java/com/google/gson/protobuf/ProtoTypeAdapter.java | 4 ++-- .../protobuf/functional/ProtosWithPrimitiveTypesTest.java | 7 +++++++ 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/proto/src/main/java/com/google/gson/protobuf/ProtoTypeAdapter.java b/proto/src/main/java/com/google/gson/protobuf/ProtoTypeAdapter.java index ff44779f..4fba080d 100644 --- a/proto/src/main/java/com/google/gson/protobuf/ProtoTypeAdapter.java +++ b/proto/src/main/java/com/google/gson/protobuf/ProtoTypeAdapter.java @@ -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) { diff --git a/proto/src/test/java/com/google/gson/protobuf/functional/ProtosWithPrimitiveTypesTest.java b/proto/src/test/java/com/google/gson/protobuf/functional/ProtosWithPrimitiveTypesTest.java index f90899e1..69b877e6 100644 --- a/proto/src/test/java/com/google/gson/protobuf/functional/ProtosWithPrimitiveTypesTest.java +++ b/proto/src/test/java/com/google/gson/protobuf/functional/ProtosWithPrimitiveTypesTest.java @@ -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()); + } + }