68 lines
1.8 KiB
C#
68 lines
1.8 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using AMWD.Common.Utilities;
|
|
using Microsoft.AspNetCore.HttpOverrides;
|
|
|
|
namespace MessagePack.Formatters
|
|
{
|
|
/// <summary>
|
|
/// Serialization of an <see cref="IPNetwork"/> list to and from <see cref="MessagePack"/>.
|
|
/// </summary>
|
|
[System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage]
|
|
public class IPNetworkListFormatter : IMessagePackFormatter<List<IPNetwork>>
|
|
{
|
|
/// <inheritdoc/>
|
|
public List<IPNetwork> 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<IPNetwork>();
|
|
while (bytePos < bytes.Length)
|
|
{
|
|
byte len = bytes.Skip(bytePos).First();
|
|
bytePos++;
|
|
|
|
byte[] buffer = bytes.Skip(bytePos).Take(len).ToArray();
|
|
bytePos += len;
|
|
|
|
list.Add(IPNetworkFormatter.DeserializeInternal(buffer));
|
|
}
|
|
|
|
return list;
|
|
}
|
|
|
|
/// <inheritdoc/>
|
|
public void Serialize(ref MessagePackWriter writer, List<IPNetwork> value, MessagePackSerializerOptions options)
|
|
{
|
|
if (value == null)
|
|
{
|
|
writer.WriteNil();
|
|
return;
|
|
}
|
|
|
|
var bytes = new List<byte>();
|
|
|
|
int length = value.Count;
|
|
byte[] buffer = BitConverter.GetBytes(length);
|
|
NetworkHelper.SwapBigEndian(buffer);
|
|
bytes.AddRange(buffer);
|
|
|
|
foreach (var network in value)
|
|
{
|
|
buffer = IPNetworkFormatter.SerializeInternal(network);
|
|
bytes.Add((byte)buffer.Length);
|
|
bytes.AddRange(buffer);
|
|
}
|
|
|
|
options.Resolver.GetFormatterWithVerify<byte[]>().Serialize(ref writer, bytes.ToArray(), options);
|
|
}
|
|
}
|
|
}
|