JSON.net不包含带有EmitDefaultValue false的null属性 [英] JSON.net not including null properties with EmitDefaultValue false

查看:132
本文介绍了JSON.net不包含带有EmitDefaultValue false的null属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含许多数据合同的系统,其中的成员装饰有:

I have a system with many data contracts where the members are decorated with:

[DataMember(EmitDefaultValue = false)]  

在某些情况下,当序列化为JSON时为null时,我需要包含成员.无论我选择什么序列化程序设置,我都无法使用它.

I have a case where I need the members to be included when null when serialized to JSON. No matter what serializer settings I choose, I can not get this to work.

   [TestMethod]
    public void SerializationTest()
    {

        var contract = Activator.CreateInstance(typeof(TestContract));

        var formatter = new JsonMediaTypeFormatter
        {
            SerializerSettings = new JsonSerializerSettings
            {
                NullValueHandling = NullValueHandling.Include,
                DefaultValueHandling = DefaultValueHandling.Ignore,
                TypeNameHandling = TypeNameHandling.All,
                ContractResolver = new DataContractResolver(),
                Binder = new DataContractBinder()
            }
        };

        var result = JsonConvert.SerializeObject(contract, formatter.SerializerSettings);
    }

我什至创建了一个自定义数据合同解析器,该解析器为每个属性提供了其他替代.

I have even created a custom data contract resolver which provides additional overrides per property.

        protected override JsonProperty CreateProperty(MemberInfo member, MemberSerialization memberSerialization)
    {                        
        var property = base.CreateProperty(member, MemberSerialization.Fields);                        
        property.NullValueHandling = NullValueHandling.Include;            
        property.DefaultValueHandling = DefaultValueHandling.Ignore;
        property.ShouldSerialize = o => true;
        return property;
    }

我尝试了空值处理和默认值处理的所有变体,以忽略发出默认值= false.根据json net文档,这些设置应为我的对象提供null属性.在调试json网络源之前,还有什么我想念的吗?

I've tried all the variations of null value handling and default value handling to ignore the emit default value = false. According to the json net docs, these settings should give me null properties on my object. Before I go debug the the json net source, is there anything else I am missing?

推荐答案

您需要设置property.DefaultValueHandling = DefaultValueHandling.Include而不是 DefaultValueHandling.Ignore :

You need to set property.DefaultValueHandling = DefaultValueHandling.Include rather than DefaultValueHandling.Ignore in the contract resolver:

public class DataContractResolver : DefaultContractResolver
{
    protected override JsonProperty CreateProperty(MemberInfo member, MemberSerialization memberSerialization)
    {
        var property = base.CreateProperty(member, memberSerialization);
        property.NullValueHandling = NullValueHandling.Include;
        property.DefaultValueHandling = DefaultValueHandling.Include;
        property.ShouldSerialize = o => true;
        return property;
    }
}

Json.NET同时检查 DefaultValueHandling NullValueHandling ,然后对成员进行序列化,然后检查必须通过才能将成员序列化.而且,您已经注意到,如果在合同的属性上明确设置了其中一个,则它将覆盖 JsonSerializerSettings .这解释了为什么需要自定义合同解析器.

Json.NET checks both DefaultValueHandling and NullValueHandling before serializing a member, and both checks have to pass in order for the member to be serialized. And, as you have noticed, if either are set explicitly on the contract's property then that will override the settings in JsonSerializerSettings. This explains why a custom contract resolver is required.

您可能想要缓存合同解析器以获得最佳性能

You might want to cache the contract resolver for best performance.

这篇关于JSON.net不包含带有EmitDefaultValue false的null属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆