FastJSON应用前测试 fastjson maven

原创作品,允许转载,转载时请务必以超链接形式标明文章原始出处、作者信息和本声明。否则将追究法律责任。http://lavasoft.blog.51cto.com/62575/723192

FastJSON 应用前测试

FastJSON是一个很好的java开源json工具类库,相比其他同类的json类库,它的速度的确是fast,最快!但是文档做得不好,在应用前不得不亲测一些功能。

实际上其他的json处理工具都和它差不多,api也有几分相似。

一、JSON规范

JSON是一个标准规范,用于数据交互,规范的中文文档如下:

http://www.json.org/json-zh.html

参看规范,来对比JSON类库处理结果。

二、fastjson的一些简要说明

1、了解fastjson

Fastjson介绍

Fastjson是一个Java语言编写的JSON处理器。

1、遵循http://json.org标准,为其官方网站收录的参考实现之一。

2、功能qiang打,支持JDK的各种类型,包括基本的JavaBean、Collection、Map、Date、Enum、泛型。

3、无依赖,不需要例外额外的jar,能够直接跑在JDK上。

4、开源,使用Apache License 2.0协议开源。http://code.alibabatech.com/wiki/display/FastJSON/Home

如果获得Fastjson?

SVN:http://code.alibabatech.com/svn/fastjson/trunk/

WIKI:http://code.alibabatech.com/wiki/display/FastJSON/Home

Issue Tracking:http://code.alibabatech.com/jira/browse/FASTJSON

2、主要的使用入口

Fastjson API入口类是com.alibaba.fastjson.JSON,常用的序列化操作都可以在JSON类上的静态方法直接完成。

publicstaticfinalObject parse(String text);// 把JSON文本parse为JSONObject或者JSONArray

publicstaticfinalJSONObject parseObject(String text);// 把JSON文本parse成JSONObject

publicstaticfinal<T> T parseObject(String text, Class<T> clazz);// 把JSON文本parse为JavaBean

publicstaticfinalJSONArray parseArray(String text);// 把JSON文本parse成JSONArray

publicstaticfinal<T> List<T> parseArray(String text, Class<T> clazz);//把JSON文本parse成JavaBean集合

publicstaticfinalString toJSONString(Object object);// 将JavaBean序列化为JSON文本

publicstaticfinalString toJSONString(Object object,booleanprettyFormat);// 将JavaBean序列化为带格式的JSON文本

publicstaticfinalObject toJSON(Object javaObject); 将JavaBean转换为JSONObject或者JSONArray。

3、有关类库的一些说明

SerializeWriter:相当于StringBuffer

JSONArray:相当于List<Object>

JSONObject:相当于Map<String, Object>

JSON反序列化没有真正数组,本质类型都是List<Object>

4、fastjson还有很多很高级的特性,比如支持注解、支持全类型序列化,这些都是很好的特性,功能强大,不在本次测试范围。

三、测试代码

packagelavasoft.stu.json;

importcom.alibaba.fastjson.JSON;

importcom.alibaba.fastjson.serializer.SerializeConfig;

importcom.alibaba.fastjson.serializer.SimpleDateFormatSerializer;

importjava.util.*;

/**

* Created by IntelliJ IDEA.

*

* @author leizhimin 11-11-22 上午9:15

*/

publicclassFoo {

privateString vString ="vStringhehhehe";

privatecharvchar = 'x';

privatebytevbyte = 64;

privateshortvshort = 128;

privateintvint = 65535;

privatelongvlong = 9999999L;

privatefloatvfloat = 12.1f;

privatedoublevdouble = 22.203d;

privatebooleanvboolean =false;

//private Date vdate = new Date();

privateDate dddd =newDate();

privateDate vDate =newDate();

privateDate v_Date =newDate();

privateObject vnull =null;

privateString[] avString = {"aaa","bbb","ccc"};

privateint[] avint = {1, 2, 3, 4};

privateboolean[] avboolean = {true,false,true,true};

privateList<String> listString =newArrayList<String>();

privateMap<String, String> map =newHashMap<String, String>();

privateBar bar =newBar();

privateBar[] avBar = {newBar(),newBar()};

privateList<Bar> listBar =newArrayList<Bar>();

{

listString.add("listString1");

listString.add("listString2");

listString.add("listString3");

map.put("x","s11111x");

map.put("y","s22222y");

map.put("z","s33333z");

listBar.add(newBar());

listBar.add(newBar());

listBar.add(newBar());

}

publicString getvString() {

returnvString;

}

publicvoidsetvString(String vString) {

this.vString = vString;

}

publicchargetVchar() {

returnvchar;

}

publicvoidsetVchar(charvchar) {

this.vchar = vchar;

}

publicbytegetVbyte() {

returnvbyte;

}

publicvoidsetVbyte(bytevbyte) {

this.vbyte = vbyte;

}

publicshortgetVshort() {

returnvshort;

}

publicvoidsetVshort(shortvshort) {

this.vshort = vshort;

}

publicintgetVint() {

returnvint;

}

publicvoidsetVint(intvint) {

this.vint = vint;

}

publiclonggetVlong() {

returnvlong;

}

publicvoidsetVlong(longvlong) {

this.vlong = vlong;

}

publicfloatgetVfloat() {

returnvfloat;

}

publicvoidsetVfloat(floatvfloat) {

this.vfloat = vfloat;

}

publicdoublegetVdouble() {

returnvdouble;

}

publicvoidsetVdouble(doublevdouble) {

this.vdouble = vdouble;

}

publicbooleanisVboolean() {

returnvboolean;

}

publicvoidsetVboolean(booleanvboolean) {

this.vboolean = vboolean;

}

publicDate getDddd() {

returndddd;

}

publicvoidsetDddd(Date dddd) {

this.dddd = dddd;

}

publicDate getV_Date() {

returnv_Date;

}

publicvoidsetV_Date(Date v_Date) {

this.v_Date = v_Date;

}

publicDate getvDate() {

returnvDate;

}

publicvoidsetvDate(Date vDate) {

this.vDate = vDate;

}

publicObject getVnull() {

returnvnull;

}

publicvoidsetVnull(Object vnull) {

this.vnull = vnull;

}

publicString[] getAvString() {

returnavString;

}

publicvoidsetAvString(String[] avString) {

this.avString = avString;

}

publicint[] getAvint() {

returnavint;

}

publicvoidsetAvint(int[] avint) {

this.avint = avint;

}

publicboolean[] getAvboolean() {

returnavboolean;

}

publicvoidsetAvboolean(boolean[] avboolean) {

this.avboolean = avboolean;

}

publicList<String> getListString() {

returnlistString;

}

publicvoidsetListString(List<String> listString) {

this.listString = listString;

}

publicMap<String, String> getMap() {

returnmap;

}

publicvoidsetMap(Map<String, String> map) {

this.map = map;

}

publicBar getBar() {

returnbar;

}

publicvoidsetBar(Bar bar) {

this.bar = bar;

}

publicBar[] getAvBar() {

returnavBar;

}

publicvoidsetAvBar(Bar[] avBar) {

this.avBar = avBar;

}

publicList<Bar> getListBar() {

returnlistBar;
FastJSON应用前测试 fastjson maven

}

publicvoidsetListBar(List<Bar> listBar) {

this.listBar = listBar;

}

}

packagelavasoft.stu.json;

importcom.alibaba.fastjson.JSON;

importcom.alibaba.fastjson.serializer.SerializeConfig;

importcom.alibaba.fastjson.serializer.SimpleDateFormatSerializer;

importjava.util.Date;

importjava.util.Random;

/**

* Created by IntelliJ IDEA.

*

* @author leizhimin 11-11-22 上午9:15

*/

publicclassBar {

publicstaticSerializeConfig mapping =newSerializeConfig();

privateString barName;

privateintbarAge;

privateDate barDate =newDate();

static{

mapping.put(Date.class,newSimpleDateFormatSerializer("yyyy-MM-dd"));

}

{

Random r =newRandom();

barName ="sss_"+String.valueOf(r.nextFloat());

barAge = r.nextInt();

}

publicstaticvoidmain(String[] args) {

Object obj = JSON.toJSON(newBar());

String x1 = JSON.toJSONString(newBar(),true);

System.out.println(x1);

String x2 = JSON.toJSONString(newBar(), mapping);

System.out.println(x2);

}

publicString getBarName() {

returnbarName;

}

publicvoidsetBarName(String barName) {

this.barName = barName;

}

publicintgetBarAge() {

returnbarAge;

}

publicvoidsetBarAge(intbarAge) {

this.barAge = barAge;

}

publicDate getBarDate() {

returnbarDate;

}

publicvoidsetBarDate(Date barDate) {

this.barDate = barDate;

}

@Override

publicString toString() {

return"Bar{"+

"barName='"+ barName + ''' +

", barAge="+ barAge +

", barDate="+ barDate +

'}';

}

}

packagelavasoft.stu.json;

importcom.alibaba.fastjson.JSON;

importcom.alibaba.fastjson.JSONArray;

importcom.alibaba.fastjson.serializer.SerializeConfig;

importcom.alibaba.fastjson.serializer.SimpleDateFormatSerializer;

importjava.util.*;

/**

* Created by 2IntelliJ IDEA.

*

* @author leizhimin 11-11-22 上午9:45

*/

publicclassTest {

privatestaticSerializeConfig mapping =newSerializeConfig();static{

mapping.put(Date.class,newSimpleDateFormatSerializer("yyyy-MM-dd HH:mm:ss"));

}

publicstaticvoidmain(String[] args) {

Foo f1 =newFoo();

Date date =newDate();

String text = JSON.toJSONString(date, mapping);

System.out.println(text);

System.out.println(JSON.toJSONString(f1,true));

String x2 =JSON.toJSONString(f1, mapping);

System.out.println(x2);

}

publicstaticvoidjson2List(){

//List -> JSON array

List<Bar> barList =newArrayList<Bar>();

barList.add(newBar());

barList.add(newBar());

barList.add(newBar());

String json= JSON.toJSONString(barList,true);

System.out.println(json);

//JSON array -> List

List<Bar> barList1 = JSON.parseArray(json,Bar.class);

for(Bar bar : barList1) {

System.out.println(bar.toString());

}

}

publicstaticvoidjson2Map(){

//Map -> JSON

Map<String,Bar> map =newHashMap<String, Bar>();

map.put("a",newBar());

map.put("b",newBar());

map.put("c",newBar());

String json = JSON.toJSONString(map,true);

System.out.println(json);

//JSON -> Map

Map<String,Bar> map1 = (Map<String,Bar>)JSON.parse(json);

for(String key : map1.keySet()) {

System.out.println(key+":"+map1.get(key));

}

}

publicstaticvoidarray2JSON(){

String[] arr_String= {"a","b","c"};

String json_arr_String = JSON.toJSONString(arr_String,true);

System.out.println(json_arr_String);

JSONArray jsonArray = JSON.parseArray(json_arr_String);

for(Object o : jsonArray) {

System.out.println(o);

}

System.out.println(jsonArray);

}

publicstaticvoidarray2JSON2(){

Bar[] arr_Bar= {newBar(),newBar(),newBar()};

String json_arr_Bar = JSON.toJSONString(arr_Bar,true);

System.out.println(json_arr_Bar);

JSONArray jsonArray = JSON.parseArray(json_arr_Bar);

for(Object o : jsonArray) {

System.out.println(o);

}

System.out.println(jsonArray);

}

publicstaticvoidmap2JSON(){

Map map =newHashMap();

map.put("a","aaa");

map.put("b","bbb");

map.put("c","ccc");

String json=JSON.toJSONString(map);

System.out.println(json);

Map map1 = JSON.parseObject(json);

for(Object o : map.entrySet()) {

Map.Entry<String,String> entry = (Map.Entry<String,String>)o;

System.out.println(entry.getKey()+"--->"+entry.getValue());

}

}

}

说明:

SerializeConfig:是对序列化过程中一些序列化过程的特殊配置,这里用作日期格式的定义。

有关需要带类型的全类型序列化过程,需要调用JSON.toJSONStringZ()方法。

需要美化输出时候,需要打开序列化美化开关,在方法中true那个参数。

输出结果:

"2011-11-23 23:30:33"

{

"avBar":[{

"barAge":174398800,

"barDate":1322062233062,

"barName":"sss_0.62378174"

},{

"barAge":38938962,

"barDate":1322062233062,

"barName":"sss_0.36014742"

}],

"avString":["aaa","bbb","ccc"],

"avboolean":[true,false,true,true],

"avint":[1,2,3,4],

"bar":{

"barAge":1601495948,

"barDate":1322062233062,

"barName":"sss_0.46644872"

},

"dddd":1322062233046,

"listBar":[

{

"barAge":-1090113522,

"barDate":1322062233062,

"barName":"sss_0.83562374"

},

{

"barAge":478603194,

"barDate":1322062233062,

"barName":"sss_0.59483266"

},

{

"barAge":1118357669,

"barDate":1322062233062,

"barName":"sss_0.9961642"

}

],

"listString":[

"listString1",

"listString2",

"listString3"

],

"map":{"x":"s11111x","y":"s22222y","z":"s33333z"},

"v_Date":1322062233046,

"vboolean":false,

"vbyte":64,

"vchar":"x",

"vdouble":22.203,

"vfloat":12.1,

"vint":65535,

"vlong":9999999,

"vshort":128

}

{"avBar":[{"barAge":174398800,"barDate":"2011-11-23 23:30:33","barName":"sss_0.62378174"},{"barAge":38938962,"barDate":"2011-11-23 23:30:33","barName":"sss_0.36014742"}],"avString":["aaa","bbb","ccc"],"avboolean":[true,false,true,true],"avint":[1,2,3,4],"bar":{"barAge":1601495948,"barDate":"2011-11-23 23:30:33","barName":"sss_0.46644872"},"dddd":"2011-11-23 23:30:33","listBar":[{"barAge":-1090113522,"barDate":"2011-11-23 23:30:33","barName":"sss_0.83562374"},{"barAge":478603194,"barDate":"2011-11-23 23:30:33","barName":"sss_0.59483266"},{"barAge":1118357669,"barDate":"2011-11-23 23:30:33","barName":"sss_0.9961642"}],"listString":["listString1","listString2","listString3"],"map":{"x":"s11111x","y":"s22222y","z":"s33333z"},"v_Date":"2011-11-23 23:30:33","vboolean":false,"vbyte":64,"vchar":"x","vdouble":22.203,"vfloat":12.1,"vint":65535,"vlong":9999999,"vshort":128}

Process finished with exit code 0

fastjson通过各方面测试都很好,功能性能都是No.1,喜欢,它的源代码质量很高,作者也煞费苦心,将性能做到了最好,全免超越其他的json类库。

从其中的源代码中,可以学到很多东西,在阅读源码后,不得不佩服作者高超的程序设计水平,和高质量的编码。

感谢alibaba对此工具开源。

fastjson-1.1.10-sources.jar

由于fastjson缺乏良好的api文档,在阅读源码过程中,参考了网上一些文档:

http://code.alibabatech.com/wiki/display/FastJSON/Home

http://www.ibm.com/developerworks/cn/education/java/j-generics/section5.html

http://archive.cnblogs.com/a/1713531/

http://www.iflym.com/index.php/code/alibaba-fastjson-json-serializer-chapter-source-analyse-one-global-analyse.html

http://www.cnblogs.com/hoojo/archive/2011/04/21/2023805.html

http://archive.cnblogs.com/a/1713531/

本文出自 “熔 岩” 博客,请务必保留此出处http://lavasoft.blog.51cto.com/62575/723192

  

爱华网本文地址 » http://www.413yy.cn/a/25101011/91734.html

更多阅读

photoshop 前景色和背景色的应用 前景色和背景色的区别

? ??查了很多资料,前景色和背景色的含义?很是模糊,后来联想到现实中画家的画板,就很清楚了。前景色和背景色只是根据人们画画的效果来分的。把整个画布都用一种颜色填充,就是背景色;工具如画笔,画刷使用的颜色就是前景色。一前一后,很明显。

如何通过iPerf来测定网络性能 iperf测试网络

如何通过iPerf来测定网络性能?Iperf是一个简单易用测量TCP/UDP的吞吐量、损耗和延迟的工具。应用前的准备iPerf是专门用于简化TCP性能优化的工具,使用它可以很容易地测量吞吐量和带宽的最大值。当与UDP一起使用时,iPerf还可以测量数据

旧文:《命令与征服:将军》Command&Conquer:Generals 初体验

继魔兽争霸系列和帝国时代系列去年底相继成功跨入3D时代后,三大即时战略中最正统的命令与征服系列新作将军终于在这新春佳节之际姗姗来迟。粉墨登场的将军首先就以靓丽的外表令我吃了一惊,想想几月前测试版时的样子,简直就是丑小鸭一

天津工业大学光源与照明专业简介 天津科技大学

来源:招生办 作者: 时间:2011-05-24 16:27:00 点击数:812本专业是国内首家以培养半导体照明技术领域人才为主的特色学科专业,于2010年被列为“天津市普通高等学校品牌专业”。本专业培养具有光源与照明技术领域的基本理论、基本知识和

声明:《FastJSON应用前测试 fastjson maven》为网友宇宙第一帅胚分享!如侵犯到您的合法权益请联系我们删除