Java JSON技术框架选型、测试及简单使用
1.多处理模式,且可很好协作
从基本开始,有多种方法来使用和生产JSON数据,尽管多数JSON包仅提供单一的处理方式,但却有三种互补的JSON处理方式(详细解释见:There are Tree ways...):
尽管多角度的好处显而易见,且各自提供最佳的用例,很少有(如果有的话?)其他的Java JSON包提供这些规范的处理模式。
大多数只提供一个模式(org.json以树组织数据; Gson实现了数据绑定)。 Jackson提供所有模式,所有模式完全支持,且最棒的是,它很容易在两种模式之间转换,混合和适配。 例如,处理非常大的JSON流时,通常始于流解析,而使用数据绑定器将子数据段绑定到Java对象:这允许处理巨大的文件而没有过多的内存占用,但却提供完整便利的数据绑定能力。
2.?可使用任何构造及工厂方法(不只是默的零参方法)
大多数数据(对JSON和XML)绑定工具需要一个定义和无参数的构造函数,实例化Java对象,并通过setter设置属性或直接访问字段。 不幸的是它使人们难以利用用“不可变对象”模式,也不同于正常代码中的访问模式。
Jackson thinks that developers deserve ability to specify whatever constructor or factory methods they want for instantiation; just annotate thing you want like so:Jacson认为,开发者应该能够指定他们想为实例化的任何工厂或构造方法,只要你喜欢,注释即可:
参考(http://hi.baidu.com/suofang/blog/item/c4f3a351d83a9faf8d543069.html)
3.设置循环策略?:解决json最头疼的问题死循环????
能够干净地处理循环类型的某些子集,成为父/子链接。 这些都是互相紧密耦合的引用,其中两个对象以层次结构的方式交叉引用,如父/子树节点的关联,或更常见的,ORM采用的表间连接(join)的描述。
对引用问题(或更普遍的,循环引用),JSON没有处理它们的自然方法。,不像Java对象,没有标识信息可用。
??常用的解决方法是只标记一个被忽略的引用(Jackson可通过使用@ JsonIgnore注解实现),但其缺点是反序列化时会丢失实际的耦合目标。
Jackson?有简单的基于注解的解决该问题的方案:两个引用需要一个注解(对“子”连接作@JsonManagedReference?注解,对“父”或“返回”连接作@JsonBackReference注解),并在此基础,Jackson知道要省略反向引用的序列化,但反序列化对象时要恢复它。此方式适用于典型的ORM用例。
1.父子关系引用
直接输出肯定是报循环错误,Jackson 提供了两个注解
- ??????????.fromObjectToJson(departmentList);
- ??????System.out.println(json);
- ???? List<Department> list = Jacksons.json().fromJsonToObject(json, List.class);
- ????System.out.println(list);
- }复制代码
?
测试结果:
[{"version":null,"orgId":null,"orgPath":null,"depId":10000,"depName":"技术部","depDesc":null,"depLevel":2,"parentId":1,"path":"0.1.10000.","orgType":1,"creatorId":1,"createtime":"2012-07-18 10:37:56","updateId":1,"updatetime":"2012-07-18","sn":null,"chargeIds":"10020","chargeNames":"超级管理员","demension":{"version":null,"orgId":null,"orgPath":null,"demId":1,"demName":"行政维度","demDesc":"行政维度","demType":1,"organizations":[]}},{"version":null,"orgId":null,"orgPath":null,"depId":10002,"depName":"技术2部","depDesc":"技术2部","depLevel":3,"parentId":10000,"path":"0.1.10000.10002.","orgType":2,"creatorId":1,"createtime":"2012-07-18 10:38:47","updateId":1,"updatetime":"2012-07-18","sn":null,"chargeIds":"","chargeNames":"","demension":{"version":null,"orgId":null,"orgPath":null,"demId":1,"demName":"行政维度","demDesc":"行政维度","demType":1,"organizations":[]}},{"version":null,"orgId":null,"orgPath":null,"depId":1,"depName":"宏天软件","depDesc":"宏天软件","depLevel":1,"parentId":0,"path":"0.1.","orgType":1,"creatorId":1,"createtime":"2011-08-29 00:00:00","updateId":1,"updatetime":"2011-08-29","sn":null,"chargeIds":"","chargeNames":"","demension":{"version":null,"orgId":null,"orgPath":null,"demId":1,"demName":"行政维度","demDesc":"行政维度","demType":1,"organizations":[]}},{"version":null,"orgId":null,"orgPath":null,"depId":10001,"depName":"技术1部","depDesc":"技术1部","depLevel":3,"parentId":10000,"path":"0.1.10000.10001.","orgType":2,"creatorId":1,"createtime":"2012-07-18 10:38:15","updateId":1,"updatetime":"2012-07-18","sn":null,"chargeIds":"","chargeNames":"","demension":{"version":null,"orgId":null,"orgPath":null,"demId":1,"demName":"行政维度","demDesc":"行政维度","demType":1,"organizations":[]}}]?
???? [{version=null, orgId=null, orgPath=null, depId=10000, depName=技术部, depDesc=null, depLevel=2, parentId=1, path=0.1.10000., orgType=1, creatorId=1, createtime=2012-07-18 10:37:56, updateId=1, updatetime=2012-07-18, sn=null, chargeIds=10020, chargeNames=超级管理员, demension={version=null, orgId=null, orgPath=null, demId=1, demName=行政维度, demDesc=行政维度, demType=1, organizations=[]}}, {version=null, orgId=null, orgPath=null, depId=10002, depName=技术2部, depDesc=技术2部, depLevel=3, parentId=10000, path=0.1.10000.10002., orgType=2, creatorId=1, createtime=2012-07-18 10:38:47, updateId=1, updatetime=2012-07-18, sn=null, chargeIds=, chargeNames=, demension={version=null, orgId=null, orgPath=null, demId=1, demName=行政维度, demDesc=行政维度, demType=1, organizations=[]}}, {version=null, orgId=null, orgPath=null, depId=1, depName=宏天软件, depDesc=宏天软件, depLevel=1, parentId=0, path=0.1., orgType=1, creatorId=1, createtime=2011-08-29 00:00:00, updateId=1, updatetime=2011-08-29, sn=null, chargeIds=, chargeNames=, demension={version=null, orgId=null, orgPath=null, demId=1, demName=行政维度, demDesc=行政维度, demType=1, organizations=[]}}, {version=null, orgId=null, orgPath=null, depId=10001, depName=技术1部, depDesc=技术1部, depLevel=3, parentId=10000, path=0.1.10000.10001., orgType=2, creatorId=1, createtime=2012-07-18 10:38:15, updateId=1, updatetime=2012-07-18, sn=null, chargeIds=, chargeNames=, demension={version=null, orgId=null, orgPath=null, demId=1, demName=行政维度, demDesc=行政维度, demType=1, organizations=[]}}]
?