Fastjson 1.1.3是一个重要的功能增强版本,在这个版本开始,fastjson支持循环引用,完善支持WriteClassName的特性,使得fastjson开始具备替换hessian和java serialize的能力。
支持循环引用。
很多用户在初次使用fastjson时都遇到了循环引用的问题,这是反映最多的问题,我越来越认识到支持循环引用的必要性。经过测试,加入循环应用的支持对性能的影响很小,小于5%。我决定在1.1.3加入循环应用支持,而且是缺省打开这个特性。
fastjson支持的循环应用是自然的,不需要象jackson那样,需要Annotation设置。fastjson支持的引用,是基于path的,方便javascript解析。
语法
{"$ref":"$"} // 引用根对象 {"$ref":"$"} // 引用根对象 {"$ref":"@"} // 引用自己 {"$ref":".."} // 引用父对象 {"$ref":"../.."} // 引用父对象的父对象 {"$ref":"$.members[0].reportTo"} // 基于路径的引用
具体例子看这里:http://code.alibabatech.com/wiki/pages/viewpage.action?pageId=5832796
自省支持
在标准中,将对象序列化为JSON文本之后,就丢失了类型信息。虽然fastjson能够支持parse时传入Class来实现类型的识别,但是这个有局限性,在fastjson 1.1.3之后,完善支持一个特性,就是自省。
具体例子请看这里:http://code.alibabatech.com/wiki/pages/viewpage.action?pageId=5832803
支持非缺省构造函数和工厂方法构造对象
fastjson 1.1.3开始支持没有缺省构造函数的Java对象的反序列化。通过@JSONCreator来标识构造对象的方式。
具体请看这里:
* 非缺省构造函数 http://code.alibabatech.com/wiki/pages/viewpage.action?pageId=5832818
* 工厂方法 http://code.alibabatech.com/wiki/pages/viewpage.action?pageId=5832821
原型接口
以接口的方式来操作json数据,这个特性是借鉴了jackson的功能。
具体例子看这里:http://code.alibabatech.com/wiki/pages/viewpage.action?pageId=5832824
How to get it?
If you're Maven user, just use our maven repository(http://code.alibabatech.com/mvn/releases/) with folloging dependency
<dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>1.1.3</version> </dependency>
Downlaods
Binary : http://code.alibabatech.com/mvn/releases/com/alibaba/fastjson/1.1.3/fastjson-1.1.3.jar
Source :http://code.alibabatech.com/mvn/releases/com/alibaba/fastjson/1.1.3/fastjson-1.1.3-sources.jar
Subversion : http://code.alibabatech.com/svn/fastjson/trunk/fastjson/
Issue tracking
http://code.alibabatech.com/jira/plugins/servlet/project-config/FASTJSON/versions
* Bug
[FASTJSON-73] - 不支持Set类型数据的反序列
[FASTJSON-77] - JSONSerializerMap存在兼容问题
[FASTJSON-78] - java.util.concurrent.TimeUnit序列化数据不对
* Improvement
[FASTJSON-71] - 使用Base64编码优化byte[]字段的序列化和反序列化性能
[FASTJSON-72] - 使用SoftReference处理ThreadLocalCache
* New Feature
[FASTJSON-74] - 支持序列化时输出类型信息
[FASTJSON-75] - 支持Key类型不是String的Map的序列化和反序列化
[FASTJSON-80] - 原型接口
[FASTJSON-81] - 支持循环引用
1 楼
hukl424
2011-09-02
温少,fastjson最开始好像是没有使用asm方式的吧
2 楼
issboy
2011-09-03
版本:fastjson 1.1.3
问题:JavaBean中包含List时序列化出错。
public class Cafe implements Serializable {
private static final long serialVersionUID = 1L;
private String id;
private String name;
private double lat;
private double lon;
private double rank;
private List<Food> foods;
}
序列化List<Cafe>输出为:
{"totalCount":551,"pageSize":15,"pageNo":1,"totalPages":37,"result":[
{
"id":"2025076",
"lat":39.97832,
"lon":116.31568,
"name":"味百年(新中关店)",
"rank":4.137166746083727
},
{
"foods":[
{
"cafeId":"3512086",
"id":"01012fb4eebe50095b",
"img":"/img/food/20110503/01012fb4eebdcf095a.jpg",
"name":"绿茶冰激凌黑凉粉",
"rank":5,
"status":1
},
{
"cafeId":"3512086",
"id":"01012f10c9b5d210d8",
"img":"/img/food/20110401/01012f10c9b51310d7.jpg",
"name":"芒果椰汁黑糯米甜甜",
"rank":3,
"status":1
},
{
"cafeId":"3512086",
"id":"01012f39926ca622e7",
"img":"/img/food/20110409/01012f39926c4722e6.jpg",
"name":"布丁",
"rank":0,
"status":1
},
{
"cafeId":"3512086",
"id":"01012f10cb40ff10e4",
"img":"/img/food/20110401/01012f10cb403d10e3.jpg",
"name":"百合莲子红豆沙",
"rank":0,
"status":1
}
],
"id":"3512086",
"lat":39.97835,
"lon":116.31568,
"name":"满记甜品(新中关店)",
"rank":0.932199408246478
},
{
"foods":[
{
"cafeId":"4295691",
"id":"01012f71587c3801d4",
"img":"/img/food/20110420/01012f7158718201d3.jpg",
"name":"鸡翅",
"rank":7,
"status":1
},
{
"cafeId":"4295691",
"id":"01012e1858cc851207",
"img":"/img/food/20110212/01012e1858cb831206.jpg",
"name":"笑脸牛肉饼",
"rank":4,
"status":1
},
{
"cafeId":"4295691",
"id":"01012e185aa111120f",
"img":"/img/food/20110212/01012e185aa071120e.jpg",
"name":"面",
"rank":2,
"status":1
},
{
"cafeId":"4295691",
"id":"01012e18596cd1120b",
"img":"/img/food/20110212/01012e18596c34120a.jpg",
"name":"柠檬水",
"rank":0,
"status":1
}
],
"id":"4295691",
"lat":39.97835,
"lon":116.31568,
"name":"SEVENANA",
"rank":2.105837501003912
},
{
"foods":[{
"cafeId":"3525268",
"id":"01012f91d7eea80f6b",
"img":"/img/food/20110426/01012f91d7edc10f6a.jpg",
"name":"红豆布丁牛奶冰",
"rank":4,
"status":1
}],
"id":"3525268",
"lat":39.97835,
"lon":116.31568,
"name":"找茶(新中关店)",
"rank":3.4340278689546793
},
{
"id":"1997878",
"lat":39.97835,
"lon":116.31568,
"name":"康师傅私房牛肉面(新中关店)",
"rank":3.5820154670853954
},
{
"id":"4285818",
"lat":39.97835,
"lon":116.31568,
"name":"钰花溪(新中关店)",
"rank":3.6299263877804346
},
{
"id":"2048929",
"lat":39.97835,
"lon":116.31568,
"name":"鹭鹭酒家(中关村店)",
"rank":4.132180958543946
},
{
"id":"3636082",
"lat":39.97835,
"lon":116.31568,
"name":"查里屋果仁(新中关大厦店)",
"rank":4.422332371358068
},
{
"id":"3473497",
"lat":39.97835,
"lon":116.31568,
"name":"纽约纽约多纳圈咖啡(新中关店)",
"rank":4.6451203522322455
},
{
"foods":[{
"cafeId":"3570217",
"id":"01012e70e52bbb1f0f",
"img":"/img/food/20110301/01012e70e52afe1f0e.jpg",
"name":"鸳鸯锅",
"rank":3,
"status":1
}],
"id":"3570217",
"lat":39.97806,
"lon":116.31558,
"name":"新辣道梭边鱼(欧美汇店)",
"rank":1.0208611536075325
},
{
"id":"1974373",
"lat":39.97832,
"lon":116.31596,
"name":"呷哺呷哺(新中关购物中心店)",
"rank":2.159466426443568
},
{
"foods":[
{
"cafeId":"2025079",
"id":"01012f66b8d442365e",
"img":"/img/food/20110418/01012f66b8c9f9365d.jpg",
"name":"巧克力口味+乳酪口味泡芙",
"rank":3,
"status":1
},
{
"cafeId":"2025079",
"id":"01012f70caf9f80118",
"img":"/img/food/20110420/01012f70caf94b0117.jpg",
"name":"提拉米苏",
"rank":2,
"status":1
},
{
"cafeId":"2025079",
"id":"01012e707e69751ece",
"img":"/img/food/20110301/01012e707e688d1ecd.jpg",
"name":"泡芙",
"rank":1,
"status":1
},
{
"cafeId":"2025079",
"id":"01012e707dfb7a1eca",
"img":"/img/food/20110301/01012e707dfa5a1ec9.jpg",
"name":"布丁",
"rank":0,
"status":1
}
],
"id":"2025079",
"lat":39.97813,
"lon":116.31533,
"name":"贝儿多爸爸泡芙工房(中关村店)",
"rank":0.6826562904361041
},
{
"id":"3203302",
"lat":39.97785,
"lon":116.31587,
"name":"玖柒港式甜品(新中关店)",
"rank":4.0139544630667645
},
{
"id":"3287599",
"lat":39.97785,
"lon":116.31587,
"name":"梅霖水吧",
"rank":4.643808634468885
},
{
"id":"4232755",
"lat":39.97848,
"lon":116.31607,
"name":"芭克闪冰(新中关店)",
"rank":3.081963969031375
}
]}
问题:JavaBean中包含List时序列化出错。
public class Cafe implements Serializable {
private static final long serialVersionUID = 1L;
private String id;
private String name;
private double lat;
private double lon;
private double rank;
private List<Food> foods;
}
序列化List<Cafe>输出为:
{"totalCount":551,"pageSize":15,"pageNo":1,"totalPages":37,"result":[
{
"id":"2025076",
"lat":39.97832,
"lon":116.31568,
"name":"味百年(新中关店)",
"rank":4.137166746083727
},
{
"foods":[
{
"cafeId":"3512086",
"id":"01012fb4eebe50095b",
"img":"/img/food/20110503/01012fb4eebdcf095a.jpg",
"name":"绿茶冰激凌黑凉粉",
"rank":5,
"status":1
},
{
"cafeId":"3512086",
"id":"01012f10c9b5d210d8",
"img":"/img/food/20110401/01012f10c9b51310d7.jpg",
"name":"芒果椰汁黑糯米甜甜",
"rank":3,
"status":1
},
{
"cafeId":"3512086",
"id":"01012f39926ca622e7",
"img":"/img/food/20110409/01012f39926c4722e6.jpg",
"name":"布丁",
"rank":0,
"status":1
},
{
"cafeId":"3512086",
"id":"01012f10cb40ff10e4",
"img":"/img/food/20110401/01012f10cb403d10e3.jpg",
"name":"百合莲子红豆沙",
"rank":0,
"status":1
}
],
"id":"3512086",
"lat":39.97835,
"lon":116.31568,
"name":"满记甜品(新中关店)",
"rank":0.932199408246478
},
{
"foods":[
{
"cafeId":"4295691",
"id":"01012f71587c3801d4",
"img":"/img/food/20110420/01012f7158718201d3.jpg",
"name":"鸡翅",
"rank":7,
"status":1
},
{
"cafeId":"4295691",
"id":"01012e1858cc851207",
"img":"/img/food/20110212/01012e1858cb831206.jpg",
"name":"笑脸牛肉饼",
"rank":4,
"status":1
},
{
"cafeId":"4295691",
"id":"01012e185aa111120f",
"img":"/img/food/20110212/01012e185aa071120e.jpg",
"name":"面",
"rank":2,
"status":1
},
{
"cafeId":"4295691",
"id":"01012e18596cd1120b",
"img":"/img/food/20110212/01012e18596c34120a.jpg",
"name":"柠檬水",
"rank":0,
"status":1
}
],
"id":"4295691",
"lat":39.97835,
"lon":116.31568,
"name":"SEVENANA",
"rank":2.105837501003912
},
{
"foods":[{
"cafeId":"3525268",
"id":"01012f91d7eea80f6b",
"img":"/img/food/20110426/01012f91d7edc10f6a.jpg",
"name":"红豆布丁牛奶冰",
"rank":4,
"status":1
}],
"id":"3525268",
"lat":39.97835,
"lon":116.31568,
"name":"找茶(新中关店)",
"rank":3.4340278689546793
},
{
"id":"1997878",
"lat":39.97835,
"lon":116.31568,
"name":"康师傅私房牛肉面(新中关店)",
"rank":3.5820154670853954
},
{
"id":"4285818",
"lat":39.97835,
"lon":116.31568,
"name":"钰花溪(新中关店)",
"rank":3.6299263877804346
},
{
"id":"2048929",
"lat":39.97835,
"lon":116.31568,
"name":"鹭鹭酒家(中关村店)",
"rank":4.132180958543946
},
{
"id":"3636082",
"lat":39.97835,
"lon":116.31568,
"name":"查里屋果仁(新中关大厦店)",
"rank":4.422332371358068
},
{
"id":"3473497",
"lat":39.97835,
"lon":116.31568,
"name":"纽约纽约多纳圈咖啡(新中关店)",
"rank":4.6451203522322455
},
{
"foods":[{
"cafeId":"3570217",
"id":"01012e70e52bbb1f0f",
"img":"/img/food/20110301/01012e70e52afe1f0e.jpg",
"name":"鸳鸯锅",
"rank":3,
"status":1
}],
"id":"3570217",
"lat":39.97806,
"lon":116.31558,
"name":"新辣道梭边鱼(欧美汇店)",
"rank":1.0208611536075325
},
{
"id":"1974373",
"lat":39.97832,
"lon":116.31596,
"name":"呷哺呷哺(新中关购物中心店)",
"rank":2.159466426443568
},
{
"foods":[
{
"cafeId":"2025079",
"id":"01012f66b8d442365e",
"img":"/img/food/20110418/01012f66b8c9f9365d.jpg",
"name":"巧克力口味+乳酪口味泡芙",
"rank":3,
"status":1
},
{
"cafeId":"2025079",
"id":"01012f70caf9f80118",
"img":"/img/food/20110420/01012f70caf94b0117.jpg",
"name":"提拉米苏",
"rank":2,
"status":1
},
{
"cafeId":"2025079",
"id":"01012e707e69751ece",
"img":"/img/food/20110301/01012e707e688d1ecd.jpg",
"name":"泡芙",
"rank":1,
"status":1
},
{
"cafeId":"2025079",
"id":"01012e707dfb7a1eca",
"img":"/img/food/20110301/01012e707dfa5a1ec9.jpg",
"name":"布丁",
"rank":0,
"status":1
}
],
"id":"2025079",
"lat":39.97813,
"lon":116.31533,
"name":"贝儿多爸爸泡芙工房(中关村店)",
"rank":0.6826562904361041
},
{
"id":"3203302",
"lat":39.97785,
"lon":116.31587,
"name":"玖柒港式甜品(新中关店)",
"rank":4.0139544630667645
},
{
"id":"3287599",
"lat":39.97785,
"lon":116.31587,
"name":"梅霖水吧",
"rank":4.643808634468885
},
{
"id":"4232755",
"lat":39.97848,
"lon":116.31607,
"name":"芭克闪冰(新中关店)",
"rank":3.081963969031375
}
]}
3 楼
issboy
2011-09-03
自己看走眼了,是误报
4 楼
alosin
2011-09-05
温少,解析数组形如:
[,[[832186,,,'1.545',,,,,,0,,],[,,,1]],[1,[[8595637,,],[1,[3,,'0.90','0.92','1.50'], [2,1]]]]]
逗号前怎么不是用null来占位呢,而是直接pass掉了。这样解析不能真正还原数据的格式啊,json-lib采用的JSONNull来占位的
[,[[832186,,,'1.545',,,,,,0,,],[,,,1]],[1,[[8595637,,],[1,[3,,'0.90','0.92','1.50'], [2,1]]]]]
逗号前怎么不是用null来占位呢,而是直接pass掉了。这样解析不能真正还原数据的格式啊,json-lib采用的JSONNull来占位的
5 楼
alosin
2011-09-05
[size=large][size=small]温少,解析数组形如:
[,[[832186,,,'1.545',,,,,,0,,],[,,,1]],[1,[[8595637,,],[1,[3,,'0.90','0.92','1.50'], [2,1]]]]] [/size][/size]
[,[[832186,,,'1.545',,,,,,0,,],[,,,1]],[1,[[8595637,,],[1,[3,,'0.90','0.92','1.50'], [2,1]]]]] [/size][/size]