Asp.Net Core 2.0中的ConfigurationBuilder正在使用Json.Net/Newtonsoft.Json来实现.AddJsonFile()
功能。我在解析错误以及Microsoft.Extensions.Configuration.Json.JsonConfigurationFileParser
的源代码中看到过它。但是,它忽略了我用来装饰我的属性的属性(例如JsonProperty
)。
有没有办法让这种情况发生?
{
"ExampleObjectSection": {
"Prop": "Hello World!"
}
}
class Example
{
[JsonProperty(PropertyName = "Prop")]
public string Property { get; set; }
}
static void Main(string[] args)
{
IConfigurationRoot cfg = new ConfigurationBuilder()
.SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile("example.json", optional: false, reloadOnChange: true)
.Build();
Example e1 = cfg.GetSection("ExampleObjectSection").Get<Example>();
// Here: e1.Property = null
string str = File.ReadAllText(Path.Combine(Directory.GetCurrentDirectory(), "example.json"));
Example e2 = JsonConvert.DeserializeObject<Dictionary<string, Example>>(str)["ExampleObjectSection"];
// Here: e2.Property = "Hello World!"
}
直接通过JsonConvert时,对象不会反序列化。
再次:可以使用正常的Newtonsoft反序列化功能吗?或者有新方法吗? (DataMember / DataContract属性似乎也没有做任何事情,但我真的没有去那个兔子洞。)