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 AddJsonKnown <T>(this DiscriminatorValues discriminatorValues) { var attrs = AttributesManager.GetJsonKnownAttributes(typeof(T)); foreach (var attr in attrs) { var discriminator = attr.Discriminator ?? attr.Type.Name; discriminatorValues.AddType(attr.Type, discriminator); } }
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 static void AddJsonKnownThis(this DiscriminatorValues discriminatorValues, Type[] inherited) { foreach (var type in inherited) { var attr = AttributesManager.GetJsonKnownThisAttribute(type); if (attr != null) { var discriminator = attr.Discriminator ?? type.Name; discriminatorValues.AddType(type, discriminator); } } }
internal static void AddJsonKnown <T>(this DiscriminatorValues discriminatorValues) { var attrs = AttributesManager.GetJsonKnownAttributes(typeof(T)); foreach (var attr in attrs) { var discriminator = attr.Discriminator ?? attr.Type.Name; discriminatorValues.AddType(attr.Type, discriminator); } var fallbackTypeAttribute = AttributesManager.GetJsonKnownTypeFallbackAttribute(typeof(T)); if (fallbackTypeAttribute != null) { discriminatorValues.AddFallbackType(fallbackTypeAttribute.FallbackType); } }
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 = GetFilteredDerived <T>(); typeSettings.AddJsonKnownThis(allTypes); if (discriminatorSettings.UseClassNameAsDiscriminator) { typeSettings.AddAutoDiscriminators(allTypes); } return(typeSettings); }