当前位置:网站首页>Gson and fastjson
Gson and fastjson
2022-07-25 14:31:00 【Gai Linan】
background
The company pays more attention to safety , And then recently fastjson Vulnerabilities appear a little more frequently , So ask for , Try not to use fastjson, So the team leader chose gson As a substitute .
Use
About json, I'm in the code , There are two places most commonly used , One is used for type conversion , The other is laziness , When calling the third-party interface, it is used to receive data . Let's take a look at gson and fastjson Use
gson
/** * List《==》json */
List<String> stringList = Lists.newArrayList("a", "b", "c", "d");
String listJsonString = gson.toJson(stringList);
System.out.println(stringList);
System.out.println(listJsonString);
List<String> jsonString = gson.fromJson(listJsonString, new TypeToken<List<String>>(){
}.getType());
System.out.println(jsonString);
/** * map <==> json */
Map<Integer, String> stringMap = new HashMap<>();
stringMap.put(1, "one");
stringMap.put(2, "two");
String mapJson = gson.toJson(stringMap);
Map<Integer, String> jsonD = gson.fromJson(mapJson, new TypeToken<Map<Integer, String>>(){
}.getType());
System.out.println(jsonD);
System.out.println(jsonD.get(1));
/** * List<object> <==> json */
Customer customer = customerMapper.selectByPrimaryKey(1);
List<Customer> customers = new ArrayList<>();
customers.add(customer);
customers.add(customer);
String customerListString = gson.toJson(customers);
System.out.println(customerListString);
List<Customer> deS = gson.fromJson(customerListString, List.class);
System.out.println(deS);
/** * object <==> json */
String customerJson = gson.toJson(customer);
JsonObject jsonObject = gson.fromJson(customerJson, JsonObject.class);
String crmId = jsonObject.get("crmId").getAsString();
System.out.println("crmId:" + crmId);
Customer customer1 = gson.fromJson(customerJson, Customer.class);
System.out.println(customer1);
fastjson
/** * List《==》fastjson */
List<String> stringList = Lists.newArrayList("a", "b", "c", "d");
String listJsonString = JSONObject.toJSONString(stringList);
System.out.println(stringList);
System.out.println(listJsonString);
List<String> jsonString = JSONObject.parseArray(listJsonString, String.class);
System.out.println(jsonString);
/** * map <==> json */
Map<Integer, String> stringMap = new HashMap<>();
stringMap.put(1, "one");
stringMap.put(2, "two");
String mapJson = JSONObject.toJSONString(stringMap);
Map<Integer, String> jsonD = (Map<Integer, String>) JSONObject.parse(mapJson);
System.out.println(jsonD);
System.out.println(jsonD.get(1));
/** * List<object> <==> json */
Customer customer = customerMapper.selectByPrimaryKey(1);
List<Customer> customers = new ArrayList<>();
customers.add(customer);
customers.add(customer);
String customerListString = JSONObject.toJSONString(customers);
System.out.println(customerListString);
List<Customer> deS = JSONObject.parseArray(customerListString, Customer.class);
System.out.println(deS);
/** * object <==> json */
String customerJson = JSONObject.toJSONString(customer);
JSONObject jsonObject = JSONObject.parseObject(customerJson);
String crmId = jsonObject.getString("crmId");
System.out.println("crmId:" + crmId);
Customer customer1 = JSONObject.parseObject(customerJson, Customer.class);
System.out.println(customer1);
choice
In terms of speed ,fastjson It's better than gson fast , At present, our service , It's not time to consider performance , So now from fastjson Switch to gson, Don't solve the performance problem for the time being .
边栏推荐
- 快速搭建Dobbo小Demo
- CTS test introduction (how to introduce interface test in interview)
- Gateway reports an error service_ UNAVAILABLE
- 用GaussDB(for Redis)存画像,推荐业务轻松降本60%
- Writing standard of physical quantities and unit symbols
- Idea regular expression replacement (idea regular search)
- 如何设计一个高并发系统?
- MySQL table operation
- PS制作加载GIF图片教程
- That day, I installed a database for my sister... Just help her sort out another shortcut
猜你喜欢

Why do China Construction and China Railway need this certificate? What is the reason?

The main function of component procurement system, digital procurement helps component enterprises develop rapidly

Matplotlib data visualization three minutes entry, half an hour enchanted?

Feiwo technology IPO meeting: annual revenue of 1.13 billion Hunan Cultural Tourism and Yuanli investment are shareholders

How to design a high concurrency system?

Vs2017 large factory ERP management system source code factory general ERP source code

Maya modeling exercise

Can the variable name be in Chinese? Directly fooled people

The concept and operation rules of calculus of variations

Cologne new energy IPO was terminated: the advanced manufacturing and Zhanxin fund to be raised is the shareholder
随机推荐
Reverse Integer
Resource not found: rgbd_launch 解决方案
基于浏览器的分屏阅读
实现一个家庭安防与环境监测系统(一)
Nuc980 set up SSH xshell connection
Go语言创始人从Google离职
[cartographer_ros] VIII: Official demo parameter configuration and effect
快速搭建Dobbo小Demo
优质数对的数目[位运算特点+抽象能力考察+分组快速统计]
Realsense-Ros安装配置介绍与问题解决
Apple failed to synchronize on its mobile terminal, so it exited the login. As a result, it could not log in again
gson与fastjson
Detailed explanation of how R language converts large Excel files into DTA format
轻松入门自然语言处理系列 12 隐马尔可夫模型
thymeleaf通过样式控制display是否显示
Typora无法打开提示安装新版本解决办法
opencv视频跟踪「建议收藏」
Feiwo technology IPO meeting: annual revenue of 1.13 billion Hunan Cultural Tourism and Yuanli investment are shareholders
Mysql表的操作
Application practice: Great integrator of the paddy classification model [paddlehub, finetune, prompt]