JsonConstructorAttribute |
This sample uses the JsonConstructorAttribute to specify that a constructor should be used to create a class during deserialization.
public class User { public string UserName { get; private set; } public bool Enabled { get; private set; } public User() { } [JsonConstructor] public User(string userName, bool enabled) { UserName = userName; Enabled = enabled; } }
string json = @"{ ""UserName"": ""domain\\username"", ""Enabled"": true }"; User user = JsonConvert.DeserializeObject<User>(json); Console.WriteLine(user.UserName); // domain\username