You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

在System.Text.Json中序列化/反序列化时忽略JsonIgnore特性

在System.Text.Json中忽略[JsonIgnore]特性的解决方案

System.Text.Json没有Newtonsoft.Json里的ContractResolver,但可以通过两种方式实现忽略[JsonIgnore]特性,让标记了该特性的属性参与序列化和反序列化:

一、全局通用方案(类似ContractResolver的效果)

通过修改DefaultJsonTypeInfoResolver的元数据处理逻辑,全局忽略[JsonIgnore]的作用:

  1. 定义一个TypeInfo修改器方法,遍历属性并取消[JsonIgnore]的限制:
public void IgnoreJsonIgnoreAttribute(JsonTypeInfo typeInfo)
{
    if (typeInfo.Kind != JsonTypeInfoKind.Object)
        return;

    foreach (JsonPropertyInfo property in typeInfo.Properties)
    {
        // 检查属性是否标记了JsonIgnoreAttribute
        if (property.AttributeProvider?.GetCustomAttributes(typeof(JsonIgnoreAttribute), inherit: true).Any() == true)
        {
            // 强制允许序列化和反序列化
            property.ShouldSerialize = _ => true;
            property.ShouldDeserialize = _ => true;
        }
    }
}
  1. 在JsonSerializerOptions中配置这个修改器:
var jsonOptions = new JsonSerializerOptions
{
    TypeInfoResolver = new DefaultJsonTypeInfoResolver
    {
        Modifiers = { IgnoreJsonIgnoreAttribute }
    }
};

之后用这个jsonOptions进行序列化/反序列化,所有标记了[JsonIgnore]的属性都会被正常处理。

二、单个类专属方案(自定义JsonConverter)

如果只需要针对特定类处理,可以写一个自定义的JsonConverter,手动控制属性的序列化和反序列化:

假设目标类是:

public class MyTargetClass
{
    public string NormalProperty { get; set; }
    
    [JsonIgnore]
    public string IgnoredProperty { get; set; }
}

对应的转换器实现:

public class MyTargetClassConverter : JsonConverter<MyTargetClass>
{
    public override MyTargetClass Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
    {
        var instance = new MyTargetClass();
        using (JsonDocument doc = JsonDocument.ParseValue(ref reader))
        {
            foreach (JsonProperty prop in doc.RootElement.EnumerateObject())
            {
                switch (prop.Name)
                {
                    case nameof(MyTargetClass.NormalProperty):
                        instance.NormalProperty = prop.Value.GetString();
                        break;
                    case nameof(MyTargetClass.IgnoredProperty):
                        instance.IgnoredProperty = prop.Value.GetString();
                        break;
                }
            }
        }
        return instance;
    }

    public override void Write(Utf8JsonWriter writer, MyTargetClass value, JsonSerializerOptions options)
    {
        writer.WriteStartObject();
        writer.WriteString(nameof(MyTargetClass.NormalProperty), value.NormalProperty);
        writer.WriteString(nameof(MyTargetClass.IgnoredProperty), value.IgnoredProperty);
        writer.WriteEndObject();
    }
}

使用时将转换器加入配置:

var jsonOptions = new JsonSerializerOptions();
jsonOptions.Converters.Add(new MyTargetClassConverter());

这样只有MyTargetClass的IgnoredProperty会被正常序列化/反序列化,其他类不受影响。

内容的提问来源于stack exchange,提问作者Bart

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.13 19:58:24