c# - 基于 System.Text.Json 的另一个属性动态忽略写入对象的属性
问题描述
如果另一个属性包含某个值,我需要能够序列化一个类并动态忽略某些属性(而不是将它们写到 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;}
}
如果我们在Types
列表中有一个字符串,Type1
我希望将该属性序列化为 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
属性,因为列表中不包含 a Type2
。
解决方案
我无法按照您的意愿解决您的问题,此功能看起来没有在 中实现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>
类等等,我的业务逻辑在这个例子中很差,但它有效。PS您也可以使用接口,但是如果不将对象转换为接口,我无法实现这一点。
// 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",
};
// ...
推荐阅读
- javascript - 在包含多数组的对象中分组、排序、合并
- dart - Dart - 类型冲突
- javafx - 在 javafx 中创建缩略图时出错
- node.js - 在 mongodb 中使用 Nodejs 更新深度嵌套的数组
- dialogflow-es - 有问题和无问题的 Dialogflow 意图
- azure - Azure management.core Api 无法创建工作区
- ruby - 使用 popen3 实时输出 rake 任务
- hadoop - hbase 到 hive 集成失败
- python-requests - 无法使用 rest api 在 Outlook 中更新具有附件的事件
- c# - 类和方法看不到属性asp