SystemTextJsonReflectionService
public class SystemTextJsonReflectionService : ReflectionServiceBase<SystemTextJsonSchemaGeneratorSettings>
using Namotion.Reflection;
using System;
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.Properties.OfType<ContextualAccessorInfo>().Concat(contextualType.Fields)) {
if (!(item.MemberInfo.DeclaringType != contextualType.Type)) {
FieldInfo fieldInfo = item.MemberInfo as FieldInfo;
if ((object)fieldInfo == null || (!fieldInfo.IsPrivate && !fieldInfo.IsStatic && CustomAttributeExtensions.IsDefined(fieldInfo, typeof(DataMemberAttribute)))) {
PropertyInfo propertyInfo = item.MemberInfo as PropertyInfo;
if (((object)propertyInfo == null || (!(propertyInfo.GetMethod == (MethodInfo)null) && !propertyInfo.GetMethod.IsPrivate && !propertyInfo.GetMethod.IsStatic) || (!(propertyInfo.SetMethod == (MethodInfo)null) && !propertyInfo.SetMethod.IsPrivate && !propertyInfo.SetMethod.IsStatic) || CustomAttributeExtensions.IsDefined(propertyInfo, typeof(DataMemberAttribute))) && (!(item.Name == "EqualityContract") || !item.IsAttributeDefined<CompilerGeneratedAttribute>(true))) {
PropertyInfo propertyInfo2 = item.MemberInfo as PropertyInfo;
if ((object)propertyInfo2 == null || propertyInfo2.GetIndexParameters().Length == 0) {
bool flag = false;
Attribute[] objects = item.GetAttributes(true).ToArray();
Attribute attribute = objects.FirstAssignableToTypeNameOrDefault("System.Text.Json.Serialization.JsonIgnoreAttribute", TypeNameStyle.FullName);
if (attribute != null) {
object obj = attribute.TryGetPropertyValue<object>("Condition", null);
if (obj == null || obj.ToString() == "Always")
flag = true;
}
if (!flag && !schemaGenerator.IsPropertyIgnoredBySettings(item) && objects.FirstAssignableToTypeNameOrDefault("System.Text.Json.Serialization.JsonExtensionDataAttribute", TypeNameStyle.FullName) == null && Array.IndexOf(settings.ExcludedTypeNames, item.AccessorType.Type.FullName) == -1) {
JsonTypeDescription description = GetDescription(item.AccessorType, settings.DefaultReferenceTypeNullHandling, settings);
string propertyNameInternal = GetPropertyNameInternal(item, settings);
if (schema.Properties.ContainsKey(propertyNameInternal)) {
if (!settings.GetActualFlattenInheritanceHierarchy(contextualType.Type))
throw new InvalidOperationException("The JSON property '" + propertyNameInternal + "' is defined multiple times on type '" + contextualType.Type.FullName + "'.");
schema.Properties.Remove(propertyNameInternal);
}
Attribute attribute2 = objects.FirstAssignableToTypeNameOrDefault("System.ComponentModel.DataAnnotations.RequiredAttribute", TypeNameStyle.FullName);
dynamic val = schemaGenerator.GetDataMemberAttribute(item, contextualType.Type)?.IsRequired == true;
bool flag2 = attribute2 != null;
if (flag2 || ((flag2 | val) ? true : false))
schema.RequiredProperties.Add(propertyNameInternal);
bool isNullable = description.IsNullable && !flag2;
schemaGenerator.AddProperty(schema, item, description, propertyNameInternal, 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 GetPropertyNameInternal(accessorInfo, (SystemTextJsonSchemaGeneratorSettings)settings);
}
private static string GetPropertyNameInternal(ContextualAccessorInfo accessorInfo, SystemTextJsonSchemaGeneratorSettings settings)
{
dynamic val = accessorInfo.GetAttributes(true).FirstAssignableToTypeNameOrDefault("System.Text.Json.Serialization.JsonPropertyNameAttribute", TypeNameStyle.FullName);
if ((!string.IsNullOrEmpty(val?.Name)))
return (string)val.Name;
if (settings.SerializerOptions.PropertyNamingPolicy != null)
return settings.SerializerOptions.PropertyNamingPolicy.ConvertName(accessorInfo.Name);
return accessorInfo.Name;
}
}
}