常見的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;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章