常见的gson的使用方法与fastJson的对比

fastJson 是我们很常用Json转化使用的依赖,但是fastJson经常报漏洞。在github也有很问题,并没有得到及时的解决。可能需要fastJson切换成其他json的工具。这里将fastJson和gson常用一下方法做一下对比。让只熟悉fastJson更容易切换Gson.

这里采用fastJson 2.6.2 和gson 2.2.4 做对比进行切换

 <!-- https://mvnrepository.com/artifact/com.alibaba/fastjson -->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>1.2.62</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/com.google.code.gson/gson -->
        <dependency>
            <groupId>com.google.code.gson</groupId>
            <artifactId>gson</artifactId>
            <version>2.2.4</version>
        </dependency>

测试依赖:

  //gson 转化为对应
    @Test
    public void testConvertSpecialObject(){
        TimeVO timeVO = new TimeVO();
        timeVO.setStartTime("2020-06-25");
        timeVO.setEndTime("2020-06-26");
        timeVO.setTimestamp(1224343L);

        List<String> list = new ArrayList<>();
        list.add("123");
        list.add("456");
        timeVO.setList(list);

        //1、将Object转化json对象
        String fastJsonStr = JSON.toJSONString(timeVO);
        Gson gson = new Gson();
        String gsonStr = gson.toJson(timeVO);

        System.out.println("fastJsonStr is :"+fastJsonStr+" , gsonStr is :"+gsonStr);



        //2、将json字符串转化为对应对象
        TimeVO fastTimeVO = JSON.parseObject(fastJsonStr, new TypeReference<TimeVO>() {
        });
        TimeVO gsonTimeVO = gson.fromJson(gsonStr, TimeVO.class);
        //或者
        TimeVO gsonTimeVO2 = gson.fromJson(gsonStr, new TypeToken<TimeVO>(){}.getType());

        System.out.println("fastTimeVO is " + fastTimeVO + ", gsonVO is " + gsonTimeVO);





        //3、将字符串JsonObject 对象,在获取对应属性
      
        JSONObject fastJsonObject = JSON.parseObject(fastJsonStr);
        String fastStartTime = fastJsonObject.getString("startTime");
        Long fastTimeStamp = fastJsonObject.getLong("timeStamp");
        String listStr = fastJsonObject.getString("list");
        List<String> fastList1 = JSONObject.parseArray(listStr, String.class);
        //或者
        List<String> fastList2 = JSONObject.parseArray(fastJsonObject.getJSONArray("list").toJSONString(), String.class);


        JsonParser jsonParser = new JsonParser();
        JsonObject jsonObject = jsonParser.parse(gsonStr).getAsJsonObject();
        String gsonStartTime  = jsonObject.get("startTime").getAsString();
        //没有Long的包装类型,默认为0
        long gsonTimeStamp = jsonObject.get("timestamp").getAsLong();
        //这里需要注意 fastJson 可以直接获取成string ,在转化list
        //gson必须直接获取jsonArray
        JsonArray list1 = jsonObject.get("list").getAsJsonArray();
        List gsonList = gson.fromJson(list1, new TypeToken<List<String>>() {
        }.getType());




        //4、借助jsonObject 放置参数
        JSONObject jsonObject1 = new JSONObject();
        jsonObject1.put("key1", "value1");
        jsonObject1.put("key2", 10L);
        String fastJson = jsonObject1.toJSONString();
        System.out.println(fastJson);

        HashMap<String, Object> tempMap = new HashMap<>();
        tempMap.put("key1", "value1");
        tempMap.put("key2", 10L);
        String mapJson = gson.toJson(tempMap);
        System.out.println(mapJson);
    }
}

在这里插入图片描述
附上TimeVO类:

@Data
public class TimeVO {
    private String startTime;
    private String endTime;
    private long timestamp;
    private List<String> list;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章