当前位置:网站首页>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
- Matplotlib剑客行——布局指南与多图实现(更新)
- From concept to method, the statistical learning method -- Chapter 3, k-nearest neighbor method
- Knowledge points are very detailed (code is annotated) number structure (C language) -- Chapter 3, stack and queue
- Beats (filebeat, metricbeat), kibana, logstack tutorial of elastic stack
- [go practical basis] how to install and use gin
- 企业级SaaS CRM实现
- ClassFile - Attributes - Code
- BugkuCTF-web21(详细解题思路及步骤)
- 自定义Redis连接池
猜你喜欢
个人经历&&博客现状
"Redis source code series" learning and thinking about source code reading
微服务实战|原生态实现服务的发现与调用
Timed thread pool implements request merging
Programmers with ten years of development experience tell you, what core competitiveness do you lack?
Matplotlib剑客行——初相识Matplotlib
[go practical basis] how to set the route in gin
Activity的创建和跳转
图像识别-数据增广
A detailed explanation takes you to reproduce the statistical learning method again -- Chapter 2, perceptron model
随机推荐
Solution to amq4036 error in remote connection to IBM MQ
Chrome user script manager tempermonkey monkey
Redis installation and deployment (windows/linux)
Break the cocoon | one article explains what is the real cloud primordial
别找了,Chrome浏览器必装插件都在这了
web安全与防御
Learn combinelatest through a practical example
Navicat 远程连接Mysql报错1045 - Access denied for user ‘root‘@‘222.173.220.236‘ (using password: YES)
自定義Redis連接池
JVM指令助记符
攻防世界-Web进阶区-unserialize3
College Students' CET-4 and CET-6 composition template (self created version, successfully crossed CET-6)
Oracle modifies tablespace names and data files
互联网API接口幂等设计
Hystrix implements request consolidation
FragmentTabHost实现房贷计算器界面
Discussion on improving development quality and reducing test bug rate
How to use pyqt5 to make a sensitive word detection tool
Typeerror: X () got multiple values for argument 'y‘
道阻且长,行则将至