如何迫使Newtonsoft Json序列化所有属性? (具有“指定"属性的奇怪行为) [英] How to force Newtonsoft Json to serialize all properties? (Strange behavior with "Specified" property)

查看:93
本文介绍了如何迫使Newtonsoft Json序列化所有属性? (具有“指定"属性的奇怪行为)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

资深程序员, 我在Newtonsoft.Json中遇到了奇怪的行为.

Fellow programmers, I've encountered a strange behavior in Newtonsoft.Json.

当我尝试序列化如下所示的对象时:

When I'm trying to serialize an object looking like this:

public class DMSDocWorkflowI
{
    [JsonProperty("DMSDocWorkflowIResult")]
    public bool DMSDocWorkflowIResult { get; set; }

    [JsonProperty("DMSDocWorkflowIResultSpecified")]
    public bool DMSDocWorkflowIResultSpecified { get; set; }
}

使用此简单调用,而无需自定义转换器/活页夹/合同解析器:

Using this simple call with no custom converters / binders / contract resolvers:

var testObject = new DMSDocWorkflowI();
var json = JsonConvert.SerializeObject(testObject, Formatting.Indented);

甚至使用JToken.FromObject(...),我总是始终只有一个属性:

or even with JToken.FromObject(...) I always get only one property:

{
   "DMSDocWorkflowIResultSpecified": false
}

当我附加跟踪编写器时,它仅捕获以下内容:

When I attach the trace writer, it catches only this:

[0]: "2016-08-30T11:06:27.779 Info Started serializing *****DMSDocWorkflowI. Path ''."
[1]: "2016-08-30T11:06:27.779 Verbose IsSpecified result for property 'DMSDocWorkflowIResult' on *****DMSDocWorkflowI: False. Path ''."
[2]: "2016-08-30T11:06:27.779 Info Finished serializing *****.DMSDocWorkflowI. Path ''."
[3]: "2016-08-30T11:06:27.780 Verbose Serialized JSON: \r\n{\r\n  \"DMSDocWorkflowIResultSpecified\": false\r\n}"

因此,似乎Newtonsoft.Json有点神奇地对待这个"Specified"属性. 我可以关闭吗? 我需要在具有这些名称的结果JSON中同时使用这两个属性.

So it seems Newtonsoft.Json treats this "Specified" property somewhat magically. Can I turn this off? I need both these properties in resulting JSON with exactly these names.

推荐答案

MinOccurs属性绑定支持 :

[对于可选字段] Xsd.exe生成类型为bool的公共字段,其名称是元素字段的名称并附加了指定的名称.例如,如果元素字段的名称为startDate,则bool字段的名称为startDateSpecified.将对象序列化为XML时,XmlSerializer类检查bool字段的值以确定是否写入该元素.

[For optional fields] Xsd.exe generates a public field of type bool whose name is the element field's name with Specified appended. For example, if the element field's name is startDate, the bool field's name becomes startDateSpecified. When serializing an object to XML, the XmlSerializer class checks the value of the bool field to determine whether to write the element.

我觉得应该在此处中记录此功能,但事实并非如此.您可能想用Newtonsoft打开文档问题.

I feel as though this functionality should be documented here, but is not. You might want to open a documentation issue with Newtonsoft.

由于您不希望出现这种情况,因此,如果您使用的是 Json.NET 11.0.1或更高版本,您可以通过实例化自己的 DefaultContractResolver 并设置 DefaultContractResolver.IgnoreIsSpecifiedMembers = true :

Since you don't want this behavior, if you are using Json.NET 11.0.1 or later, you can disable it for all classes by instantiating your own DefaultContractResolver and settting DefaultContractResolver.IgnoreIsSpecifiedMembers = true:

public static class JsonContractResolvers
{
    // Newtonsoft recommends caching and reusing contract resolvers for best performance:
    // https://www.newtonsoft.com/json/help/html/Performance.htm#ReuseContractResolver
    // But be sure not to modify IgnoreIsSpecifiedMembers after the contract resolver is first used to generate a contract.

    public static readonly DefaultContractResolver IgnoreIsSpecifiedMembersResolver =
        new DefaultContractResolver { IgnoreIsSpecifiedMembers = true };
}

然后按如下所示将其传递给JsonConvert:

Then pass it to JsonConvert as follows:

var settings = new JsonSerializerSettings { ContractResolver = JsonContractResolvers.IgnoreIsSpecifiedMembersResolver };
var json = JsonConvert.SerializeObject(testObject, Formatting.Indented, settings);

或创建一个JToken做:

var jToken = JToken.FromObject(testObject, JsonSerializer.CreateDefault(settings));

如果使用的是早期版本,则需要创建并缓存自定义合同解析器:

If you are using an earlier version, you will need to create and cache a custom contract resolver:

public static class JsonContractResolvers
{
    public static readonly DefaultContractResolver IgnoreIsSpecifiedMembersResolver =
        new IgnoreSpecifiedContractResolver();
}

internal class IgnoreSpecifiedContractResolver : DefaultContractResolver
{
    protected override JsonProperty CreateProperty(MemberInfo member, MemberSerialization memberSerialization)
    {
        var property = base.CreateProperty(member, memberSerialization);
        property.GetIsSpecified = null;
        property.SetIsSpecified = null;
        return property;
    }
}

这篇关于如何迫使Newtonsoft Json序列化所有属性? (具有“指定"属性的奇怪行为)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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