// 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 : IMessagePackFormatter where T : struct { public void Serialize(ref MessagePackWriter writer, T? value, MessagePackSerializerOptions options) { if (value == null) { writer.WriteNil(); } else { options.Resolver.GetFormatterWithVerify().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().Deserialize(ref reader, options); } } } public sealed class StaticNullableFormatter : IMessagePackFormatter where T : struct { private readonly IMessagePackFormatter underlyingFormatter; public StaticNullableFormatter(IMessagePackFormatter 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); } } } }