Skip to content

Commit

Permalink
Merge pull request google#863 from nykolaslima/explicit-null-in-json
Browse files Browse the repository at this point in the history
handle explicit null values in JSON
  • Loading branch information
inder123 committed May 24, 2016
2 parents bb451ea + 2928b3e commit 2360cfa
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -262,8 +262,8 @@ public GeneratedMessage deserialize(JsonElement json, Type typeOfT,
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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,4 +66,11 @@ public void testDeserializeProto() {
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());
}

}

0 comments on commit 2360cfa

Please sign in to comment.