当前位置:网站首页>Fastjson 2 is coming, the performance continues to improve, and it can fight for another ten years
Fastjson 2 is coming, the performance continues to improve, and it can fight for another ten years
2022-04-23 12:02:00 【Program ape DD_】
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 .
FASJTONS2 Code https://github.com/alibaba/fastjson2/releases/tag/2.0.1
JSONB Format document https://github.com/alibaba/fastjson2/wiki/jsonb_format_cn
FASTJSON 2 Performance has been greatly improved , See here for specific performance data https://github.com/alibaba/fastjson2/wiki/fastjson_benchmark
2. Prepare before use
2.1 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.2
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 .
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>2.0.1</version>
</dependency>
2.2 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)
}
3. Read JSON object
String str = "{\"id\":123}";
JSONObject jsonObject = JSON.parseObject(str);
int id = jsonObject.getIntValue("id");
String str = "[\"id\", 123]";
JSONArray jsonArray = JSON.parseArray(str);
String name = jsonArray.getString(0);
int id = jsonArray.getIntValue(1);
4. take JavaBean Object to generate JSON
4.1 take JavaBean Object to generate JSON Format string
class Product {
public int id;
public String name;
}
Product product = new Product();
product.id = 1001;
product.name = "DataWorks";
JSON.toJSONString(product);
// The following results are generated
{
"id" : 1001,
"name" : "DataWorks"
}
JSON.toJSONString(product, JSONWriter.Feature.BeanToArray);
// The following results are generated
[123, "DataWorks"]
4.2 take JavaBean Object to generate UTF8 Coded byte[]
Product product = ...;
byte[] utf8JSONBytes = JSON.toJSONBytes(product);
4.3 take JavaBean Object to generate JSONB Format byte[]
Product product = ...;
byte[] jsonbBytes = JSONB.toBytes(product);
byte[] jsonbBytes = JSONB.toBytes(product, JSONWriter.Feature.BeanToArray);
5. Read JavaBean
5.1 Read the string as JavaBean
String str = "{\"id\":123}";
Product product = JSON.parseObject(str, Product.class);
5.2 take UTF8 Coded byte[] Read into JavaBean
byte[] utf8Bytes = "{\"id\":123}".getBytes(StandardCharsets.UTF_8);
Product product = JSON.parseObject(utf8Bytes, Product.class);
5.3 take JSONB The data is read as JavaBean
byte[] jsonbBytes = ...
Product product = JSONB.parseObject(jsonbBytes, Product.class);
Product product = JSONB.parseObject(jsonbBytes, Product.class, JSONReader.Feature.SupportBeanArrayMapping);
6. Use JSONPath
6.1 Use JSONPath Partial read data
String str = ...;
JSONPath path = JSONPath.of("$.id"); // Caching and reuse can improve performance
JSONReader parser = JSONReader.of(str);
Object result = path.extract(parser);
6.2 Use JSONPath Read part utf8Bytes The data of
byte[] utf8Bytes = ...;
JSONPath path = JSONPath.of("$.id"); // Caching and reuse can improve performance
JSONReader parser = JSONReader.of(utf8Bytes);
Object result = path.extract(parser);
6.3 Use JSONPath Read part jsonbBytes The data of
byte[] jsonbBytes = ...;
JSONPath path = JSONPath.of("$.id"); // Caching and reuse can improve performance
JSONReader parser = JSONReader.ofJSONB(jsonbBytes); // Be careful , It's using ofJSONB Method
Object result = path.extract(parser);
source :https://github.com/alibaba/fastjson2/releases
------
We have created a high-quality technical exchange group , With good people , I will be excellent myself , hurriedly Click Add group , Enjoy growing up together . in addition , If you want to change jobs recently , Years ago, I spent 2 A wave of large factory classics were collected in a week , Those who are ready to change jobs after the festival can Click here to get !
Recommended reading
Is this open source project going to make me laugh to death ?
video :Java 18 new function Java Code to start the static server
··································
Hello , I'm a procedural ape DD,10 Old driver developed in 、 Alibaba cloud MVP、 Tencent cloud TVP、 Published books 、 Entrepreneurship 、 State-owned enterprises 4 In the Internet 6 year .10 Graduated years ago and joined the universe , The salary is not high 、 Not too busy , Amateurs insist on studying technology and doing what they want to do .4 He left state-owned enterprises after two years , Join the entrepreneurial team of Yonghui Internet , From development 、 To architecture 、 To partner . Come all the way , My deepest feeling is that I must keep learning and pay attention to the frontier . As long as you can hold on , Think more 、 Don't complain 、 Do it frequently , It's easy to overtake on a curve ! therefore , Don't ask me what I'm doing now, whether it's in time . If you are optimistic about one thing , It must be persistence to see hope , Instead of sticking to it when you see hope . believe me , Just stick to it , You must be better than now ! If you don't have any direction , You can pay attention to me first , Some cutting-edge information is often shared here , Help you accumulate the capital to overtake on the curve .
版权声明
本文为[Program ape DD_]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204231158582732.html
边栏推荐
- 如果你是一个Golang面试官,你会问哪些问题?
- Yunna | fixed assets inventory supports multiple inventory methods (asset inventory)
- Chapter 4 is a tutorial on forced filling of in memory objects with IM enabled filling objects (IM 4.7)
- ImportError: libX11. so. 6: cannot open shared object file: No such file or directory
- Resolution due to AMD not found_ ags_ x64. DLL, unable to continue code execution. Reinstallation of the program may solve this problem, Forza horizon 5
- 软银愿景基金进军Web3安全行业 领投CertiK 6000万美元新一轮投资
- 全网最细的短网址系统设计与实战
- 为什么要有包装类,顺便说一说基本数据类型、包装类、String类该如何转换?
- Fabric 1.0 source code analysis (33) implementation of peer channel command and subcommand
- Nacos Foundation (9): Nacos configuration management from single architecture to microservices
猜你喜欢
AI 视频云 VS 窄带高清,谁是视频时代的宠儿
一文详解头部位姿估计【收藏好文】
力扣-1137.第N个泰波那契数
面了一圈,整理了这套面试题。。
魔域来了H5游戏详细图文架设教程
Running error: unable to find or load the main class com xxx. Application
Interpretation 3 of gdpr series: how do European subsidiaries return data to domestic parent companies?
Significance of actively participating in middle school robot competition
On the integration of steam education in early childhood education
简易投票系统数据库设计
随机推荐
力扣-1137.第N个泰波那契数
IMEU如何与IMCU相关联(IM 5.5)
激活函数之relu函数
远程访问家里的树莓派(上)
Chapter 4: enable and disable im column storage for materialized view (IM 4.6)
九十八、freemarker框架报错 s.e.ErrorMvcAutoConfiguration$StaticView : Cannot render error page for request
Idea code formatting plug-in save actions
程序员如何用130行代码敲定核酸统计
ImportError: libX11.so.6: cannot open shared object file: No such file or directory
第四章 为IM 启用填充对象之启用和禁用表空间的IM列存储(IM 4.5)
激活函数之阶跃函数
Fabric 1.0源代码分析(33) Peer #peer channel命令及子命令实现
用户接口和IM表达式(IM 5.6)
论文解读(CGC)《CGC: Contrastive Graph Clustering for Community Detection and Tracking》
C# F23.StringSimilarity库 字符串重复度、文本相似度、防抄袭
Summary of convolution layer and pooling layer
Link sorting of tutorials such as assembly language running environment setting
AcWing 1874. Moo encryption (enumeration, hash)
Analyze the rules for the use of robots with good performance
Database design of forum system