using MessagePack.Formatters; using MessagePack.Internal; using MessagePack.Resolvers; using System; using System.Collections; using System.Collections.Generic; using System.Reflection; using System.Text; namespace MessagePack.Resolvers { public sealed class BuiltinResolver : IFormatterResolver { public static readonly IFormatterResolver Instance = new BuiltinResolver(); BuiltinResolver() { } public IMessagePackFormatter GetFormatter() { return FormatterCache.formatter; } static class FormatterCache { public static readonly IMessagePackFormatter formatter; static FormatterCache() { // Reduce IL2CPP code generate size(don't write long code in ) formatter = (IMessagePackFormatter)BuiltinResolverGetFormatterHelper.GetFormatter(typeof(T)); } } } } namespace MessagePack.Internal { internal static class BuiltinResolverGetFormatterHelper { static readonly Dictionary formatterMap = new Dictionary() { // Primitive {typeof(Int16), Int16Formatter.Instance}, {typeof(Int32), Int32Formatter.Instance}, {typeof(Int64), Int64Formatter.Instance}, {typeof(UInt16), UInt16Formatter.Instance}, {typeof(UInt32), UInt32Formatter.Instance}, {typeof(UInt64), UInt64Formatter.Instance}, {typeof(Single), SingleFormatter.Instance}, {typeof(Double), DoubleFormatter.Instance}, {typeof(bool), BooleanFormatter.Instance}, {typeof(byte), ByteFormatter.Instance}, {typeof(sbyte), SByteFormatter.Instance}, {typeof(DateTime), DateTimeFormatter.Instance}, {typeof(char), CharFormatter.Instance}, // Nulllable Primitive {typeof(Nullable), NullableInt16Formatter.Instance}, {typeof(Nullable), NullableInt32Formatter.Instance}, {typeof(Nullable), NullableInt64Formatter.Instance}, {typeof(Nullable), NullableUInt16Formatter.Instance}, {typeof(Nullable), NullableUInt32Formatter.Instance}, {typeof(Nullable), NullableUInt64Formatter.Instance}, {typeof(Nullable), NullableSingleFormatter.Instance}, {typeof(Nullable), NullableDoubleFormatter.Instance}, {typeof(Nullable), NullableBooleanFormatter.Instance}, {typeof(Nullable), NullableByteFormatter.Instance}, {typeof(Nullable), NullableSByteFormatter.Instance}, {typeof(Nullable), NullableDateTimeFormatter.Instance}, {typeof(Nullable), NullableCharFormatter.Instance}, // StandardClassLibraryFormatter {typeof(string), NullableStringFormatter.Instance}, {typeof(decimal), DecimalFormatter.Instance}, {typeof(decimal?), new StaticNullableFormatter(DecimalFormatter.Instance)}, {typeof(TimeSpan), TimeSpanFormatter.Instance}, {typeof(TimeSpan?), new StaticNullableFormatter(TimeSpanFormatter.Instance)}, {typeof(DateTimeOffset), DateTimeOffsetFormatter.Instance}, {typeof(DateTimeOffset?), new StaticNullableFormatter(DateTimeOffsetFormatter.Instance)}, {typeof(Guid), GuidFormatter.Instance}, {typeof(Guid?), new StaticNullableFormatter(GuidFormatter.Instance)}, {typeof(Uri), UriFormatter.Instance}, {typeof(Version), VersionFormatter.Instance}, {typeof(StringBuilder), StringBuilderFormatter.Instance}, {typeof(BitArray), BitArrayFormatter.Instance}, // special primitive {typeof(byte[]), ByteArrayFormatter.Instance}, // Nil {typeof(Nil), NilFormatter.Instance}, {typeof(Nil?), NullableNilFormatter.Instance}, // otpmitized primitive array formatter {typeof(Int16[]), Int16ArrayFormatter.Instance}, {typeof(Int32[]), Int32ArrayFormatter.Instance}, {typeof(Int64[]), Int64ArrayFormatter.Instance}, {typeof(UInt16[]), UInt16ArrayFormatter.Instance}, {typeof(UInt32[]), UInt32ArrayFormatter.Instance}, {typeof(UInt64[]), UInt64ArrayFormatter.Instance}, {typeof(Single[]), SingleArrayFormatter.Instance}, {typeof(Double[]), DoubleArrayFormatter.Instance}, {typeof(Boolean[]), BooleanArrayFormatter.Instance}, {typeof(SByte[]), SByteArrayFormatter.Instance}, {typeof(DateTime[]), DateTimeArrayFormatter.Instance}, {typeof(Char[]), CharArrayFormatter.Instance}, {typeof(string[]), NullableStringArrayFormatter.Instance}, // well known collections {typeof(List), new ListFormatter()}, {typeof(List), new ListFormatter()}, {typeof(List), new ListFormatter()}, {typeof(List), new ListFormatter()}, {typeof(List), new ListFormatter()}, {typeof(List), new ListFormatter()}, {typeof(List), new ListFormatter()}, {typeof(List), new ListFormatter()}, {typeof(List), new ListFormatter()}, {typeof(List), new ListFormatter()}, {typeof(List), new ListFormatter()}, {typeof(List), new ListFormatter()}, {typeof(List), new ListFormatter()}, {typeof(List), new ListFormatter()}, { typeof(ArraySegment), ByteArraySegmentFormatter.Instance }, { typeof(ArraySegment?),new StaticNullableFormatter>(ByteArraySegmentFormatter.Instance) }, #if NETSTANDARD {typeof(System.Numerics.BigInteger), BigIntegerFormatter.Instance}, {typeof(System.Numerics.BigInteger?), new StaticNullableFormatter(BigIntegerFormatter.Instance)}, {typeof(System.Numerics.Complex), ComplexFormatter.Instance}, {typeof(System.Numerics.Complex?), new StaticNullableFormatter(ComplexFormatter.Instance)}, {typeof(System.Threading.Tasks.Task), TaskUnitFormatter.Instance}, #endif }; internal static object GetFormatter(Type t) { object formatter; if (formatterMap.TryGetValue(t, out formatter)) { return formatter; } return null; } } }