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
Json.Net4.5 序列化问题的更多相关文章
- Python下Json和Msgpack序列化比较
最近用Python时,遇到了序列化对象的问题,传统的json和新型序列化工具包msgpack都有涉及,于是做一个简单的总结: 通俗的讲:序列化:将对象信息转化为可以存储或传输的形式:反序列化:把这个 ...
- php json与xml序列化/反序列化
在web开发中对象的序列化与反序列化经常使用,比较主流的有json格式与xml格式的序列化与反序列化,今天想写个jsop的小demo,结果发现不会使用php序列化,查了一下资料,做个笔记 简单数组js ...
- js实现对json数据的序列化(兼容ie6以上浏览器)
/** * 增加对JSON数据的序列化方法, * 主要用于IE6.7不支持JSON对象的浏览器 */ var xue = xue || {};xue.json = xue.json || {}; xu ...
- 使用Json.NET来序列化所需的数据
我们在做开发的时候,很多时候需要和Json数据格式打交道,如Web开发里面,很多时候,数据通过Json进行传递到页面上,然后在进行处理的.而使用Json的时候,我们很多时候会涉及到几个序列化对象的使用 ...
- Python之路-python(装饰器、生成器、迭代器、Json & pickle 数据序列化、软件目录结构规范)
装饰器: 首先来认识一下python函数, 定义:本质是函数(功能是装饰其它函数),为其它函数添加附件功能 原则: 1.不能修改被装饰的函数的源代码. 2.不 ...
- 【ASP.NET Web API教程】6.2 ASP.NET Web API中的JSON和XML序列化
谨以此文感谢关注此系列文章的园友!前段时间本以为此系列文章已没多少人关注,而不打算继续下去了.因为文章贴出来之后,看的人似乎不多,也很少有人对这些文章发表评论,而且几乎无人给予“推荐”.但前几天有人询 ...
- Java下利用Jackson进行JSON解析和序列化
Java下利用Jackson进行JSON解析和序列化 Java下常见的Json类库有Gson.JSON-lib和Jackson等,Jackson相对来说比较高效,在项目中主要使用Jackson进行 ...
- Python-Day4 Python基础进阶之生成器/迭代器/装饰器/Json & pickle 数据序列化
一.生成器 通过列表生成式,我们可以直接创建一个列表.但是,受到内存限制,列表容量肯定是有限的.而且,创建一个包含100万个元素的列表,不仅占用很大的存储空间,如果我们仅仅需要访问前面几个元素,那后面 ...
- .net MVC 使用 JSON JavaScriptSerializer 进行序列化或反序列化时出错,字符串的长度超过了为 maxJsonLength 属性设置的值
在.net mvc的controller中,方法返回JsonResult,一般我们这么写: [HttpPost] public JsonResult QueryFeature(string url, ...
随机推荐
- ecshop的特点,持续加新
一.目录文件结构 入口文件index.php,define('IN_ECS', true); 只有为true时才可以进入. 首先加入init.php,在这个文件里: @ini_set('memory_ ...
- UVM的factory机制
在UVM中使用工厂模式基本上分为三个步骤: 1. 注册 当定义一个类的时候,它的类型必须要注册,UVM已经提供了专用的宏. `uvm_component_utils(class_type_name) ...
- LoadRunner参数化详解(转)
距离上次使用loadrunner 已经有一年多的时间了.初做测试时在项目中用过,后面项目中用不到,自己把重点放在了工具之外的东西上,认为性能测试不仅仅是会用工具,最近又想有一把好的利器毕竟可以帮助自己 ...
- mysql convert
SELECT id,boshidianshu,boshidianshu_shuzi,CONVERT(REPLACE(boshidianshu, '个', ''),SIGNED) aaa from lg ...
- libcurl安装使用方法-简单实用(摘录)
http://curl.haxx.se/libcurl/c/example.html 官网c例子http://curl.haxx.se/download/curl-7.21.3.tar.gz 下载地址 ...
- eclipse使用技巧、快捷键
1.alt+/ 自动提示符,可以快速补整,提高效率. 输入Sysout,再按下alt+/,就可以打印了. 输入main,再按下alt+/,可以直接显示main方法. 2.ctrl+左键,快速进入 ...
- xll调试方法
1)打开编译好的debug下的xll 2)project property->Configuration Properties Debugging set Attach as "Yes ...
- instr函数
在Oracle/PLSQL中,instr函数返回要截取的字符串在源字符串中的位置. 语法如下:instr( string1, string2 [, start_position [, nth_appe ...
- Python.Scrapy.14-scrapy-source-code-analysis-part-4
Scrapy 源代码分析系列-4 scrapy.commands 子包 子包scrapy.commands定义了在命令scrapy中使用的子命令(subcommand): bench, check, ...
- MongoDB数据库简介及安装
一.MongoDB数据库简介 简介 MongoDB是一个高性能,开源,无模式的,基于分布式文件存储的文档型数据库,由C++语言编写,其名称来源取自"humongous",是一种开源 ...