Click or drag to resize
Json.NET

JsonConverterAttribute on a property

 

This sample uses the JsonConverterAttribute to specify that a JsonConverter should be used when serializing and deserializing a property.

Sample
Types
public enum UserStatus
{
    NotConfirmed,
    Active,
    Deleted
}

public class User
{
    public string UserName { get; set; }

    [JsonConverter(typeof(StringEnumConverter))]
    public UserStatus Status { get; set; }
}
Usage
User user = new User
{
    UserName = @"domain\username",
    Status = UserStatus.Deleted
};

string json = JsonConvert.SerializeObject(user, Formatting.Indented);

Console.WriteLine(json);
// {
//   "UserName": "domain\\username",
//   "Status": "Deleted"
// }