FastJson---高性能JSON开发包

程树
2023-12-01

参考:http://git.oschina.net/eric_ds/jfire-codejson

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
5、具有超高的性能,java世界里没有其他的json库能够和fastjson可相比了。


如果获得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

如果你使用了Maven,maven repository配置如下:

<span style="font-size:13px"><repository>
	<id>opensesame</id>
	<name>Alibaba OpenSource Repsoitory</name>
	<url>http:</span><span class="code-comment" style="color:#808080;background-color:inherit"><span style="font-size:13px">//code.alibabatech.com/mvn/releases/</url>
</span></span><span style="font-size:13px">	<snapshots>
		<enabled></span><span class="code-keyword" style="color:#0091;background-color:inherit"><span style="font-size:13px">false</span></span><span style="font-size:13px"></enabled>
	</snapshots>
</repository>
</span>

pom.xml文件中加入依赖依赖:

<span style="font-size:13px"><dependency>
	<groupId>com.alibaba</groupId>
	<artifactId>fastjson</artifactId>
	<version>1.0.4</version>
</dependency>
</span>

如果没有使用maven,可以直接下载:

Binary : http://code.alibabatech.com/mvn/releases/com/alibaba/fastjson/1.1.1/fastjson-1.1.1.jar
Source :
http://code.alibabatech.com/mvn/releases/com/alibaba/fastjson/1.1.1/fastjson-1.1.1-sources.jar
Subversion : 
http://code.alibabatech.com/svn/fastjson/

使用介绍:
Fastjson的最主要的使用入口是com.alibaba.fastjson.JSON

<span class="code-keyword" style="color:#0091;background-color:inherit"><span style="font-size:13px">import</span></span><span style="font-size:13px"> com.alibaba.fastjson.JSON;

</span><span class="code-keyword" style="color:#0091;background-color:inherit"><span style="font-size:13px">public</span></span><span style="font-size:13px"> </span><span class="code-keyword" style="color:#0091;background-color:inherit"><span style="font-size:13px">static</span></span><span style="font-size:13px"> </span><span class="code-keyword" style="color:#0091;background-color:inherit"><span style="font-size:13px">final</span></span><span style="font-size:13px"> </span><span class="code-object" style="color:#91091;background-color:inherit"><span style="font-size:13px">Object</span></span><span style="font-size:13px"> parse(</span><span class="code-object" style="color:#91091;background-color:inherit"><span style="font-size:13px">String</span></span><span style="font-size:13px"> text); </span><span class="code-comment" style="color:#808080;background-color:inherit"><span style="font-size:13px">// 把JSON文本parse为JSONObject或者JSONArray
</span></span><span class="code-keyword" style="color:#0091;background-color:inherit"><span style="font-size:13px">public</span></span><span style="font-size:13px"> </span><span class="code-keyword" style="color:#0091;background-color:inherit"><span style="font-size:13px">static</span></span><span style="font-size:13px"> </span><span class="code-keyword" style="color:#0091;background-color:inherit"><span style="font-size:13px">final</span></span><span style="font-size:13px"> JSONObject parseObject(</span><span class="code-object" style="color:#91091;background-color:inherit"><span style="font-size:13px">String</span></span><span style="font-size:13px"> text); </span><span class="code-comment" style="color:#808080;background-color:inherit"><span style="font-size:13px">// 把JSON文本parse成JSONObject
</span></span><span class="code-keyword" style="color:#0091;background-color:inherit"><span style="font-size:13px">public</span></span><span style="font-size:13px"> </span><span class="code-keyword" style="color:#0091;background-color:inherit"><span style="font-size:13px">static</span></span><span style="font-size:13px"> </span><span class="code-keyword" style="color:#0091;background-color:inherit"><span style="font-size:13px">final</span></span><span style="font-size:13px"> <T> T parseObject(</span><span class="code-object" style="color:#91091;background-color:inherit"><span style="font-size:13px">String</span></span><span style="font-size:13px"> text, </span><span class="code-object" style="color:#91091;background-color:inherit"><span style="font-size:13px">Class</span></span><span style="font-size:13px"><T> clazz); </span><span class="code-comment" style="color:#808080;background-color:inherit"><span style="font-size:13px">// 把JSON文本parse为JavaBean
</span></span><span class="code-keyword" style="color:#0091;background-color:inherit"><span style="font-size:13px">public</span></span><span style="font-size:13px"> </span><span class="code-keyword" style="color:#0091;background-color:inherit"><span style="font-size:13px">static</span></span><span style="font-size:13px"> </span><span class="code-keyword" style="color:#0091;background-color:inherit"><span style="font-size:13px">final</span></span><span style="font-size:13px"> JSONArray parseArray(</span><span class="code-object" style="color:#91091;background-color:inherit"><span style="font-size:13px">String</span></span><span style="font-size:13px"> text); </span><span class="code-comment" style="color:#808080;background-color:inherit"><span style="font-size:13px">// 把JSON文本parse成JSONArray
</span></span><span class="code-keyword" style="color:#0091;background-color:inherit"><span style="font-size:13px">public</span></span><span style="font-size:13px"> </span><span class="code-keyword" style="color:#0091;background-color:inherit"><span style="font-size:13px">static</span></span><span style="font-size:13px"> </span><span class="code-keyword" style="color:#0091;background-color:inherit"><span style="font-size:13px">final</span></span><span style="font-size:13px"> <T> List<T> parseArray(</span><span class="code-object" style="color:#91091;background-color:inherit"><span style="font-size:13px">String</span></span><span style="font-size:13px"> text, </span><span class="code-object" style="color:#91091;background-color:inherit"><span style="font-size:13px">Class</span></span><span style="font-size:13px"><T> clazz); </span><span class="code-comment" style="color:#808080;background-color:inherit"><span style="font-size:13px">//把JSON文本parse成JavaBean集合
</span></span><span class="code-keyword" style="color:#0091;background-color:inherit"><span style="font-size:13px">public</span></span><span style="font-size:13px"> </span><span class="code-keyword" style="color:#0091;background-color:inherit"><span style="font-size:13px">static</span></span><span style="font-size:13px"> </span><span class="code-keyword" style="color:#0091;background-color:inherit"><span style="font-size:13px">final</span></span><span style="font-size:13px"> </span><span class="code-object" style="color:#91091;background-color:inherit"><span style="font-size:13px">String</span></span><span style="font-size:13px"> toJSONString(</span><span class="code-object" style="color:#91091;background-color:inherit"><span style="font-size:13px">Object</span></span><span style="font-size:13px"> object); </span><span class="code-comment" style="color:#808080;background-color:inherit"><span style="font-size:13px">// 将JavaBean序列化为JSON文本
</span></span><span class="code-keyword" style="color:#0091;background-color:inherit"><span style="font-size:13px">public</span></span><span style="font-size:13px"> </span><span class="code-keyword" style="color:#0091;background-color:inherit"><span style="font-size:13px">static</span></span><span style="font-size:13px"> </span><span class="code-keyword" style="color:#0091;background-color:inherit"><span style="font-size:13px">final</span></span><span style="font-size:13px"> </span><span class="code-object" style="color:#91091;background-color:inherit"><span style="font-size:13px">String</span></span><span style="font-size:13px"> toJSONString(</span><span class="code-object" style="color:#91091;background-color:inherit"><span style="font-size:13px">Object</span></span><span style="font-size:13px"> object, </span><span class="code-object" style="color:#91091;background-color:inherit"><span style="font-size:13px">boolean</span></span><span style="font-size:13px"> prettyFormat); </span><span class="code-comment" style="color:#808080;background-color:inherit"><span style="font-size:13px">// 将JavaBean序列化为带格式的JSON文本
</span></span><span class="code-keyword" style="color:#0091;background-color:inherit"><span style="font-size:13px">public</span></span><span style="font-size:13px"> </span><span class="code-keyword" style="color:#0091;background-color:inherit"><span style="font-size:13px">static</span></span><span style="font-size:13px"> </span><span class="code-keyword" style="color:#0091;background-color:inherit"><span style="font-size:13px">final</span></span><span style="font-size:13px"> </span><span class="code-object" style="color:#91091;background-color:inherit"><span style="font-size:13px">Object</span></span><span style="font-size:13px"> toJSON(</span><span class="code-object" style="color:#91091;background-color:inherit"><span style="font-size:13px">Object</span></span><span style="font-size:13px"> javaObject); 将JavaBean转换为JSONObject或者JSONArray。
</span>

代码示例:
代码示例用到类User和Group:

<span class="code-keyword" style="color:#0091;background-color:inherit"><span style="font-size:13px">public</span></span><span style="font-size:13px"> class User {
	</span><span class="code-keyword" style="color:#0091;background-color:inherit"><span style="font-size:13px">private</span></span><span style="font-size:13px"> </span><span class="code-object" style="color:#91091;background-color:inherit"><span style="font-size:13px">Long</span></span><span style="font-size:13px"> id;
	</span><span class="code-keyword" style="color:#0091;background-color:inherit"><span style="font-size:13px">private</span></span><span style="font-size:13px"> </span><span class="code-object" style="color:#91091;background-color:inherit"><span style="font-size:13px">String</span></span><span style="font-size:13px"> name;

	</span><span class="code-keyword" style="color:#0091;background-color:inherit"><span style="font-size:13px">public</span></span><span style="font-size:13px"> </span><span class="code-object" style="color:#91091;background-color:inherit"><span style="font-size:13px">Long</span></span><span style="font-size:13px"> getId() { </span><span class="code-keyword" style="color:#0091;background-color:inherit"><span style="font-size:13px">return</span></span><span style="font-size:13px"> id; }
	</span><span class="code-keyword" style="color:#0091;background-color:inherit"><span style="font-size:13px">public</span></span><span style="font-size:13px"> void setId(</span><span class="code-object" style="color:#91091;background-color:inherit"><span style="font-size:13px">Long</span></span><span style="font-size:13px"> id) { </span><span class="code-keyword" style="color:#0091;background-color:inherit"><span style="font-size:13px">this</span></span><span style="font-size:13px">.id = id; }

	</span><span class="code-keyword" style="color:#0091;background-color:inherit"><span style="font-size:13px">public</span></span><span style="font-size:13px"> </span><span class="code-object" style="color:#91091;background-color:inherit"><span style="font-size:13px">String</span></span><span style="font-size:13px"> getName() { </span><span class="code-keyword" style="color:#0091;background-color:inherit"><span style="font-size:13px">return</span></span><span style="font-size:13px"> name; }
	</span><span class="code-keyword" style="color:#0091;background-color:inherit"><span style="font-size:13px">public</span></span><span style="font-size:13px"> void setName(</span><span class="code-object" style="color:#91091;background-color:inherit"><span style="font-size:13px">String</span></span><span style="font-size:13px"> name) { </span><span class="code-keyword" style="color:#0091;background-color:inherit"><span style="font-size:13px">this</span></span><span style="font-size:13px">.name = name; }
}
</span><span class="code-keyword" style="color:#0091;background-color:inherit"><span style="font-size:13px">public</span></span><span style="font-size:13px"> class Group {
	</span><span class="code-keyword" style="color:#0091;background-color:inherit"><span style="font-size:13px">private</span></span><span style="font-size:13px"> </span><span class="code-object" style="color:#91091;background-color:inherit"><span style="font-size:13px">Long</span></span><span style="font-size:13px"> id;
	</span><span class="code-keyword" style="color:#0091;background-color:inherit"><span style="font-size:13px">private</span></span><span style="font-size:13px"> </span><span class="code-object" style="color:#91091;background-color:inherit"><span style="font-size:13px">String</span></span><span style="font-size:13px"> name;
	</span><span class="code-keyword" style="color:#0091;background-color:inherit"><span style="font-size:13px">private</span></span><span style="font-size:13px"> List<User> users = </span><span class="code-keyword" style="color:#0091;background-color:inherit"><span style="font-size:13px">new</span></span><span style="font-size:13px"> ArrayList<User>();

	</span><span class="code-keyword" style="color:#0091;background-color:inherit"><span style="font-size:13px">public</span></span><span style="font-size:13px"> </span><span class="code-object" style="color:#91091;background-color:inherit"><span style="font-size:13px">Long</span></span><span style="font-size:13px"> getId() { </span><span class="code-keyword" style="color:#0091;background-color:inherit"><span style="font-size:13px">return</span></span><span style="font-size:13px"> id; }
	</span><span class="code-keyword" style="color:#0091;background-color:inherit"><span style="font-size:13px">public</span></span><span style="font-size:13px"> void setId(</span><span class="code-object" style="color:#91091;background-color:inherit"><span style="font-size:13px">Long</span></span><span style="font-size:13px"> id) { </span><span class="code-keyword" style="color:#0091;background-color:inherit"><span style="font-size:13px">this</span></span><span style="font-size:13px">.id = id; }

	</span><span class="code-keyword" style="color:#0091;background-color:inherit"><span style="font-size:13px">public</span></span><span style="font-size:13px"> </span><span class="code-object" style="color:#91091;background-color:inherit"><span style="font-size:13px">String</span></span><span style="font-size:13px"> getName() { </span><span class="code-keyword" style="color:#0091;background-color:inherit"><span style="font-size:13px">return</span></span><span style="font-size:13px"> name; }
	</span><span class="code-keyword" style="color:#0091;background-color:inherit"><span style="font-size:13px">public</span></span><span style="font-size:13px"> void setName(</span><span class="code-object" style="color:#91091;background-color:inherit"><span style="font-size:13px">String</span></span><span style="font-size:13px"> name) { </span><span class="code-keyword" style="color:#0091;background-color:inherit"><span style="font-size:13px">this</span></span><span style="font-size:13px">.name = name; }

	</span><span class="code-keyword" style="color:#0091;background-color:inherit"><span style="font-size:13px">public</span></span><span style="font-size:13px"> List<User> getUsers() { </span><span class="code-keyword" style="color:#0091;background-color:inherit"><span style="font-size:13px">return</span></span><span style="font-size:13px"> users; }
	</span><span class="code-keyword" style="color:#0091;background-color:inherit"><span style="font-size:13px">public</span></span><span style="font-size:13px"> void setUsers(List<User> users) { </span><span class="code-keyword" style="color:#0091;background-color:inherit"><span style="font-size:13px">this</span></span><span style="font-size:13px">.users = users; }
}
</span>

Encode代码示例:

<span class="code-keyword" style="color:#0091;background-color:inherit"><span style="font-size:13px">import</span></span><span style="font-size:13px"> com.alibaba.fastjson.JSON;

Group group = </span><span class="code-keyword" style="color:#0091;background-color:inherit"><span style="font-size:13px">new</span></span><span style="font-size:13px"> Group();
group.setId(0L);
group.setName(</span><span class="code-quote" style="color:#0910;background-color:inherit"><span style="font-size:13px">"admin"</span></span><span style="font-size:13px">);

User guestUser = </span><span class="code-keyword" style="color:#0091;background-color:inherit"><span style="font-size:13px">new</span></span><span style="font-size:13px"> User();
guestUser.setId(2L);
guestUser.setName(</span><span class="code-quote" style="color:#0910;background-color:inherit"><span style="font-size:13px">"guest"</span></span><span style="font-size:13px">);

User rootUser = </span><span class="code-keyword" style="color:#0091;background-color:inherit"><span style="font-size:13px">new</span></span><span style="font-size:13px"> User();
rootUser.setId(3L);
rootUser.setName(</span><span class="code-quote" style="color:#0910;background-color:inherit"><span style="font-size:13px">"root"</span></span><span style="font-size:13px">);

group.getUsers().add(guestUser);
group.getUsers().add(rootUser);

</span><span class="code-object" style="color:#91091;background-color:inherit"><span style="font-size:13px">String</span></span><span style="font-size:13px"> jsonString = JSON.toJSONString(group);

</span><span class="code-object" style="color:#91091;background-color:inherit"><span style="font-size:13px">System</span></span><span style="font-size:13px">.out.println(jsonString);
</span>

Decode 代码示例:

<span style="font-size:13px">Group group2 = JSON.parseObject(jsonString, Group.class);
</span>

之前的一个版本是1.1.0,1.1.0采用asm和SortFastMatch算法提高性能,由于过于着急展示其优越的性能,没有进行严格测试就发布了。

1.1.1相对于1.1.0,这是一个比较稳定的版本了,行测试覆盖率重新提升到90%以上,build verify testcase 983个。

这个版本进一步完善了asm和SortFieldFastMatch算法,进一步提升了性能,同时补充了大量的testcase,提升了稳定性,我向你推荐使用这个版本,使用这个版本你将会得到令人惊奇的性能。

1.1.1版本的asm来源自objectweb的asm项目,根据fastjson的需要做裁剪,确保引入asm的同时不引起包大小的过渡变大。

为了更好使用sort field martch优化算法提升parser的性能,fastjson序列化的时候,缺省把SerializerFeature.SortField特性打开了。反序列化的时候也缺省把SortFeidFastMatch的选项打开了。这样,如果你用fastjson序列化的文本,输出的结果是按照fieldName排序输出的,parser时也能利用这个顺序进行优化读取。这种情况下,parser能够获得非常好的性能。

我使用github.com/eishay/jvm-serializers/提供的程序做测试,性能数据如下:


序列化时间 反序列化时间 大小 压缩后大小
java序列化 8546 43199 889 541
hessian 6643 10043 501 313
protobuf 3008 1694 239 149
thrift 3182 1951 349 197
avro 3575 2095 221 133
json-lib 45734 149741 485 263
jackson 3245 2986 503 271
fastjson 2292 1499 468 251

测试跑的脚本是:

<span style="font-size:13px">./run -chart -include=`cat serializers.txt | tr </span><span class="code-quote" style="color:#0910;background-color:inherit"><span style="font-size:13px">"\\n"</span></span><span style="font-size:13px"> </span><span class="code-quote" style="color:#0910;background-color:inherit"><span style="font-size:13px">","</span></span><span style="font-size:13px">`  data/media.1.cks
</span>

从上面的数据来看,fastjson的性能已经超越protobuf、thrift、avro这些二进制协议了。一个文本协议的性能超越二进制协议是很难的,我很高兴向大家宣布我做到了!!

鉴于fastjson优越的性能表现,我建议做如下事情;
1、替换其他所有的json库,java世界里没有其他的json库能够和fastjson可相比了。
2、使用fastjson的序列化和反序列化替换java serialize,java serialize不单性能慢,而且体制大。
3、使用fastjson替换hessian,json协议不必hessian体积大,而且fastjson性能优越,数倍于hessian
4、把fastjson用于memached缓存对象数据。

How to get it?

If you're Maven user, just use our maven repository(http://code.alibabatech.com/mvn/releases/) with folloging dependency

<span style="font-size:13px"><dependency>
     <groupId>com.alibaba</groupId>
     <artifactId>fastjson</artifactId>
     <version>1.1.1</version>
</dependency>
</span>

Downlaods

Binary : http://code.alibabatech.com/mvn/releases/com/alibaba/fastjson/1.1.1/fastjson-1.1.1.jar
Source :
http://code.alibabatech.com/mvn/releases/com/alibaba/fastjson/1.1.1/fastjson-1.1.1-sources.jar
Subversion : 
http://code.alibabatech.com/svn/fastjson/

 类似资料: