From 822b924d593f67d2536c7bdeaf55cedf614da244 Mon Sep 17 00:00:00 2001 From: Jon Skeet Date: Thu, 1 Mar 2018 13:20:40 +0000 Subject: Allow list values to be null when parsing --- csharp/src/Google.Protobuf.Test/JsonParserTest.cs | 16 ++++++++++++++++ csharp/src/Google.Protobuf/JsonParser.cs | 5 +++-- 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/csharp/src/Google.Protobuf.Test/JsonParserTest.cs b/csharp/src/Google.Protobuf.Test/JsonParserTest.cs index 329ae9be..a6cf04ab 100644 --- a/csharp/src/Google.Protobuf.Test/JsonParserTest.cs +++ b/csharp/src/Google.Protobuf.Test/JsonParserTest.cs @@ -695,6 +695,22 @@ namespace Google.Protobuf Assert.AreEqual(Value.ForList(Value.ForNumber(1), Value.ForString("x")), Value.Parser.ParseJson("[1, \"x\"]")); } + [Test] + public void Value_List_WithNullElement() + { + var expected = Value.ForList(Value.ForString("x"), Value.ForNull(), Value.ForString("y")); + var actual = Value.Parser.ParseJson("[\"x\", null, \"y\"]"); + Assert.AreEqual(expected, actual); + } + + [Test] + public void StructValue_NullElement() + { + var expected = Value.ForStruct(new Struct { Fields = { { "x", Value.ForNull() } } }); + var actual = Value.Parser.ParseJson("{ \"x\": null }"); + Assert.AreEqual(expected, actual); + } + [Test] public void ParseListValue() { diff --git a/csharp/src/Google.Protobuf/JsonParser.cs b/csharp/src/Google.Protobuf/JsonParser.cs index 3621b0c0..284bce93 100644 --- a/csharp/src/Google.Protobuf/JsonParser.cs +++ b/csharp/src/Google.Protobuf/JsonParser.cs @@ -264,11 +264,12 @@ namespace Google.Protobuf return; } tokenizer.PushBack(token); - if (token.Type == JsonToken.TokenType.Null) + object value = ParseSingleValue(field, tokenizer); + if (value == null) { throw new InvalidProtocolBufferException("Repeated field elements cannot be null"); } - list.Add(ParseSingleValue(field, tokenizer)); + list.Add(value); } } -- cgit v1.2.3 From 5140bae3834c40208e44b1eeb947485a832387a7 Mon Sep 17 00:00:00 2001 From: Jon Skeet Date: Thu, 1 Mar 2018 13:45:13 +0000 Subject: Add conformance test for null value in list JSON --- conformance/conformance_test.cc | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/conformance/conformance_test.cc b/conformance/conformance_test.cc index e4b046dc..d44c909a 100644 --- a/conformance/conformance_test.cc +++ b/conformance/conformance_test.cc @@ -2320,6 +2320,24 @@ bool ConformanceTestSuite::RunSuite(ConformanceTestRunner* runner, } } )"); + RunValidJsonTest( + "ValueAcceptListWithNull", REQUIRED, + R"({"optionalValue": ["x", null, "y"]})", + R"( + optional_value: { + list_value: { + values: { + string_value: "x" + } + values: { + null_value: NULL_VALUE + } + values: { + string_value: "y" + } + } + } + )"); RunValidJsonTest( "ValueAcceptObject", REQUIRED, R"({"optionalValue": {"value": 1}})", -- cgit v1.2.3