Click or drag to resize
Json.NET

JsonConstructorAttribute

 

This sample uses the JsonConstructorAttribute to specify that a constructor should be used to create a class during deserialization.

Sample
Types
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;
    }
}
Usage
string json = @"{
  ""UserName"": ""domain\\username"",
  ""Enabled"": true
}";

User user = JsonConvert.DeserializeObject<User>(json);

Console.WriteLine(user.UserName);
// domain\username