当前位置:网站首页>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
边栏推荐
- Chapter 4: enable and disable im column storage for materialized view (IM 4.6)
- 画结果图推荐网址
- The way to change children's programming structure
- Idea code quality specification plug-in sonarlint
- 力扣-70.爬楼梯
- Master slave replication configuration of MySQL
- How imeu is associated with imcu (IM 5.5)
- Sofa weekly | excellent Committee of the year, contributor of this week, QA of this week
- 激活函数之sigmoid函数
- TclError: no display name and no $DISPLAY environment variable
猜你喜欢
Force buckle - 1137 Nth teponacci number
配电房远程综合监控系统在10kV预制舱项目中的应用
Nacos Foundation (9): Nacos configuration management from single architecture to microservices
Design and practice of the smallest short website system in the whole network
How to count fixed assets and how to generate an asset count report with one click
软银愿景基金进军Web3安全行业 领投CertiK 6000万美元新一轮投资
如果你是一个Golang面试官,你会问哪些问题?
Laravel adds custom helper functions
Database design of forum system
远程访问家里的树莓派(上)
随机推荐
第二十四课 经典问题解析
项目实训-火爆辣椒
5-minute NLP: text to text transfer transformer (T5) unified text to text task model
外包干了五年,废了...
第四章 为IM 启用填充对象之为IM列存储启用ADO(IM 4.8)
2022 love analysis · panoramic report of industrial Internet manufacturers
Sofa weekly | excellent Committee of the year, contributor of this week, QA of this week
PSCP 基本使用
九十八、freemarker框架报错 s.e.ErrorMvcAutoConfiguration$StaticView : Cannot render error page for request
How to expand the capacity of the server in the 100 million level traffic architecture? Well written!
远程访问家里的树莓派(上)
VMware虚拟机使用esxi 导出硬盘vmdk文件
Database Navigator 使用默认MySQL连接提示:The server time zone value ‘Öйú±ê׼ʱ¼ä’ is unrecognized or repres
Next.js 静态数据生成以及服务端渲染的方式
AcWing 1874. Moo encryption (enumeration, hash)
golang之笔试题&面试题01
Practical data Lake iceberg lesson 30 MySQL - > iceberg, time zone problems of different clients
Here comes the detailed picture and text installation tutorial of H5 game
Running error: unable to find or load the main class com xxx. Application
配电房远程综合监控系统在10kV预制舱项目中的应用