IPAddress
如您所见,该类对序列化不是很友好。SocketException
如果您尝试访问IPv4 地址的字段,它不仅会抛出 a ,而且如果您尝试直接访问 IPv6 地址 ScopeID
的字段,它也会抛出。Address
要绕过异常,您将需要一个自定义JsonConverter
. 转换器允许您准确地告诉 Json.Net 您希望它如何序列化和/或反序列化特定类型的对象。对于一个IPAddress
,似乎最简单的方法来获得满足每个人的数据只是简单地将其转换为它的字符串表示形式并返回。我们可以在转换器中做到这一点。这是我的写法:
class IPAddressConverter : JsonConverter
{
public override bool CanConvert(Type objectType)
{
return (objectType == typeof(IPAddress));
}
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
{
writer.WriteValue(value.ToString());
}
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
{
return IPAddress.Parse((string)reader.Value);
}
}
很简单,随着这些事情的发展。但是,这并不是故事的结局。如果您需要与您的 . 进行往返IPEndPoint
,那么您也需要一个转换器。为什么?因为IPEndPoint
不包含默认构造函数,所以 Json.Net 将不知道如何实例化它。幸运的是,这个转换器也不难写:
class IPEndPointConverter : JsonConverter
{
public override bool CanConvert(Type objectType)
{
return (objectType == typeof(IPEndPoint));
}
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
{
IPEndPoint ep = (IPEndPoint)value;
JObject jo = new JObject();
jo.Add("Address", JToken.FromObject(ep.Address, serializer));
jo.Add("Port", ep.Port);
jo.WriteTo(writer);
}
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
{
JObject jo = JObject.Load(reader);
IPAddress address = jo["Address"].ToObject<IPAddress>(serializer);
int port = (int)jo["Port"];
return new IPEndPoint(address, port);
}
}
那么,既然我们有了转换器,我们如何使用它们呢?这是一个演示的简单示例程序。它首先创建几个端点,使用自定义转换器将它们序列化为 JSON,然后立即使用相同的转换器再次将 JSON 反序列化回端点。
public class Program
{
static void Main(string[] args)
{
var endpoints = new IPEndPoint[]
{
new IPEndPoint(IPAddress.Parse("8.8.4.4"), 53),
new IPEndPoint(IPAddress.Parse("2001:db8::ff00:42:8329"), 81)
};
var settings = new JsonSerializerSettings();
settings.Converters.Add(new IPAddressConverter());
settings.Converters.Add(new IPEndPointConverter());
settings.Formatting = Formatting.Indented;
string json = JsonConvert.SerializeObject(endpoints, settings);
Console.WriteLine(json);
var endpoints2 = JsonConvert.DeserializeObject<IPEndPoint[]>(json, settings);
foreach (IPEndPoint ep in endpoints2)
{
Console.WriteLine();
Console.WriteLine("AddressFamily: " + ep.AddressFamily);
Console.WriteLine("Address: " + ep.Address);
Console.WriteLine("Port: " + ep.Port);
}
}
}
这是输出:
[
{
"Address": "8.8.4.4",
"Port": 53
},
{
"Address": "2001:db8::ff00:42:8329",
"Port": 81
}
]
AddressFamily: InterNetwork
Address: 8.8.4.4
Port: 53
AddressFamily: InterNetworkV6
Address: 2001:db8::ff00:42:8329
Port: 81
小提琴:https ://dotnetfiddle.net/tK7NKY