public LookupFormatter() { keyFormatter = Formatter <TKey> .Default; valueFormatter = Formatter <TElement> .Default; valuesFormatter = Formatter <IEnumerable <TElement> > .Default; }
public InterfaceSetFormatter() { this.formatter = Formatter <T> .Default; this.formatterLength = formatter.GetLength(); }
public DictionaryFormatter() { this.formatter = Formatter <KeyValuePair <TKey, TValue> > .Default; }
public InterfaceEnumerableFormatter() { this.formatter = Formatter <T> .Default; }
public CollectionFormatter() { this.formatter = Formatter <TElement> .Default; this.formatterLength = formatter.GetLength(); this.isList = typeof(TCollection).GetGenericTypeDefinition() == typeof(List <>); }
public ArrayFormatter() { this.formatter = Formatter <T> .Default; this.formatterLength = formatter.GetLength(); }
public static void Register(Formatter <TTypeResolver, T> formatter) { defaultFormatter = formatter; }
public ListFormatter() { this.formatter = Formatter <T> .Default; }
public object ResolveFormatter(Type type) { return(Formatter.ResolveFormatter(type)); }
public void RegisterDynamicUnion(Type unionType, DynamicUnionResolver resolver) { Formatter.ResolveDynamicUnion(unionType, resolver); }
static Formatter() { var resolver = ResolverCache <TTypeResolver> .Default; object formatter = null; var t = typeof(T); var ti = t.GetTypeInfo(); try { // Primitive if (t == typeof(Int16)) { formatter = new Int16Formatter <TTypeResolver>(); } else if (t == typeof(Int32)) { formatter = new Int32Formatter <TTypeResolver>(); } else if (t == typeof(Int64)) { formatter = new Int64Formatter <TTypeResolver>(); } else if (t == typeof(UInt16)) { formatter = new UInt16Formatter <TTypeResolver>(); } else if (t == typeof(UInt32)) { formatter = new UInt32Formatter <TTypeResolver>(); } else if (t == typeof(UInt64)) { formatter = new UInt64Formatter <TTypeResolver>(); } else if (t == typeof(Single)) { formatter = new SingleFormatter <TTypeResolver>(); } else if (t == typeof(Double)) { formatter = new DoubleFormatter <TTypeResolver>(); } else if (t == typeof(bool)) { formatter = new BooleanFormatter <TTypeResolver>(); } else if (t == typeof(byte)) { formatter = new ByteFormatter <TTypeResolver>(); } else if (t == typeof(sbyte)) { formatter = new SByteFormatter <TTypeResolver>(); } else if (t == typeof(decimal)) { formatter = new DecimalFormatter <TTypeResolver>(); } else if (t == typeof(TimeSpan)) { formatter = new TimeSpanFormatter <TTypeResolver>(); } else if (t == typeof(DateTime)) { formatter = new DateTimeFormatter <TTypeResolver>(); } else if (t == typeof(DateTimeOffset)) { formatter = new DateTimeOffsetFormatter <TTypeResolver>(); } else if (t == typeof(Guid)) { formatter = new GuidFormatter <TTypeResolver>(); } // Nulllable else if (t == typeof(Nullable <Int16>)) { formatter = new NullableInt16Formatter <TTypeResolver>(); } else if (t == typeof(Nullable <Int32>)) { formatter = new NullableInt32Formatter <TTypeResolver>(); } else if (t == typeof(Nullable <Int64>)) { formatter = new NullableInt64Formatter <TTypeResolver>(); } else if (t == typeof(Nullable <UInt16>)) { formatter = new NullableUInt16Formatter <TTypeResolver>(); } else if (t == typeof(Nullable <UInt32>)) { formatter = new NullableUInt32Formatter <TTypeResolver>(); } else if (t == typeof(Nullable <UInt64>)) { formatter = new NullableUInt64Formatter <TTypeResolver>(); } else if (t == typeof(Nullable <Single>)) { formatter = new NullableSingleFormatter <TTypeResolver>(); } else if (t == typeof(Nullable <Double>)) { formatter = new NullableDoubleFormatter <TTypeResolver>(); } else if (t == typeof(Nullable <bool>)) { formatter = new NullableBooleanFormatter <TTypeResolver>(); } else if (t == typeof(Nullable <byte>)) { formatter = new NullableByteFormatter <TTypeResolver>(); } else if (t == typeof(Nullable <sbyte>)) { formatter = new NullableSByteFormatter <TTypeResolver>(); } else if (t == typeof(Nullable <decimal>)) { formatter = new NullableDecimalFormatter <TTypeResolver>(); } else if (t == typeof(Nullable <TimeSpan>)) { formatter = new NullableTimeSpanFormatter <TTypeResolver>(); } else if (t == typeof(Nullable <DateTime>)) { formatter = new NullableDateTimeFormatter <TTypeResolver>(); } else if (t == typeof(Nullable <DateTimeOffset>)) { formatter = new NullableDateTimeOffsetFormatter <TTypeResolver>(); } else if (t == typeof(Guid?)) { formatter = new NullableGuidFormatter <TTypeResolver>(); } // Others else if (t == typeof(String)) { formatter = new NullableStringFormatter <TTypeResolver>(); } else if (t == typeof(Char)) { formatter = new CharFormatter <TTypeResolver>(); } else if (t == typeof(Char?)) { formatter = new NullableCharFormatter <TTypeResolver>(); } else if (t == typeof(IList <int>)) // known generic formatter... { formatter = new ListFormatter <TTypeResolver, int>(); } else if (t.IsArray) { var elementType = t.GetElementType(); switch (Type.GetTypeCode(elementType)) { case TypeCode.Boolean: formatter = new BooleanArrayFormatter <TTypeResolver>(); break; case TypeCode.Char: formatter = new CharArrayFormatter <TTypeResolver>(); break; case TypeCode.SByte: formatter = new SByteArrayFormatter <TTypeResolver>(); break; case TypeCode.Byte: formatter = new ByteArrayFormatter <TTypeResolver>(); break; case TypeCode.Int16: formatter = new Int16ArrayFormatter <TTypeResolver>(); break; case TypeCode.UInt16: formatter = new UInt16ArrayFormatter <TTypeResolver>(); break; case TypeCode.Int32: formatter = new Int32ArrayFormatter <TTypeResolver>(); break; case TypeCode.UInt32: formatter = new UInt32ArrayFormatter <TTypeResolver>(); break; case TypeCode.Int64: formatter = new Int64ArrayFormatter <TTypeResolver>(); break; case TypeCode.UInt64: formatter = new UInt64ArrayFormatter <TTypeResolver>(); break; case TypeCode.Single: formatter = new SingleArrayFormatter <TTypeResolver>(); break; case TypeCode.Double: formatter = new DoubleArrayFormatter <TTypeResolver>(); break; default: #if !UNITY var formatterType = typeof(ArrayFormatter <,>).MakeGenericType(typeof(TTypeResolver), elementType); formatter = Activator.CreateInstance(formatterType); #endif break; } } #if !UNITY else if (ti.IsGenericType && t.GetGenericTypeDefinition() == typeof(IList <>)) { var formatterType = typeof(ListFormatter <,>).MakeGenericType(ti.GetGenericArguments().StartsWith(typeof(TTypeResolver))); formatter = Activator.CreateInstance(formatterType); } else if (ti.IsGenericType && t.GetGenericTypeDefinition() == typeof(IReadOnlyList <>)) { var formatterType = typeof(ReadOnlyListFormatter <,>).MakeGenericType(ti.GetGenericArguments().StartsWith(typeof(TTypeResolver))); formatter = Activator.CreateInstance(formatterType); } else if (ti.IsEnum) { var underlyingType = Enum.GetUnderlyingType(t); switch (Type.GetTypeCode(underlyingType)) { case TypeCode.SByte: formatter = new SByteEnumFormatter <TTypeResolver, T>(); break; case TypeCode.Byte: formatter = new ByteEnumFormatter <TTypeResolver, T>(); break; case TypeCode.Int16: formatter = new Int16EnumFormatter <TTypeResolver, T>(); break; case TypeCode.UInt16: formatter = new UInt16EnumFormatter <TTypeResolver, T>(); break; case TypeCode.Int32: formatter = new Int32EnumFormatter <TTypeResolver, T>(); break; case TypeCode.UInt32: formatter = new UInt32EnumFormatter <TTypeResolver, T>(); break; case TypeCode.Int64: formatter = new Int64EnumFormatter <TTypeResolver, T>(); break; case TypeCode.UInt64: formatter = new UInt64EnumFormatter <TTypeResolver, T>(); break; default: throw new Exception(); } } else if (ti.IsGenericType && t.GetGenericTypeDefinition() == typeof(Nullable <>) && ti.GetGenericArguments()[0].GetTypeInfo().IsEnum) { var underlyingType = Enum.GetUnderlyingType(ti.GetGenericArguments()[0]); switch (Type.GetTypeCode(underlyingType)) { case TypeCode.SByte: { var formatterType = typeof(NullableSByteEnumFormatter <,>).MakeGenericType(ti.GetGenericArguments().StartsWith(typeof(TTypeResolver))); formatter = Activator.CreateInstance(formatterType); } break; case TypeCode.Byte: { var formatterType = typeof(NullableByteEnumFormatter <,>).MakeGenericType(ti.GetGenericArguments().StartsWith(typeof(TTypeResolver))); formatter = Activator.CreateInstance(formatterType); } break; case TypeCode.Int16: { var formatterType = typeof(NullableInt16EnumFormatter <,>).MakeGenericType(ti.GetGenericArguments().StartsWith(typeof(TTypeResolver))); formatter = Activator.CreateInstance(formatterType); } break; case TypeCode.UInt16: { var formatterType = typeof(NullableUInt16EnumFormatter <,>).MakeGenericType(ti.GetGenericArguments().StartsWith(typeof(TTypeResolver))); formatter = Activator.CreateInstance(formatterType); } break; case TypeCode.Int32: { var formatterType = typeof(NullableInt32EnumFormatter <,>).MakeGenericType(ti.GetGenericArguments().StartsWith(typeof(TTypeResolver))); formatter = Activator.CreateInstance(formatterType); } break; case TypeCode.UInt32: { var formatterType = typeof(NullableUInt32EnumFormatter <,>).MakeGenericType(ti.GetGenericArguments().StartsWith(typeof(TTypeResolver))); formatter = Activator.CreateInstance(formatterType); } break; case TypeCode.Int64: { var formatterType = typeof(NullableInt64EnumFormatter <,>).MakeGenericType(ti.GetGenericArguments().StartsWith(typeof(TTypeResolver))); formatter = Activator.CreateInstance(formatterType); } break; case TypeCode.UInt64: { var formatterType = typeof(NullableUInt64EnumFormatter <,>).MakeGenericType(ti.GetGenericArguments().StartsWith(typeof(TTypeResolver))); formatter = Activator.CreateInstance(formatterType); } break; default: throw new Exception(); } } else if (ti.IsGenericType) { if (t.GetGenericTypeDefinition() == typeof(IDictionary <,>)) { var formatterType = typeof(InterfaceDictionaryFormatter <, ,>).MakeGenericType(ti.GetGenericArguments().StartsWith(typeof(TTypeResolver))); formatter = Activator.CreateInstance(formatterType); } else if (t.GetGenericTypeDefinition() == typeof(ILazyDictionary <,>)) { var formatterType = typeof(LazyDictionaryFormatter <, ,>).MakeGenericType(ti.GetGenericArguments().StartsWith(typeof(TTypeResolver))); formatter = Activator.CreateInstance(formatterType); } else if (t.GetGenericTypeDefinition() == typeof(IReadOnlyDictionary <,>)) { var formatterType = typeof(InterfaceReadOnlyDictionaryFormatter <, ,>).MakeGenericType(ti.GetGenericArguments().StartsWith(typeof(TTypeResolver))); formatter = Activator.CreateInstance(formatterType); } else if (t.GetGenericTypeDefinition() == typeof(ILazyReadOnlyDictionary <,>)) { var formatterType = typeof(LazyReadOnlyDictionaryFormatter <, ,>).MakeGenericType(ti.GetGenericArguments().StartsWith(typeof(TTypeResolver))); formatter = Activator.CreateInstance(formatterType); } else if (t.GetGenericTypeDefinition() == typeof(DictionaryEntry <, ,>)) { var formatterType = typeof(DictionaryEntryFormatter <, ,>).MakeGenericType(ti.GetGenericArguments()); formatter = Activator.CreateInstance(formatterType); } else if (t.GetGenericTypeDefinition() == typeof(ILookup <,>)) { var formatterType = typeof(LookupFormatter <, ,>).MakeGenericType(ti.GetGenericArguments().StartsWith(typeof(TTypeResolver))); formatter = Activator.CreateInstance(formatterType); } else if (t.GetGenericTypeDefinition() == typeof(ILazyLookup <,>)) { var formatterType = typeof(LazyLookupFormatter <, ,>).MakeGenericType(ti.GetGenericArguments().StartsWith(typeof(TTypeResolver))); formatter = Activator.CreateInstance(formatterType); } else if (t.GetGenericTypeDefinition() == typeof(GroupingSegment <, ,>)) { var formatterType = typeof(GroupingSegmentFormatter <, ,>).MakeGenericType(ti.GetGenericArguments()); formatter = Activator.CreateInstance(formatterType); } else if (ti.GetGenericTypeDefinition() == typeof(KeyValuePair <,>)) { var formatterType = typeof(KeyValuePairFormatter <, ,>).MakeGenericType(ti.GetGenericArguments().StartsWith(typeof(TTypeResolver))); formatter = Activator.CreateInstance(formatterType); } else if (t.GetGenericTypeDefinition() == typeof(Dictionary <,>)) { var formatterType = typeof(DictionaryFormatter <, ,>).MakeGenericType(ti.GetGenericArguments().StartsWith(typeof(TTypeResolver))); formatter = Activator.CreateInstance(formatterType); } else if (t.GetGenericTypeDefinition() == typeof(ReadOnlyDictionary <,>)) { var formatterType = typeof(ReadOnlyDictionaryFormatter <, ,>).MakeGenericType(ti.GetGenericArguments().StartsWith(typeof(TTypeResolver))); formatter = Activator.CreateInstance(formatterType); } else if (t.GetGenericTypeDefinition() == typeof(ReadOnlyCollection <>)) { var formatterType = typeof(ReadOnlyCollectionFormatter <,>).MakeGenericType(ti.GetGenericArguments().StartsWith(typeof(TTypeResolver))); formatter = Activator.CreateInstance(formatterType); } else if (t.GetGenericTypeDefinition() == typeof(ICollection <>)) { var formatterType = typeof(InterfaceCollectionFormatter <,>).MakeGenericType(ti.GetGenericArguments().StartsWith(typeof(TTypeResolver))); formatter = Activator.CreateInstance(formatterType); } else if (t.GetGenericTypeDefinition() == typeof(IEnumerable <>)) { var formatterType = typeof(InterfaceEnumerableFormatter <,>).MakeGenericType(ti.GetGenericArguments().StartsWith(typeof(TTypeResolver))); formatter = Activator.CreateInstance(formatterType); } else if (t.GetGenericTypeDefinition() == typeof(ISet <>)) { var formatterType = typeof(InterfaceSetFormatter <,>).MakeGenericType(ti.GetGenericArguments().StartsWith(typeof(TTypeResolver))); formatter = Activator.CreateInstance(formatterType); } else if (t.GetGenericTypeDefinition() == typeof(IReadOnlyCollection <>)) { var formatterType = typeof(InterfaceReadOnlyCollectionFormatter <,>).MakeGenericType(ti.GetGenericArguments().StartsWith(typeof(TTypeResolver))); formatter = Activator.CreateInstance(formatterType); } else if (t.GetGenericTypeDefinition() == typeof(IReadOnlyDictionary <,>)) { var formatterType = typeof(InterfaceReadOnlyDictionaryFormatter <, ,>).MakeGenericType(ti.GetGenericArguments().StartsWith(typeof(TTypeResolver))); formatter = Activator.CreateInstance(formatterType); } else if (ti.FullName.StartsWith("System.Tuple")) { Type tupleFormatterType = null; switch (ti.GetGenericArguments().Length) { case 1: tupleFormatterType = typeof(TupleFormatter <,>); break; case 2: tupleFormatterType = typeof(TupleFormatter <, ,>); break; case 3: tupleFormatterType = typeof(TupleFormatter <, , ,>); break; case 4: tupleFormatterType = typeof(TupleFormatter <, , , ,>); break; case 5: tupleFormatterType = typeof(TupleFormatter <, , , , ,>); break; case 6: tupleFormatterType = typeof(TupleFormatter <, , , , , ,>); break; case 7: tupleFormatterType = typeof(TupleFormatter <, , , , , , ,>); break; case 8: tupleFormatterType = typeof(TupleFormatter <, , , , , , , ,>); break; default: break; } var formatterType = tupleFormatterType.MakeGenericType(ti.GetGenericArguments().StartsWith(typeof(TTypeResolver))); formatter = Activator.CreateInstance(formatterType); } else if (ti.GetInterfaces().Any(x => x == typeof(IKeyTuple))) { Type tupleFormatterType = null; switch (ti.GetGenericArguments().Length) { case 1: tupleFormatterType = typeof(KeyTupleFormatter <,>); break; case 2: tupleFormatterType = typeof(KeyTupleFormatter <, ,>); break; case 3: tupleFormatterType = typeof(KeyTupleFormatter <, , ,>); break; case 4: tupleFormatterType = typeof(KeyTupleFormatter <, , , ,>); break; case 5: tupleFormatterType = typeof(KeyTupleFormatter <, , , , ,>); break; case 6: tupleFormatterType = typeof(KeyTupleFormatter <, , , , , ,>); break; case 7: tupleFormatterType = typeof(KeyTupleFormatter <, , , , , , ,>); break; case 8: tupleFormatterType = typeof(KeyTupleFormatter <, , , , , , , ,>); break; default: break; } var formatterType = tupleFormatterType.MakeGenericType(ti.GetGenericArguments().StartsWith(typeof(TTypeResolver))); formatter = Activator.CreateInstance(formatterType); } else if (ti.GetInterfaces().Any(x => { var iti = x.GetTypeInfo(); if (iti.IsGenericType && iti.GetGenericTypeDefinition() == typeof(ICollection <>)) { return(true); } return(false); })) { var formatterType = typeof(CollectionFormatter <, ,>).MakeGenericType(typeof(TTypeResolver), ti.GetGenericArguments()[0], t); formatter = Activator.CreateInstance(formatterType); } // custom nullable struct else if (t.GetGenericTypeDefinition() == typeof(Nullable <>) && ti.GetGenericArguments()[0].GetTypeInfo().IsValueType) { if (resolver.IsUseBuiltinDynamicSerializer) { formatter = DynamicStructFormatter.Create <TTypeResolver, T>(); } } } else if (ti.GetCustomAttributes(typeof(UnionAttribute), false).FirstOrDefault() != null) { if (resolver.IsUseBuiltinDynamicSerializer) { formatter = DynamicUnionFormatter.Create <TTypeResolver, T>(); } } else if (ti.GetCustomAttributes(typeof(DynamicUnionAttribute), false).FirstOrDefault() != null) { var unionResolver = new DynamicUnionResolver(); resolver.RegisterDynamicUnion(t, unionResolver); if (unionResolver.isStartRegister) { formatter = DynamicUnionFormatter.CreateRuntimeUnion <TTypeResolver, T>(unionResolver); } } else if (ti.GetCustomAttributes(typeof(ZeroFormattableAttribute), true).FirstOrDefault() != null) { if (resolver.IsUseBuiltinDynamicSerializer) { if (ti.IsValueType) { formatter = DynamicStructFormatter.Create <TTypeResolver, T>(); } else { formatter = DynamicFormatter.Create <TTypeResolver, T>(); } } } #endif } catch (InvalidOperationException ex) { formatter = new ErrorFormatter <TTypeResolver, T>(ex); } catch (Exception ex) { formatter = new ErrorFormatter <TTypeResolver, T>(ex); } // resolver if (formatter == null || formatter is ErrorFormatter <TTypeResolver, T> ) { try { var resolvedFormatter = resolver.ResolveFormatter(typeof(T)); if (resolvedFormatter != null) { formatter = resolvedFormatter; } else if (formatter == null) { formatter = new ErrorFormatter <TTypeResolver, T>(); } } catch (Exception ex) { formatter = new ErrorFormatter <TTypeResolver, T>(ex); } } Default = (Formatter <TTypeResolver, T>)formatter; }
public NullableKeyTupleFormatter() { this.formatter1 = Formatter <T1> .Default; }
public ReadOnlyListFormatter() { this.formatter = Formatter <T> .Default; }
public InterfaceReadOnlyCollectionFormatter() { this.formatter = Formatter <T> .Default; this.formatterLength = formatter.GetLength(); }
public NullableStructFormatter(Formatter <T> innerFormatter) { this.innerFormatter = innerFormatter; }
public InterfaceReadOnlyDictionaryFormatter() { this.formatter = Formatter <KeyValuePair <TKey, TValue> > .Default; }
public static void Register(Formatter <T> formatter) { defaultFormatter = formatter; }