Closed
Description
Description
System.Text.Json deserialization requires that a property type match the constructor type for immutable properties even though the constructor can convert the type.
This is a simple example of a class that will convert an incoming IEnumerable to a ReadOnlyObservableCollection for XAML binding.
[JsonConstructor]
public Logger(IEnumerable<LogEntry> entries)
{
this.Entries = new ReadOnlyObservableCollection<LogEntry>(this.entries);
}
public ReadOnlyObservableCollection<LogEntry> Entries { get; }
When desrializing from JSON, this fails.
Changing the property to be IEnumerable allows the deserialization to succeed, but that means I would need to add “another” property to this class for XAML binding to work. (Which is what this class is used for). The below just doesn’t seem right and was not something I had to do when using NewtonSoft
public Logger(IEnumerable<LogEntry> entries)
{
this.Entries = entries;
this.ObersvableEntries = new ReadOnlyObservableCollection<LogEntry>(this.entries);
}
public IEnumerable<LogEntry> Entries { get; }
[JsonIgnore]
public ReadOnlyObservableCollection<LogEntry> ObersvableEntries { get; }