当前位置:网站首页>[JSON] gson use and step on the pit
[JSON] gson use and step on the pit
2022-07-02 02:31:00 【The slag height of writing bug】
List of articles
Serialization and deserialization tests
1.int
@org.junit.jupiter.api.Test
public void testInt(){
Gson gson=new Gson();
System.out.println(" test int serialize "+gson.toJson(1));
Integer number1=gson.fromJson("1", Integer.class);
int number=gson.fromJson("265",int.class);
}
2.double
@org.junit.jupiter.api.Test
public void testDouble(){
Gson gson=new Gson();
System.out.println(" test double serialize "+gson.toJson(10.156));
System.out.println(" test double Deserialization "+gson.fromJson("123.00",Double.class));
}
3.string
@org.junit.jupiter.api.Test
public void testString(){
Gson gson=new Gson();
System.out.println(" Test string serialization "+gson.toJson("ggzx"));
System.out.println(" Test string deserialization "+gson.fromJson(gson.toJson("ggzx",String.class)));
}
4.Array
@org.junit.jupiter.api.Test
public void testArray(){
Gson gson=new Gson();
int[] values={
1,2,5,898,4};
System.out.println(" Test array serialization "+gson.toJson(values));
System.out.println(" Test array deserialization ");
int[] values1=gson.fromJson(gson.toJson(values),int[].class);
for (int a:values1) {
System.out.print(a);
}
}
5.Class
@org.junit.jupiter.api.Test
public void testClass(){
Gson gson=new Gson();
User user=new User("123","send-data",12,"123456",56.0);
System.out.println(" Test object serialization "+gson.toJson(user));
System.out.println(" Test object deserialization "+user);
}
6.List<>
@org.junit.jupiter.api.Test
public void testClassList(){
Gson gson=new Gson();
User user1=new User("123","send-data",12,"123456",56.0);
User user2=new User("123","send-data",12,"123456",56.0);
User user3=new User("123","send-data",12,"123456",56.0);
List<User> userList=new LinkedList<>();
userList.add(user1);
userList.add(user2);
userList.add(user3);
System.out.println(" test List<> serialize :"+gson.toJson(userList));
Type userListType = new TypeToken<ArrayList<User>>(){
}.getType();
List<User> userList1=gson.fromJson(gson.toJson(userList),userListType);
System.out.println(" test List<> Deserialization "+gson.fromJson(gson.toJson(userList),userListType));
for (User user:userList1) {
System.out.println(user.toString());
}
}
- Map
@org.junit.jupiter.api.Test
public void testMap(){
Gson gson=new Gson();
Map<String ,String > map=new HashMap<>();
map.put("user","ggzx");
map.put("password","123");
System.out.println(" test map serialize "+gson.toJson(map));
Type mapType = new TypeToken<Map<String,String>>(){
}.getType();
System.out.println(" test map Deserialization "+gson.fromJson(gson.toJson(map),mapType));
}
8. Complex object
@org.junit.jupiter.api.Test
public void testComplexClass(){
Gson gson=new Gson();
Zoo zoo=new Zoo();
ZooKeeper zooKeeper=new ZooKeeper("ggzx",18);
zoo.setZooKeeper(zooKeeper);
Lion lion=new Lion("lion1",3);
Lion lion2=new Lion("lion2",5);
Lion lion3=new Lion("lion3",5);
List<Lion> lionList=new LinkedList<>();
lionList.add(lion);
lionList.add(lion2);
lionList.add(lion3);
Monkey monkey=new Monkey("monkey1",7);
Monkey monkey2=new Monkey("monkey2",8);
Monkey monkey3=new Monkey("monkey3",9);
List<Monkey> monkeyList=new LinkedList<>();
monkeyList.add(monkey);
monkeyList.add(monkey2);
monkeyList.add(monkey3);
zoo.setLionList(lionList);
zoo.setMonkeyList(monkeyList);
System.out.println(" Test serialization of complex objects "+gson.toJson(zoo));
System.out.println(" Test complex object deserialization "+gson.fromJson(gson.toJson(zoo),Zoo.class));
}
Result display :
The following categories are used to test serialization and deserialization =======>>>>
test int serialize 1
test double serialize 10.156
test double Deserialization 123.0
Test array serialization [1,2,5,898,4]
Test array deserialization
1258984 Test string serialization "ggzx"
Test string deserialization ggzx
Test object serialization {
"code":"123","mode":"send-data","age":12,"password":"123456","salary":56.0}
Test object deserialization User{
code='123', mode='send-data', age=12, password='123456', salary=56.0}
test List<> serialize :[{
"code":"123","mode":"send-data","age":12,"password":"123456","salary":56.0},{
"code":"123","mode":"send-data","age":12,"password":"123456","salary":56.0},{
"code":"123","mode":"send-data","age":12,"password":"123456","salary":56.0}]
test List<> Deserialization [User{
code='123', mode='send-data', age=12, password='123456', salary=56.0}, User{
code='123', mode='send-data', age=12, password='123456', salary=56.0}, User{
code='123', mode='send-data', age=12, password='123456', salary=56.0}]
User{
code='123', mode='send-data', age=12, password='123456', salary=56.0}
User{
code='123', mode='send-data', age=12, password='123456', salary=56.0}
User{
code='123', mode='send-data', age=12, password='123456', salary=56.0}
test map serialize {
"password":"123","user":"ggzx"}
test map Deserialization {
password=123, user=ggzx}
Test serialization of complex objects {
"zooKeeper":{
"name":"ggzx","age":18},"lionList":[{
"name":"lion1","age":3},{
"name":"lion2","age":5},{
"name":"lion3","age":5}],"monkeyList":[{
"name":"monkey1","age":7},{
"name":"monkey2","age":8},{
"name":"monkey3","age":9}]}
Test complex object deserialization Zoo{
zooKeeper=ZooKeeper{
name='ggzx', age=18}, lionList=[Lion{
name='lion1', age=3}, Lion{
name='lion2', age=5}, Lion{
name='lion3', age=5}], monkeys=[Monkey{
name='monkey1', age=7}, Monkey{
name='monkey2', age=8}, Monkey{
name='monkey3', age=9}]}
9. Custom object
If you want to use complex objects after all , such as
user: Store user information
dataList: Store many kinds of List<> data ,
We can encapsulate these two objects in the same class , For example, package into Data class
After initializing the data , Directly gson.toJson that will do ;
At the time of receiving , It just needs gons.fromJson(json,Data.class);
Next section :Spring+gson/jackson
边栏推荐
- Open那啥的搭建文档
- 剑指 Offer 29. 顺时针打印矩阵
- STM32__ 05 - PWM controlled DC motor
- 剑指 Offer 47. 礼物的最大价值
- Realize the code scanning function of a custom layout
- 【OpenCV】-5种图像滤波的综合示例
- Start from scratch - Web Host - 01
- Iterative unified writing method of binary tree
- Vsocde has cli every time it is opened js
- How to build and use redis environment
猜你喜欢
CVPR 2022 | Dalian Institute of technology proposes a self calibration lighting framework for low light level image enhancement of real scenes
剑指 Offer 62. 圆圈中最后剩下的数字
How to execute an SQL in MySQL
SAP ui5 beginner tutorial 19 - SAP ui5 data types and complex data binding
How to turn off debug information in rtl8189fs
【毕业季】研究生学长分享怎样让本科更有意义
花一个星期时间呕心沥血整理出高频软件测试/自动化测试面试题和答案
How to hide the scroll bar of scroll view in uniapp
Additional: information desensitization;
CVPR 2022 | 大连理工提出自校准照明框架,用于现实场景的微光图像增强
随机推荐
Comparative analysis of MVC, MVP and MVVM, source code analysis
Pytest testing framework
[learn C and fly] 2day Chapter 8 pointer (practice 8.1 password unlocking)
Set status bar color
【读书笔记】程序员修炼手册—实战式学习最有效(项目驱动)
JVM面试篇
The basic steps of using information theory to deal with scientific problems are
CVPR 2022 | Dalian Institute of technology proposes a self calibration lighting framework for low light level image enhancement of real scenes
Yyds dry goods inventory accelerating vacuum in PG
Open那啥的搭建文档
The middle element and the rightmost element of the shutter
C return multiple values getter setter queries the database and adds the list return value to the window
STM32__ 05 - PWM controlled DC motor
剑指 Offer II 031. 最近最少使用缓存
Questions d'entrevue
LFM信号加噪、时频分析、滤波
Mathematics in Sinorgchem: computational geometry
Open that kind of construction document
附加:信息脱敏;
Additional: information desensitization;