首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >在使用System.Text.Json基于另一个属性写入对象时动态忽略属性

在使用System.Text.Json基于另一个属性写入对象时动态忽略属性
EN

Stack Overflow用户
提问于 2021-09-08 10:23:21
回答 1查看 80关注 0票数 1

如果另一个属性包含某个特定值,我需要能够序列化一个类并动态忽略某些属性(而不是将它们写到JSON中)。

因此,想象一下下面的类:

代码语言:javascript
运行
复制
public class MyClass
{
    public List<string> Types { get; set; }
    public string PropertyValidForType1 {get; set;}
    public string PropertyValidForType2 {get; set;}
    public string PropertyValidForType2 {get; set;}
}

如果Type1Types列表中有一个字符串,我希望将该属性序列化为JSON字符串,PropertyValidForType2PropertyValidForType2也是如此。

它们将是许多不需要序列化的属性,因此能够使用属性来实现这一点将是有益的。我知道可以添加[JsonIgnore]属性,但这些属性不允许有条件地忽略属性。

下面是我想要介绍的一个例子

代码语言:javascript
运行
复制
public class MyClass
{
    public List<string> Types { get; set; }

    [IncludeIfListIncludes(typeof(Types), "Type1")]
    public string PropertyValidForType1 {get; set;}

    [IncludeIfListIncludes(typeof(Types), "Type2")]
    public string PropertyValidForType2 {get; set;}

    [IncludeIfListIncludes(typeof(Types), "Type2")]
    public string PropertyValidForType3 {get; set;}
}

var c = new MyClass
{
    Types = new List<string> { "Type1", "Type3" },
    PropertyValidForType1 = "A",
    PropertyValidForType2 = "B",
    PropertyValidForType3 = "C",
}

var jsonString = JsonSerializer.Serialize(weatherForecast);

Console.WriteLine(jsonString);
// Expected Output: { Types : [ "Type1", "Type3" ], PropertyValidForType1: "A", PropertyValidForType3: "C" } 

上面的示例将跳过PropertyValidForType2属性,因为列表中不包含Type2

EN

回答 1

Stack Overflow用户

发布于 2021-09-08 12:53:49

我不能像您希望的那样解决您的问题,这个功能看起来没有在System.Text.Json中实现,但是您可以通过实现从JsonConverter()派生的转换器类来对您的对象进行一些预序列化操作。

代码语言:javascript
运行
复制
using System.Text.Json;
using System.Text.Json.Serialization;

public static class Program
{
    public class IMyClassConverter : JsonConverter<MyClass>
    {
        public override MyClass? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
        {
            return null;            
        }

        public override void Write(Utf8JsonWriter writer, MyClass value, JsonSerializerOptions options)
        {
            writer.WriteStartObject();

            JsonSerializer.Serialize(writer, value.Types);

            if (value.Types.Contains("Type1")) JsonSerializer.Serialize(writer, value.PropertyValidForType1);
            if (value.Types.Contains("Type2")) JsonSerializer.Serialize(writer, value.PropertyValidForType2);
            if (value.Types.Contains("Type3")) JsonSerializer.Serialize(writer, value.PropertyValidForType3);

            writer.WriteEndObject();
        }
    }

    public class MyClass
    {
        public List<string> Types { get; set; }
        public string PropertyValidForType1 { get; set; }
        public string PropertyValidForType2 { get; set; }
        public string PropertyValidForType3 { get; set; }
    }

    public static void Main()
    {
        var c = new MyClass
        {
            Types = new List<string> { "Type1", "Type3" },
            PropertyValidForType1 = "A",
            PropertyValidForType2 = "B",
            PropertyValidForType3 = "C",
        };

        var options = new JsonSerializerOptions();
        options.Converters.Add(new IMyClassConverter());
        options.WriteIndented = true;

        var jsonString = JsonSerializer.Serialize(c, options);

        System.Console.WriteLine(jsonString);
    }
}

你必须以这种方式挖掘,也许创建一些<T>类,等等,我的业务逻辑在这个例子中很差,但它是有效的。附注:你也可以使用interface,但我发现如果不将对象转换为interface,我就不能实现这一点。

代码语言:javascript
运行
复制
// CODE
public interface IMyClass
{
    List<string> Types { get; set; }
    string PropertyValidForType1 { get; set; }
    string PropertyValidForType2 { get; set; }
    string PropertyValidForType3 { get; set; }
}

// ...
public class MyClass : IMyClass
// ...
IMyClass c = new MyClass
{
    Types = new List<string> { "Type1", "Type3" },
    PropertyValidForType1 = "A",
    PropertyValidForType2 = "B",
    PropertyValidForType3 = "C",
};
// ...
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/69101306

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档