diff options
author | Feng Xiao <xfxyjwf@gmail.com> | 2018-03-20 15:19:42 -0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2018-03-20 15:19:42 -0700 |
commit | 88a4884b55cf1a2a6576af0d558d2abe03ec6ba8 (patch) | |
tree | 4cc287cf181bf854e5dc77a8dd0d14077d62c18c | |
parent | d6a17aadeb6fc306d3d8c3b5c07edc41f6bc551e (diff) | |
parent | 5140bae3834c40208e44b1eeb947485a832387a7 (diff) | |
download | protobuf-88a4884b55cf1a2a6576af0d558d2abe03ec6ba8.tar.gz protobuf-88a4884b55cf1a2a6576af0d558d2abe03ec6ba8.tar.bz2 protobuf-88a4884b55cf1a2a6576af0d558d2abe03ec6ba8.zip |
Merge pull request #4345 from jskeet/list-json-null
Allow null value in JSON representation of ListValue
-rw-r--r-- | conformance/conformance_test.cc | 18 | ||||
-rw-r--r-- | csharp/src/Google.Protobuf.Test/JsonParserTest.cs | 16 | ||||
-rw-r--r-- | csharp/src/Google.Protobuf/JsonParser.cs | 5 |
3 files changed, 37 insertions, 2 deletions
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 @@ -2321,6 +2321,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}})", R"( 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 @@ -696,6 +696,22 @@ namespace Google.Protobuf } [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() { Assert.AreEqual(new ListValue { Values = { Value.ForNumber(1), Value.ForString("x") } }, ListValue.Parser.ParseJson("[1, \"x\"]")); 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); } } |