1
0
Files
common/AMWD.Common.MessagePack/Formatters/IPAddressFormatter.cs
2024-01-14 13:10:33 +01:00

35 lines
986 B
C#

using System.Net;
namespace MessagePack.Formatters
{
/// <summary>
/// Serialization of an <see cref="IPAddress"/> to and from <see cref="MessagePack"/>.
/// </summary>
[System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage]
public class IPAddressFormatter : IMessagePackFormatter<IPAddress>
{
/// <inheritdoc/>
public IPAddress Deserialize(ref MessagePackReader reader, MessagePackSerializerOptions options)
{
if (reader.IsNil)
return null;
byte[] bytes = options.Resolver.GetFormatterWithVerify<byte[]>().Deserialize(ref reader, options);
return new IPAddress(bytes);
}
/// <inheritdoc/>
public void Serialize(ref MessagePackWriter writer, IPAddress value, MessagePackSerializerOptions options)
{
if (value == null)
{
writer.WriteNil();
return;
}
byte[] bytes = value.GetAddressBytes();
options.Resolver.GetFormatterWithVerify<byte[]>().Serialize(ref writer, bytes, options);
}
}
}