Merge pull request #4345 from jskeet/list-json-null

Allow null value in JSON representation of ListValue
This commit is contained in:
Feng Xiao 2018-03-20 15:19:42 -07:00 committed by GitHub
commit 88a4884b55
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 37 additions and 2 deletions

View File

@ -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( RunValidJsonTest(
"ValueAcceptObject", REQUIRED, "ValueAcceptObject", REQUIRED,
R"({"optionalValue": {"value": 1}})", R"({"optionalValue": {"value": 1}})",

View File

@ -695,6 +695,22 @@ namespace Google.Protobuf
Assert.AreEqual(Value.ForList(Value.ForNumber(1), Value.ForString("x")), Value.Parser.ParseJson("[1, \"x\"]")); 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] [Test]
public void ParseListValue() public void ParseListValue()
{ {

View File

@ -264,11 +264,12 @@ namespace Google.Protobuf
return; return;
} }
tokenizer.PushBack(token); 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"); throw new InvalidProtocolBufferException("Repeated field elements cannot be null");
} }
list.Add(ParseSingleValue(field, tokenizer)); list.Add(value);
} }
} }