Fastjson介绍
??
目录(?)
[+]
Fastjson介绍 Fastjson是一个Java语言编写的JSON处理器,由阿里巴巴公司开发。
如果获得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: rgb(128,128,128); 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: rgb(0,145); 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 使用介绍: <span class="code-keyword" style="color: rgb(0,145); 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: rgb(0,145); background-color: inherit;"><span style="font-size: 13px;">public</span></span><span style="font-size: 13px;"> </span><span class="code-keyword" style="color: rgb(0,145); background-color: inherit;"><span style="font-size: 13px;">static</span></span><span style="font-size: 13px;"> </span><span class="code-keyword" style="color: rgb(0,145); background-color: inherit;"><span style="font-size: 13px;">final</span></span><span style="font-size: 13px;"> </span><span class="code-object" style="color: rgb(145,145); background-color: inherit;"><span style="font-size: 13px;">Object</span></span><span style="font-size: 13px;"> parse(</span><span class="code-object" style="color: rgb(145,145); background-color: inherit;"><span style="font-size: 13px;">String</span></span><span style="font-size: 13px;"> text); </span><span class="code-comment" style="color: rgb(128,128); background-color: inherit;"><span style="font-size: 13px;">// 把JSON文本parse为JSONObject或者JSONArray </span></span><span class="code-keyword" style="color: rgb(0,145); 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: rgb(145,145); background-color: inherit;"><span style="font-size: 13px;">String</span></span><span style="font-size: 13px;"> text); </span><span class="code-comment" style="color: rgb(128,128); background-color: inherit;"><span style="font-size: 13px;">// 把JSON文本parse成JSONObject </span></span><span class="code-keyword" style="color: rgb(0,145); 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: rgb(145,145); background-color: inherit;"><span style="font-size: 13px;">String</span></span><span style="font-size: 13px;"> text,</span><span class="code-object" style="color: rgb(145,145); 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: rgb(128,128); background-color: inherit;"><span style="font-size: 13px;">// 把JSON文本parse为JavaBean </span></span><span class="code-keyword" style="color: rgb(0,145); 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: rgb(145,128); background-color: inherit;"><span style="font-size: 13px;">// 把JSON文本parse成JSONArray </span></span><span class="code-keyword" style="color: rgb(0,145); 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: rgb(145,128); background-color: inherit;"><span style="font-size: 13px;">//把JSON文本parse成JavaBean集合 </span></span><span class="code-keyword" style="color: rgb(0,145); background-color: inherit;"><span style="font-size: 13px;">String</span></span><span style="font-size: 13px;"> toJSONString(</span><span class="code-object" style="color: rgb(145,145); background-color: inherit;"><span style="font-size: 13px;">Object</span></span><span style="font-size: 13px;"> object); </span><span class="code-comment" style="color: rgb(128,128); background-color: inherit;"><span style="font-size: 13px;">// 将JavaBean序列化为JSON文本 </span></span><span class="code-keyword" style="color: rgb(0,145); background-color: inherit;"><span style="font-size: 13px;">Object</span></span><span style="font-size: 13px;"> object,145); background-color: inherit;"><span style="font-size: 13px;">boolean</span></span><span style="font-size: 13px;"> prettyFormat); </span><span class="code-comment" style="color: rgb(128,128); background-color: inherit;"><span style="font-size: 13px;">// 将JavaBean序列化为带格式的JSON文本 </span></span><span class="code-keyword" style="color: rgb(0,145); background-color: inherit;"><span style="font-size: 13px;">Object</span></span><span style="font-size: 13px;"> toJSON(</span><span class="code-object" style="color: rgb(145,145); background-color: inherit;"><span style="font-size: 13px;">Object</span></span><span style="font-size: 13px;"> javaObject); 将JavaBean转换为JSONObject或者JSONArray。 </span> 代码示例: Encode代码示例:
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/提供的程序做测试,性能数据如下:
测试跑的脚本是: <span style="font-size: 13px;">./run -chart -include=`cat serializers.txt | tr </span><span class="code-quote" style="color: rgb(0,0); background-color: inherit;"><span style="font-size: 13px;">"n"</span></span><span style="font-size: 13px;"> </span><span class="code-quote" style="color: rgb(0,0); 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优越的性能表现,我建议做如下事情; 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> DownlaodsBinary :http://code.alibabatech.com/svn/fastjson/ (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |