public EnumSerializerDecorator([NotNull] IGeneralSerializerProvider serializerProvider) { if (!typeof(TEnumType).GetTypeInfo().IsEnum) { throw new InvalidOperationException($"Cannot create an enum decorator for type {typeof(TEnumType).FullName} because it is not an enum."); } if (typeof(TEnumType).GetTypeInfo().GetEnumUnderlyingType() != typeof(TBaseType)) { throw new InvalidOperationException($"Defining an Enum decorator requires {nameof(TEnumType)}'s base enum type to match {nameof(TBaseType)}."); } if (serializerProvider == null) { throw new ArgumentNullException(nameof(serializerProvider), $"Provided service {nameof(IGeneralSerializerProvider)} was null."); } try { serializerStrategy = serializerProvider.Get <TBaseType>(); } catch (InvalidOperationException e) { throw new InvalidOperationException($"Failed to create strategy for Type: {typeof(TBaseType).FullName} for enum decorator for enum Type: {typeof(TEnumType).FullName}", e); } }
public ArraySerializerDecorator([NotNull] IGeneralSerializerProvider serializerProvider, [NotNull] ICollectionSizeStrategy sizeStrategy, SerializationContextRequirement contextReq) { if (serializerProvider == null) { throw new ArgumentNullException(nameof(serializerProvider), $"Provided {nameof(IGeneralSerializerProvider)} to needed to decorate was null."); } if (sizeStrategy == null) { throw new ArgumentNullException(nameof(sizeStrategy), $"Provided {nameof(ICollectionSizeStrategy)} to needed to decorate was null."); } if (!Enum.IsDefined(typeof(SerializationContextRequirement), contextReq)) { throw new ArgumentOutOfRangeException(nameof(contextReq), "Value should be defined in the SerializationContextRequirement enum."); } /*if (!Enum.IsDefined(typeof(SerializationContextRequirement), contextReq)) * throw new InvalidEnumArgumentException(nameof(contextReq), (int) contextReq, typeof(SerializationContextRequirement));*/ ContextRequirement = contextReq; try { decoratedSerializer = serializerProvider.Get <TObjectType>(); } catch (InvalidOperationException e) { throw new InvalidOperationException($"Failed to produce a serializer for Type: {typeof(TObjectType).FullName} in required {nameof(ArraySerializerDecorator<TObjectType>)}", e); } sizeStrategyService = sizeStrategy; }
protected ComplexTypeSerializer([NotNull] IEnumerable <IMemberSerializationMediator <TType> > serializationDirections, [NotNull] IGeneralSerializerProvider serializerProvider) { //These can be empty. If there are no members on a type there won't be anything to serialize. if (serializationDirections == null) { throw new ArgumentNullException(nameof(serializationDirections), $"Provided argument {nameof(serializationDirections)} is null."); } if (serializerProvider == null) { throw new ArgumentNullException(nameof(serializerProvider), $"Provided {nameof(serializerProvider)} service was null."); } //TODO: Extract enumerable into its own class an test there. try { //Enumerating the medaitors can throw exceptions. orderedMemberInfos = serializationDirections.ToArray(); } catch (Exception e) { throw new Exception($"Failed to create serializer {GetType().FullName} for Type: {typeof(TType).FullName}.", e); } serializerProviderService = serializerProvider; }
public static bool HasSerializerFor <TTypeToCheck>([NotNull] this IGeneralSerializerProvider provider) { if (provider == null) { throw new ArgumentNullException(nameof(provider)); } return(provider.HasSerializerFor(typeof(TTypeToCheck))); }
protected SubComplexTypeSerializer([NotNull] IDeserializationPrototypeFactory <TBaseType> prototypeGenerator, [NotNull] IEnumerable <IMemberSerializationMediator <TBaseType> > serializationDirections, [NotNull] IGeneralSerializerProvider serializerProvider) : base(serializationDirections, serializerProvider) { if (prototypeGenerator == null) { throw new ArgumentNullException(nameof(prototypeGenerator)); } this.prototypeGeneratorService = prototypeGenerator; }
public static ITypeSerializerStrategy <TRequestedType> Get <TRequestedType>([NotNull] this IGeneralSerializerProvider provider) { if (provider == null) { throw new ArgumentNullException(nameof(provider)); } ITypeSerializerStrategy <TRequestedType> strat = provider.Get(typeof(TRequestedType)) as ITypeSerializerStrategy <TRequestedType>; if (strat == null) { throw new InvalidOperationException($"Unabled to locate registered Type: {typeof(TRequestedType).FullName}. Make sure to properly register the type beforing requesting a serializer."); } return(strat); }
public SubComplexTypeWithFlagsSerializerDecorator([NotNull] IDeserializationPrototypeFactory <TBaseType> prototypeGenerator, [NotNull] IEnumerable <IMemberSerializationMediator <TBaseType> > serializationDirections, [NotNull] IGeneralSerializerProvider serializerProvider, [NotNull] IChildKeyStrategy childKeyStrategy) : base(prototypeGenerator, serializationDirections, serializerProvider) { if (prototypeGenerator == null) { throw new ArgumentNullException(nameof(prototypeGenerator)); } if (serializationDirections == null) { throw new ArgumentNullException(nameof(serializationDirections)); } if (serializerProvider == null) { throw new ArgumentNullException(nameof(serializerProvider)); } if (childKeyStrategy == null) { throw new ArgumentNullException(nameof(childKeyStrategy)); } keyStrategy = childKeyStrategy; DefaultSerializer = typeof(TBaseType).GetTypeInfo().GetCustomAttribute <DefaultChildAttribute>(false) != null ? serializerProviderService.Get(typeof(TBaseType).GetTypeInfo().GetCustomAttribute <DefaultChildAttribute>(false).ChildType) : null; //We no longer reserve 0. Sometimes type information of a child is sent as a 0 in WoW protocol. We can opt for mostly metadata market style interfaces. List <ChildKeyPair> pairs = new List <ChildKeyPair>(); foreach (WireDataContractBaseTypeByFlagsAttribute waf in typeof(TBaseType).GetTypeInfo().GetCustomAttributes <WireDataContractBaseTypeByFlagsAttribute>(false)) { if (!typeof(TBaseType).GetTypeInfo().IsAssignableFrom(waf.ChildType.GetTypeInfo())) { throw new InvalidOperationException($"Failed to register Type: {typeof(TBaseType).GetType().FullName} because a provided ChildType: {waf.ChildType.FullName} was not actually a child."); } //TODO: Maybe add a priority system for flags that may override others? pairs.Add(new ChildKeyPair(waf.Flag, serializerProviderService.Get(waf.ChildType))); } serializers = pairs.ToArray(); }
public GenericPrimitiveArraySerializerDecorator([NotNull] IGeneralSerializerProvider serializerProvider, [NotNull] ICollectionSizeStrategy sizeStrategy, SerializationContextRequirement contextReq) { if (serializerProvider == null) { throw new ArgumentNullException(nameof(serializerProvider), $"Provided {nameof(IGeneralSerializerProvider)} to needed to decorate was null."); } if (sizeStrategy == null) { throw new ArgumentNullException(nameof(sizeStrategy), $"Provided {nameof(ICollectionSizeStrategy)} to needed to decorate was null."); } if (!Enum.IsDefined(typeof(SerializationContextRequirement), contextReq)) { throw new ArgumentOutOfRangeException(nameof(contextReq), "Value should be defined in the SerializationContextRequirement enum."); } ContextRequirement = contextReq; SizeStrategyService = sizeStrategy; DecoratedSerializer = serializerProvider.Get <TType>(); }
public SubComplexTypeSerializerDecorator([NotNull] IDeserializationPrototypeFactory <TBaseType> prototypeGenerator, [NotNull] IEnumerable <IMemberSerializationMediator <TBaseType> > serializationDirections, [NotNull] IGeneralSerializerProvider serializerProvider, [NotNull] IChildKeyStrategy childKeyStrategy) : base(prototypeGenerator, serializationDirections, serializerProvider) { if (childKeyStrategy == null) { throw new ArgumentNullException(nameof(childKeyStrategy), $"Provided {nameof(IChildKeyStrategy)} used for key read and write is null."); } keyStrategy = childKeyStrategy; typeToKeyLookup = new Dictionary <Type, int>(); keyToTypeLookup = new Dictionary <int, Type>(); DefaultSerializer = typeof(TBaseType).GetTypeInfo().GetCustomAttribute <DefaultChildAttribute>(false) != null ? serializerProviderService.Get(typeof(TBaseType).GetTypeInfo().GetCustomAttribute <DefaultChildAttribute>(false).ChildType) : null; //We no longer reserve 0. Sometimes type information of a child is sent as a 0 in WoW protocol. We can opt for mostly metadata marker style interfaces. //TODO: Add support for basetype serialization metadata marking. foreach (WireDataContractBaseTypeAttribute wa in typeof(TBaseType).GetTypeInfo().GetCustomAttributes <WireDataContractBaseTypeAttribute>(false)) { if (wa.Index == keyStrategy.DefaultKey) { throw new InvalidOperationException($"Encountered reserved BaseType Key: {wa.Index} on Type: {wa.ChildType.Name}. This key value is reserved for internal handling."); } RegisterPair(wa.ChildType, wa.Index); } //If we're not abstract we may be asked to serialize ourselves if (!typeof(TBaseType).GetTypeInfo().IsAbstract) { InternallyManagedComplexSerializer = new ComplexTypeSerializerDecorator <TBaseType>(serializationDirections, prototypeGenerator, serializerProvider); } }
public ComplexTypeSerializerDecorator([NotNull] IEnumerable <IMemberSerializationMediator <TComplexType> > serializationDirections, [NotNull] IDeserializationPrototypeFactory <TComplexType> prototypeGenerator, [NotNull] IGeneralSerializerProvider serializerProvider) //todo: create a better way to provide serialization instructions : base(serializationDirections, serializerProvider) { if (prototypeGenerator == null) { throw new ArgumentNullException(nameof(prototypeGenerator)); } //We no longer require registered types to be classes //if(!typeof(TComplexType).GetTypeInfo().IsClass) // throw new ArgumentException($"Provided generic Type: {typeof(TComplexType).FullName} must be a erence type.", nameof(TComplexType)); prototypeGeneratorService = prototypeGenerator; //This serializer is the finally link the chain when it comes to polymorphic serialization //Theore it must deal with deserialization of all members by dispatching from top to bottom (this serializer) to read the members //to do so efficiently we must cache an array of the Type that represents the linear class hierarchy reversed List <Type> typeHierarchy = ComputeTypeHierarchy(typeof(TComplexType).GetTypeInfo(), typeof(object)); //reverse the collection to the proper order typeHierarchy.Reverse(); reversedInheritanceHierarchy = typeHierarchy; }