The ConfigurationBuilder in Asp.Net Core 2.0 is using Json.Net/Newtonsoft.Json under the hood for the .AddJsonFile() functionality. I've seen it referenced in parsing errors as well as in the source for Microsoft.Extensions.Configuration.Json.JsonConfigurationFileParser. However, it's ignoring the Attributes I'm used to decorating my properties with (e.g. JsonProperty).
Is there a way to get that to happen?
Given this example.json file:
{
"ExampleObjectSection": {
"Prop": "Hello World!"
}
}
...this class
class Example
{
[JsonProperty(PropertyName = "Prop")]
public string Property { get; set; }
}
...and this code:
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!"
}
The result is that
The object isn't deserialized as it is when going directly through JsonConvert.
Again: Can normal Newtonsoft deserialization functionality be used? Or is there a new way to do this? (DataMember/DataContract attributes didn't seem to do anything either, but I really didn't go down that rabbit hole.)