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; }
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 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 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>(); }