当前位置:网站首页>Alibaba / popular JSON parsing open source project fastjson2
Alibaba / popular JSON parsing open source project fastjson2
2022-07-02 09:34:00 【Janson666】
One 、 Introduce
FASTJSON v2 yes FASTJSON Important upgrade of the project , The goal is to provide a high-performance platform for the next decade JSON library . Through the same set of API,
GitHub Address : https://github.com/alibaba/fastjson2
- Support JSON/JSONB Two protocols ,JSONPath First class citizen .
- Support full resolution and partial resolution .
- Support Java Server side 、 client Android、 Big data scenario .
- Support kotlin
1. Use maven Add dependency
<dependency>
<groupId>com.alibaba.fastjson2</groupId>
<artifactId>fastjson2</artifactId>
<version>2.0.3</version>
</dependency>
2. fastjson v1 Compatible modules
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.3</version>
</dependency>
Two 、 Easy to use
1. Create entity class objects
package com.janson.demo.fastjson;
import lombok.Data;
/** * @Author Janson * @Date 2022/5/16 9:05 * @Version 1.0 */
@Data
public class DemoData {
private Integer id;
private String name;
private Integer age;
private String sex;
}
2. Three methods commonly used in testing JSON.toJSONString(JavaBean),JSON.parseObject(str, DemoData.class);JSON.parseArray(str, DemoData.class);
Methods to introduce :
- JSON.parseObject(str, DemoData.class) take JSON It can be interpreted as JSONObject
- JSON.parseArray(str, DemoData.class); take JSON It can be interpreted as JSONArray
- JSON.toJSONString(JavaBean), take Java Object serialization to JSON
package com.janson.demo.fastjson;
import com.alibaba.fastjson2.JSON;
import java.util.List;
/** * @Author Janson * @Date 2022/5/16 9:03 * @Version 1.0 */
public class TestFastjson {
public static void main(String[] args) {
System.out.println("-----JSON.toJSONString()----- test javaBean To json character string ---------------");
DemoData demoData = new DemoData();
demoData.setAge(18);
demoData.setId(11108);
demoData.setName(" Frenchman Zhang San ");
demoData.setSex(" male "); //
String stoJSONString = JSON.toJSONString(demoData);
System.out.println(stoJSONString);
System.out.println("----------- End of test --------------\n");
System.out.println("-----JSON.parseObject(s, DemoData.class);----- test json String to javaBean---------------");
DemoData demoDataparseObject = JSON.parseObject(stoJSONString, DemoData.class);
System.out.println(demoDataparseObject.getAge() + " " + demoDataparseObject.getName()+" " + demoDataparseObject.getId() + " "+demoDataparseObject.getSex());
System.out.println("----------- End of test --------------\n");
System.out.println("-----JSON.parseArray(str, DemoData.class);----- Test a group json String to javaBean Set (list)---------------");
String str = "[{\"age\":18,\"id\":11108,\"name\":\" Frenchman Zhang San \",\"sex\":\" male \"},{\"age\":80,\"id\":188808,\"name\":\" Outlaw Trump \",\"sex\":\" Unknown \"}]";
List<DemoData> demoDataparseArray = JSON.parseArray(str, DemoData.class);
int i = 0;
for (DemoData demoDataList: demoDataparseArray) {
System.out.println(" Set index :" + i+ " Aggregate data " + demoDataList);
i++;
}
System.out.println("----------- End of test --------------");
}
}
3、 ... and 、 Advanced use Use JSONB (JSONB finger JSON Byte[])
3.1 take JavaBean Object serialization JSONB =>
- Method JSONB.toBytes(JavaBean)
- take JSONB The data is parsed as JavaBean :JSONB.parseObject(bytes,DemoData.class)
System.out.println("-----JSONB.toBytes()----- test javaBean serialize JSONB, Byte array --------------");
DemoData demoData1 = new DemoData();
demoData1.setAge(18);
demoData1.setId(11108);
demoData1.setName(" Frenchman Zhang San ");
demoData1.setSex(" male ");
byte[] bytes = JSONB.toBytes(demoData1);
System.out.println(bytes.length);
for (int b = 0;b<bytes.length;b++) {
System.out.println( b +" "+ bytes[b]);
}
System.out.println("----------- End of test --------------\n");
System.out.println("-----JSONB.parseObject(bytes,DemoData.class)----- test JSONB It can be interpreted as javaBean--------------");
DemoData demoDataJSONB = JSONB.parseObject(bytes,DemoData.class);
System.out.println(demoDataJSONB.getId() + " "+ demoDataJSONB.getName() + " "
+ demoDataJSONB.getAge() + " "+demoDataJSONB.getSex());
System.out.println("----------- End of test --------------\n");
``
边栏推荐
- 在SQL注入中,为什么union联合查询,id必须等于0
- What are the waiting methods of selenium
- 每天睡觉前30分钟阅读_day3_Files
- 上班第一天的报错(Nessus安装winpcap报错)
- Break the cocoon | one article explains what is the real cloud primordial
- A detailed explanation takes you to reproduce the statistical learning method again -- Chapter 2, perceptron model
- Redis installation and deployment (windows/linux)
- Safety production early warning system software - Download safety production app software
- 数构(C语言--代码有注释)——第二章、线性表(更新版)
- VIM操作命令大全
猜你喜欢

idea查看字节码配置

Microservice practice | fuse hytrix initial experience

Navicat 远程连接Mysql报错1045 - Access denied for user ‘root‘@‘222.173.220.236‘ (using password: YES)
![[go practical basis] how to set the route in gin](/img/23/f38d68c4fd238d453b9a7670483002.png)
[go practical basis] how to set the route in gin

Chrome browser tag management plug-in – onetab

Hystrix implements request consolidation

How to install PHP in CentOS

《统计学习方法》——第五章、决策树模型与学习(上)

深入剖析JVM是如何执行Hello World的

分享一篇博客(水一篇博客)
随机推荐
Microservice practice | Eureka registration center and cluster construction
西瓜书--第六章.支持向量机(SVM)
Redis installation and deployment (windows/linux)
双非本科生进大厂,而我还在底层默默地爬树(上)
Oracle delete tablespace and user
Activity的创建和跳转
Chrome用户脚本管理器-Tampermonkey 油猴
微服务实战|微服务网关Zuul入门与实战
知识点很细(代码有注释)数构(C语言)——第三章、栈和队列
每天睡前30分钟阅读Day5_Map中全部Key值,全部Value值获取方式
Number structure (C language) -- Chapter 4, compressed storage of matrices (Part 2)
Supplier selection and prequalification of Oracle project management system
C语言之二进制与十进制
记录下对游戏主机配置的个人理解与心得
BugkuCTF-web24(解题思路及步骤)
每天睡觉前30分钟阅读_day4_Files
企业级SaaS CRM实现
Solutions to Chinese garbled code in CMD window
kinect dk 获取CV::Mat格式的彩色RGB图像(openpose中使用)
一篇详解带你再次重现《统计学习方法》——第二章、感知机模型