当前位置:网站首页>Fastjson 2 来了,性能继续提升,还能再战十年
Fastjson 2 来了,性能继续提升,还能再战十年
2022-04-23 11:59:00 【程序猿DD_】
FASTJSON 2.0是FASTJSON项目的重要升级,目标是为下一个十年提供一个高性能的JSON库,同一套API支持JSON/JSONB两种协议,JSONPath是一等公民,支持全量解析和部分解析,支持Java服务端、客户端Android、大数据场景。
FASJTONS2代码 https://github.com/alibaba/fastjson2/releases/tag/2.0.1
JSONB格式文档 https://github.com/alibaba/fastjson2/wiki/jsonb_format_cn
FASTJSON 2性能有了很大提升,具体性能数据看这里 https://github.com/alibaba/fastjson2/wiki/fastjson_benchmark
2. 使用前准备
2.1 Maven依赖
在fastjson 2.0中,groupId和1.x不一样,是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
如果原来使用fastjson 1.2.x版本,可以使用兼容包,兼容包不能保证100%兼容,请仔细测试验证,发现问题请及时反馈。
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>2.0.1</version>
</dependency>
2.2 常用类和方法
在fastjson 2.0中,package和1.x不一样,是com.alibaba.fastjson2。如果你之前用的是fastjson1,大多数情况直接更包名就即可。
package com.alibaba.fastjson2;
class JSON {
// 将字符串解析成JSONObject
static JSONObject parseObject(String str);
// 将字符串解析成JSONArray
static JSONArray parseArray(String str);
// 将字符串解析成Java对象
static T parseObject(byte[] utf8Bytes, Class<T> objectClass);
// 将Java对象输出成字符串
static String toJSONString(Object object);
// 将Java对象输出成UT8编码的byte[]
static byte[] toJSONBytes(Object object);
}
class JSONB {
// 将jsonb格式的byte[]解析成Java对象
static T parseObject(byte[] jsonbBytes, Class<T> objectClass);
// 将Java对象输出成jsonb格式的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);
// 将JSONObject对象转换为Java对象
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 {
// 构造JSONPath
static JSONPath of(String path);
// 根据path直接解析输入,会部分解析优化,不会全部解析
Object extract(JSONReader jsonReader);
// 根据path对对象求值
Object eval(Object rootObject);
}
class JSONReader {
// 构造基于String输入的JSONReader
static JSONReader of(String str);
// 构造基于ut8编码byte数组输入的JSONReader
static JSONReader of(byte[] utf8Bytes);
// 构造基于char[]输入的JSONReader
static JSONReader of(char[] chars);
// 构造基于json格式byte数组输入的JSONReader
static JSONReader ofJSONB(byte[] jsonbBytes)
}
3. 读取JSON对象
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. 将JavaBean对象生成JSON
4.1 将JavaBean对象生成JSON格式的字符串
class Product {
public int id;
public String name;
}
Product product = new Product();
product.id = 1001;
product.name = "DataWorks";
JSON.toJSONString(product);
// 生成如下的结果
{
"id" : 1001,
"name" : "DataWorks"
}
JSON.toJSONString(product, JSONWriter.Feature.BeanToArray);
// 生成如下的结果
[123, "DataWorks"]
4.2 将JavaBean对象生成UTF8编码的byte[]
Product product = ...;
byte[] utf8JSONBytes = JSON.toJSONBytes(product);
4.3 将JavaBean对象生成JSONB格式的byte[]
Product product = ...;
byte[] jsonbBytes = JSONB.toBytes(product);
byte[] jsonbBytes = JSONB.toBytes(product, JSONWriter.Feature.BeanToArray);
5. 读取JavaBean
5.1 将字符串读取成JavaBean
String str = "{\"id\":123}";
Product product = JSON.parseObject(str, Product.class);
5.2 将UTF8编码的byte[]读取成JavaBean
byte[] utf8Bytes = "{\"id\":123}".getBytes(StandardCharsets.UTF_8);
Product product = JSON.parseObject(utf8Bytes, Product.class);
5.3 将JSONB数据读取成JavaBean
byte[] jsonbBytes = ...
Product product = JSONB.parseObject(jsonbBytes, Product.class);
Product product = JSONB.parseObject(jsonbBytes, Product.class, JSONReader.Feature.SupportBeanArrayMapping);
6. 使用JSONPath
6.1 使用JSONPath部分读取数据
String str = ...;
JSONPath path = JSONPath.of("$.id"); // 缓存起来重复使用能提升性能
JSONReader parser = JSONReader.of(str);
Object result = path.extract(parser);
6.2 使用JSONPath读取部分utf8Bytes的数据
byte[] utf8Bytes = ...;
JSONPath path = JSONPath.of("$.id"); // 缓存起来重复使用能提升性能
JSONReader parser = JSONReader.of(utf8Bytes);
Object result = path.extract(parser);
6.3 使用JSONPath读取部分jsonbBytes的数据
byte[] jsonbBytes = ...;
JSONPath path = JSONPath.of("$.id"); // 缓存起来重复使用能提升性能
JSONReader parser = JSONReader.ofJSONB(jsonbBytes); // 注意,这是利用ofJSONB方法
Object result = path.extract(parser);
来源:https://github.com/alibaba/fastjson2/releases
------
我们创建了一个高质量的技术交流群,与优秀的人在一起,自己也会优秀起来,赶紧点击加群,享受一起成长的快乐。另外,如果你最近想跳槽的话,年前我花了2周时间收集了一波大厂面经,节后准备跳槽的可以点击这里领取!
推荐阅读
··································
你好,我是程序猿DD,10年开发老司机、阿里云MVP、腾讯云TVP、出过书、创过业、国企4年互联网6年。10年前毕业加入宇宙行,工资不高、也不算太忙,业余坚持研究技术和做自己想做的东西。4年后离开国企,加入永辉互联网板块的创业团队,从开发、到架构、到合伙人。一路过来,给我最深的感受就是一定要不断学习并关注前沿。只要你能坚持下来,多思考、少抱怨、勤动手,就很容易实现弯道超车!所以,不要问我现在干什么是否来得及。如果你看好一个事情,一定是坚持了才能看到希望,而不是看到希望才去坚持。相信我,只要坚持下来,你一定比现在更好!如果你还没什么方向,可以先关注我,这里会经常分享一些前沿资讯,帮你积累弯道超车的资本。
版权声明
本文为[程序猿DD_]所创,转载请带上原文链接,感谢
https://didispace-wx.blog.csdn.net/article/details/124357654
边栏推荐
- 抓包整理————tcp 协议[八]
- Golang Pen & interview 01
- Nacos Foundation (7): Configuration Management
- 论文解读(CGC)《CGC: Contrastive Graph Clustering for Community Detection and Tracking》
- A detailed explanation of head pose estimation [collection of good articles]
- Database design of forum system
- Laravel always returns JSON response
- How to count fixed assets and how to generate an asset count report with one click
- 第四章 为IM 启用填充对象之强制填充In-Memory对象:教程(IM 4.7)
- Purpose of IM expression (IM 5.2)
猜你喜欢
Analyze the rules for the use of robots with good performance
In idea Solution to the problem of garbled code in Chinese display of properties file
[Web 每日一练] 八色拼图(float)
力扣-70.爬楼梯
Maker education for primary and middle school students to learn in happiness
Understanding of MQ
Significance of actively participating in middle school robot competition
《通用数据保护条例》(GDPR)系列解读三:欧洲子公司如何向国内母公司回传数据?
The listing of saiweidian Technology Innovation Board broke: a decrease of 26% and the market value of the company was 4.4 billion
Nacos Foundation (7): Configuration Management
随机推荐
项目实训-火爆辣椒
用户接口和IM表达式(IM 5.6)
Application of remote integrated monitoring system in power distribution room in 10kV prefabricated cabin project
IM 体系结构:CPU架构:SIMD向量处理(IM-2.3)
编程辅助工具推荐:图片工具snipaste
5-minute NLP: text to text transfer transformer (T5) unified text to text task model
Fabric 1.0 source code analysis (33) implementation of peer channel command and subcommand
kettle复制记录到结果和从结果获取记录使用
Tips for installing MySQL service in windows11: Install / Remove of the Service denied
第二十五课 类的静态成员变量
Cognition and R & D technology of micro robot
Docker MySQL master-slave backup
The fourth chapter is the enable and disable columns of IM enabled fill objects (Part III of im-4.3)
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
AcWing 1874. Moo encryption (enumeration, hash)
抓包整理————tcp 协议[八]
Interpreting the art created by robots
ThinkPHP adds image text watermark to generate promotion poster with QR code
IDEA 中 .properties文件的中文显示乱码问题的解决办法
VMware虚拟机使用esxi 导出硬盘vmdk文件