当前位置:网站首页>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
边栏推荐
- 一刷314-剑指 Offer 09. 用两个栈实现队列(e)
- Connect PHP to MySQL via PDO ODBC
- [backtrader source code analysis 18] Yahoo Py code comments and analysis (boring, interested in the code, you can refer to)
- s16. One click installation of containerd script based on image warehouse
- C language --- string + memory function
- c语言---指针进阶
- 服务器中毒了怎么办?服务器怎么防止病毒入侵?
- Best practices of Apache APIs IX high availability configuration center based on tidb
- Go并发和通道
- 编译,连接 -- 笔记
猜你喜欢
建设星际计算网络的愿景
API IX JWT auth plug-in has an error. Risk announcement of information disclosure in response (cve-2022-29266)
网站建设与管理的基本概念
新动态:SmartMesh和MeshBox的合作新动向
2022年中国数字科技专题分析
How did the computer reinstall the system? The display has no signal
KNN, kmeans and GMM
Demonstration meeting on startup and implementation scheme of swarm intelligence autonomous operation smart farm project
Application of Bloom filter in 100 million flow e-commerce system
服务器中毒了怎么办?服务器怎么防止病毒入侵?
随机推荐
Recommended search common evaluation indicators
Load Balancer
[AI weekly] NVIDIA designs chips with AI; The imperfect transformer needs to overcome the theoretical defect of self attention
pywintypes.com_error: (-2147221020, ‘无效的语法‘, None, None)
Upgrade MySQL 5.1 to 5.611
小程序知识点积累
PHP function
Pytorch中named_parameters、named_children、named_modules函数
Configuration of multi spanning tree MSTP
Demonstration meeting on startup and implementation scheme of swarm intelligence autonomous operation smart farm project
cadence SPB17.4 - Active Class and Subclass
Cap theorem
服务器中毒了怎么办?服务器怎么防止病毒入侵?
Accumulation of applet knowledge points
Go语言条件,循环,函数
大型互联网为什么禁止ip直连
Upgrade MySQL 5.1 to 5.66
c语言---指针进阶
Use bitnami PostgreSQL docker image to quickly set up stream replication clusters
CVPR 2022 优质论文分享