论坛首页 Java企业应用论坛

Fastjson技术内幕

浏览 87020 次
该帖已经被评为精华帖
作者 正文
   发表时间:2011-08-08  
wenshao 写道
flyfan 写道
fastjson之前都在侧重于快,希望后面着重于功能方面,现在的功能感觉还是不够。


能把你的需求列出来么?fastjson的完善需要大家的帮忙。


关于循环引用的解决办法的问题:
1、transient和hibernate注解同时使用时报错;
2、使用@JSONField时可以,但实际使用中会经常会需要显示部门的上级部门的,用@JSONField设置不做序列化后,在界面上无法显示上级部门,用jackjson时是不会报错的,只是会把上级的上级的上级……都序列化出来而已,fastjson能不能在加入一个Feature,可以设置循环引用的深度呢?另外,还有一种情况,用户关联了部门,部门关联了自己(上级部门),jackjson会把用户的部门和部门关联的所有上级都序列化出来了,fastjson如果加入的循环引用深度的Feature,就可以不需要加载部门的上级……了。不知目前fastjson能否做到这点。如果不能,强烈要求加入这个功能。
关于打印空值问题:
3、WriteMapNullValue是全局的设置,但是我有特殊属性是就算是空值也需要打印出来的。比如一个用户列表界面,用户关联了部门,如果部门是NULL,则界面需要“{}”这样的值,否则界面报错,fastjson好像也没有提供这样的功能。

我之前提过这些问题和建议的,不知现在解决没?现在就是问题2和3没有找到解决办法是我还没从jackjson转到fastjson的原因。希望温少可以帮我解决下。
0 请登录后投票
   发表时间:2011-08-09  
wenshao 写道
lanblue173 写道
发现一个问题,我在用fastjson 来对己新浪微博的提供的SDK(weiboandroid4)里的Stuts类进行操作

发现使用 toStringJSON 能正常将Stuts转为json 但通过parseObject方法还原时! 就报异常!

请问是什么原因!


请告诉我具体异常信息!






执行:Status o=JSON.parseObject(st,Status.class);报以下错误
产生的异常
com.alibaba.fastjson.JSONException: class not has default constructor : weibo4android.Status



st参数的值为的值为

{"bmiddle_pic":"","createdAt":1312816303000,"favorited":false,"id":3344256320529927,"inReplyToScreenName":"","inReplyToStatusId":-1,"inReplyToUserId":-1,"latitude":-1,"longitude":-1,"mid":"3344256320529927","original_pic":"","rateLimitLimit":-1,"rateLimitRemaining":-1,"rateLimitReset":-1,"retweet":true,"retweetDetails":{"rateLimitLimit":-1,"rateLimitRemaining":-1,"rateLimitReset":-1,"retweetId":0,"retweetingUser":{"favouritesCount":0,"followersCount":0,"following":false,"friendsCount":0,"geoEnabled":false,"id":0,"notificationEnabled":false,"notifications":false,"protected":false,"rateLimitLimit":-1,"rateLimitRemaining":-1,"rateLimitReset":-1,"statusFavorited":false,"statusId":-1,"statusInReplyToStatusId":-1,"statusInReplyToUserId":-1,"statusTruncated":false,"statusesCount":0,"utcOffset":0,"verified":false}},"retweeted_status":{"bmiddle_pic":"http://ww3.sinaimg.cn/bmiddle/62c76a9dtw1djxwakxs7lj.jpg","createdAt":1312767831000,"favorited":false,"id":3344053013979515,"inReplyToScreenName":"","inReplyToStatusId":-1,"inReplyToUserId":-1,"latitude":-1,"longitude":-1,"mid":"3344053013979515","original_pic":"http://ww3.sinaimg.cn/large/62c76a9dtw1djxwakxs7lj.jpg","rateLimitLimit":-1,"rateLimitRemaining":-1,"rateLimitReset":-1,"retweet":false,"source":"<a href=\"http://weibo.com\" rel=\"nofollow\">新浪微博</a>","text":"“中国人每日辛勤劳作生产产品给美国人用,而我们给他们一些纸片。当他们想用这些纸片时,却发现这纸片不能达到他们想要的目的。”——在谈及美国大量借债行为时,巴菲特在2009年股东大会上如是说","thumbnail_pic":"http://ww3.sinaimg.cn/thumbnail/62c76a9dtw1djxwakxs7lj.jpg","truncated":false,"user":{"createdAt":1256832000000,"description":"《环球企业家》官方微博:关于中国和全球商业的严肃口水","favouritesCount":134,"followersCount":476617,"following":false,"friendsCount":555,"gender":"m","geoEnabled":false,"id":1657236125,"location":"北京 东城区","name":"环球企业家杂志","notificationEnabled":false,"notifications":false,"profileBackgroundColor":"","profileBackgroundImageUrl":"","profileBackgroundTile":"","profileImageURL":"http://tp2.sinaimg.cn/1657236125/50/5606978745/1","profileLinkColor":"","profileSidebarBorderColor":"","profileSidebarFillColor":"","profileTextColor":"","protected":false,"rateLimitLimit":-1,"rateLimitRemaining":-1,"rateLimitReset":-1,"screenName":"环球企业家杂志","statusFavorited":false,"statusId":-1,"statusInReplyToStatusId":-1,"statusInReplyToUserId":-1,"statusTruncated":false,"statusesCount":5247,"timeZone":"","uRL":"http://www.gemag.com.cn","userDomain":"gemag","utcOffset":-1,"verified":true}},"source":"<a href=\"http://weibo.com\" rel=\"nofollow\">新浪微博</a>","text":"//@王兆春 我党为什么把那么多的老百姓劳动成果换成绿纸?还不给劳动人民呢?真是奇怪!","thumbnail_pic":"","truncated":false,"user":{"createdAt":1256832000000,"description":"《环球企业家》官方微博:关于中国和全球商业的严肃口水","favouritesCount":134,"followersCount":476617,"following":false,"friendsCount":555,"gender":"m","geoEnabled":false,"id":1657236125,"location":"北京 东城区","name":"环球企业家杂志","notificationEnabled":false,"notifications":false,"profileBackgroundColor":"","profileBackgroundImageUrl":"","profileBackgroundTile":"","profileImageURL":"http://tp2.sinaimg.cn/1657236125/50/5606978745/1","profileLinkColor":"","profileSidebarBorderColor":"","profileSidebarFillColor":"","profileTextColor":"","protected":false,"rateLimitLimit":-1,"rateLimitRemaining":-1,"rateLimitReset":-1,"screenName":"环球企业家杂志","statusFavorited":false,"statusId":-1,"statusInReplyToStatusId":-1,"statusInReplyToUserId":-1,"statusTruncated":false,"statusesCount":5247,"timeZone":"","uRL":"http://www.gemag.com.cn","userDomain":"gemag","utcOffset":-1,"verified":true}}
0 请登录后投票
   发表时间:2011-08-09  
flyfan 写道
wenshao 写道
flyfan 写道
fastjson之前都在侧重于快,希望后面着重于功能方面,现在的功能感觉还是不够。


能把你的需求列出来么?fastjson的完善需要大家的帮忙。


关于循环引用的解决办法的问题:
1、transient和hibernate注解同时使用时报错;
2、使用@JSONField时可以,但实际使用中会经常会需要显示部门的上级部门的,用@JSONField设置不做序列化后,在界面上无法显示上级部门,用jackjson时是不会报错的,只是会把上级的上级的上级……都序列化出来而已,fastjson能不能在加入一个Feature,可以设置循环引用的深度呢?另外,还有一种情况,用户关联了部门,部门关联了自己(上级部门),jackjson会把用户的部门和部门关联的所有上级都序列化出来了,fastjson如果加入的循环引用深度的Feature,就可以不需要加载部门的上级……了。不知目前fastjson能否做到这点。如果不能,强烈要求加入这个功能。
关于打印空值问题:
3、WriteMapNullValue是全局的设置,但是我有特殊属性是就算是空值也需要打印出来的。比如一个用户列表界面,用户关联了部门,如果部门是NULL,则界面需要“{}”这样的值,否则界面报错,fastjson好像也没有提供这样的功能。

我之前提过这些问题和建议的,不知现在解决没?现在就是问题2和3没有找到解决办法是我还没从jackjson转到fastjson的原因。希望温少可以帮我解决下。


问题2还没有,这个不难,在1.1.3中实现好了。
问题3已经实现,在字段上可以使用@JSONField设置WriteMapNullValue属性

0 请登录后投票
   发表时间:2011-08-09  
lanblue173 写道
wenshao 写道
lanblue173 写道
发现一个问题,我在用fastjson 来对己新浪微博的提供的SDK(weiboandroid4)里的Stuts类进行操作

发现使用 toStringJSON 能正常将Stuts转为json 但通过parseObject方法还原时! 就报异常!

请问是什么原因!


请告诉我具体异常信息!






执行:Status o=JSON.parseObject(st,Status.class);报以下错误
产生的异常
com.alibaba.fastjson.JSONException: class not has default constructor : weibo4android.Status



st参数的值为的值为

{"bmiddle_pic":"","createdAt":1312816303000,"favorited":false,"id":3344256320529927,"inReplyToScreenName":"","inReplyToStatusId":-1,"inReplyToUserId":-1,"latitude":-1,"longitude":-1,"mid":"3344256320529927","original_pic":"","rateLimitLimit":-1,"rateLimitRemaining":-1,"rateLimitReset":-1,"retweet":true,"retweetDetails":{"rateLimitLimit":-1,"rateLimitRemaining":-1,"rateLimitReset":-1,"retweetId":0,"retweetingUser":{"favouritesCount":0,"followersCount":0,"following":false,"friendsCount":0,"geoEnabled":false,"id":0,"notificationEnabled":false,"notifications":false,"protected":false,"rateLimitLimit":-1,"rateLimitRemaining":-1,"rateLimitReset":-1,"statusFavorited":false,"statusId":-1,"statusInReplyToStatusId":-1,"statusInReplyToUserId":-1,"statusTruncated":false,"statusesCount":0,"utcOffset":0,"verified":false}},"retweeted_status":{"bmiddle_pic":"http://ww3.sinaimg.cn/bmiddle/62c76a9dtw1djxwakxs7lj.jpg","createdAt":1312767831000,"favorited":false,"id":3344053013979515,"inReplyToScreenName":"","inReplyToStatusId":-1,"inReplyToUserId":-1,"latitude":-1,"longitude":-1,"mid":"3344053013979515","original_pic":"http://ww3.sinaimg.cn/large/62c76a9dtw1djxwakxs7lj.jpg","rateLimitLimit":-1,"rateLimitRemaining":-1,"rateLimitReset":-1,"retweet":false,"source":"<a href=\"http://weibo.com\" rel=\"nofollow\">新浪微博</a>","text":"“中国人每日辛勤劳作生产产品给美国人用,而我们给他们一些纸片。当他们想用这些纸片时,却发现这纸片不能达到他们想要的目的。”——在谈及美国大量借债行为时,巴菲特在2009年股东大会上如是说","thumbnail_pic":"http://ww3.sinaimg.cn/thumbnail/62c76a9dtw1djxwakxs7lj.jpg","truncated":false,"user":{"createdAt":1256832000000,"description":"《环球企业家》官方微博:关于中国和全球商业的严肃口水","favouritesCount":134,"followersCount":476617,"following":false,"friendsCount":555,"gender":"m","geoEnabled":false,"id":1657236125,"location":"北京 东城区","name":"环球企业家杂志","notificationEnabled":false,"notifications":false,"profileBackgroundColor":"","profileBackgroundImageUrl":"","profileBackgroundTile":"","profileImageURL":"http://tp2.sinaimg.cn/1657236125/50/5606978745/1","profileLinkColor":"","profileSidebarBorderColor":"","profileSidebarFillColor":"","profileTextColor":"","protected":false,"rateLimitLimit":-1,"rateLimitRemaining":-1,"rateLimitReset":-1,"screenName":"环球企业家杂志","statusFavorited":false,"statusId":-1,"statusInReplyToStatusId":-1,"statusInReplyToUserId":-1,"statusTruncated":false,"statusesCount":5247,"timeZone":"","uRL":"http://www.gemag.com.cn","userDomain":"gemag","utcOffset":-1,"verified":true}},"source":"<a href=\"http://weibo.com\" rel=\"nofollow\">新浪微博</a>","text":"//@王兆春 我党为什么把那么多的老百姓劳动成果换成绿纸?还不给劳动人民呢?真是奇怪!","thumbnail_pic":"","truncated":false,"user":{"createdAt":1256832000000,"description":"《环球企业家》官方微博:关于中国和全球商业的严肃口水","favouritesCount":134,"followersCount":476617,"following":false,"friendsCount":555,"gender":"m","geoEnabled":false,"id":1657236125,"location":"北京 东城区","name":"环球企业家杂志","notificationEnabled":false,"notifications":false,"profileBackgroundColor":"","profileBackgroundImageUrl":"","profileBackgroundTile":"","profileImageURL":"http://tp2.sinaimg.cn/1657236125/50/5606978745/1","profileLinkColor":"","profileSidebarBorderColor":"","profileSidebarFillColor":"","profileTextColor":"","protected":false,"rateLimitLimit":-1,"rateLimitRemaining":-1,"rateLimitReset":-1,"screenName":"环球企业家杂志","statusFavorited":false,"statusId":-1,"statusInReplyToStatusId":-1,"statusInReplyToUserId":-1,"statusTruncated":false,"statusesCount":5247,"timeZone":"","uRL":"http://www.gemag.com.cn","userDomain":"gemag","utcOffset":-1,"verified":true}}


这个是因为weibo4android.Status这个类没有缺省构造函数,所以无法反序列化。
0 请登录后投票
   发表时间:2011-08-09  
lanblue173 写道
wenshao 写道
lanblue173 写道
发现一个问题,我在用fastjson 来对己新浪微博的提供的SDK(weiboandroid4)里的Stuts类进行操作

发现使用 toStringJSON 能正常将Stuts转为json 但通过parseObject方法还原时! 就报异常!

请问是什么原因!


请告诉我具体异常信息!






执行:Status o=JSON.parseObject(st,Status.class);报以下错误
产生的异常
com.alibaba.fastjson.JSONException: class not has default constructor : weibo4android.Status

这个是因为weibo4android.Status这个类没有缺省构造函数,所以无法反序列化。



Status有构造函数,是否要默认符合fastjson指定格式的构造函数,我现在把Status这个类上传到附件中!
0 请登录后投票
   发表时间:2011-08-09  
楼上的bean类,包含了非java自定义类型,这种bean设计不应该被推荐,复杂的项目容易引起问题
0 请登录后投票
   发表时间:2011-08-09  
Status类还存在循环引用
0 请登录后投票
   发表时间:2011-08-09   最后修改:2011-08-09
我想知道怎么和spring整合起来,能搞个插件集成下吧?和jackson一样内置到spring中
0 请登录后投票
   发表时间:2011-08-09  
文档几乎没有。。我就想知道,对于一个实体,如果属性为null,在序列化后,我仍希望全部序列化所有字段,为null的字段则序列化为空,而不是不输出。这该怎么设置?
0 请登录后投票
   发表时间:2011-08-09  
还要补充下文档哦,温少,官方上面很多是以前版本的例子和说明。
0 请登录后投票
论坛首页 Java企业应用版

跳转论坛:
Global site tag (gtag.js) - Google Analytics