Is there a way to use System.Text.Json.JsonSerializer.Deserialize with object that contains private setters properties, and fill those properties? (like Newtonsoft.Json does)
As per the official docs (C# 9), you got 2 options:
Use init instead of set on the property. E.g. public string Summary { get; init; }
Add JsonInclude attribute on the properties with private setters. E.g.
[JsonInclude]
public string Summary { get; private set; }
A bonus option (starting from .NET 5) would be handling private fields by either adding the same JsonInclude attribute (docs) or setting JsonSerializerOptions.IncludeFields option (example). Would it be ideologically correct is a different question...
Either way, JsonSerializer.DeserializeAsync will do it for you.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With