using System; using System.Collections.Generic; using System.Text; namespace MessagePack.Formatters { public sealed class NullableFormatter : IMessagePackFormatter 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().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().Deserialize(bytes, offset, formatterResolver, out readSize); } } } public sealed class StaticNullableFormatter : IMessagePackFormatter where T : struct { readonly IMessagePackFormatter underlyingFormatter; public StaticNullableFormatter(IMessagePackFormatter 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); } } } }