Json.Net4.5 序列化问题

1.子类序列化 依赖父类属性

复制代码
    [DataContract]
    public class pcc
    {
        [DataMember]
        public string Name { get; set; }
    }

    public class ccc : pcc
    {
        public string cName { get; set; }
    }
复制代码

 

序列化 ccc 的时候, cName 不会被序列化!

 

由于ccc 的父类pcc 定义了 DataContract ,所以要求子类的所有属性要定义 DataMember 才能进行序列化。否则按 IgnoreDataMember 处理。

Json.Net,你 太自大了,谁给你的权力?!

修改源码

类:JsonTypeReflector.GetObjectMemberSerialization   想办法让它返回  MemberSerialization.OptOut

复制代码
        public static MemberSerialization GetObjectMemberSerialization(Type objectType, bool ignoreSerializableAttribute)
        {
            JsonObjectAttribute objectAttribute = GetCachedAttribute<JsonObjectAttribute>(objectType);
            if (objectAttribute != null)
                return objectAttribute.MemberSerialization;

#if !NET20
            DataContractAttribute dataContractAttribute = GetDataContractAttribute(objectType);
            if (dataContractAttribute != null)
                return MemberSerialization.OptIn;
#endif

#if !(NETFX_CORE || PORTABLE40 || PORTABLE)
            if (!ignoreSerializableAttribute)
            {
                SerializableAttribute serializableAttribute = GetCachedAttribute<SerializableAttribute>(objectType);
                if (serializableAttribute != null)
                    return MemberSerialization.Fields;
            }
#endif

            // the default
            return MemberSerialization.OptOut;
        }
复制代码

 

复制代码
        public static DataContractAttribute GetDataContractAttribute(Type type)
        {
            // DataContractAttribute does not have inheritance
            Type currentType = type;

            while (currentType != null)
            {
                DataContractAttribute result = CachedAttributeGetter<DataContractAttribute>.GetAttribute(currentType);
                if (result != null)
                    return result;

                currentType = currentType.BaseType();
            }

            return null;
        }
复制代码

 

原方法为:

复制代码
        public static DataContractAttribute GetDataContractAttribute(Type type)
        {
            // DataContractAttribute does not have inheritance
            Type currentType = type;

            while (currentType != null)
            {
                DataContractAttribute result = CachedAttributeGetter<DataContractAttribute>.GetAttribute(currentType);
                if (result != null)
                    return result;

                currentType = currentType.BaseType();
            }

            return null;
        }
复制代码

修改为:

        public static DataContractAttribute GetDataContractAttribute(Type type)
        {
       //不判断基类的 DataContract 属性 by udi @2015年4月14日
return CachedAttributeGetter<DataContractAttribute>.GetAttribute(type); }

 

即不判断基类的 DataContract 属性。

8.0.3 未解决。

2. 无法反序列化 英文日期

如:Apr 14, 2015 6:05:28 PM

找到: IsoDateTimeConverter.ReadJson

 

最后的代码:

            if (!string.IsNullOrEmpty(_dateTimeFormat))
                return DateTime.ParseExact(dateText, _dateTimeFormat, Culture, _dateTimeStyles);
            else
                return DateTime.Parse(dateText, Culture, _dateTimeStyles);

修改为:

复制代码
            //在反序列化的时候,就不要使用 _dateTimeFormat 了,因为反序列的途径很多,而 _dateTimeFormat 指定的输出格式 
            var retDate = DateTime.MinValue;
            if (DateTime.TryParse(dateText, out retDate))
            {
                return retDate;
            }


            if (!string.IsNullOrEmpty(_dateTimeFormat))
                return DateTime.ParseExact(dateText, _dateTimeFormat, Culture, _dateTimeStyles);
            else
                return DateTime.Parse(dateText, Culture, _dateTimeStyles);
复制代码

8.0.3 未解决

 

3. 还有未实现的属性

Json.Net 中 JObject , 继承自 IDictionary<string, JToken>,实现了 Keys , 但是没有实现 Values , 这个坑被我踩到了。艹,这玩意太他妈衰了!!!

        ICollection<JToken> IDictionary<string, JToken>.Values
        {
            get
            {
                // todo: need to wrap _properties.Values with a collection to get the JProperty value
                throw new NotImplementedException();
            }
        }

修改为:

复制代码
        ICollection<JToken> IDictionary<string, JToken>.Values
        {
            get
            {
                return _properties.Values.Values().ToList();
                // todo: need to wrap _properties.Values with a collection to get the JProperty value
                //throw new NotImplementedException();
            }
        }
复制代码

 

 

这个问题到 8.0.3 一直没有解决

 

4. 版本 8.0.3 中 Newtonsoft.Json.Net40.sln 是使用 C#6.0编写的

调试 8.3 的时候发现的。 导致的问题是:Vs2013 编译不了 .net40.sln

 

 vs2013 编译Json.Net 的问题

发现的 Vs2012,Vs2013编译问题

一个Solution,两个Web Mvc 项目(A,B),编译其中A,B无法运行(Json.net程序集变为了老的程序集),编译B,A无法运行(Json.net 程序集变为老的程序集)。

编译DbEnt,Mvc项目的bin里,也会自动Copy一些Dll,包含老的Json.Net 。

Json.Net 程序集并不是从指定目录Copy的。而是一个老版本, 4.5.11.* 。它是从 D:\Program Files (x86)\Microsoft Visual Studio 12.0\Blend 这里Copy 的。

把D:\Program Files (x86)\Microsoft Visual Studio 12.0\Blend 里的 json.net 删除,就好了。

 

关于修改Json.Net后,不能引用原来的Json.Net的问题

1.首先,修改Json.Net的在项目属性,程序集名称改为: MyJson , 也可以直接修改Dll的名字

2.添加MyJson的引用,在引用的Dll属性上,修改 别名 为: MyJson,默认是 global

3.在需要使用 MyJson 的地方,在文件最前面,添加 extern alias MyJson;

4.使用 MyJson:: 前缀,来指定 MyJson 程序集里的类。

5.如果使用新的 Json.Net 程序集类,则直接使用,无影响。 

之前的文章: 

http://www.cnblogs.com/newsea/archive/2010/02/25/1673468.html

posted @   NewSea  阅读(1020)  评论(0编辑  收藏  举报
编辑推荐:
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
阅读排行:
· 周边上新:园子的第一款马克杯温暖上架
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器
· DeepSeek如何颠覆传统软件测试?测试工程师会被淘汰吗?
· 使用C#创建一个MCP客户端
点击右上角即可分享
微信分享提示