配置 JSON.NET 以忽略 DataContract/DataMember 属性

Configure JSON.NET to ignore DataContract/DataMember attributes(配置 JSON.NET 以忽略 DataContract/DataMember 属性)
本文介绍了配置 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 属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

相关文档推荐

Force JsonConvert.SerializeXmlNode to serialize node value as an Integer or a Boolean(强制 JsonConvert.SerializeXmlNode 将节点值序列化为整数或布尔值)
Using JSON to Serialize/Deserialize TimeSpan(使用 JSON 序列化/反序列化 TimeSpan)
Could not determine JSON object type for type quot;Classquot;(无法确定类型“Class的 JSON 对象类型.)
How to deserialize a JSONP response (preferably with JsonTextReader and not a string)?(如何反序列化 JSONP 响应(最好使用 JsonTextReader 而不是字符串)?)
how to de-serialize JSON data in which Timestamp it-self contains fields?(如何反序列化时间戳本身包含字段的JSON数据?)
JSON.Net custom contract serialization and Collections(JSON.Net 自定义合约序列化和集合)