gson与fastjson

简介: gson与fastjson

背景


公司里对安全比较看重,然后最近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,先暂时不解决性能问题。

目录
相关文章
|
JSON fastjson Java
FastJson、JackJson 以及 Gson 的区别
FastJson、JackJson 以及 Gson 是 Java 生态圈中三种常用的 Json 解析器,它们均可将 Java 对象序列化为 Json 格式的字符串,也可将 Json 字符串反序列化为 Java 对象。下面我们讨论一下三者在序列化和反序列化操作中的一些区别。
850 0
|
16天前
|
JSON fastjson Java
Gson与FastJson详解
综上,Gson和FastJson都是用于Java对象和JSON数据互相转换的优秀库,选择哪个取决于性能、功能需求和个人偏好。 买CN2云服务器,免备案服务器,高防服务器,就选蓝易云。百度搜索:蓝易云
26 2
|
6月前
|
JSON 数据格式
gson坑
gson坑
33 0
|
存储 缓存 JSON
fastjson2为什么这么快
fastjson2 提升速度的核心技术
75665 6
fastjson2为什么这么快
|
JSON fastjson Java
FastJson使用技巧
FastJson使用技巧
|
fastjson Java
fastjson的使用
fastjson的使用
112 0
|
JSON fastjson Java
scala使用Gson和FastJson解析JSON
kafka传过来的数据好多都是JSON格式,需要对其解析,抽取出应用需要的数据。Gson和FastJson是两个不错的解析工具,以后可能经常会使用到,记录一下,防止以后遗忘。
818 0
|
JSON fastjson Java
Fastjson 使用
fastjson 是阿里巴巴的开源 JSON 解析库,它可以解析 JSON 格式的字符串,支持将 Java Bean序列化为JSON字符串,也可以从 JSON 字符串反序列化到 JavaBean。 功能完备: 支持泛型,支持流处理超大文本,支持枚举,支持序列化和反序列化扩展。 下载 jar包 或者配置 maven 依赖:
2162 0
|
XML JSON 缓存
关于 FastJson
因为公司提供的基础框架使用的是 FastJson 框架、而部门的架构师推荐使用 Jackson。所以特此了解下 FastJson 相关的东西。
652 0
|
JSON 数据格式
GSON - 基础篇
GSON - 基础篇
169 0