我们在使用 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模板网!