【发布时间】:2019-09-18 04:13:09
【问题描述】:
我有一个FilterDto 类,用于将过滤器信息发送到我的客户端应用程序。
例如:
public class FilterDto
{
public string Code { get; set; } = "Default code";
public string Description { get; set; }
}
这个序列化如下:
[
{
field: 'Code',
type: 'text',
defaultValue: 'Default code'
},
{
field: 'Description ',
type: 'text',
defaultValue: null
}
]
因此,在我的客户端中,我可以为给定字段呈现两个 input text。当用户过滤返回的 JSON 是这样的:
{
Code: 'code to filter',
Description: 'description to filter'
}
然后我像这样反序列化它:
var filter = JsonConvert.DeserializeObject(json, typeof(FilterDto));
Console.WriteLine(filter.Code); // code to filter
问题是,如果用户决定删除代码的默认值,在上面的示例中,我将拥有 JSON:
{
Description: 'description to filter'
}
反序列化时,我将拥有:
var filter = JsonConvert.DeserializeObject(json, typeof(FilterDto));
Console.WriteLine(filter.Code); // Default code
当 JSON 中缺少 Code 时,是否可以将其设置为 null 而不是其默认值?
谢谢
【问题讨论】:
标签: c# serialization .net-core json.net