开发者

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

开发者 https://www.devze.com 2023-03-20 15:55 出处:网络
Does JSON.NET support the IgnoreDataMember attribute or do开发者_StackOverflow中文版 I have to use JsonIgnore instead? Is this something that will be support in future?

Does JSON.NET support the IgnoreDataMember attribute or do开发者_StackOverflow中文版 I have to use JsonIgnore instead? Is this something that will be support in future?

In addition I've found that JSON.NET is serializing properties that are get only - is this intended behaviour? Is it something we can switch off at serializer level?


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

精彩评论

暂无评论...
验证码 换一张
取 消