未验证 提交 2948bb9a 编写于 作者: T Tamás Turnyánszki 提交者: GitHub

fixes #71295 (#71924)

上级 8cb4e934
......@@ -34,13 +34,13 @@ internal sealed class JsonConfigurationFileParser
{
throw new FormatException(SR.Format(SR.Error_InvalidTopLevelJSONElement, doc.RootElement.ValueKind));
}
VisitElement(doc.RootElement);
VisitObjectElement(doc.RootElement);
}
return _data;
}
private void VisitElement(JsonElement element)
private void VisitObjectElement(JsonElement element)
{
var isEmpty = true;
......@@ -52,6 +52,26 @@ private void VisitElement(JsonElement element)
ExitContext();
}
SetNullIfElementIsEmpty(isEmpty);
}
private void VisitArrayElement(JsonElement element)
{
int index = 0;
foreach (JsonElement arrayElement in element.EnumerateArray())
{
EnterContext(index.ToString());
VisitValue(arrayElement);
ExitContext();
index++;
}
SetNullIfElementIsEmpty(isEmpty: index == 0);
}
private void SetNullIfElementIsEmpty(bool isEmpty)
{
if (isEmpty && _paths.Count > 0)
{
_data[_paths.Peek()] = null;
......@@ -65,18 +85,11 @@ private void VisitValue(JsonElement value)
switch (value.ValueKind)
{
case JsonValueKind.Object:
VisitElement(value);
VisitObjectElement(value);
break;
case JsonValueKind.Array:
int index = 0;
foreach (JsonElement arrayElement in value.EnumerateArray())
{
EnterContext(index.ToString());
VisitValue(arrayElement);
ExitContext();
index++;
}
VisitArrayElement(value);
break;
case JsonValueKind.Number:
......
......@@ -254,5 +254,37 @@ public void TrailingCommas()
Assert.Equal("9.10.11.12", jsonConfigSource.Get("ip:1:0"));
Assert.Equal("13.14.15.16", jsonConfigSource.Get("ip:1:1"));
}
[Fact]
public void EmptyArrayNotIgnored()
{
var json = @"{
""ip"": {
""array"": [
],
""object"":{
}
}
}";
var jsonConfigSource = new JsonConfigurationSource { FileProvider = TestStreamHelpers.StringToFileProvider(json) };
var configurationBuilder = new ConfigurationBuilder();
configurationBuilder.Add(jsonConfigSource);
var config = configurationBuilder.Build();
var configurationSection = config.GetSection("ip");
var ipSectionChildren = configurationSection.GetChildren().ToArray();
Assert.Equal(1, config.GetChildren().Count());
Assert.Equal(2, ipSectionChildren.Count());
Assert.Equal("array", ipSectionChildren[0].Key);
Assert.Null(ipSectionChildren[0].Value);
Assert.Equal("object", ipSectionChildren[1].Key);
Assert.Null(ipSectionChildren[1].Value);
Assert.Equal(0, ipSectionChildren[0].GetChildren().Count());
Assert.Equal(0, ipSectionChildren[1].GetChildren().Count());
}
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册