致梦想

jackson替换fastjson

最近fastjson频繁爆出漏洞,项目组决定将项目中的fastjson全部替换成jackson,该篇博客记录替换的过程:

可以定义一个JacksonUtil,里面包含几个函数:

1.序列化函数

原先:fastjson-JSON.toJSONString

替换:jackson-JacksonUtil.beanToJsonStr(Object object)

主要实现:ObjectMapper().writeValueAsString(object)

2.反序列化

原先:fastjson-JSONObject.parseObject

替换:jackson-JacksonUtil.jsonStrToBean(String str, Class<T> targetClass)

主要实现:ObjectMapper().readValue(str, targetClass)

3.属性定义和序列化和反序列化顺序

原先:fastjson-@JSONFiled(name = "xxx", ordinal = 2)

替换:jackson-@JsonProperty(value = "xxx", index = 2)

4.JSONObject

可以替换为Map<String, Object>

5.JSONArray

可以替换为List<Object>

6.map<String, Long>类型序列化和反序列化

fastjson可以利用TypeReference 复杂类型转换,比如

JSON.parseObject(getNotifyRule(), new TypeReference<Map<String, Long>>()

但是用Jackson,JackUtil.jsonStrToBean(getNotifyRule(), Map.class);转换的时候,不能直接解析为Long,而是Integer

参考链接:

https://blog.csdn.net/zzti_erlie/article/details/79779253

https://www.cnblogs.com/larva-zhh/p/11544317.html#@jsonfield

https://baijiahao.baidu.com/s?id=1668639549525572630&wfr=spider&for=pc

https://www.cnblogs.com/jpfss/p/9056485.html

https://blog.csdn.net/GarfieldEr007/article/details/83829722

posted on 2020-06-16 16:22  致梦想  阅读(4374)  评论(1编辑  收藏  举报

导航