JSON.NET - read-only properties & support for IgnoreDataMember

前端 未结 1 1082
佛祖请我去吃肉
佛祖请我去吃肉 2021-01-14 00:47

Does JSON.NET support the IgnoreDataMember attribute or do I have to use JsonIgnore instead? Is this something that will be support in future?

相关标签:
1条回答
  • 2021-01-14 01:30

    Answering my own question, but thought it may be helpful to others...

    We ended up implementing this using a custom IContractResolver. We want the functionality of the DefaultContractResolver so we derive from that then tweak CreateProperty to ignore things we don't really care to serialise. E.g.

        internal class IgnoreDataMemberContractResolver : DefaultContractResolver
        {
            protected override JsonProperty CreateProperty(MemberInfo member, MemberSerialization memberSerialization)
            {
                JsonProperty property =  base.CreateProperty(member, memberSerialization);
                property.Ignored |= member.GetCustomAttributes(typeof(IgnoreDataMemberAttribute), true).Length > 0;
                return property;
            }
        }
    
    0 讨论(0)
提交回复
热议问题