当前位置:网站首页>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
边栏推荐
- 通过 PDO ODBC 将 PHP 连接到 MSSQL
- 【开源工具分享】单片机调试助手(示波/改值/日志) - LinkScope
- Connect PHP to MySQL via PDO ODBC
- Multi level cache usage
- 字符串排序
- Best practices of Apache APIs IX high availability configuration center based on tidb
- 【Leetcode-每日一题】安装栅栏
- pywintypes.com_error: (-2147221020, ‘无效的语法‘, None, None)
- IronPDF for . NET 2022.4.5455
- Node. JS ODBC connection PostgreSQL
猜你喜欢

WPS品牌再升级专注国内,另两款国产软件低调出国门,却遭禁令

Mumu, go all the way

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

时序模型:长短期记忆网络(LSTM)

API IX JWT auth plug-in has an error. Risk announcement of information disclosure in response (cve-2022-29266)

Recommended search common evaluation indicators

大厂技术实现 | 行业解决方案系列教程

Why is IP direct connection prohibited in large-scale Internet

Neodynamic Barcode Professional for WPF V11.0

Explanation 2 of redis database (redis high availability, persistence and performance management)
随机推荐
Application of Bloom filter in 100 million flow e-commerce system
pgpool-II 4.3 中文手册 - 入门教程
自动化测试框架常见类型▏自动化测试就交给软件测评机构
[backtrader source code analysis 18] Yahoo Py code comments and analysis (boring, interested in the code, you can refer to)
Explanation of redis database (IV) master-slave replication, sentinel and cluster
Pytorch中named_parameters、named_children、named_modules函数
php函数
Temporal model: long-term and short-term memory network (LSTM)
Go language, condition, loop, function
The El tree implementation only displays a certain level of check boxes and selects radio
Go语言条件,循环,函数
新动态:SmartMesh和MeshBox的合作新动向
【AI周报】英伟达用AI设计芯片;不完美的Transformer要克服自注意力的理论缺陷
删除字符串中出现次数最少的字符
C language --- advanced pointer
What is CNAs certification? What are the software evaluation centers recognized by CNAs?
Connect PHP to MSSQL via PDO ODBC
shell脚本中的DATE日期计算
WPS品牌再升级专注国内,另两款国产软件低调出国门,却遭禁令
一刷312-简单重复set-剑指 Offer 03. 数组中重复的数字(e)