JsonConverterAttribute on a property |
This sample uses the JsonConverterAttribute to specify that a JsonConverter should be used when serializing and deserializing a property.
public enum UserStatus { NotConfirmed, Active, Deleted } public class User { public string UserName { get; set; } [JsonConverter(typeof(StringEnumConverter))] public UserStatus Status { get; set; } }
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" // }