itfx/src/InterfaceForward.Application/Services/InterfaceMap/CustomContractResolver.cs
2025-08-01 10:21:04 +08:00

48 lines
1.5 KiB
C#

using System.Reflection;
using InterfaceForward.Domain.Shared.Dtos;
using Newtonsoft.Json;
using Newtonsoft.Json.Serialization;
namespace InterfaceForward.Application.Services.InterfaceMap;
internal class CustomContractResolver : DefaultContractResolver
{
protected override JsonProperty CreateProperty(MemberInfo member, MemberSerialization memberSerialization)
{
JsonProperty property = base.CreateProperty(member, memberSerialization);
if (ShouldIgnoreProperty(member))
{
property.ShouldSerialize = _ => false; // 设置不序列化的条件
}
return property;
}
protected override IList<JsonProperty> CreateProperties(Type type, MemberSerialization memberSerialization)
{
IList<JsonProperty> properties = base.CreateProperties(type, memberSerialization);
var children = properties.FirstOrDefault(x => x.PropertyName == nameof(ParameterNode.Children));
if (children != null)
{
properties.Remove(children);
properties.Add(children);
}
return properties;
}
private bool ShouldIgnoreProperty(MemberInfo member)
{
if (member.DeclaringType == typeof(InterfaceParameterMapDto))
{
if (member.Name.IsIn(nameof(InterfaceParameterMapDto.Id), nameof(InterfaceParameterMapDto.PId),
nameof(InterfaceParameterMapDto.InterfaceId),
nameof(InterfaceParameterMapDto.FreeMap))) return true;
}
// 其他判断逻辑...
return false;
}
}