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.

 

posted @ 2023-02-23 17:06  ChuckLu  阅读(27)  评论(0)    收藏  举报