Added Converters/Formatters for Serialization of Json/MessagePack
This commit is contained in:
77
AMWD.Common/Formatters/IPAddressArrayFormatter.cs
Normal file
77
AMWD.Common/Formatters/IPAddressArrayFormatter.cs
Normal file
@@ -0,0 +1,77 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Net;
|
||||
|
||||
namespace MessagePack.Formatters
|
||||
{
|
||||
/// <summary>
|
||||
/// Serialization of an <see cref="IPAddress"/> array to and from <see cref="MessagePack"/>.
|
||||
/// </summary>
|
||||
[System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage]
|
||||
public class IPAddressArrayFormatter : IMessagePackFormatter<IPAddress[]>
|
||||
{
|
||||
/// <inheritdoc/>
|
||||
public IPAddress[] Deserialize(ref MessagePackReader reader, MessagePackSerializerOptions options)
|
||||
{
|
||||
if (reader.IsNil)
|
||||
return null;
|
||||
|
||||
int bytePos = 0;
|
||||
byte[] bytes = options.Resolver.GetFormatterWithVerify<byte[]>().Deserialize(ref reader, options);
|
||||
byte[] buffer = bytes.Skip(bytePos).Take(sizeof(int)).ToArray();
|
||||
bytePos += sizeof(int);
|
||||
|
||||
Swap(buffer);
|
||||
int length = BitConverter.ToInt32(buffer, 0);
|
||||
|
||||
int arrayPos = 0;
|
||||
var array = new IPAddress[length];
|
||||
while (bytePos < bytes.Length)
|
||||
{
|
||||
byte len = bytes.Skip(bytePos).First();
|
||||
bytePos++;
|
||||
|
||||
buffer = bytes.Skip(bytePos).Take(len).ToArray();
|
||||
bytePos += len;
|
||||
|
||||
array[arrayPos] = new IPAddress(buffer);
|
||||
arrayPos++;
|
||||
}
|
||||
|
||||
return array;
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
public void Serialize(ref MessagePackWriter writer, IPAddress[] value, MessagePackSerializerOptions options)
|
||||
{
|
||||
if (value == null)
|
||||
{
|
||||
writer.WriteNil();
|
||||
return;
|
||||
}
|
||||
|
||||
var bytes = new List<byte>();
|
||||
|
||||
int length = value.Length;
|
||||
byte[] buffer = BitConverter.GetBytes(length);
|
||||
Swap(buffer);
|
||||
bytes.AddRange(buffer);
|
||||
|
||||
foreach (var ip in value)
|
||||
{
|
||||
buffer = ip.GetAddressBytes();
|
||||
bytes.Add((byte)buffer.Length);
|
||||
bytes.AddRange(buffer);
|
||||
}
|
||||
|
||||
options.Resolver.GetFormatterWithVerify<byte[]>().Serialize(ref writer, bytes.ToArray(), options);
|
||||
}
|
||||
|
||||
private void Swap(byte[] bytes)
|
||||
{
|
||||
if (BitConverter.IsLittleEndian)
|
||||
Array.Reverse(bytes);
|
||||
}
|
||||
}
|
||||
}
|
||||
34
AMWD.Common/Formatters/IPAddressFormatter.cs
Normal file
34
AMWD.Common/Formatters/IPAddressFormatter.cs
Normal file
@@ -0,0 +1,34 @@
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
72
AMWD.Common/Formatters/IPAddressListFormatter.cs
Normal file
72
AMWD.Common/Formatters/IPAddressListFormatter.cs
Normal file
@@ -0,0 +1,72 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Net;
|
||||
|
||||
namespace MessagePack.Formatters
|
||||
{
|
||||
/// <summary>
|
||||
/// Serialization of an <see cref="IPAddress"/> list to and from <see cref="MessagePack"/>.
|
||||
/// </summary>
|
||||
[System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage]
|
||||
public class IPAddressListFormatter : IMessagePackFormatter<List<IPAddress>>
|
||||
{
|
||||
/// <inheritdoc/>
|
||||
public List<IPAddress> Deserialize(ref MessagePackReader reader, MessagePackSerializerOptions options)
|
||||
{
|
||||
if (reader.IsNil)
|
||||
return null;
|
||||
|
||||
byte[] bytes = options.Resolver.GetFormatterWithVerify<byte[]>().Deserialize(ref reader, options);
|
||||
|
||||
// skipping the length information
|
||||
int bytePos = sizeof(int);
|
||||
|
||||
var list = new List<IPAddress>();
|
||||
while (bytePos < bytes.Length)
|
||||
{
|
||||
byte len = bytes.Skip(bytePos).First();
|
||||
bytePos++;
|
||||
|
||||
byte[] buffer = bytes.Skip(bytePos).Take(len).ToArray();
|
||||
bytePos += len;
|
||||
|
||||
list.Add(new IPAddress(buffer));
|
||||
}
|
||||
|
||||
return list;
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
public void Serialize(ref MessagePackWriter writer, List<IPAddress> value, MessagePackSerializerOptions options)
|
||||
{
|
||||
if (value == null)
|
||||
{
|
||||
writer.WriteNil();
|
||||
return;
|
||||
}
|
||||
|
||||
var bytes = new List<byte>();
|
||||
|
||||
int length = value.Count;
|
||||
byte[] buffer = BitConverter.GetBytes(length);
|
||||
Swap(buffer);
|
||||
bytes.AddRange(buffer);
|
||||
|
||||
foreach (var ip in value)
|
||||
{
|
||||
buffer = ip.GetAddressBytes();
|
||||
bytes.Add((byte)buffer.Length);
|
||||
bytes.AddRange(buffer);
|
||||
}
|
||||
|
||||
options.Resolver.GetFormatterWithVerify<byte[]>().Serialize(ref writer, bytes.ToArray(), options);
|
||||
}
|
||||
|
||||
private void Swap(byte[] bytes)
|
||||
{
|
||||
if (BitConverter.IsLittleEndian)
|
||||
Array.Reverse(bytes);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user