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