/// <summary> /// Creates a serializer for the given type and settings. /// </summary> /// <typeparam name="TExample">The user type to serialize.</typeparam> /// <param name="settings"></param> /// <returns></returns> public static IVowpalWabbitSerializerCompiler <TExample> CreateSerializer <TExample>(VowpalWabbitSettings settings = null) { Schema schema = null; Type cacheKey = null; if (settings != null && settings.Schema != null) { schema = settings.Schema; } else { // only cache non-string generating serializer if (!settings.EnableStringExampleGeneration) { cacheKey = typeof(TExample); object serializer; if (SerializerCache.TryGetValue(cacheKey, out serializer)) { return((IVowpalWabbitSerializerCompiler <TExample>)serializer); } } ITypeInspector typeInspector = settings.TypeInspector; if (typeInspector == null) { typeInspector = TypeInspector.Default; } // TODO: enhance caching based on feature list & featurizer set // if no feature mapping is provided, use [Feature] annotation on provided type. schema = typeInspector.CreateSchema(settings, typeof(TExample)); var multiExampleSerializerCompiler = VowpalWabbitMultiExampleSerializerCompiler.TryCreate <TExample>(settings, schema); if (multiExampleSerializerCompiler != null) { return(multiExampleSerializerCompiler); } } // need at least a single feature to do something sensible if (schema == null || schema.Features.Count == 0) { return(null); } var newSerializer = new VowpalWabbitSingleExampleSerializerCompiler <TExample>( schema, settings == null ? null : settings.CustomFeaturizer, !settings.EnableStringExampleGeneration); if (cacheKey != null) { SerializerCache[cacheKey] = newSerializer; } return(newSerializer); }