当前位置:网站首页>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,先暂时不解决性能问题。
边栏推荐
- 新唐NUC980设置DHCP或者静态IP
- NAT/NAPT地址转换(内外网通信)技术详解【华为eNSP】
- Detailed explanation of Telnet remote login AAA mode [Huawei ENSP]
- 物理量与单位符号的书写标准
- PT100 temperature measurement circuit diagram (AD590 typical temperature measurement circuit)
- Initial flask and simple application
- Okaleido ecological core equity Oka, all in fusion mining mode
- Deep understanding of pytorch distributed parallel processing tool DDP -- starting from bugs in engineering practice
- From fish eye to look around to multi task King bombing -- a review of Valeo's classic articles on visual depth estimation (from fisheyedistancenet to omnidet) (Part I)
- MySQL table operation
猜你喜欢

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

GameFramework制作游戏(二)制作UI界面
![优质数对的数目[位运算特点+抽象能力考察+分组快速统计]](/img/c9/8f8f0934111f7ae8f8abd656d92f12.png)
优质数对的数目[位运算特点+抽象能力考察+分组快速统计]

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

SSM framework integration, simple case

GameFramework制作游戏(一)

~5 new solution of CCF 2021-12-2 sequence query

VS2017大型工厂ERP管理系统源码 工厂通用ERP源码

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

Mongodb源码部署以及配置
随机推荐
The practice of depth estimation self-monitoring model monodepth2 in its own data set -- single card / multi card training, reasoning, onnx transformation and quantitative index evaluation
Deep understanding of pytorch distributed parallel processing tool DDP -- starting from bugs in engineering practice
swiper 一侧或两侧露出一小部分
Maya modeling exercise
Okaleido ecological core equity Oka, all in fusion mining mode
物理量与单位符号的书写标准
IDEA设置提交SVN时忽略文件配置
基于浏览器的分屏阅读
为什么中建、中铁都需要这本证书?究竟是什么原因?
如何让一套代码完美适配各种屏幕?
Xintang nuc980 set DHCP or static IP
Can the variable name be in Chinese? Directly fooled people
From Anaconda to tensorflow to jupyter, step on the pit and fill it all the way
Educational Codeforces Round 132 (Rated for Div. 2) C,D+AC自动机
BIO、NIO、AIO示例
Thymeleaf controls whether display is displayed through style
Melodic + Realsense D435i 配置及错误问题解决
Matplotlib data visualization three minutes entry, half an hour enchanted?
Idea regular expression replacement (idea regular search)
C language and SQL Server database technology