问题描述
Json.net具有一个非常有用的属性,可以在名为NullValueHandling
的JsonPropertyAttribute
上进行设置.如果您装饰这样的属性:
Json.net has a very useful property that can be set on the JsonPropertyAttribute
called NullValueHandling
. If you decorate a property like this:
[JsonProperty(NullValueHandling=NullValueHandling.Ignore)]
string MyProp { get; set; }
然后,如果MyProp
为null,则根本不会将其包含在输出中.
Then if MyProp
is null, it won't be included in the output at all.
我想做类似的事情,但排除条件不同.例如-说我们有一个枚举
I would like to do something similar, but with a different condition for exclusion. For example - say we have an enum
public enum MyEnum { None = 0, Value1 = 1, Value2 = 2, Value3 = 4 };
还有财产
MyEnum MyProp { get; set; }
然后,如果MyProp == MyEnum.None
,我希望将MyProp
从输入中完全排除.
Then I'd like to have MyProp
completely excluded from the input if MyProp == MyEnum.None
.
我知道一种解决方案是使用MyEnum?
而不是MyEnum
,然后我可以再次使用NullValueHandling
,但是我不能在这里使用nullable.我以为我也许可以使用JsonConverter
,所以我尝试对StringEnumConverter
进行子类化(因为我希望将它们理想地输出为字符串):
I know one solution would be to use MyEnum?
instead of MyEnum
and then I could use NullValueHandling
again, but I can't use nullable here. I thought I might be able to use a JsonConverter
so I tried to subclass StringEnumConverter
(because I want them outputted as strings ideally):
public class MyEnumConverter : Newtonsoft.Json.Converters.StringEnumConverter
{
public override bool CanConvert(Type objectType)
{
return objectType == typeof(MyEnum);
}
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
{
throw new NotImplementedException();
}
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
{
// don't write if value is none
var v = (MyEnum)value;
if (v == MyEnum.None)
{
// what to do here? The two options below both include the property as null
//writer.WriteNull();
//serializer.Serialize(writer, null);
return; // just returning without doing anything will break the serialization
}
base.WriteJson(writer, value, serializer);
}
}
我可以还是应该尝试对JsonProperty
进行子类化?如果是这样,我需要重写什么才能得到我想要的行为?
Can I, or should I, try subclassing JsonProperty
? And if so, what would I need to override to get the behavior I want?
我希望避免将转换器附加到包含此属性的类上,因为那样的话,我必须手动序列化整个过程,这很痛苦.
I hoping to avoid having to attach a converter to the class that contains this property because then I have to manually serialize the whole thing, which is a pain.
推荐答案
您可以使用签名为public bool ShouldSerialize*PropertyName*()
的方法.
You can use a method with the signature public bool ShouldSerialize*PropertyName*()
.
public enum MyEnum { None = 0, Value1 = 1, Value2 = 2, Value3 = 4 };
public class SomeClass
{
public MyEnum MyProp { get; set; }
public string Test = "aaaa";
public bool ShouldSerializeMyProp()
{
return MyProp != MyEnum.None;
}
}
var retVal1 = JsonConvert.SerializeObject(new SomeClass() { MyProp= MyEnum.None });
var retVal2 = JsonConvert.SerializeObject(new SomeClass() { MyProp = MyEnum.Value1 });
输出:
retVal1:{"Test":"aaaa"}
retVal2:{"Test":"aaaa","MyProp":1}
这篇关于在某些条件下进行序列化时忽略属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!