FastJSON 应用前测试

前端之家收集整理的这篇文章主要介绍了FastJSON 应用前测试前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
FastJSON 应用前测试
FastJSON是一个很好的java开源json工具类库,相比其他同类的json类库,它的速度的确是fast,最快!但是文档做得不好,在应用前不得不亲测一些功能
实际上其他的json处理工具都和它差不多,api也有几分相似。
一、JSON规范
JSON是一个标准规范,用于数据交互,规范的中文文档如下:
参看规范,来对比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
2、主要的使用入口
Fastjson API入口类是com.alibaba.fastjson.JSON,常用的序列化操作都可以在JSON类上的静态方法直接完成。
public static finalObject parse(String text); // 把JSON文本parse为JSONObject或者JSONArray
public static finalJSONObject parSEObject(String text); // 把JSON文本parse成JSONObject
public static final<T> T parSEObject(String text,Class<T> clazz); // 把JSON文本parse为JavaBean
public static finalJSONArray parseArray(String text); // 把JSON文本parse成JSONArray
public static final<T> List<T> parseArray(String text,Class<T> clazz); //把JSON文本parse成JavaBean集合
public static finalString toJSONString(Object object); // 将JavaBean序列化为JSON文本
public static finalString toJSONString(Object object,booleanprettyFormat); // 将JavaBean序列化为带格式的JSON文本
public static finalObject toJSON(Object javaObject); 将JavaBean转换为JSONObject或者JSONArray。
3、有关类库的一些说明
SerializeWriter:相当于StringBuffer
J
SONArray:相当于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
*/

public classFoo {
privateString vString = "vStringhehhehe";
private charvchar = 'x';

private bytevbyte = 64;
private shortvshort = 128;
private intvint = 65535;
private longvlong = 9999999L;

private floatvfloat = 12.1f;
private doublevdouble = 22.203d;

private booleanvboolean = false;

//private Date vdate = new Date();
privateDate dddd = newDate();
privateDate vDate = newDate();
privateDate v_Date = newDate();
privateObject vnull = null;

privateString[] avString = { "aaa","bbb","ccc"};
private int[] avint = {1,2,3,4};
private boolean[] 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;
}

public voidsetvString(String vString) {
this.vString = vString;
}

public chargetVchar() {
returnvchar;
}

public voidsetVchar( charvchar) {
this.vchar = vchar;
}

public bytegetVbyte() {
returnvbyte;
}

public voidsetVbyte( bytevbyte) {
this.vbyte = vbyte;
}

public shortgetVshort() {
returnvshort;
}

public voidsetVshort( shortvshort) {
this.vshort = vshort;
}

public intgetVint() {
returnvint;
}

public voidsetVint( intvint) {
this.vint = vint;
}

public longgetVlong() {
returnvlong;
}

public voidsetVlong( longvlong) {
this.vlong = vlong;
}

public floatgetVfloat() {
returnvfloat;
}

public voidsetVfloat( floatvfloat) {
this.vfloat = vfloat;
}

public doublegetVdouble() {
returnvdouble;
}

public voidsetVdouble( doublevdouble) {
this.vdouble = vdouble;
}

public booleanisVboolean() {
returnvboolean;
}

public voidsetVboolean( booleanvboolean) {
this.vboolean = vboolean;
}

publicDate getDddd() {
returndddd;
}

public voidsetDddd(Date dddd) {
this.dddd = dddd;
}

publicDate getV_Date() {
returnv_Date;
}

public voidsetV_Date(Date v_Date) {
this.v_Date = v_Date;
}

publicDate getvDate() {
returnvDate;
}

public voidsetvDate(Date vDate) {
this.vDate = vDate;
}

publicObject getVnull() {
returnvnull;
}

public voidsetVnull(Object vnull) {
this.vnull = vnull;
}

publicString[] getAvString() {
returnavString;
}

public voidsetAvString(String[] avString) {
this.avString = avString;
}

public int[] getAvint() {
returnavint;
}

public voidsetAvint( int[] avint) {
this.avint = avint;
}

public boolean[] getAvboolean() {
returnavboolean;
}

public voidsetAvboolean( boolean[] avboolean) {
this.avboolean = avboolean;
}

publicList<String> getListString() {
returnlistString;
}

public voidsetListString(List<String> listString) {
this.listString = listString;
}

publicMap<String,String> getMap() {
returnmap;
}

public voidsetMap(Map<String,String> map) {
this.map = map;
}

publicBar getBar() {
returnbar;
}

public voidsetBar(Bar bar) {
this.bar = bar;
}

publicBar[] getAvBar() {
returnavBar;
}

public voidsetAvBar(Bar[] avBar) {
this.avBar = avBar;
}

publicList<Bar> getListBar() {
returnlistBar;
}

public voidsetListBar(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
*/

public classBar {
public staticSerializeConfig mapping = newSerializeConfig();
privateString barName;
private intbarAge;
privateDate barDate = newDate();
static{
mapping.put(Date. class,newSimpleDateFormatSerializer( "yyyy-MM-dd"));
}
{
Random r = newRandom();
barName = "sss_"+String.valueOf(r.nextFloat());
barAge = r.nextInt();
}

public static voidmain(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;
}

public voidsetBarName(String barName) {
this.barName = barName;
}

public intgetBarAge() {
returnbarAge;
}

public voidsetBarAge( intbarAge) {
this.barAge = barAge;
}

publicDate getBarDate() {
returnbarDate;
}

public voidsetBarDate(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
*/

public classTest {
private staticSerializeConfig mapping = newSerializeConfig(); static{
mapping.put(Date. class,newSimpleDateFormatSerializer( "yyyy-MM-dd HH:mm:ss"));
}

public static voidmain(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);
}

public static voidjson2List(){
//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());
}
}

public static voidjson2Map(){
//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));
}
}

public static voidarray2JSON(){
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);
}
public static voidarray2JSON2(){
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);
}

public static voidmap2JSON(){
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,
"barName":"sss_0.36014742"
}],
"avString":["aaa","bbb","ccc"],
"avboolean":[true,false,true,true],
"avint":[1,4],
"bar":{
"barAge":1601495948,
"barName":"sss_0.46644872"
},
"dddd":1322062233046,
"listBar":[
{
"barAge":-1090113522,
"barName":"sss_0.83562374"
},
{
"barAge":478603194,
"barName":"sss_0.59483266"
},
{
"barAge":1118357669,
"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,"barName":"sss_0.36014742"}],"avString":["aaa","avboolean":[true,"avint":[1,"bar":{"barAge":1601495948,"barName":"sss_0.46644872"},"dddd":"2011-11-23 23:30:33","listBar":[{"barAge":-1090113522,"barName":"sss_0.83562374"},{"barAge":478603194,"barName":"sss_0.59483266"},{"barAge":1118357669,"barName":"sss_0.9961642"}],"listString":["listString1","listString2","listString3"],"map":{"x":"s11111x","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/

猜你在找的Json相关文章