当前位置:网站首页>[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
边栏推荐
- Spend a week painstakingly sorting out the interview questions and answers of high-frequency software testing / automated testing
- LFM信号加噪、时频分析、滤波
- What kind of good and cost-effective Bluetooth sports headset to buy
- flutter 中間一個元素,最右邊一個元素
- Iterative unified writing method of binary tree
- es面試題
- What is the function of the headphone driver
- Software testing learning notes - network knowledge
- 连通块模板及变式(共4题)
- 实现一个自定义布局的扫码功能
猜你喜欢

CSDN article underlined, font color changed, picture centered, 1 second to understand

Pat a-1165 block reversing (25 points)

WebGPU(一):基本概念

Leetcode face T10 (1-9) array, ByteDance interview sharing
![[question 008: what is UV in unity?]](/img/f7/5ee0b18d1fe21ff3b98518c46d9520.jpg)
[question 008: what is UV in unity?]

MVVM and MVC
![[technology development -21]: rapid overview of the application and development of network and communication technology -1- Internet Network Technology](/img/2d/299fa5c76416f74bd1a693c433dd09.png)
[technology development -21]: rapid overview of the application and development of network and communication technology -1- Internet Network Technology

How to hide the scroll bar of scroll view in uniapp

Which kind of sports headphones is easier to use? The most recommended sports headphones

Comparative analysis of MVC, MVP and MVVM, source code analysis
随机推荐
QT实现界面跳转
Golang lock
【带你学c带你飞】4day第2章 用C语言编写程序(练习 2.5 生成乘方表与阶乘表
QT implementation interface jump
Face++ realizes face detection in the way of flow
CoordinatorLayout + TabLayout + ViewPager2(里面再嵌套一个RecyclerView),RecyclerView的滑动冲突解决
Build a modern data architecture on the cloud with Amazon AppFlow, Amazon lake formation and Amazon redshift
OpenCASCADE7.6编译
[learn C and fly] 3day Chapter 2 program in C language (exercise 2.3 calculate piecewise functions)
Software development life cycle -- waterfall model
Pat a-1165 block reversing (25 points)
Email picture attachment
JVM面试篇
Divorce for 3 years to discover the undivided joint property, or
Sword finger offer 29 Print matrix clockwise
CSDN insertion directory in 1 second
Sword finger offer II 031 Least recently used cache
leetcode2311. Longest binary subsequence less than or equal to K (medium, weekly)
Sword finger offer 47 Maximum value of gifts
es面試題