-
Notifications
You must be signed in to change notification settings - Fork 1
/
IPAddressConverter.cs
173 lines (155 loc) · 5.59 KB
/
IPAddressConverter.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
namespace Remootio
{
/// <summary>
/// https://pingfu.net//how-to-serialise-ipaddress-ipendpoint
/// </summary>
public class IPAddressConverter : JsonConverter
{
/// <summary>
///
/// </summary>
/// <param name="objectType"></param>
/// <returns></returns>
public override bool CanConvert(Type objectType)
{
if (objectType == typeof(IPAddress)) return true;
if (objectType == typeof(List<IPAddress>)) return true;
return false;
}
/// <summary>
///
/// </summary>
/// <param name="reader"></param>
/// <param name="objectType"></param>
/// <param name="existingValue"></param>
/// <param name="serializer"></param>
/// <returns></returns>
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
{
// convert an ipaddress represented as a string into an IPAddress object and return it to the caller
if (objectType == typeof(IPAddress))
{
return IPAddress.Parse(JToken.Load(reader).ToString());
}
// convert a json array of ipaddresses represented as strings into a List<IPAddress> object and return it to the caller
if (objectType == typeof(List<IPAddress>))
{
return JToken.Load(reader).Select(address => IPAddress.Parse((string)address)).ToList();
}
throw new NotImplementedException();
}
/// <summary>
///
/// </summary>
/// <param name="writer"></param>
/// <param name="value"></param>
/// <param name="serializer"></param>
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
{
// convert an IPAddress object to a string representation of itself and write it to the serialiser
if (value.GetType() == typeof(IPAddress))
{
JToken.FromObject(value.ToString()).WriteTo(writer);
return;
}
// convert a List<IPAddress> object to an array of strings of ipaddresses and write it to the serialiser
if (value.GetType() == typeof(List<IPAddress>))
{
JToken.FromObject((from n in (List<IPAddress>)value select n.ToString()).ToList()).WriteTo(writer);
return;
}
throw new NotImplementedException();
}
}
/// <summary>
///
/// </summary>
public class IPEndPointConverter : JsonConverter
{
/// <summary>
///
/// </summary>
/// <param name="objectType"></param>
/// <returns></returns>
public override bool CanConvert(Type objectType)
{
return objectType == typeof(IPEndPoint);
}
/// <summary>
///
/// </summary>
/// <param name="reader"></param>
/// <param name="objectType"></param>
/// <param name="existingValue"></param>
/// <param name="serializer"></param>
/// <returns></returns>
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
{
return JToken.Load(reader).ToString().ToIPEndPoint();
}
/// <summary>
///
/// </summary>
/// <param name="writer"></param>
/// <param name="value"></param>
/// <param name="serializer"></param>
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
{
var ipEndPoint = value as IPEndPoint;
if (ipEndPoint != null)
{
if (ipEndPoint.Address != null || ipEndPoint.Port != 0)
{
JToken.FromObject(string.Format("{0}:{1}", ipEndPoint.Address, ipEndPoint.Port)).WriteTo(writer);
return;
}
}
writer.WriteNull();
}
}
/// <summary>
///
/// </summary>
public static class IPAddressExtensions
{
/// <summary>
///
/// </summary>
/// <param name="ipEndPoint"></param>
/// <returns></returns>
public static IPEndPoint ToIPEndPoint(this string ipEndPoint)
{
if (string.IsNullOrWhiteSpace(ipEndPoint))
{
return null;
}
var components = ipEndPoint.Split(':');
return new IPEndPoint(IPAddress.Parse(components[0]), Convert.ToInt32(components[1]));
}
/// <summary>
/// Helper for Deserializing oblects containing IPAddress, IPEndPoint
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="json"></param>
/// <param name="IPAddress"></param>
/// <returns></returns>
public static T DeserializeObject<T>(string json)
{
JsonConverter[] converters = { new IPAddressConverter(), new IPEndPointConverter() };
return JsonConvert.DeserializeObject<T>(json, converters);
}
public static string SerializeObject<T>(T obj)
{
JsonConverter[] converters = { new IPAddressConverter(), new IPEndPointConverter() };
return JsonConvert.SerializeObject(obj, converters);
}
}
}