<PackageReference Include="NJsonSchema" Version="11.0.0" />

SystemTextJsonReflectionService

using Namotion.Reflection; using System; using System.Collections.Generic; using System.Linq; using System.Reflection; using System.Runtime.CompilerServices; using System.Runtime.Serialization; using System.Text.Json; using System.Text.Json.Serialization; namespace NJsonSchema.Generation { [System.Runtime.CompilerServices.NullableContext(1)] [System.Runtime.CompilerServices.Nullable(new byte[] { 0, 1 })] public class SystemTextJsonReflectionService : ReflectionServiceBase<SystemTextJsonSchemaGeneratorSettings> { public override void GenerateProperties(JsonSchema schema, ContextualType contextualType, SystemTextJsonSchemaGeneratorSettings settings, JsonSchemaGenerator schemaGenerator, JsonSchemaResolver schemaResolver) { foreach (ContextualAccessorInfo item in contextualType.get_Properties().OfType<ContextualAccessorInfo>().Concat((IEnumerable<ContextualAccessorInfo>)contextualType.get_Fields())) { if (!(item.get_MemberInfo().DeclaringType != contextualType.get_Type())) { FieldInfo fieldInfo = item.get_MemberInfo() as FieldInfo; if ((object)fieldInfo == null || (!fieldInfo.IsPrivate && !fieldInfo.IsStatic && CustomAttributeExtensions.IsDefined(fieldInfo, typeof(DataMemberAttribute)))) { PropertyInfo propertyInfo = item.get_MemberInfo() as PropertyInfo; if (((object)propertyInfo == null || (!(propertyInfo.GetMethod?.IsPrivate ?? false) && !(propertyInfo.GetMethod?.IsStatic ?? false)) || (!(propertyInfo.SetMethod?.IsPrivate ?? false) && !(propertyInfo.SetMethod?.IsStatic ?? false)) || CustomAttributeExtensions.IsDefined(propertyInfo, typeof(DataMemberAttribute))) && (!(item.get_Name() == "EqualityContract") || !AttributeExtensions.IsAttributeDefined<CompilerGeneratedAttribute>(item, true))) { bool flag = false; Attribute attribute = EnumerableExtensions.FirstAssignableToTypeNameOrDefault<Attribute>(AttributeExtensions.GetAttributes(item, true), "System.Text.Json.Serialization.JsonIgnoreAttribute", 1); if (attribute != null) { object obj = ObjectExtensions.TryGetPropertyValue<object>((object)attribute, "Condition", (object)null); if (obj == null || obj.ToString() == "Always") flag = true; } if (!flag && !schemaGenerator.IsPropertyIgnoredBySettings(item) && EnumerableExtensions.FirstAssignableToTypeNameOrDefault<Attribute>(AttributeExtensions.GetAttributes(item, true), "System.Text.Json.Serialization.JsonExtensionDataAttribute", 1) == null) { JsonTypeDescription description = GetDescription(item.get_AccessorType(), settings.DefaultReferenceTypeNullHandling, settings); string propertyName = GetPropertyName(item, settings); if (schema.Properties.ContainsKey(propertyName)) { if (!settings.GetActualFlattenInheritanceHierarchy(contextualType.get_Type())) throw new InvalidOperationException("The JSON property '" + propertyName + "' is defined multiple times on type '" + contextualType.get_Type().FullName + "'."); schema.Properties.Remove(propertyName); } Attribute attribute2 = EnumerableExtensions.FirstAssignableToTypeNameOrDefault<Attribute>(AttributeExtensions.GetAttributes(item, true), "System.ComponentModel.DataAnnotations.RequiredAttribute", 1); dynamic val = schemaGenerator.GetDataMemberAttribute(item, contextualType.get_Type())?.IsRequired == true; bool flag2 = attribute2 != null; if (flag2 || ((flag2 | val) ? true : false)) schema.RequiredProperties.Add(propertyName); bool isNullable = description.IsNullable && !flag2; schemaGenerator.AddProperty(schema, item, description, propertyName, attribute2, flag2, isNullable, null, schemaResolver); } } } } } } public override bool IsStringEnum(ContextualType contextualType, SystemTextJsonSchemaGeneratorSettings settings) { if (!settings.SerializerOptions.Converters.OfType<JsonStringEnumConverter>().Any()) return base.IsStringEnum(contextualType, settings); return true; } [return: System.Runtime.CompilerServices.Nullable(new byte[] { 1, 1, 2 })] public override Func<object, string> GetEnumValueConverter(SystemTextJsonSchemaGeneratorSettings settings) { JsonSerializerOptions serializerOptions = new JsonSerializerOptions(); foreach (JsonConverter converter in settings.SerializerOptions.Converters) { serializerOptions.Converters.Add(converter); } if (!serializerOptions.Converters.OfType<JsonStringEnumConverter>().Any()) serializerOptions.Converters.Add(new JsonStringEnumConverter()); return (object x) => JsonSerializer.Deserialize<string>(JsonSerializer.Serialize(x, x.GetType(), serializerOptions), (JsonSerializerOptions)null); } public override string GetPropertyName(ContextualAccessorInfo accessorInfo, JsonSchemaGeneratorSettings settings) { return GetPropertyName(accessorInfo, (SystemTextJsonSchemaGeneratorSettings)settings); } private static string GetPropertyName(ContextualAccessorInfo accessorInfo, SystemTextJsonSchemaGeneratorSettings settings) { dynamic val = EnumerableExtensions.FirstAssignableToTypeNameOrDefault<Attribute>(AttributeExtensions.GetAttributes(accessorInfo, true), "System.Text.Json.Serialization.JsonPropertyNameAttribute", 1); if ((!string.IsNullOrEmpty(val?.Name))) return (string)val.Name; if (settings.SerializerOptions.PropertyNamingPolicy != null) return settings.SerializerOptions.PropertyNamingPolicy.ConvertName(accessorInfo.get_Name()); return accessorInfo.get_Name(); } } }