当前位置:网站首页>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
边栏推荐
- 删除字符串中出现次数最少的字符
- PHP function
- s16.基于镜像仓库一键安装containerd脚本
- pgpool-II 4.3 中文手册 - 入门教程
- Mumu, go all the way
- Codejock Suite Pro v20.3.0
- Pytorch中named_parameters、named_children、named_modules函数
- Advantages, disadvantages and selection of activation function
- Code live collection ▏ software test report template Fan Wen is here
- PHP operators
猜你喜欢
随机推荐
Codejock Suite Pro v20. three
WPS brand was upgraded to focus on China. The other two domestic software were banned from going abroad with a low profile
Treatment of idempotency
Accumulation of applet knowledge points
Independent operation smart farm Innovation Forum
Basic concepts of website construction and management
Open source project recommendation: 3D point cloud processing software paraview, based on QT and VTK
一刷312-简单重复set-剑指 Offer 03. 数组中重复的数字(e)
删除字符串中出现次数最少的字符
基础贪心总结
Calculate the number of occurrences of a character
How to test mobile app?
Multitimer V2 reconstruction version | an infinitely scalable software timer
贫困的无网地区怎么有钱建设网络?
Go语言数组,指针,结构体
Load Balancer
Deeply learn the skills of parameter adjustment
2022年中国数字科技专题分析
What if the package cannot be found
时序模型:长短期记忆网络(LSTM)