fastjson和jackson序列化数据的区别

发布于:2021-10-29 01:13:11



fastjson和jackson序列化数据的区别
直奔主题一言不合就上代码注意



直奔主题

1、fastjson将字符串反序列化为对象时,只会处理第一层,内部会序列化为JsonObject或者JsonArray,使用二级结构和三级结构时还要再次处理, 优点就是快,特别快。
2、jackson会将深层字符串一起序列化


一言不合就上代码

public class Demo {

public static void main(String [] args){

String orderInfo = "{
" +
""orderId":"100",
" +
""orderCode":"O3138141",
" +
""orderGoodsInfo":[
" +
"{
" +
""orderGoodsId":"4000",
" +
""orderGoodsName":"鞋子"
" +
"},{
" +
""orderGoodsId":"5000",
" +
""orderGoodsName":"袜子"
" +
"}
" +
"],
" +
""address":{
" +
""provCode":"1907",
" +
""provName":"江苏省",
" +
""provEnName":"jiangsusheng",
" +
""provEnHeader":"j",
" +
""detail":"江苏省南京市软件大道"
" +
"}
" +
"}";
System.out.println(orderInfo);

System.out.println("


======================== fastjson ===========================");
LinkedHashMap fastjson = JSON.parseObject(orderInfo, LinkedHashMap.class);
fastjson.forEach((key, value) -> {
String valueType = value.getClass().getName();
System.out.println(key + " ===== " + value + " ====== " + valueType);
});

System.out.println("


======================== jackson ===========================");
LinkedHashMap jackson = JsonTools.defaultMapper().fromJson(orderInfo, LinkedHashMap.class);
jackson.forEach((key, value) -> {
String valueType = value.getClass().getName();
System.out.println(key + " ===== " + value + " ====== " + valueType);
});

}
}

运行结果:
看到以上的序列化结果的区别了吧


注意

如果不知名反序列化的数据结构, fastjson会按照HashMap处理, 而jackson 按照 LinkedHashMap 处理

相关推荐

最新更新

猜你喜欢