JSON.NET-只读属性&支持IgnoreDataMember [英] JSON.NET - read-only properties & support for IgnoreDataMember
问题描述
JSON.NET是否支持IgnoreDataMember
属性,还是我必须使用JsonIgnore
?将来会支持吗?
Does JSON.NET support the IgnoreDataMember
attribute or do I have to use JsonIgnore
instead? Is this something that will be support in future?
此外,我发现JSON.NET正在序列化仅获得的属性-这是预期的行为吗?我们可以在序列化器级别关闭它吗?
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...
我们最终使用自定义的IContractResolver
实施了此操作.我们想要DefaultContractResolver
的功能,因此我们从中派生然后调整CreateProperty
来忽略我们并不真正关心序列化的事情.例如
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;
}
}
这篇关于JSON.NET-只读属性&支持IgnoreDataMember的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!