zoukankan      html  css  js  c++  java
  • 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.0Blend 这里Copy 的。

    把D:Program Files (x86)Microsoft Visual Studio 12.0Blend 里的 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

  • 相关阅读:
    C#使用各种时间戳及转换
    Rust借用机制的理解分析
    C# QRCoder 生成二维码
    Android 使用 JCIFS 访问共享文件
    Linux samba 创建共享盘
    WPF 等待对话框
    IIS实现Nginx功能:转发
    js jquery ajax跨域
    依赖注入
    .net standard
  • 原文地址:https://www.cnblogs.com/newsea/p/4227223.html
Copyright © 2011-2022 走看看