Newtonsoft Json.NET do not serialize [Obsolete] property

Newtonsoft Json.NET do not serialize [Obsolete] property

回答1

Yes, you can use a custom ContractResolver to exclude properties marked obsolete.
Here is the code you would need for the resolver:

public class ExcludeObsoletePropertiesResolver : DefaultContractResolver
{
    protected override JsonProperty CreateProperty(MemberInfo member, MemberSerialization memberSerialization)
    {
        JsonProperty prop = base.CreateProperty(member, memberSerialization);
        if (prop.AttributeProvider.GetAttributes(true).OfType<ObsoleteAttribute>().Any())
        {
            prop.ShouldSerialize = obj => false;
        }
        return prop;
    }
}

Then use it like this:

var settings = new JsonSerializerSettings
{
    ContractResolver = new ExcludeObsoletePropertiesResolver()
};
var json = JsonConvert.SerializeObject(foo, settings);

Working demo here: https://dotnetfiddle.net/gIRCD4

 

How can I ignore the obsolete property when serialize object(third part) to json string using Json.Net? As we can not modify third party library, so JsonIgnoreAttribute can't be used in this situation

One way to ignore obsolete properties when serializing an object with Json.Net is to use a custom contract resolver. A contract resolver is a class that controls how Json.Net serializes and deserializes .NET objects.

Here is an example of how to create a custom contract resolver that ignores properties marked as obsolete:

using Newtonsoft.Json;
using Newtonsoft.Json.Serialization;
using System;
using System.Linq;

public class ObsoletePropertyContractResolver : DefaultContractResolver
{
    protected override JsonProperty CreateProperty(System.Reflection.MemberInfo member, MemberSerialization memberSerialization)
    {
        var property = base.CreateProperty(member, memberSerialization);
        
        if (Attribute.IsDefined(member, typeof(ObsoleteAttribute), true))
        {
            property.ShouldSerialize = instance => false;
        }
        
        return property;
    }
}

This custom contract resolver extends the DefaultContractResolver class, which is used by Json.Net to create property contracts for .NET types. In the CreateProperty method, the custom resolver checks if the member has an ObsoleteAttribute, and if so, sets the ShouldSerialize property of the JsonProperty to return false. This effectively ignores the obsolete property during serialization.

To use this custom contract resolver when serializing an object, you can create a new instance of JsonSerializerSettings and set the ContractResolver property to an instance of ObsoletePropertyContractResolver. Here's an example:

var obj = new MyObject(); // object to serialize
var settings = new JsonSerializerSettings
{
    ContractResolver = new ObsoletePropertyContractResolver()
};
var json = JsonConvert.SerializeObject(obj, settings);

In this example, the MyObject instance will be serialized to JSON using the JsonConvert.SerializeObject method, passing in an instance of JsonSerializerSettings with the ContractResolver set to an instance of ObsoletePropertyContractResolver. This will cause any properties marked with the ObsoleteAttribute to be ignored during serialization.

 

作者:Chuck Lu    GitHub    
posted @   ChuckLu  阅读(22)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
历史上的今天:
2021-02-23 gpg Sign your git commits with tortoise git on windows
2021-02-23 Github在双因子验证的时候Country- code 没有中国区的解决办法
2020-02-23 HearthBuddy调试 不知道使用大漩涡传送门
2019-02-23 Glossary
2018-02-23 Bootstrap 导航元素
2017-02-23 Innosetup
2017-02-23 重定向
点击右上角即可分享
微信分享提示