Created
July 10, 2017 13:11
-
-
Save neuecc/b03ad445bb39572283fe9dd616cc3d69 to your computer and use it in GitHub Desktop.
UnixtimestampResolver
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public sealed class UnixTimestampResolver : IFormatterResolver | |
{ | |
public static readonly IFormatterResolver Instance = new UnixTimestampResolver(); | |
UnixTimestampResolver() | |
{ | |
} | |
public IMessagePackFormatter<T> GetFormatter<T>() | |
{ | |
return FormatterCache<T>.formatter; | |
} | |
static class FormatterCache<T> | |
{ | |
public static readonly IMessagePackFormatter<T> formatter; | |
static FormatterCache() | |
{ | |
formatter = (IMessagePackFormatter<T>)UnixTimestampResolverGetFormatterHelper.GetFormatter(typeof(T)); | |
} | |
} | |
} | |
internal static class UnixTimestampResolverGetFormatterHelper | |
{ | |
internal static object GetFormatter(Type t) | |
{ | |
if (t == typeof(DateTime)) | |
{ | |
return UnixTimestampDateTimeFormatter.Instance; | |
} | |
else if (t == typeof(DateTime?)) | |
{ | |
return new StaticNullableFormatter<DateTime>(UnixTimestampDateTimeFormatter.Instance); | |
} | |
else if (t == typeof(DateTime[])) | |
{ | |
return new ArrayFormatter<DateTime>(); // これが必要なのはMessagePack for C#ヨクナインデスケドネ(要修正) | |
} | |
else if (t == typeof(DateTimeOffset)) | |
{ | |
return UnixTimestampDateTimeOffsetFormatter.Instance; | |
} | |
else if (t == typeof(DateTimeOffset?)) | |
{ | |
return new StaticNullableFormatter<DateTimeOffset>(UnixTimestampDateTimeOffsetFormatter.Instance); | |
} | |
return null; | |
} | |
} | |
public sealed class UnixTimestampDateTimeFormatter : IMessagePackFormatter<DateTime> | |
{ | |
public static readonly UnixTimestampDateTimeFormatter Instance = new UnixTimestampDateTimeFormatter(); | |
UnixTimestampDateTimeFormatter() | |
{ | |
} | |
public int Serialize(ref byte[] bytes, int offset, DateTime value, IFormatterResolver formatterResolver) | |
{ | |
var dateData = ((DateTimeOffset)value).ToUnixTimeSeconds(); | |
return MessagePackBinary.WriteInt64(ref bytes, offset, dateData); | |
} | |
public DateTime Deserialize(byte[] bytes, int offset, IFormatterResolver formatterResolver, out int readSize) | |
{ | |
if (MessagePackBinary.GetMessagePackType(bytes, offset) == MessagePackType.Extension) | |
{ | |
return DateTimeFormatter.Instance.Deserialize(bytes, offset, formatterResolver, out readSize); | |
} | |
var dateData = MessagePackBinary.ReadInt64(bytes, offset, out readSize); | |
return DateTimeOffset.FromUnixTimeSeconds(dateData).UtcDateTime; | |
} | |
} | |
public sealed class UnixTimestampDateTimeOffsetFormatter : IMessagePackFormatter<DateTimeOffset> | |
{ | |
public static readonly UnixTimestampDateTimeOffsetFormatter Instance = new UnixTimestampDateTimeOffsetFormatter(); | |
UnixTimestampDateTimeOffsetFormatter() | |
{ | |
} | |
public int Serialize(ref byte[] bytes, int offset, DateTimeOffset value, IFormatterResolver formatterResolver) | |
{ | |
var dateData = value.ToUnixTimeSeconds(); | |
return MessagePackBinary.WriteInt64(ref bytes, offset, dateData); | |
} | |
public DateTimeOffset Deserialize(byte[] bytes, int offset, IFormatterResolver formatterResolver, out int readSize) | |
{ | |
if (MessagePackBinary.GetMessagePackType(bytes, offset) == MessagePackType.Extension) | |
{ | |
return DateTimeFormatter.Instance.Deserialize(bytes, offset, formatterResolver, out readSize); | |
} | |
var dateData = MessagePackBinary.ReadInt64(bytes, offset, out readSize); | |
return DateTimeOffset.FromUnixTimeSeconds(dateData); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment