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
- FastJson学习笔记
- fastjson学习笔记
- fastjson 学习笔记
- Fastjson 学习笔记
- FastJson学习笔记
- android学习笔记19:FastJson解析json
- JSON解析学习笔记(json、gson、fastjson)
- FastJson笔记
- fastjson学习
- fastjson学习
- 开源工程学习笔记之Fastjson(Part 0)
- 开源工程学习笔记之Fastjson(Part 1)
- Springboot学习笔记(一)FastJson解析JSON数据
- 【JavaEE学习笔记】JSON解析_原生JSON,GSON,FastJSON
- fastJson 笔记-简单应用
- FastJson 使用笔记
- fastjson 学习总结
- Java之fastjson学习
- HTML&CSS基础学习笔记1.34-通用选择器
- vsftpd服务器的安装和配置
- XPdf实现pdf转txt格式方法实现
- 协调色-橙色
- 基于标准的http来实现Android多文件上传
- fastjson学习笔记
- SSE(Server-sent Events)
- [Leetcode] Symmetric
- mac os sierra安装cocoapods过程
- AeroSpike之:安装AMC
- Python学习super()
- 链表:链表两数相加( LeetCode 2. Add Two Numbers(两数相加))
- android动态加载class文件(DexClassLoader可从SD卡加载)
- SSH如何通过公钥连接云服务器