我试图弄清楚如何序列化为json对象,并跳过序列化值为空列表的属性.I am not using Newtonsoft json
using System.Text.Json;
using System.Text.Json.Serialization;
using AutoMapper;
我有一个带有属性的对象.
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
[JsonPropertyName("extension")]
public List<Extension> Extension { get; set; }
当我try 使用以下命令序列化此对象时
var optionsJson = new JsonSerializerOptions
{
WriteIndented = true,
IgnoreNullValues = true,
PropertyNameCaseInsensitive = true,
};
var json = JsonSerializer.Serialize(report, optionsJson);
它仍然给我一个空数组:
"extension": [],
难道没有办法阻止它序列化这些空列表吗?我想看到extension
个消失.它根本不应该在那里.我需要这样做,因为如果我发送:
"extension": null,
序列化时,它不能是对象的一部分.
gateway error
我不想要这些空列表的原因是,我向对象发送的第三方网关需要空列表
"严重性":"错误",
我尽量避免在这上面做一些下流的字符串替换.