using System; using System.Collections; using System.Collections.Generic; using System.Text; using Utf8Json.Formatters; namespace Utf8Json.Resolvers { public sealed class BuiltinResolver : IJsonFormatterResolver { public static readonly IJsonFormatterResolver Instance = new BuiltinResolver(); BuiltinResolver() { } public IJsonFormatter GetFormatter() { return FormatterCache.formatter; } static class FormatterCache { public static readonly IJsonFormatter formatter; static FormatterCache() { // Reduce IL2CPP code generate size(don't write long code in ) formatter = (IJsonFormatter)BuiltinResolverGetFormatterHelper.GetFormatter(typeof(T)); } } // used from PrimitiveObjectFormatter internal static class BuiltinResolverGetFormatterHelper { static readonly Dictionary formatterMap = new Dictionary() { // Primitive {typeof(Int16), Int16Formatter.Default}, {typeof(Int32), Int32Formatter.Default}, {typeof(Int64), Int64Formatter.Default}, {typeof(UInt16), UInt16Formatter.Default}, {typeof(UInt32), UInt32Formatter.Default}, {typeof(UInt64), UInt64Formatter.Default}, {typeof(Single), SingleFormatter.Default}, {typeof(Double), DoubleFormatter.Default}, {typeof(bool), BooleanFormatter.Default}, {typeof(byte), ByteFormatter.Default}, {typeof(sbyte), SByteFormatter.Default}, // Nulllable Primitive {typeof(Nullable), NullableInt16Formatter.Default}, {typeof(Nullable), NullableInt32Formatter.Default}, {typeof(Nullable), NullableInt64Formatter.Default}, {typeof(Nullable), NullableUInt16Formatter.Default}, {typeof(Nullable), NullableUInt32Formatter.Default}, {typeof(Nullable), NullableUInt64Formatter.Default}, {typeof(Nullable), NullableSingleFormatter.Default}, {typeof(Nullable), NullableDoubleFormatter.Default}, {typeof(Nullable), NullableBooleanFormatter.Default}, {typeof(Nullable), NullableByteFormatter.Default}, {typeof(Nullable), NullableSByteFormatter.Default}, // StandardClassLibraryFormatter // DateTime {typeof(DateTime), ISO8601DateTimeFormatter.Default}, // ISO8601 {typeof(TimeSpan), ISO8601TimeSpanFormatter.Default}, {typeof(DateTimeOffset), ISO8601DateTimeOffsetFormatter.Default}, {typeof(DateTime?), new StaticNullableFormatter(ISO8601DateTimeFormatter.Default)}, // ISO8601 {typeof(TimeSpan?), new StaticNullableFormatter(ISO8601TimeSpanFormatter.Default)}, {typeof(DateTimeOffset?),new StaticNullableFormatter(ISO8601DateTimeOffsetFormatter.Default)}, {typeof(string), NullableStringFormatter.Default}, {typeof(char), CharFormatter.Default}, {typeof(Nullable), NullableCharFormatter.Default}, {typeof(decimal), DecimalFormatter.Default}, {typeof(decimal?), new StaticNullableFormatter(DecimalFormatter.Default)}, {typeof(Guid), GuidFormatter.Default}, {typeof(Guid?), new StaticNullableFormatter(GuidFormatter.Default)}, {typeof(Uri), UriFormatter.Default}, {typeof(Version), VersionFormatter.Default}, {typeof(StringBuilder), StringBuilderFormatter.Default}, {typeof(BitArray), BitArrayFormatter.Default}, {typeof(Type), TypeFormatter.Default}, // special primitive {typeof(byte[]), ByteArrayFormatter.Default}, // otpmitized primitive array formatter {typeof(Int16[]), Int16ArrayFormatter.Default}, {typeof(Int32[]), Int32ArrayFormatter.Default}, {typeof(Int64[]), Int64ArrayFormatter.Default}, {typeof(UInt16[]), UInt16ArrayFormatter.Default}, {typeof(UInt32[]), UInt32ArrayFormatter.Default}, {typeof(UInt64[]), UInt64ArrayFormatter.Default}, {typeof(Single[]), SingleArrayFormatter.Default}, {typeof(Double[]), DoubleArrayFormatter.Default}, {typeof(Boolean[]), BooleanArrayFormatter.Default}, {typeof(SByte[]), SByteArrayFormatter.Default}, {typeof(Char[]), CharArrayFormatter.Default}, {typeof(string[]), NullableStringArrayFormatter.Default}, // 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.Default }, { typeof(ArraySegment?),new StaticNullableFormatter>(ByteArraySegmentFormatter.Default) }, #if NETSTANDARD {typeof(System.Numerics.BigInteger), BigIntegerFormatter.Default}, {typeof(System.Numerics.BigInteger?), new StaticNullableFormatter(BigIntegerFormatter.Default)}, {typeof(System.Numerics.Complex), ComplexFormatter.Default}, {typeof(System.Numerics.Complex?), new StaticNullableFormatter(ComplexFormatter.Default)}, {typeof(System.Dynamic.ExpandoObject), ExpandoObjectFormatter.Default }, {typeof(System.Threading.Tasks.Task), TaskUnitFormatter.Default}, #endif }; internal static object GetFormatter(Type t) { object formatter; if (formatterMap.TryGetValue(t, out formatter)) { return formatter; } return null; } } } }