配置JSON.NET忽略DataContract /数据成员属性 [英] Configure JSON.NET to ignore DataContract/DataMember attributes

查看:437
本文介绍了配置JSON.NET忽略DataContract /数据成员属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们正在运行到上同时与微软JSON序列化和JSON.NET一个MVC3项目的情况。

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

大家都知道的日期时间的基本上打破了微软的序列化,所以我们切换到JSON.NET以避免此问题。除了一些我们试图序列化类都是波苏斯与DataContract /的DataMember属性,这些属性的伟大工程。它们在被在多个地方引用的组件所定义。此外,他们有没有标记为数据成员的效率其他一些显示属性。例如,一个客户

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客户端可以引用装配和使用全名就好了,但与JSON.NET序列它看到全名不是一个 [数据成员] 和不序列它。有没有传递到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属性工作正常,但有DateTime对象破。我需要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的codeS变的解决方案。因此,它可能不是最好的解决办法,但它为我工作。

I was having a problem almost related to what you're having, and managed to find a solution by going through Json.NET's codes. So it may not be the best solution, but it works for me.

要做到这一点,你需要实现自己的 IContractResolver 。这方面的一个过于简单化的实施,包括所有的参数,并忽略所有属性(不只是 DataContract ,但其他内置Json.NET的规则,所以你设置任何选项本来应该影响到会员的选择,现正由该code overidden):

To do this, you need to implement your own IContractResolver. An over-simplified implementation of that to include all parameters and ignores all attributes (not just DataContract but other built-in Json.NET's rules as well, so whatever options you set that should originally affect the members selection is now being overidden by this code):

class AllPropertiesResolver : DefaultContractResolver
{
    protected override List<MemberInfo> GetSerializableMembers(Type objectType)
    {
        return objectType.GetProperties()
            .Where(p => p.GetIndexParameters().Length == 0)
            .Cast<MemberInfo>()
            .ToList();
    }
}

和来这里的code用途例如:

And here comes the code usage example:

var json = JsonConvert.SerializeObject(result, new JsonSerializerSettings {
    ContractResolver = new AllPropertiesResolver()
});

这篇关于配置JSON.NET忽略DataContract /数据成员属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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