当前位置:网站首页>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 .
边栏推荐
- Gateway reports an error service_ UNAVAILABLE
- 安防市场进入万亿时代,安防B2B网上商城平台精准对接深化企业发展路径
- swiper 一侧或两侧露出一小部分
- Software testing -- 1. Outline of software testing knowledge
- Gateway 网关报错 SERVICE_UNAVAILABLE
- awk从入门到入土(20)awk解析命令行参数
- RuntimeError: CUDA out of memory(已解决)[通俗易懂]
- Maya modeling exercise
- Idea settings ignore file configuration when submitting SVN
- awk从入门到入土(24)提取指令网卡的ip
猜你喜欢

如何设计一个高并发系统?

用GaussDB(for Redis)存画像,推荐业务轻松降本60%

Cologne new energy IPO was terminated: the advanced manufacturing and Zhanxin fund to be raised is the shareholder

变分(Calculus of variations)的概念及运算规则

【MySQL必知必会】触发器 | 权限管理

关于ROS2安装connext RMW的进度条卡在13%问题的解决办法

Initial flask and simple application

基于redis的keys、scan删除ttl为-1的key

阿里云安装MYSQL5.7

Matplotlib data visualization three minutes entry, half an hour enchanted?
随机推荐
Mysql表的操作
Software testing -- 1. Outline of software testing knowledge
Gameframework making games (I)
51单片机学习笔记(1)
thymeleaf设置disabled
轻松入门自然语言处理系列 12 隐马尔可夫模型
Easy entry natural language processing series 12 hidden Markov models
thymeleaf通过样式控制display是否显示
idea正则表达式替换(idea正则搜索)
Mongodb source code deployment and configuration
Thymeleaf controls whether display is displayed through style
GameFramework制作游戏(一)
51单片机学习笔记(2)
English grammar_ Indefinite pronoun - other / other
QObject source code analysis -d pointer and Q pointer
阿里云安装MYSQL5.7
国联证券买股票开户安全吗?
Initial flask and simple application
【MySQL必知必会】触发器 | 权限管理
SSM framework integration, simple case