C# – Deserialize JSON with a specific constructor

When your class has multiple constructors, you can use the JsonConstructor attribute to specify which constructor to use during deserialization. Here’s an example: Note: JsonConstructor for System.Text.Json was added in .NET 5. Now deserialize: This outputs: This shows that it used the Person(int luckyNumber) constructor. It passed in the LuckyNumber JSON property to the constructor, … Read more