CompositeContractResolver.cs 1.1 KB

12345678910111213141516171819202122232425262728
  1. using Newtonsoft.Json;
  2. using Newtonsoft.Json.Serialization;
  3. using System.Linq;
  4. using System.Reflection;
  5. namespace Masuit.Tools.Systems;
  6. /// <summary>
  7. /// 支持只允许反序列化属性和多别名属性的解释器
  8. /// </summary>
  9. public class CompositeContractResolver : FallbackJsonPropertyResolver
  10. {
  11. protected override JsonProperty CreateProperty(MemberInfo member, MemberSerialization memberSerialization)
  12. {
  13. var property = base.CreateProperty(member, memberSerialization);
  14. if (property.AttributeProvider.GetAttributes(typeof(DeserializeOnlyJsonPropertyAttribute), true).Union(property.AttributeProvider.GetAttributes(typeof(SerializeIgnoreAttribute), true)).Any())
  15. {
  16. property.ShouldSerialize = _ => false;
  17. }
  18. if (property.AttributeProvider.GetAttributes(typeof(SerializeOnlyJsonPropertyAttribute), true).Union(property.AttributeProvider.GetAttributes(typeof(DeserializeIgnoreAttribute), true)).Any())
  19. {
  20. property.ShouldDeserialize = _ => false;
  21. }
  22. return property;
  23. }
  24. }