jsonlib转换树型数据没其它框架好
性能总结 写道
测试总结:
1、显而易见,无论是哪种形式的转换,Jackson > Gson > Json-lib。
Jackson的处理能力甚至高出Json-lib有10倍左右
2、JSON-lib似乎已经停止更新,最新的版本也是基于JDK15,而Jackson的社区则较为活跃;
3、在测试性能的同时,又以人肉方式对这三个类库转换的正确性 进行了检查 ,三者均达100%正确 ;
4、JSON-lib在转换诸如Date类型时较为累赘,如以下是两者的转换结果:
JSON-lib:
{"brithday":{"date":17,"day":2,"hours":9,"minutes":24,"month":7,"seconds":26,"time":1282008266398,"timezoneOffset":-480,"year":110}}
Jackson:
{"brithday":1282008123101}
5、JSON-lib依赖commons系列的包及 ezmorph包共 5个,而Jackson除自身的以外只依赖于commons-logging
6、Jackson提供完整基于节点的Tree Model,以及完整的OJM数据绑定功能。
1、显而易见,无论是哪种形式的转换,Jackson > Gson > Json-lib。
Jackson的处理能力甚至高出Json-lib有10倍左右
2、JSON-lib似乎已经停止更新,最新的版本也是基于JDK15,而Jackson的社区则较为活跃;
3、在测试性能的同时,又以人肉方式对这三个类库转换的正确性 进行了检查 ,三者均达100%正确 ;
4、JSON-lib在转换诸如Date类型时较为累赘,如以下是两者的转换结果:
JSON-lib:
{"brithday":{"date":17,"day":2,"hours":9,"minutes":24,"month":7,"seconds":26,"time":1282008266398,"timezoneOffset":-480,"year":110}}
Jackson:
{"brithday":1282008123101}
5、JSON-lib依赖commons系列的包及 ezmorph包共 5个,而Jackson除自身的以外只依赖于commons-logging
6、Jackson提供完整基于节点的Tree Model,以及完整的OJM数据绑定功能。
现在有个树型结构的json数据,使用json-lib转换成对象,转换调试发现得到的对象有“失真”,后面对比,jackjson,fastjson,gson,结果发现只有json-lib转换出现问题。
下面是调试对比:
json格式数据:
{ "attr": { "desc": "", "id": 0, "name": "一层", "parentId": -1 }, "data": { "icon": "", "title": "一层标题" }, "children": [ { "attr": { "desc": "", "id": 1, "name": "二层", "parentId": 0 }, "data": { "icon": "", "title": "二层标题" }, "children": [ { "attr": { "desc": "", "id": 2, "name": "三层", "parentId": 1 }, "data": { "icon": "", "title": "三层标题" }, "children": [ { "attr": { "desc": "", "id": 3, "name": "四层", "parentId": 2 }, "data": { "icon": "", "title": "四层标题" } } ] } ] } ] }
对应java对象如下:
public class NodeBean { private AttributeBean attr; private DataBean data; private List<NodeBean> children; } public class AttributeBean { private Long id; private String name; private Long parentId; private String desc; } public class DataBean { private String title; private String icon; }
测试代码:
//将树型json数据转换为对象 //jsonlib 2.4 转换 NodeBean jsonlibBean = (NodeBean) JSONObject.toBean(JSONObject.fromObject(responseStr), NodeBean.class); //jackson 2.1.0 转换 ObjectMapper mapper = new ObjectMapper(); try { NodeBean jacksonToBean = mapper.readValue(responseStr, NodeBean.class); } catch (Exception e) { e.printStackTrace(); } //fastjson 1.1.28 转换 NodeBean fastJsonToBean = JSON.parseObject(responseStr, NodeBean.class); //gson 2.2.2 转换树 Gson gson = new Gson(); NodeBean gsonToBean = gson.fromJson(responseStr, NodeBean.class);
json-lib调试:注意观察在children 默认会转成MorphDynaBean,MorphdynaClass,HashMap,MorpherRegistry
jackson调试:
fastjson调试:
gson调试:
效率测试结果json-lib性能最差,不推荐使用
相关推荐
yangkang 2020-11-09
lbyd0 2020-11-17
sushuanglei 2020-11-12
85477104 2020-11-17
KANSYOUKYOU 2020-11-16
wushengyong 2020-10-28
lizhengjava 2020-11-13
星月情缘 2020-11-13
huangxiaoyun00 2020-11-13
luyong0 2020-11-08
腾讯soso团队 2020-11-06
Apsaravod 2020-11-05
PeterChangyb 2020-11-05
gaobudong 2020-11-04
wwwjun 2020-11-02
gyunwh 2020-11-02
EchoYY 2020-10-31
dingyahui 2020-10-30