上海苏宁宝丽嘉酒店,2020.11.17 15:30改为单机版本,以后合成单机版本可以以此版本为模板
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

71 lines
2.2 KiB

using System;
using System.Collections.Generic;
using System.Text;
namespace MessagePack.Formatters
{
public sealed class NullableFormatter<T> : IMessagePackFormatter<T?>
where T : struct
{
public int Serialize(ref byte[] bytes, int offset, T? value, IFormatterResolver formatterResolver)
{
if (value == null)
{
return MessagePackBinary.WriteNil(ref bytes, offset);
}
else
{
return formatterResolver.GetFormatterWithVerify<T>().Serialize(ref bytes, offset, value.Value, formatterResolver);
}
}
public T? Deserialize(byte[] bytes, int offset, IFormatterResolver formatterResolver, out int readSize)
{
if (MessagePackBinary.IsNil(bytes, offset))
{
readSize = 1;
return null;
}
else
{
return formatterResolver.GetFormatterWithVerify<T>().Deserialize(bytes, offset, formatterResolver, out readSize);
}
}
}
public sealed class StaticNullableFormatter<T> : IMessagePackFormatter<T?>
where T : struct
{
readonly IMessagePackFormatter<T> underlyingFormatter;
public StaticNullableFormatter(IMessagePackFormatter<T> underlyingFormatter)
{
this.underlyingFormatter = underlyingFormatter;
}
public int Serialize(ref byte[] bytes, int offset, T? value, IFormatterResolver formatterResolver)
{
if (value == null)
{
return MessagePackBinary.WriteNil(ref bytes, offset);
}
else
{
return underlyingFormatter.Serialize(ref bytes, offset, value.Value, formatterResolver);
}
}
public T? Deserialize(byte[] bytes, int offset, IFormatterResolver formatterResolver, out int readSize)
{
if (MessagePackBinary.IsNil(bytes, offset))
{
readSize = 1;
return null;
}
else
{
return underlyingFormatter.Deserialize(bytes, offset, formatterResolver, out readSize);
}
}
}
}