Skip to content

Commit afb6662

Browse files
authored
Merge pull request #37 from volkov/master
Null field from json deserialization fix
2 parents e70bb5a + ea368d8 commit afb6662

File tree

4 files changed

+19
-0
lines changed

4 files changed

+19
-0
lines changed

src/main/java/com/googlecode/protobuf/format/JsonJacksonFormat.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -539,6 +539,9 @@ private Object handleObject(JsonParser parser,
539539
}
540540

541541
JsonToken token = parser.getCurrentToken();
542+
if (JsonToken.VALUE_NULL == token) {
543+
return null;
544+
}
542545

543546
if (unknown) {
544547
ByteString data = ByteString.copyFrom(parser.getBinaryValue());

src/test/java/com/googlecode/protobuf/format/JsonFormatTest.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
import java.nio.charset.Charset;
1717
import java.util.Arrays;
1818

19+
import static org.hamcrest.CoreMatchers.equalTo;
1920
import static org.hamcrest.CoreMatchers.is;
2021
import static org.hamcrest.MatcherAssert.assertThat;
2122

@@ -75,4 +76,14 @@ public void testIssue23() throws Exception {
7576
JSON_FORMATTER.merge(message, extensionRegistry, issue23Builder);
7677
assertThat("No unknown field 4", issue23Builder.getUnknownFields().hasField(4));
7778
}
79+
80+
@Test
81+
public void testDeserializeNullFieldFromJson() throws Exception {
82+
UnittestProto.TestNullField.Builder builder = UnittestProto.TestNullField.newBuilder();
83+
new JsonJacksonFormat().merge(JsonFormatTest.class.getResourceAsStream("/json_format_null_field_data.txt"), builder);
84+
85+
final UnittestProto.TestNullField actual = builder.build();
86+
System.out.println(actual);
87+
assertThat(actual, equalTo(UnittestProto.TestNullField.newBuilder().build()));
88+
}
7889
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"field" : null}

src/test/resources/proto/unittest.proto

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -629,3 +629,7 @@ service TestService {
629629

630630
message BarRequest {}
631631
message BarResponse {}
632+
633+
message TestNullField {
634+
optional TestNullField field = 1;
635+
}

0 commit comments

Comments
 (0)