当前位置:网站首页>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");
``
边栏推荐
猜你喜欢
hystrix 实现请求合并
[go practical basis] how to bind and use URL parameters in gin
个人经历&&博客现状
Learn combinelatest through a practical example
DTM distributed transaction manager PHP collaboration client V0.1 beta release!!!
每天睡觉前30分钟阅读_day4_Files
一篇详解带你再次重现《统计学习方法》——第二章、感知机模型
How to use PHP spoole to implement millisecond scheduled tasks
Solutions to Chinese garbled code in CMD window
微服务实战|熔断器Hystrix初体验
随机推荐
C语言之二进制与十进制
Required request body is missing:(跨域问题)
Difference between redis serialization genericjackson2jsonredisserializer and jackson2jsonredisserializer
[go practical basis] gin efficient artifact, how to bind parameters to structures
Programmers with ten years of development experience tell you, what core competitiveness do you lack?
Machine learning practice: is Mermaid a love movie or an action movie? KNN announces the answer
自定義Redis連接池
Matplotlib剑客行——没有工具用代码也能画图的造型师
破茧|一文说透什么是真正的云原生
Complete solution of servlet: inheritance relationship, life cycle, container, request forwarding and redirection, etc
Learn combinelatest through a practical example
FragmentTabHost实现房贷计算器界面
每天睡前30分钟阅读Day6_Day6_Date_Calendar_LocalDate_TimeStamp_LocalTime
一次聊天勾起的回忆
Redis 序列化 GenericJackson2JsonRedisSerializer和Jackson2JsonRedisSerializer的区别
上班第一天的报错(Nessus安装winpcap报错)
微服务实战|手把手教你开发负载均衡组件
微服务实战|微服务网关Zuul入门与实战
Pdf document of distributed service architecture: principle + Design + practice, (collect and see again)
[go practical basis] how to customize and use a middleware in gin