当前位置:网站首页>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
边栏推荐
- 移动app测试如何进行?
- Timing model: gated cyclic unit network (Gru)
- GFS distributed file system (Theory)
- PHP PDO ODBC loads files from one folder into the blob column of MySQL database and downloads the blob column to another folder
- Extract non duplicate integers
- C language --- string + memory function
- 自动化测试框架常见类型▏自动化测试就交给软件测评机构
- Codejock Suite Pro v20.3.0
- Introduction to dynamic programming of leetcode learning plan day3 (198213740)
- 多生成树MSTP的配置
猜你喜欢
Why is IP direct connection prohibited in large-scale Internet
新动态:SmartMesh和MeshBox的合作新动向
布隆过滤器在亿级流量电商系统的应用
【AI周报】英伟达用AI设计芯片;不完美的Transformer要克服自注意力的理论缺陷
王启亨谈Web3.0与价值互联网“通证交换”
JVM-第2章-类加载子系统(Class Loader Subsystem)
MySQL集群模式與應用場景
Advantages, disadvantages and selection of activation function
c语言---指针进阶
幂等性的处理
随机推荐
[AI weekly] NVIDIA designs chips with AI; The imperfect transformer needs to overcome the theoretical defect of self attention
计算某字符出现次数
编译,连接 -- 笔记
一刷313-剑指 Offer 06. 从尾到头打印链表(e)
建设星际计算网络的愿景
Go语言切片,范围,集合
One brush 312 - simple repetition set - Sword finger offer 03 Duplicate number in array (E)
The El tree implementation only displays a certain level of check boxes and selects radio
现在做自媒体能赚钱吗?看完这篇文章你就明白了
移动app测试如何进行?
c语言---指针进阶
Load Balancer
Cookie&Session
Go language, array, pointer, structure
携号转网最大赢家是中国电信,为何人们嫌弃中国移动和中国联通?
大厂技术实现 | 行业解决方案系列教程
考试考试自用
Configuration of multi spanning tree MSTP
导入地址表分析(根据库文件名求出:导入函数数量、函数序号、函数名称)
el-tree实现只显示某一级复选框且单选