JsonConverterAttribute on a class |
This sample uses the JsonConverterAttribute to specify that a JsonConverter should be used when serializing and deserializing a class.
public class UserConverter : JsonConverter { public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer) { User user = (User)value; writer.WriteValue(user.UserName); } public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer) { User user = new User(); user.UserName = (string)reader.Value; return user; } public override bool CanConvert(Type objectType) { return objectType == typeof(User); } } [JsonConverter(typeof(UserConverter))] public class User { public string UserName { get; set; } }
User user = new User { UserName = @"domain\username" }; string json = JsonConvert.SerializeObject(user, Formatting.Indented); Console.WriteLine(json); // "domain\\username"