Make a custom contract resolver:
public class ShouldSerializeContractResolver : DefaultContractResolver
{
public static ShouldSerializeContractResolver Instance { get; } = new ShouldSerializeContractResolver();
protected override JsonProperty CreateProperty(MemberInfo member, MemberSerialization memberSerialization)
{
JsonProperty property = base.CreateProperty(member, memberSerialization);
if (typeof(Account).IsAssignableFrom(member.DeclaringType) && member.Name == nameof(Account.PasswordHash))
{
property.Ignored = true;
}
return property;
}
}
How I test it:
var account = new Account
{
PasswordHash = "XXAABB"
};
var settings = new JsonSerializerSettings
{
ContractResolver = ShouldSerializeContractResolver.Instance
};
var json = JsonConvert.SerializeObject(account, settings);
Console.WriteLine(json);
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…