当前位置:网站首页>gson与fastjson
gson与fastjson
2022-07-25 14:26:00 【盖丽男】
背景
公司里对安全比较看重,然后最近fastjson漏洞出现的频繁了一点,所以要求,尽量不要使用fastjson,因此组长选了gson作为代替品。
使用
关于json,我在代码里,最常用的有两个地方,一个是做类型转换的时候用,另一个是偷懒,在调用第三方接口的时候接数据用。下面分别看一下gson和fastjson的使用
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);
选择
从速度上来说,fastjson是比gson快的,我们的服务目前来说,还没有到考虑性能的时候,所以目前从fastjson切换到gson,先暂时不解决性能问题。
边栏推荐
- DVWA practice - brute force cracking
- Huawei ENSP router static route (the next hop address of the default route)
- NAT/NAPT地址转换(内外网通信)技术详解【华为eNSP】
- Comprehensive sorting and summary of maskrcnn code structure process of target detection and segmentation
- bond0脚本
- GameFramework制作游戏(二)制作UI界面
- 元器件采购系统的主要功能,数字化采购助力元器件企业飞速发展
- 如何让一套代码完美适配各种屏幕?
- MySQL table operation
- Realize a family security and environmental monitoring system (II)
猜你喜欢

Initial flask and simple application

Realize a family security and environmental monitoring system (II)
知名手写笔记软件 招 CTO·坐标深圳

Digital Twins - cognition

From Anaconda to tensorflow to jupyter, step on the pit and fill it all the way

A small part is exposed on one or both sides of the swiper

51单片机学习笔记(1)

English语法_不定代词 - other / another

Products on Apple's official website can save 600 yuan by buying iPhone 13 Pro max at a discount

VS2017大型工厂ERP管理系统源码 工厂通用ERP源码
随机推荐
Introducing mlops interpretation (I)
swiper 一侧或两侧露出一小部分
Polymorphism and interface
结构体大小
Thymeleaf controls whether display is displayed through style
Summary of some problems about left value and right value [easy to understand]
C language and SQL Server database technology
Save the image with gaussdb (for redis), and the recommended business can easily reduce the cost by 60%
filters获取data中的数据;filters使用data中的数据
优质数对的数目[位运算特点+抽象能力考察+分组快速统计]
Huawei ENSP router static route (the next hop address of the default route)
Throwing OutOfMemoryError “Could not allocate JNI Env“
A small part is exposed on one or both sides of the swiper
物理量与单位符号的书写标准
Oka pass rights and interests analysis is the best choice to participate in okaleido ecological construction
NAT/NAPT地址转换(内外网通信)技术详解【华为eNSP】
CDA level Ⅰ 2021 new version simulation question 2 (with answers)
牛客多校 E G J L
Pytorch training code writing skills, dataloader, Einstein logo
~4.2 CCF 2021-12-1 sequence query