[英]Jil serializer ignore null properties
是否有一个属性可防止Jil序列化为null的属性?
在Newtonsoft中是:
[JsonProperty(NullValueHandling = NullValueHandling.Ignore)]
对于整个对象, Options
上的excludeNulls
参数是您想要的(许多不同的Option配置是预先计算的,类似于Options.ExcludeNulls
也可以)。
您可以使用条件序列化来控制单个属性的序列化 。 (我在原始答案中忘记了此选项)。
例如
class ExampleClass
{
public string DontSerializeIfNull {get;set;}
public string AlwaysSerialize {get;set;}
public bool ShouldSerializeDontSerializeIfNull()
{
return DontSerializeIfNull != null;
}
}
JSON.Serialize(new ExampleClass { DontSerializeIfNull = null, AlwaysSerialize = null });
// {"AlwaysSerialize":null}
JSON.Serialize(new ExampleClass { DontSerializeIfNull = "foo", AlwaysSerialize = null });
// {"AlwaysSerialize":null,"DontSerializeIfNull":"foo"}
JSON.Serialize(new ExampleClass { DontSerializeIfNull = null, AlwaysSerialize = "bar" });
// {"AlwaysSerialize":"bar"}
JSON.Serialize(new ExampleClass { DontSerializeIfNull = "foo", AlwaysSerialize = "bar" });
// {"AlwaysSerialize":"bar","DontSerializeIfNull":"foo"}
Jil仅尊重[DataMember]
上的Name
选项。 我想兑现EmitDefaultValue
不会是最难的事情,但没有被打开过的问题吧。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.