fastjosn在低版本丢字段问题
简单的说:
对于java bean中有字段类似pId这种写法,特征是第一个字母小写,第二个字母大写,在eclipse中生成的getter setter方法是 getpId, setpId。
在低版本的fastjson,比如1.1.41中将java bean序列化成json字符串的时候,会丢掉pId这个字段。 使用高版本,比如1.2.46,就不会有这个问题。
那么,这到底是谁的锅?
先说结论,fastjson的锅。
先说,eclipse生成的对不对?生成的是对的,参见这里
This is not the problem of eclipse it is the logic of getter and setter.
再说eclipse为什么要这么生成?因为符合java bean规范
简单地说,getter setter方法是读取java bean字段的入口。 如果生成的是getPId和setPId,通过getter setter 方法拿到的属性字段名将是PId,不会是pId。为了能通过getter setter方法拿到的字段是pId,所以倒推出getter setter方法应该是getpId和setpId,参见这里
So when you have the introspection of a class that contains Long getRPersonId() and setRPersonId(Long), a property can be extracted from it. The name of the property generally follows from lower-casing the first letter, and keeping the rest unchanged. But this is not always the case, the exact rule is in paragraph 8.8:
在高版本的fastjson中,不论写的是getpId还是getPId都不会丢字段。
fastjson在1.1.41版本中有问题的获取代码如下:
if (Character.isUpperCase(c3)) {
if (compatibleWithJavaBean) {
propertyName = Introspector.decapitalize(methodName.substring(3));
} else {
propertyName = Character.toLowerCase(methodName.charAt(3)) + methodName.substring(4);
}
} else if (c3 == '_') {
propertyName = methodName.substring(4);
} else if (c3 == 'f') {
propertyName = methodName.substring(3);
} else {
continue;
}
fastjson在1.1.42版本中已经修复。
1.1.42的com.alibaba.fastjson.util.TypeUtils.computeGetters(Class<?>, Map<String, String>, boolean)
if (Character.isUpperCase(c3)) {
if (compatibleWithJavaBean) {
propertyName = decapitalize(methodName.substring(3));
} else {
propertyName = Character.toLowerCase(methodName.charAt(3)) + methodName.substring(4);
}
} else if (c3 == '_') {
propertyName = methodName.substring(4);
} else if (c3 == 'f') {
propertyName = methodName.substring(3);
} else if (methodName.length()>=5 && Character.isUpperCase(methodName.charAt(4))){
propertyName = decapitalize(methodName.substring(3));
}
fastjson在1.2.46实现如下:
com.alibaba.fastjson.util.TypeUtils.buildBeanInfo(Class<?>, Map<String, String>, PropertyNamingStrategy, boolean)
List<FieldInfo> fieldInfoList = fieldBased
? computeGettersWithFieldBase(beanType, aliasMap, false, propertyNamingStrategy) //
: computeGetters(beanType, jsonType, aliasMap, fieldCacheMap, false, propertyNamingStrategy);
已经有基于field还是基于getter setter来解析了。
fastjosn在低版本丢字段问题的更多相关文章
- [杂] 将高版本iTunes备份恢复到低版本iOS设备中
除非开发测试用设备,自用设备不要随便升iOS beta,不要随便升iOS beta,不要随便升iOS beta. 对于升级了高版本iOS的用户,默认情况下重刷低版本iOS时,iTunes不允许向低版本 ...
- 如何让VMware低版本运行VMware高版本创建的虚拟机
如何让VMware低版本运行VMware高版本创建的虚拟机 问题描述: 本机安装的VMware Workstation是10版本,之前VMware Workstation 11版本创建的虚拟机,在运行 ...
- IE低版本下实现html5的placeholder(表单提示)功能
placeholder 属性提供可描述输入字段预期值的提示信息(hint). 该提示会在输入字段为空时显示,并会在字段获得焦点时消失. 注释:placeholder 属性适用于以下的 <inpu ...
- 低版本系统兼容的ActionBar(一)设置颜色+添加Menu+添加ActionMode
之前我一直用ActionBarSherlock这个开源项目来做ActionBar,因为它可以让低版本的设备也能用上ActionBar.但是在最新的SDK中Google提供了一个AppCompa ...
- WebSocket兼容到低版本浏览器
就目前而言,WebSocket是最好的Web通信解决方案了.但是IE从10才开始兼容它,对于目前大量IE8存在的市场,原生的WebSocket显然不太实用,我们需要低版本兼容的解决方案.于是我模拟We ...
- mysql 低版本导入表中包含两个TIMESTAMP报错问题
错误代码: 1293 Incorrect table definition; there can be only one TIMESTAMP column with CURRENT_TIMESTAM ...
- xcode低版本调试高版本真机系统
低版本xcode调试本真机高版本系统 //打开此路径把最新的文件拷贝到这里就可以了 /Applications/Xcode.app/Contents/Developer/Platforms/iPhon ...
- 低版本GCC程序向高版本移植的兼容性问题
将低版本gcc编译过的程序移植到高版本GCC时, 可能会出现一些兼容性问题. 原因是, 为了适应新的标准,一些旧的语法规则被废弃了. 关于这方面的一些具体资料可从该处查询. 这里只是自己遇到的其中一个 ...
- android中导入低版本project可能会遇到的编译问题(转自: Victor@Beijing)
使用高版本的SDK后再导入以前用低版本的project时,会遇到一些兼容性的问题. (1)Unable to resolve target 'android-5' 因为本机中现在使用的是2.2的SDK ...
随机推荐
- 跨平台移动开发phonegap/cordova 3.3全系列教程-目录
目录(更新完成后会附上源码供参考) 第一章 android平台开发 phonegap/cordova简介 1.开发环境搭建 2.helloworld 3.启动画面 4.结合asp.net/jqmboi ...
- xp_delete_files不起作用解决方法
xp_delete_file用来删除数据库的备份文件和维护计划文本报告.示例: ,N'D:\Backup\Diff',N'bak',N'2019-05-29T10:03:41' 第一个参数表示文件类型 ...
- [Git] Create a new repository on the command line
echo "# xxx" >> README.md git init git add README.md git commit -m "first commi ...
- 【转】CentOS 7.0 安装Redis 3.2.1详细过程和使用常见问题
http://www.linuxidc.com/Linux/2016-09/135071.htm 环境:CentOS 7.0 Redis 3.2.1 Redis的安装与启动 这里我把Redis放在/h ...
- numpy各函数简介之生成数组函数
1.empty(shape[, dtype, order]) 依据给定形状和类型(shape[, dtype, order])返回一个新的空数组. 参数: shape : 整数或者整型元组 定义返回数 ...
- python之道12
整理今天笔记,课上代码最少敲3遍. 用列表推导式做下列小题 过滤掉长度小于3的字符串列表,并将剩下的转换成大写字母 l = ['wusir', 'laonanhai', 'aa', 'b', 'tai ...
- Maven父子模块引入依赖问题
公共模块如何放到父pom中,而子pom无需再次引入???
- c++ 程序设计question 001:我们的开发工具是什么?
我们使用的开发工具是dev cpp (c plus plus),这是一个集成开发环境,我们称之为IDE(integrated development environment)
- I am too vegetable to all kill the 51nod problems on level 2 and 3.
51nod level 2:50/51 剩的一个题是切比雪夫距离转曼哈顿距离,现学的,bzoj3710过了,51nod上全wa了,很迷,可能有坑⑧. level 3:62/68 之前有的题有思路但是不 ...
- BZOJ3679: 数字之积(数位dp)
题意 题目链接 Sol 推什么结论啊. 直接大力dp,$f[i][j]$表示第$i$位,乘积为$j$,第二维直接开map 能赢! /* */ #include<iostream> #inc ...