System.Text.Json在运行时添加JsonIgnore属性

3
我可以帮您翻译以下内容,如下所示:

我正在寻找一种使用System.Text.Json序列化对象的方法,但某些属性基于其他属性值被忽略。

例如:

public class Data
{
   public string Type { get; set; }
   public string A { get; set; }
   public string B { get; set; }
   public string BC { get; set; }
}

我使用这些数据填充了该类

var data = new Data
{
   Type = "A",
   A = "data A",
   B = "data B",
   BC = "data BC"
};

Type的值为A时,我想序列化属性TypeA
Type的值为B时,我想序列化属性TypeBBC
以此类推。
有没有办法做到这一点?
我以前用自定义属性制作过Xml版本,但在搜索System.Text.Json解决方案时,没有找到任何内容。
以下是我的Xml版本:
public static bool HasAttribute<TAttribute>(
            this PropertyInfo prop)
            where TAttribute : Attribute
{
    return prop.GetCustomAttributes(typeof(TAttribute), false).Any();
}

public static TValue GetAttributeValue<TAttribute, TValue>(
            this PropertyInfo prop,
            Func<TAttribute, TValue> valueSelector)
            where TAttribute : Attribute
{
    var att = prop.GetCustomAttributes(typeof(TAttribute), false).FirstOrDefault() as TAttribute;
    if (att != null)
    {
        return valueSelector(att);
    }
    return default(TValue);
}

private void GenerateAttributeOverrides<T>(string type, XmlAttributeOverrides attributeOverrides, PropertyInfo[] properties)
{
    foreach (var prop in properties)
    {
        var attributes = new XmlAttributes();
        if (prop.HasAttribute<ShouldSerializeAttribute>())
        {
            var acceptedTypes = prop.GetAttributeValue<ShouldSerializeAttribute, string[]>(x => x.AcceptedTypes);
            if (acceptedTypes.Contains(type))
            {
                attributes.XmlElements.Add(new XmlElementAttribute(prop.Name, prop.PropertyType);
            }
            else
            {
                attributes.XmlIgnore = true;
            }
        }
    }

thanks in advance.


5
你可能需要查看自定义转换器。 - undefined
@pinkfloydx33 你对自定义转换器有什么了解吗?有没有相关的示例呢? - undefined
只需谷歌一下。示例可以在System.Text.Json的主要文档页面中找到。 - undefined
你正在使用.NET 5还是.NET Core 3.x? - undefined
@dbc 是的,我正在使用 NET 5 - undefined
1个回答

3

在查看自定义转换器后,我已经找到了答案。

这是我的转换器

public class DataConverter : JsonConverter<Data>
{
    public override Data Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
    {
        throw new NotImplementedException();
    }

    public override void Write(Utf8JsonWriter writer, Data value, JsonSerializerOptions options)
    {
        var properties = value.GetType().GetProperties();

        writer.WriteStartObject();

        foreach (var prop in properties)
        {
            if (prop.HasAttribute<ShouldSerializeAttribute>())
            {
                var acceptedTypes = prop.GetAttributeValue<ShouldSerializeAttribute, string[]>(x => x.AcceptedTypes);
                if (acceptedTypes != null && acceptedTypes.Contains(value.Type))
                {
                    var propValue = prop.GetValue(value);
                    writer.WritePropertyName(prop.Name);
                    JsonSerializer.Serialize(writer, propValue, prop.PropertyType, options);
                }
            }
        }

        writer.WriteEndObject();
    }
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接