<i id='NHv4E'><tr id='NHv4E'><dt id='NHv4E'><q id='NHv4E'><span id='NHv4E'><b id='NHv4E'><form id='NHv4E'><ins id='NHv4E'></ins><ul id='NHv4E'></ul><sub id='NHv4E'></sub></form><legend id='NHv4E'></legend><bdo id='NHv4E'><pre id='NHv4E'><center id='NHv4E'></center></pre></bdo></b><th id='NHv4E'></th></span></q></dt></tr></i><div id='NHv4E'><tfoot id='NHv4E'></tfoot><dl id='NHv4E'><fieldset id='NHv4E'></fieldset></dl></div>

      <bdo id='NHv4E'></bdo><ul id='NHv4E'></ul>

    <small id='NHv4E'></small><noframes id='NHv4E'>

    <tfoot id='NHv4E'></tfoot>
      <legend id='NHv4E'><style id='NHv4E'><dir id='NHv4E'><q id='NHv4E'></q></dir></style></legend>
    1. 配置 JSON.NET 以忽略 DataContract/DataMember 属性

      时间:2023-08-23

            <small id='CZ1eC'></small><noframes id='CZ1eC'>

            <i id='CZ1eC'><tr id='CZ1eC'><dt id='CZ1eC'><q id='CZ1eC'><span id='CZ1eC'><b id='CZ1eC'><form id='CZ1eC'><ins id='CZ1eC'></ins><ul id='CZ1eC'></ul><sub id='CZ1eC'></sub></form><legend id='CZ1eC'></legend><bdo id='CZ1eC'><pre id='CZ1eC'><center id='CZ1eC'></center></pre></bdo></b><th id='CZ1eC'></th></span></q></dt></tr></i><div id='CZ1eC'><tfoot id='CZ1eC'></tfoot><dl id='CZ1eC'><fieldset id='CZ1eC'></fieldset></dl></div>
            <legend id='CZ1eC'><style id='CZ1eC'><dir id='CZ1eC'><q id='CZ1eC'></q></dir></style></legend>
            • <bdo id='CZ1eC'></bdo><ul id='CZ1eC'></ul>
              <tfoot id='CZ1eC'></tfoot>

                <tbody id='CZ1eC'></tbody>
              1. 本文介绍了配置 JSON.NET 以忽略 DataContract/DataMember 属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                问题描述

                我们在使用 Microsoft JSON 序列化程序和 JSON.NET 的 MVC3 项目中遇到了情况.

                We are running into a situation on an MVC3 project with both the Microsoft JSON serializers and JSON.NET.

                每个人都知道微软的序列化器中的 DateTime 基本上被破坏了,所以我们切换到 JSON.NET 来避免这个问题.这很好用,除了我们尝试序列化的一些类是具有 DataContract/DataMember 属性的 POCO.它们是在一个在多个位置引用的程序集中定义的.此外,它们还有一些其他显示属性未标记为 DataMembers 以提高效率.例如,客户

                Everybody knows DateTime's are basically broken in Microsoft's serializers, so we switched to JSON.NET to avoid this issue. That works great, except that some of the classes we are trying to serialize are POCOs with DataContract/DataMember attributes. They are defined in an assembly that is referenced in multiple places. Additionally, they have some other display properties that are not marked as DataMembers for efficiency. For instance, a Customer

                [DataContract]
                public class Customer
                {
                   [DataMember]
                   public string FirstName { get; set;}
                   [DataMember]
                   public string LastName { get; set;}
                   public string FullName 
                   {
                       get
                       {  return FirstName + " " + LastName; }
                   }
                
                }
                

                当此客户通过 WCF 传递时,客户端可以引用该程序集并使用 FullName 就好了,但是当使用 JSON.NET 序列化时,它会发现 FullName 不是 [DataMember] 并且不序列化它.是否有一个选项可以传递给 JSON.NET 来告诉它忽略一个类应用了 [DataContract] 属性的事实?

                When this customer is passed over WCF the client side can reference that assembly and use the FullName just fine, but when serialized with JSON.NET it sees that FullName isn't a [DataMember] and doesn't serialize it. Is there an option to pass to JSON.NET to tell it to ignore the fact that a class has [DataContract] attribute applied?

                注意:在 .NET 中使用 JavaScriptSerializer 可以很好地用于 FullName 属性,但 DateTimes 已损坏.我需要 JSON.NET 来忽略此类具有 DataContract/DataMember 属性的事实,并且只执行标准的公共字段序列化,就像它们不存在时一样.

                Note: Using the JavaScriptSerializer in .NET works fine for the FullName property, but DateTimes are broken. I need JSON.NET to ignore the fact that this class has DataContract/DataMember attributes and just do standard public field serialization like it would if they weren't there.

                推荐答案

                只需使用 Json.Net 的 OptOut 属性即可.它将优先于 DataContract.

                Simply use Json.Net's OptOut attribute. It will take precedence over DataContract.

                [DataContract]
                [JsonObject(MemberSerialization.OptOut)]
                

                这篇关于配置 JSON.NET 以忽略 DataContract/DataMember 属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                上一篇:如何使用 Json.Net 序列化/反序列化带有自定义键的 下一篇:在 Json.Net 中全局注册自定义 JsonConverter

                相关文章

                最新文章

                  <bdo id='D9Ers'></bdo><ul id='D9Ers'></ul>

                  <i id='D9Ers'><tr id='D9Ers'><dt id='D9Ers'><q id='D9Ers'><span id='D9Ers'><b id='D9Ers'><form id='D9Ers'><ins id='D9Ers'></ins><ul id='D9Ers'></ul><sub id='D9Ers'></sub></form><legend id='D9Ers'></legend><bdo id='D9Ers'><pre id='D9Ers'><center id='D9Ers'></center></pre></bdo></b><th id='D9Ers'></th></span></q></dt></tr></i><div id='D9Ers'><tfoot id='D9Ers'></tfoot><dl id='D9Ers'><fieldset id='D9Ers'></fieldset></dl></div>
                1. <tfoot id='D9Ers'></tfoot>

                  <small id='D9Ers'></small><noframes id='D9Ers'>

                  <legend id='D9Ers'><style id='D9Ers'><dir id='D9Ers'><q id='D9Ers'></q></dir></style></legend>