当前位置:网站首页>[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
边栏推荐
- 【带你学c带你飞】2day 第8章 指针(练习8.1 密码开锁)
- [learn C and fly] day 5 chapter 2 program in C language (Exercise 2)
- MVVM and MVC
- [punch in questions] integrated daily 5-question sharing (phase II)
- how to add one row in the dataframe?
- AR增强现实可应用的场景
- Duplicate keys detected: ‘0‘. This may cause an update error. found in
- Provincial election + noi Part IV graph theory
- 剑指 Offer II 031. 最近最少使用缓存
- [pit] how to understand "parameter fishing"
猜你喜欢

Formatting logic of SAP ui5 currency amount display

CVPR 2022 | 大连理工提出自校准照明框架,用于现实场景的微光图像增强

Leetcode question brushing (10) - sequential question brushing 46 to 50

【带你学c带你飞】1day 第2章 (练习2.2 求华氏温度 100°F 对应的摄氏温度

Pat a-1165 block reversing (25 points)

Summary of some experiences in the process of R & D platform splitting
![[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
![[Chongqing Guangdong education] Sichuan University concise university chemistry · material structure part introductory reference materials](/img/ae/7edbdf55795400166650c795c8bd58.jpg)
[Chongqing Guangdong education] Sichuan University concise university chemistry · material structure part introductory reference materials

【liuyubobobo-玩转Leetcode算法面试】【00】课程概述

【读书笔记】程序员修炼手册—实战式学习最有效(项目驱动)
随机推荐
[learn C and fly] 3day Chapter 2 program in C language (exercise 2.3 calculate piecewise functions)
【带你学c带你飞】2day 第8章 指针(练习8.1 密码开锁)
Calculation (computer) code of suffix expression
Which kind of sports headphones is easier to use? The most recommended sports headphones
oracle创建只读权限的用户简单四步走
QT uses sqllite
How to solve MySQL master-slave delay problem
flutter 中間一個元素,最右邊一個元素
[pit] how to understand "parameter fishing"
QT使用sqllite
Sword finger offer 62 The last remaining number in the circle
Deployment practice and problem solving of dash application development environment based on jupyter Lab
How to run oddish successfully from 0?
Webgpu (I): basic concepts
STM32F103——两路PWM控制电机
Duplicate keys detected: ‘0‘. This may cause an update error. found in
MySQL constraints and multi table query example analysis
STM32__ 05 - PWM controlled DC motor
Connected block template and variants (4 questions in total)
[learn C and fly] 2day Chapter 8 pointer (practice 8.1 password unlocking)