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,先暂时不解决性能问题。

目录
相关文章
|
计算机视觉 Python
最快速度写出一个识别效果——OpenCV模板匹配(含代码)
最快速度写出一个识别效果——OpenCV模板匹配(含代码)
1084 0
|
测试技术 数据库
深入探索MyBatis-Plus中Service接口的lambdaUpdate用法及示例
深入探索MyBatis-Plus中Service接口的lambdaUpdate用法及示例
2076 0
|
编解码 开发工具 开发者
Flutter 中的 WidgetInspector 小部件:全面指南
但它主要用于调试目的,在生产环境中应该谨慎使用。
175 2
|
机器学习/深度学习 人工智能 自然语言处理
“魔搭”来了!一文深度解读达摩院推出的AI模型社区
一文详解ModelScope魔搭社区,模型即服务开启AI开发使用新范式
“魔搭”来了!一文深度解读达摩院推出的AI模型社区
|
机器学习/深度学习 人工智能 物联网
快速玩转 Llama2!机器学习 PAI 最佳实践(三)—快速部署WebUI
本实践将采用阿里云机器学习平台PAI-EAS 模块针对 Llama-2-13B-chat 进行部署。PAI-EAS是模型在线服务平台,支持将模型一键部署为在线推理服务或AI-Web应用,具备弹性扩缩的特点,适合需求高性价比模型服务的开发者。
1946 4
|
JSON Java API
Gson-更新中
Gson-更新中
329 0
|
存储 自然语言处理 物联网
|
存储 弹性计算 缓存
开源FaaS平台(四):Fission
Kubernetes中文社区对Fission的描述是:Fission是一款基于Kubernetes的FaaS框架。通过Fission可以轻而易举地将函数发布成HTTP服务。它通过读取用户的源代码,抽象出容器镜像并执行。同时它帮助开发者们减轻了Kubernetes的学习负担,开发者无需了解太多Kubernetes也可以搭建出实用的服务。Fission可以与HTTP路由、Kubernetes Events和其他的事件触发器结合,所有这些函数都只有在运行的时候才会消耗CPU和内存。
1421 1
|
存储 负载均衡 固态存储
服务器硬件RAID性能横评(4)
服务器硬件RAID性能横评(4)
服务器硬件RAID性能横评(4)