gpt4 book ai didi

c# - 无法从 Json 序列化 IConfigurationSection

转载 作者:太空狗 更新时间:2023-10-29 23:36:01 25 4
gpt4 key购买 nike

我有以下基于 Json 的配置文件:

{
"PostProcessing": {
"ValidationHandlerConfiguration": {
"MinimumTrustLevel": 80,
"MinimumMatchingTrustLevel": 75
},
"MatchingCharacterRemovals": [
"-",
"''",
":"
]
},
"Processing": {
"OrderSelection": {
"SelectionDaysInterval": 30,
"SelectionDaysMaximum": 365
}
}
}

作为序列化框架,我使用 Newtonsoft。为了将此配置序列化为对象,我实现了以下类:

[JsonObject(MemberSerialization.OptIn)]
public class RecognitionConfiguration {
[JsonProperty(PropertyName = "PostProcessing", Required = Required.Always)]
public PostRecognitionConfiguration PostRecognitionConfiguration { get; set; }

[JsonProperty(PropertyName = "Processing", Required = Required.Always)]
public ProcessRecognitionConfiguration ProcessRecognitionConfiguration { get; set; }
}

[JsonObject(MemberSerialization.OptIn)]
public class PostRecognitionConfiguration {
[JsonProperty(Required = Required.Always)]
public ValidationHandlerConfiguration ValidationHandlerConfiguration { get; set; }

[JsonProperty] public List<string> MatchingCharacterRemovals { get; set; }
}

[JsonObject(MemberSerialization.OptIn)]
public class ProcessRecognitionConfiguration {
[JsonProperty(PropertyName = "OrderSelection", Required = Required.Always)]
public OrderSelectionConfiguration OrderSelectionConfiguration { get; set; }
}

在一个类中,我尝试使用 IConfigurationSection.Get() 将特定配置部分序列化到这些类结构中。

var serializedConfiguration = this.ConfigurationSection.Get<RecognitionConfiguration>();

但是当我调试代码时,我总是得到一个“空”变量 serializedConfiguration,它不是 null,但所有属性都是 null。

enter image description here

如果我用

this.ConfigurationSection.GetSection("Processing").Get<ProcessRecognitionConfiguration>()

或者更改 json 文件中属性的命名,使其与类中的属性名称完全匹配,如下所示:

{   
"ProcessRecognitionConfiguration": {
"OrderSelectionConfiguration": {
"SelectionDaysInterval": 30,
"SelectionDaysMaximum": 365
}
}
}

它工作正常。您知道吗,为什么在 JsonProperty 上设置 PropertyName 似乎没有任何效果?

最佳答案

这是设计使然。通过配置绑定(bind)到 POCO 是按照惯例完成的。不像模型绑定(bind)到 Controller 操作参数。

它将 POCO 上的属性名称与提供的 JSON 中的键进行匹配。

引用 Configuration in ASP.NET Core

因此,要么更改设置以匹配原始问题中显示的类,要么更改类以匹配基于 Json 的配置文件中的设置键。

[JsonObject(MemberSerialization.OptIn)]
public class RecognitionConfiguration {
[JsonProperty(PropertyName = "PostProcessing", Required = Required.Always)]
public PostRecognitionConfiguration PostProcessing{ get; set; }

[JsonProperty(PropertyName = "Processing", Required = Required.Always)]
public ProcessRecognitionConfiguration Processing{ get; set; }
}

[JsonObject(MemberSerialization.OptIn)]
public class PostRecognitionConfiguration {
[JsonProperty(Required = Required.Always)]
public ValidationHandlerConfiguration ValidationHandlerConfiguration { get; set; }

[JsonProperty]
public List<string> MatchingCharacterRemovals { get; set; }
}

[JsonObject(MemberSerialization.OptIn)]
public class ProcessRecognitionConfiguration {
[JsonProperty(PropertyName = "OrderSelection", Required = Required.Always)]
public OrderSelectionConfiguration OrderSelection { get; set; }
}

public partial class ValidationHandlerConfiguration {
[JsonProperty("MinimumTrustLevel")]
public long MinimumTrustLevel { get; set; }

[JsonProperty("MinimumMatchingTrustLevel")]
public long MinimumMatchingTrustLevel { get; set; }
}


public partial class OrderSelectionConfiguration {
[JsonProperty("SelectionDaysInterval")]
public long SelectionDaysInterval { get; set; }

[JsonProperty("SelectionDaysMaximum")]
public long SelectionDaysMaximum { get; set; }
}

关于c# - 无法从 Json 序列化 IConfigurationSection,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54945930/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com