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
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
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类上的静态方法直接完成。
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。
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>
三、测试代码
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;
}
}
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;
}
}