如果另一个属性包含某个特定值,我需要能够序列化一个类并动态忽略某些属性(而不是将它们写到JSON中)。
因此,想象一下下面的类:
public class MyClass
{
public List<string> Types { get; set; }
public string PropertyValidForType1 {get; set;}
public string PropertyValidForType2 {get; set;}
public string PropertyValidForType2 {get; set;}
}
如果Type1
的Types
列表中有一个字符串,我希望将该属性序列化为JSON字符串,PropertyValidForType2
和PropertyValidForType2
也是如此。
它们将是许多不需要序列化的属性,因此能够使用属性来实现这一点将是有益的。我知道可以添加[JsonIgnore]
属性,但这些属性不允许有条件地忽略属性。
下面是我想要介绍的一个例子
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
。
发布于 2021-09-08 12:53:49
我不能像您希望的那样解决您的问题,这个功能看起来没有在System.Text.Json
中实现,但是您可以通过实现从JsonConverter()
派生的转换器类来对您的对象进行一些预序列化操作。
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,我就不能实现这一点。
// 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",
};
// ...
https://stackoverflow.com/questions/69101306
复制相似问题