我有一个需要打印的字段是假的,另一个整数需要为 0。 我不想在我的应用程序中的所有其他属性上添加忽略 atribute
我试过这个:[JsonProperty(Required = Newtonsoft.Json.Required.AllowNull(]
我已经尝试过应该序列化属性名称((
我试过编写自己的合同解析器...并添加属性。忽略 = 假
我还能做什么来打印这两个属性?
我的创业公司看起来像这样:
services.AddMvc().AddJsonOptions(options =>
{
options.SerializerSettings.ContractResolver = new ShouldSerializeContractResolver();
options.SerializerSettings.DefaultValueHandling = DefaultValueHandling.Ignore;
options.SerializerSettings.NullValueHandling = NullValueHandling.Ignore;
options.SerializerSettings.Converters.Add(new StringEnumConverter());
});
我的POCO看起来像这样:
public class Extension : Element
{
[JsonProperty(NullValueHandling = NullValueHandling.Include)]
public Boolean valueBoolean { get; set; }
public DateTime valueDateTime { get; set; }
[JsonProperty(NullValueHandling = NullValueHandling.Include)]
public int valueInteger { get; set; }
public bool ShouldSerializevalueBoolean()
{
//this never gets called - I have played around with the case of this upper lower camel etc and neither of these are getting called... a few answers have had that issue as well fyi
return true;
}
我在解析器中尝试了很多...似乎没有任何效果:
protected override JsonProperty CreateProperty(MemberInfo member, MemberSerialization memberSerialization)
{
JsonProperty property = base.CreateProperty(member, memberSerialization);
if (property.DeclaringType == typeof(Extension) && property.PropertyName == "valueBoolean")
{
property.NullValueHandling = NullValueHandling.Include;
// property.Ignored = false;
//if (property.Ignored)
//{
// property.Ignored = false;
//}
//property.ShouldSerialize =
// instance =>
// {
// Extension e = (Extension)instance;
// e.valueString = e.valueString + "made it here valueBoolean";
// return true;//e.ShouldSerializeValueBoolean();
// };
}
return property;
}
再次查看此内容后,我的问题是这些属性不可为空。
[JsonProperty(NullValueHandling = NullValueHandling.Include)]
public Boolean? valueBoolean { get; set; }
[JsonProperty(NullValueHandling = NullValueHandling.Include)]
public int? valueInteger { get; set; }
解决了我的问题!
谢谢大家!