internal static DiscriminatorValues GetDiscriminatorValues <T>() { var discriminatorAttribute = AttributesManager.GetJsonDiscriminatorAttribute(typeof(T)); var discriminatorSettings = discriminatorAttribute == null ? DefaultDiscriminatorSettings : Mapper.Map(discriminatorAttribute); var typeSettings = new DiscriminatorValues(discriminatorSettings.DiscriminatorFieldName); typeSettings.AddJsonKnown <T>(); var allTypes = GetAllInheritance <T>(); typeSettings.AddJsonKnownThis(allTypes); if (discriminatorSettings.UseClassNameAsDiscriminator) { typeSettings.AddAutoDiscriminators(allTypes); } else if (!allTypes.All(typeSettings.Contains)) { var missingTypes = allTypes.Where(x => !typeSettings.Contains(x)).Select(x => x.Name); throw new JsonKnownTypesException($"Not all classes registered for { nameof(T) } type hierarchy." + "Enable UseClassNameAsDiscriminator or add JsonKnown attributes for all classes." + $"Missing classes: { string.Join(", ", missingTypes) }"); } return(typeSettings); }
public static void AddAutoDiscriminators(this DiscriminatorValues discriminatorValues, Type[] inherited) { foreach (var type in inherited) { if (discriminatorValues.Contains(type)) { continue; } discriminatorValues.AddType(type, type.Name); } }
public override bool CanConvert(Type objectType) => TypesDiscriminatorValues.Contains(objectType);