日期:2014-05-16  浏览次数:20386 次

jsonlib转换树型数据没其它框架好

jsonlib的性能请看这篇文章:http://wangym.iteye.com/blog/738933

性能总结 写道
测试总结:

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调试:



?

g