Newtonsoft Json.NET在序列化时忽略默认的日期时间值

时间:2015-11-05 15:07:36

标签: json json.net

如何设置JsonSerializer忽略默认值0001-01-01T00:00:00的DateTime属性,但是同时使用默认值序列化枚举和数字?

这也忽略了枚举:

_jsonSerializer = new JsonSerializer();
_jsonSerializer.DefaultValueHandling = DefaultValueHandling.Ignore;

这也包括默认日期时间:

_jsonSerializer.DefaultValueHandling = DefaultValueHandling.Include;

1 个答案:

答案 0 :(得分:0)

我想这是类似的问题并得到回答:Exclude property from serialization via custom attribute (json.net)

它指的是json.net docs中的Conditional Property Serialization文章

_jsonSerializer.DefaultValueHandling = DefaultValueHandling.Include;
_jsonSerializer.ContractResolver = new MyShouldSerializeContractResolver();