当前位置:网站首页>Fastjon2 here he is, the performance is significantly improved, and he can fight for another ten years
Fastjon2 here he is, the performance is significantly improved, and he can fight for another ten years
2022-04-23 15:45:00 【Have a drink together】
List of articles
summary
The official definition given by Ali is ,FASTJSON It's Alibaba's open source JSON Parsing library , It can parse JSON Format string , Support will Java Bean Serialize to JSON character string , You can also get it from JSON Deserialize string to JavaBean.
FASTJSON Relative to others JSON The characteristic of the library is fast , from 2011 year fastjson Release 1.1.x After the version , Its performance has never been other Java Realized JSON Kuroshio .
FASTJSON 2.0 yes FASTJSON Important upgrade of the project , The goal is to provide a high-performance platform for the next decade JSON library , The same set of API Support JSON/JSONB Two protocols ,JSONPath First class citizen , Support full resolution and partial resolution , Support Java Server side 、 client Android、 Big data scenario .
Use
introduce Maven rely on
stay FASTJSON 2.0 in ,groupId and 1.x Dissimilarity , yes com.alibaba.fastjson2
<dependency>
<groupId>com.alibaba.fastjson2</groupId>
<artifactId>fastjson2</artifactId>
<version>2.0.1</version>
</dependency>
https://repo1.maven.org/maven2/com/alibaba/fastjson2/fastjson2/
2.0 Previous FASTJSON Coordinates are as follows , The latest version is 1.2.79:
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version> 1.2.79</version>
</dependency>
https://github.com/alibaba/fastjson/releases
Official statement : If you used fastjson 1.2.x edition , Compatible packages can be used , Compatibility packages do not guarantee 100% compatible , Please test carefully to verify , If you find any problems, please give feedback in time . The compatible package coordinates are as follows :
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>2.0.1</version>
</dependency>
Common classes and methods
stay fastjson 2.0 in ,package and 1.x Dissimilarity , yes com.alibaba.fastjson2. If you used fastjson1, In most cases, just change the package name directly .
package com.alibaba.fastjson2;
class JSON {
// Parse a string into JSONObject
static JSONObject parseObject(String str);
// Parse a string into JSONArray
static JSONArray parseArray(String str);
// Parse a string into Java object
static T parseObject(byte[] utf8Bytes, Class<T> objectClass);
// take Java Object is output as a string
static String toJSONString(Object object);
// take Java Object output to UT8 Coded byte[]
static byte[] toJSONBytes(Object object);
}
class JSONB {
// take jsonb Format byte[] It can be interpreted as Java object
static T parseObject(byte[] jsonbBytes, Class<T> objectClass);
// take Java Object output to jsonb Format byte[]
static byte[] toBytes(Object object);
}
class JSONObject {
Object get(String key);
int getIntValue(String key);
Integer getInteger(String key);
long getLongValue(String key);
Long getLong(String key);
T getObject(String key, Class<T> objectClass);
// take JSONObject Object to Java object
T toJavaObject(Class<T> objectClass);
}
class JSONArray {
Object get(int index);
int getIntValue(int index);
Integer getInteger(int index);
long getLongValue(int index);
Long getLong(int index);
T getObject(int index, Class<T> objectClass);
}
class JSONPath {
// structure JSONPath
static JSONPath of(String path);
// according to path Directly parse the input , Will partially analyze and optimize , Will not resolve all
Object extract(JSONReader jsonReader);
// according to path Evaluate objects
Object eval(Object rootObject);
}
class JSONReader {
// Construction is based on String Input JSONReader
static JSONReader of(String str);
// Construction is based on ut8 code byte Array input JSONReader
static JSONReader of(byte[] utf8Bytes);
// Construction is based on char[] Input JSONReader
static JSONReader of(char[] chars);
// Construction is based on json Format byte Array input JSONReader
static JSONReader ofJSONB(byte[] jsonbBytes)
}
Common cases
String rotation JSON object /JSON Array
object :
String jsonObjectStr = "{\"id\":\"1\",\"name\":\" Zhang San \"}";
JSONObject jsonObject = JSON.parseObject(jsonObjectStr);
int id = jsonObject.getIntValue("id");
String name = jsonObject.getString("name");
Array objects :
// Normal array
String str = "[\"id\", 123]";
JSONArray jsonArray1 = JSON.parseArray(str);
String key = jsonArray1.getString(0);
int value = jsonArray1.getIntValue(1);
log.info(key+":"+value);
// Array objects
String jsonArrayObjectStr = "[{\"id\":\"1\",\"name\":\" Zhang San \"},{\"id\":\"2\",\"name\":\" Li Si \"}]";
JSONArray jsonArray = JSON.parseArray(jsonArrayObjectStr);
JSONObject jsonObject = jsonArray.getJSONObject(1);
int id = jsonObject.getIntValue("id");
String name = jsonObject.getString("name");
log.info(id+" "+name);
JavaBean Object turn JSON Format string
User user = new User();
user.setId(1);
user.setName(" Xiao Zhan ");
String userStr = JSON.toJSONString(user);
log.info(userStr);
//JSONWriter.Feature.BeanToArray yes fastjson2 New characteristics ,fastjson1 There is no
String toJSONString = JSON.toJSONString(user, JSONWriter.Feature.BeanToArray);
log.info(toJSONString);
//JavaBean Object to generate UTF8 Coded byte[]
byte[] utf8JSONBytes = JSON.toJSONBytes(user);
log.info(new String(utf8JSONBytes));
@Data
class User{
private int id;
private String name;
}
Output is as follows :
INFO com.zjq.dailyrecord.utils.fastjson.Fastjson2 - {
"id":1,"name":" Xiao Zhan "}
INFO com.zjq.dailyrecord.utils.fastjson.Fastjson2 - [1," Xiao Zhan "]
INFO com.zjq.dailyrecord.utils.fastjson.Fastjson2 - {
"id":1,"name":" Xiao Zhan "}
JSON Format string to JavaBean object
String jsonObjectStr = "{\"id\":\"1\",\"name\":\" Zhang San \"}";
User user = JSON.parseObject(jsonObjectStr, User.class);
log.info(user.toString());
Output is as follows :
INFO com.zjq.dailyrecord.utils.fastjson.Fastjson2 - Fastjson2.User(id=1, name= Zhang San )
JSON Format string to JavaBean An array of objects
String jsonObjectStr = "[{\"id\":\"1\",\"name\":\" Xiao Zhan \"},{\"id\":\"2\",\"name\":\"zjq\"}]";
List<User> userList = JSON.parseArray(jsonObjectStr, User.class);
log.info(userList.toString());
Output is as follows :
INFO com.zjq.dailyrecord.utils.fastjson.Fastjson2 - [Fastjson2.User(id=1, name= Xiao Zhan ), Fastjson2.User(id=2, name=zjq)]
Fastjson2 relative fastjson1 Performance improvement
Compare versions
- Fastjson 2.0.1
- Fastjson 1.2.79
- Jackson 2.12.4
Parse Performance comparison
Test code
package com.alibaba.fastjson_perf.eishay;
import org.openjdk.jmh.annotations.Mode;
import org.openjdk.jmh.runner.Runner;
import org.openjdk.jmh.runner.RunnerException;
import org.openjdk.jmh.runner.options.Options;
import org.openjdk.jmh.runner.options.OptionsBuilder;
import java.util.concurrent.TimeUnit;
public class EishayParse {
public static void main(String[] args) throws RunnerException {
Options options = new OptionsBuilder()
.include(EishayParse.class.getName())
.mode(Mode.Throughput)
.timeUnit(TimeUnit.MILLISECONDS)
.forks(1)
.build();
new Runner(options).run();
}
}
Scene introduction and conclusion
- EishayParseTreeString scene , take String It can be interpreted as JSONObject/JSONArray perhaps HashMap/ArrayList. In this scene ,fastjson2 Show twice as much as fastjson1 Performance of
- EishayParseString scene , take String Deserialize to JavaBean object , In this scene fastjson2 be relative to fastjson1 Improved performance 30% Performance of .
- EishayParseStringPretty, Format with spaces and line breaks indented String Deserialize to JavaBean object ,fastjson2 stay 3.44 Twice as much as fastjson1. The scene is fastjson1 Middle is weak , stay fastjson2 A new analytical algorithm is used in , The performance has been greatly improved .
- EishayParseUTF8Bytes, take UTF8 Format byte[] Deserialize to JavaBean object .
WriteString
Test code
package com.alibaba.fastjson_perf.eishay;
import org.openjdk.jmh.annotations.Mode;
import org.openjdk.jmh.runner.Runner;
import org.openjdk.jmh.runner.RunnerException;
import org.openjdk.jmh.runner.options.Options;
import org.openjdk.jmh.runner.options.OptionsBuilder;
import java.util.concurrent.TimeUnit;
public class EishayWrite {
public static void main(String[] args) throws RunnerException {
Options options = new OptionsBuilder()
.include(EishayWrite.class.getName())
.mode(Mode.Throughput)
.timeUnit(TimeUnit.MILLISECONDS)
.forks(1)
.build();
new Runner(options).run();
}
}
Scene introduction and conclusion
- EishayWriteString scene , take JavaBean Objects are serialized as strings . In this scenario ,fastjson2 Than fastjson1 and jackson There were 164% and 85% Performance improvement of
- EishayWriteUTF8Bytes scene , take JavaBean Object serialization to UTF8 Format byte Array . In this scenario ,fastjson2 Than fastjson1 and jackson There were 185% and 93% Performance improvement of
source :
https://github.com/alibaba/fastjson/releases
https://github.com/alibaba/fastjson2/releases/tag/2.0.1
https://github.com/alibaba/fastjson2/wiki/fastjson_benchmark
版权声明
本文为[Have a drink together]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204231544553434.html
边栏推荐
- cadence SPB17. 4 - Active Class and Subclass
- 建设星际计算网络的愿景
- 一刷312-简单重复set-剑指 Offer 03. 数组中重复的数字(e)
- Best practices of Apache APIs IX high availability configuration center based on tidb
- 一刷313-剑指 Offer 06. 从尾到头打印链表(e)
- 现在做自媒体能赚钱吗?看完这篇文章你就明白了
- 一刷314-剑指 Offer 09. 用两个栈实现队列(e)
- 【Leetcode-每日一题】安装栅栏
- 控制结构(二)
- s16.基于镜像仓库一键安装containerd脚本
猜你喜欢

导入地址表分析(根据库文件名求出:导入函数数量、函数序号、函数名称)

MetaLife与ESTV建立战略合作伙伴关系并任命其首席执行官Eric Yoon为顾问

ICE -- 源码分析

Best practices of Apache APIs IX high availability configuration center based on tidb

Treatment of idempotency

KNN, kmeans and GMM

【AI周报】英伟达用AI设计芯片;不完美的Transformer要克服自注意力的理论缺陷

Cap theorem

What if the server is poisoned? How does the server prevent virus intrusion?
![[AI weekly] NVIDIA designs chips with AI; The imperfect transformer needs to overcome the theoretical defect of self attention](/img/bf/2b4914276ec1083df697383fec8f22.png)
[AI weekly] NVIDIA designs chips with AI; The imperfect transformer needs to overcome the theoretical defect of self attention
随机推荐
PHP operators
【AI周报】英伟达用AI设计芯片;不完美的Transformer要克服自注意力的理论缺陷
C language --- string + memory function
API IX JWT auth plug-in has an error. Risk announcement of information disclosure in response (cve-2022-29266)
MySQL optimistic lock to solve concurrency conflict
The El tree implementation only displays a certain level of check boxes and selects radio
建设星际计算网络的愿景
What role does the software performance test report play? How much is the third-party test report charged?
2022年中国数字科技专题分析
Redis主从复制过程
Deeply learn the skills of parameter adjustment
One brush 314 sword finger offer 09 Implement queue (E) with two stacks
提取不重复的整数
How do you think the fund is REITs? Is it safe to buy the fund through the bank
北京某信护网蓝队面试题目
WPS品牌再升级专注国内,另两款国产软件低调出国门,却遭禁令
Treatment of idempotency
开源项目推荐:3D点云处理软件ParaView,基于Qt和VTK
大厂技术实现 | 行业解决方案系列教程
shell脚本中的DATE日期计算