ConfigurationBuilder binding fails when using JsonPropertyName to decorate a property
- Dominant language
- C#
- Stars
- 38.4k
- Forks
- 10.9k
- Avg merge
- 2d 6h
- Merged PRs (30d)
- 290
Description
_This issue has been moved from [a ticket on Developer Community](https://developercommunity.visualstudio.com/t/ConfigurationBuilder-binding-fails-when-/10358751)._
---
Using an example from [Learn Configuration](https://learn.microsoft.com/en-us/dotnet/core/extensions/configuration) attached as
[config.without.hosting.zip](https://aka.ms/dc/file?name=B40fe44374f6d4224a3d4ae63f88e4c4a638192588444378668_config.without.hosting.zip&tid=40fe44374f6d4224a3d4ae63f88e4c4a638192588444378668)
The configuration binder correctly binds `Settings` value from
```
{
"Settings": {
"KeyOne": 1,
"KeyTwo": true,
"KeyThree": {
"Message": "Oh, that's nice...",
"SupportedVersions": {
"v1": "1.0.0",
"v3": "3.0.7"
}
},
"IPAddressRange": [
"46.36.198.121",
"46.36.198.122",
"46.36.198.123",
"46.36.198.124",
"46.36.198.125"
]
}
}
```
to
```
public sealed class Settings
{
public required int KeyOne { get; set; }
public required bool KeyTwo { get; set; }
public required NestedSettings KeyThree { get; set; } = null!;
}
```
yielding a console output:
```
KeyOne = 1
KeyTwo = True
KeyThree:Message = Oh, that's nice...
```
Renaming the `KeyOne` property to `Key1" and adding `[JsonPropertyName( "KeyOne" )]` as such:
```
public sealed class Settings
{
[JsonPropertyName( "KeyOne" )]
public required int Key1 { get; set; }
public required bool KeyTwo { get; set; }
public required NestedSettings KeyThree { get; set; } = null!;
}
```
the output is now:
```
KeyOne = 0
KeyTwo = True
KeyThree:Message = Oh, that's nice...
```
The binder fails to 'see' the Json value for `Key1`.
Being able to name the properties is critically important when dealing with complex Json files. For example, a typical Serilog settings includes properties such as:
```
"Serilog": {
"LevelSwitches": {
"$consoleSwitch": "Verbose",
```
which would correspond to a class property such as:
```
[JsonPropertyName( "$consoleSwitch" )]
LogLevel ConsoleSwitch { get; set; }
```
Consequently, I am unable to bind a class to some frequently used Json settings.
While I can use the `System.Text.JsonDocument` to do the work, the `JsonDocument ` does not do binding, which means that a new instance of the class needs to be created on each read, which is a major draw back.
I am using .NET Core 7 with the `[latest]` setting in my project and visual studio 17.5.5.
Thank you.
As a side, I do appreciate how quickly I get responses from Microsoft, so thank you for keeping visual studio and .NET up-to-date!
---
### Original Comments
#### Feedback Bot on 5/9/2023, 08:47 PM:
(private comment, text removed)
#### David Hary on 5/10/2023, 11:31 AM:
(private comment, text removed)
#### Feedback Bot on 5/11/2023, 02:33 AM:
(private comment, text removed)
#### David Hary on 5/11/2023, 10:57 AM:
(private comment, text removed)
---
### Original Solutions
(no solutions)
Contributor guide
Assessment
This issue has not been assessed yet.