public class IpAddressConverter : TypeConverter
{
public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
{
return (sourceType == typeof(string));
}
public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)
{
return IPAddress.Parse(value.ToString());
}
}
{
"IPAddress": {
"Value": "192.168.0.1",
"Converter": "YourNamespace.IpAddressConverter"
}
}
3.在相应的类中,使用JsonPropertyAttribute来指定属性名和JsonConverterAttribute来指定自定义的IpAddress转换器。
public class MyClass
{
[JsonProperty("IPAddress")]
[JsonConverter(typeof(IpAddressConverter))]
public IPAddress MyIPAddress { get; set; }
}