在使用json.net序列化时,我使用了DefaultValueHandling。在序列化设置中忽略,如果bool设置为false,则会导致删除键。我想只对bool类型免税,并申请其他类型和类。请帮助
序列化设置中的DefaultValueHandling.Ignore
可以通过用[JsonProperty(DefaultValueHandling = DefaultValueHandling.Include)]
属性装饰任何属性来覆盖。下面是类:
public class Person
{
public string FirstName { get; set; }
public string LastName { get; set; }
[JsonProperty(DefaultValueHandling = DefaultValueHandling.Include)]
public bool IsEmployed { get; set; }
}
假设我们有以下样品:
var person = new Person
{
FirstName = "John",
IsEmployed = false
};
var json = JsonConvert.SerializeObject(person, new JsonSerializerSettings { DefaultValueHandling = DefaultValueHandling.Ignore });
将导致如下json:
{
"FirstName": "John",
"IsEmployed": false
}