Exemplo n.º 1
0
        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);
        }
        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);
        }