74 lines
2.2 KiB
C#
74 lines
2.2 KiB
C#
|
// Copyright (c) All contributors. All rights reserved.
|
|||
|
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
|
|||
|
|
|||
|
using System.Buffers;
|
|||
|
|
|||
|
#pragma warning disable SA1649 // File name should match first type name
|
|||
|
|
|||
|
namespace MessagePack.Formatters
|
|||
|
{
|
|||
|
public sealed class NullableFormatter<T> : IMessagePackFormatter<T?>
|
|||
|
where T : struct
|
|||
|
{
|
|||
|
public void Serialize(ref MessagePackWriter writer, T? value, MessagePackSerializerOptions options)
|
|||
|
{
|
|||
|
if (value == null)
|
|||
|
{
|
|||
|
writer.WriteNil();
|
|||
|
}
|
|||
|
else
|
|||
|
{
|
|||
|
options.Resolver.GetFormatterWithVerify<T>().Serialize(ref writer, value.Value, options);
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
public T? Deserialize(ref MessagePackReader reader, MessagePackSerializerOptions options)
|
|||
|
{
|
|||
|
if (reader.IsNil)
|
|||
|
{
|
|||
|
reader.ReadNil();
|
|||
|
return null;
|
|||
|
}
|
|||
|
else
|
|||
|
{
|
|||
|
return options.Resolver.GetFormatterWithVerify<T>().Deserialize(ref reader, options);
|
|||
|
}
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
public sealed class StaticNullableFormatter<T> : IMessagePackFormatter<T?>
|
|||
|
where T : struct
|
|||
|
{
|
|||
|
private readonly IMessagePackFormatter<T> underlyingFormatter;
|
|||
|
|
|||
|
public StaticNullableFormatter(IMessagePackFormatter<T> underlyingFormatter)
|
|||
|
{
|
|||
|
this.underlyingFormatter = underlyingFormatter;
|
|||
|
}
|
|||
|
|
|||
|
public void Serialize(ref MessagePackWriter writer, T? value, MessagePackSerializerOptions options)
|
|||
|
{
|
|||
|
if (value == null)
|
|||
|
{
|
|||
|
writer.WriteNil();
|
|||
|
}
|
|||
|
else
|
|||
|
{
|
|||
|
this.underlyingFormatter.Serialize(ref writer, value.Value, options);
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
public T? Deserialize(ref MessagePackReader reader, MessagePackSerializerOptions options)
|
|||
|
{
|
|||
|
if (reader.TryReadNil())
|
|||
|
{
|
|||
|
return null;
|
|||
|
}
|
|||
|
else
|
|||
|
{
|
|||
|
return this.underlyingFormatter.Deserialize(ref reader, options);
|
|||
|
}
|
|||
|
}
|
|||
|
}
|
|||
|
}
|