-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathJsonIPEndPointConverter.cs
113 lines (102 loc) · 3.7 KB
/
JsonIPEndPointConverter.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
#if NETSTANDARD2_1_OR_GREATER || NET6_0_OR_GREATER
using System.Buffers;
using System.Globalization;
using System.Net;
using System.Net.Sockets;
#else
using System.Globalization;
using System.Net;
using System.Text.RegularExpressions;
#endif
using Macross.Json.Extensions;
namespace System.Text.Json.Serialization
{
/// <summary>
/// <see cref="JsonConverterFactory"/> to convert <see cref="IPEndPoint"/> to and from strings.
/// </summary>
public class JsonIPEndPointConverter : JsonConverter<IPEndPoint>
{
#if NETSTANDARD2_0
private static readonly Regex s_IPEndPointRegex = new("^[[]?(.*?)[]]?:(\\d+)$", RegexOptions.Compiled | RegexOptions.Singleline);
#endif
/// <inheritdoc/>
public override IPEndPoint Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
if (reader.TokenType != JsonTokenType.String)
throw ThrowHelper.GenerateJsonException_DeserializeUnableToConvertValue(typeof(IPEndPoint));
#if NETSTANDARD2_1_OR_GREATER || NET6_0_OR_GREATER
Span<char> charData = stackalloc char[53];
int count = Encoding.UTF8.GetChars(
reader.HasValueSequence ? reader.ValueSequence.ToArray() : reader.ValueSpan,
charData);
int addressLength = count;
int lastColonPos = charData.LastIndexOf(':');
if (lastColonPos > 0)
{
if (charData[lastColonPos - 1] == ']')
{
addressLength = lastColonPos;
}
else if (charData[..lastColonPos].LastIndexOf(':') == -1)
{
// Look to see if this is IPv4 with a port (IPv6 will have another colon)
addressLength = lastColonPos;
}
}
if (!IPAddress.TryParse(charData[..addressLength], out IPAddress? address))
throw ThrowHelper.GenerateJsonException_DeserializeUnableToConvertValue(typeof(IPEndPoint));
uint port = 0;
return addressLength == charData.Length ||
(uint.TryParse(charData[(addressLength + 1)..], NumberStyles.None, CultureInfo.InvariantCulture, out port) && port <= IPEndPoint.MaxPort)
? new IPEndPoint(address, (int)port)
: throw ThrowHelper.GenerateJsonException_DeserializeUnableToConvertValue(typeof(IPEndPoint));
#else
string value = reader.GetString()!;
Match match = s_IPEndPointRegex.Match(value);
if (!match.Success)
throw ThrowHelper.GenerateJsonException_DeserializeUnableToConvertValue(typeof(IPEndPoint), value);
try
{
return new IPEndPoint(
IPAddress.Parse(match.Groups[1].Value),
int.Parse(match.Groups[2].Value, CultureInfo.InvariantCulture));
}
catch (Exception ex)
{
throw ThrowHelper.GenerateJsonException_DeserializeUnableToConvertValue(typeof(IPEndPoint), value, ex);
}
#endif
}
/// <inheritdoc/>
public override void Write(Utf8JsonWriter writer, IPEndPoint value, JsonSerializerOptions options)
#pragma warning disable CA1062 // Don't perform checks for performance. Trust our callers will be nice.
#if NETSTANDARD2_1_OR_GREATER || NET6_0_OR_GREATER
{
bool isIpv6 = value.AddressFamily == AddressFamily.InterNetworkV6;
Span<char> data = isIpv6
? stackalloc char[21]
: stackalloc char[53];
int offset = 0;
if (isIpv6)
{
data[0] = '[';
offset++;
}
if (!value.Address.TryFormat(data[offset..], out int addressCharsWritten))
throw new JsonException($"IPEndPoint [{value}] could not be written to JSON.");
offset += addressCharsWritten;
if (isIpv6)
{
data[offset++] = ']';
}
data[offset++] = ':';
if (!value.Port.TryFormat(data[offset..], out int portCharsWritten))
throw new JsonException($"IPEndPoint [{value}] could not be written to JSON.");
writer.WriteStringValue(data[..(offset + portCharsWritten)]);
}
#else
=> writer.WriteStringValue(value.ToString());
#endif
#pragma warning restore CA1062 // Validate arguments of public methods
}
}