fastjson学习笔记

来源:互联网 发布:c 比java难 编辑:程序博客网 时间:2024/06/16 10:48

         项目中经常用到json序列化工具json-lib, jackson, gson等,最近听说fastjson,便找时间来学习了解一下。

        援引alibaba官方定义:Fastjson是一个Java语言编写的高性能功能完善的JSON库。它采用一种“假定有序快速匹配”的算法,把JSON Parse的性能提升到极致,是目前Java语言中最快的JSON库。Fastjson接口简单易用,已经被广泛使用在缓存序列化、协议交互、Web输出、Android客户端等多种应用场景。

     项目中导入fastjson可通过:

     1.下载jar包:下载链接

  2.maven导入:

<dependency>    <groupId>com.alibaba</groupId>    <artifactId>fastjson</artifactId>    <version>VERSION_CODE</version></dependency>

       开始之前先给个简单的示例:

import com.alibaba.fastjson.JSON;
 
Group group = new Group();
group.setId(0L);
group.setName("admin");
 
User guestUser = new User();
guestUser.setId(2L);
guestUser.setName("guest");
 
User rootUser = new User();
rootUser.setId(3L);
rootUser.setName("root");
 
group.getUsers().add(guestUser);
group.getUsers().add(rootUser); 
String jsonString = JSON.toJSONString(group); 
System.out.println(jsonString);
Group g = Json.parseObject(jsonString, Group.class);


    自称java中最快,只依赖JDK,功能强大(支持多种数据结构)


   简要阅读下源码:

   画了个类图,水平有限:

 

    入口类是com.alibaba.fastjson.JSON

<span style="font-size: 13px;"></span><span class="code-keyword" style="color: rgb(0, 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, 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, 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, 0, 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, 0, 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, 128); background-color: inherit;"><span style="font-size: 13px;">// 把JSON文本parse为JSONObject或者JSONArray</span></span><span class="code-keyword" style="color: rgb(0, 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, 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, 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, 0, 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, 128); background-color: inherit;"><span style="font-size: 13px;">// 把JSON文本parse成JSONObject</span></span><span class="code-keyword" style="color: rgb(0, 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, 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, 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, 0, 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, 0, 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, 128); background-color: inherit;"><span style="font-size: 13px;">// 把JSON文本parse为JavaBean</span></span><span class="code-keyword" style="color: rgb(0, 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, 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, 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, 0, 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, 128); background-color: inherit;"><span style="font-size: 13px;">// 把JSON文本parse成JSONArray</span></span><span class="code-keyword" style="color: rgb(0, 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, 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, 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, 0, 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, 0, 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, 128); background-color: inherit;"><span style="font-size: 13px;">//把JSON文本parse成JavaBean集合</span></span><span class="code-keyword" style="color: rgb(0, 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, 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, 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, 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, 0, 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, 128); background-color: inherit;"><span style="font-size: 13px;">// 将JavaBean序列化为JSON文本</span></span><span class="code-keyword" style="color: rgb(0, 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, 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, 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, 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, 0, 145); background-color: inherit;"><span style="font-size: 13px;">Object</span></span><span style="font-size: 13px;"> object, </span><span class="code-object" style="color: rgb(145, 0, 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, 128); background-color: inherit;"><span style="font-size: 13px;">// 将JavaBean序列化为带格式的JSON文本</span></span><span class="code-keyword" style="color: rgb(0, 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, 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, 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, 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, 0, 145); background-color: inherit;"><span style="font-size: 13px;">Object</span></span><span style="font-size: 13px;"> javaObject); 将JavaBean转换为JSONObject或者JSONArray。</span>
<span style="font-size: 13px;"></span>
<span style="font-size: 13px;"></span>

0 0
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 孩子五年级学习成绩差该怎么办 孩子临近中考学习成绩很差该怎么办 初中生成绩不好家长该怎么办 成绩差该怎么办贴吧 宝宝只会匍匐爬怎么办 一年级的孩子数学不好怎么办 小学一年级孩子数学不好怎么办 孩子上一年级数学太差怎么办 智商低情商也低怎么办? 孩子字写得很大怎么办 孩子拿笔重 写字太黑 怎么办 孩子语文成绩好数学不行怎么办? 孩子现在二年级特别叛逆怎么办 孩子又笨又蠢怎么办 四年级孩子数学计算能力差怎么办 孩子四年级数学理解能力差怎么办 孩子小学四年级数学很差怎么办 孩子做作业太慢怎么办 小学三年级数学成绩差怎么办 初一数学考了3分怎么办 初二物理太差该怎么办 三年级孩子字写的差怎么办 小学三年级数学才考86怎么办 小孩子一发脾气就打妈妈怎么办 孩子做错事不肯道歉怎么办 小孩写作业注意力不集中怎么办 六年级的数学下册差怎么办 一年级小孩做作业慢怎么办 静不下心写作业怎么办 二年级应用题太差怎么办 小学二年级数学差怎么办 小学二年级成绩差怎么办 6个月小孩爱动怎么办 儿子叛逆期我该怎么办 宝宝两岁好动不听话怎么办 生宝宝后奶水少怎么办 生了孩子没出来怎么办 孩子在学校表现不好怎么办 3岁半宝宝话太多怎么办 孩子不喜欢和小朋友玩怎么办 孩子不喜欢和小朋友说话怎么办