---
title: "欢迎帮忙测试fastjson 1.1.3版本"
author: "wenshao"
date: "2011-08-13T18:08:46Z"
tags: ["Java综合"]
categories: ["开源软件"]
slug: "fastjson-1-1-3"
draft: false
lastmod: "2011-08-13T18:08:46Z"
id: 1148136
url: "https://wenshao.iteye.com/blog/1148136"
---

下载地址： http://code.alibabatech.com/mvn/snapshots/com/alibaba/fastjson/1.1.3-SNAPSHOT/ 这个版本修复了volking反映了List类型序列化时的bug。\[url]http://code.alibabatech.com/jira/browse/FASTJSON-70\[/url] 这个版本引入了一些很酷的特性： \[b]1、支持循环引用\[/b] 很多用户在初次使用fastjson时都遇到了循环引用的问题，这是反映最多的问题，我越来越认识到支持循环引用的必要性。经过测试，加入循环应用的支持对性能的影响很小，小于5％。我决定在1.1.3加入循环应用支持，而且是缺省打开这个特性。 \[code="java"]public static class Department { private int id; private String name; private Department parent; private Department root; private Collection children = new ArrayList(); public Department(){ } public Department getRoot() { return root; } public void setRoot(Department root) { this.root = root; } public Department(int id, String name){ this.id = id; this.name = name; } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Department getParent() { return parent; } public void setParent(Department parent) { this.parent = parent; } public Collection getChildren() { return children; } public void setChildren(Collection children) { this.children = children; } }\[/code] \[code="java"] Department tech = new Department(1, "技术部"); tech.setRoot(tech); { Department pt = new Department(2, "平台技术部"); pt.setParent(tech); pt.setRoot(tech); tech.getChildren().add(pt); { Department sysbase = new Department(3, "系统基础"); sysbase.setParent(pt); sysbase.setRoot(tech); pt.getChildren().add(sysbase); } } { Department cn = new Department(4, "中文站技术部"); cn.setParent(tech); cn.setRoot(tech); tech.getChildren().add(cn); } { //JSON.toJSONString(tech); } { String prettyText = JSON.toJSONString(tech; System.out.println(prettyText); String text = JSON.toJSONString(tech); Department dept = JSON.parseObject(text, Department.class); Assert.assertTrue(dept == dept.getRoot()); } \[/code] 输出的内容： \[code="java"] { "children":\[ { "children":\[{ "children":\[], "id":3, "name":"系统基础", "parent":{"$ref":".."}, "root":{"$ref":"$"} }], "id":2, "name":"平台技术部", "parent":{"$ref":".."}, "root":{"$ref":".."} }, { "children":\[], "id":4, "name":"中文站技术部", "parent":{"$ref":".."}, "root":{"$ref":".."} } ], "id":1, "name":"技术部", "root":{"$ref":"@"} } { "children":\[ { "children":\[{ "children":\[], "id":3, "name":"系统基础", "parent":{"$ref":".."}, // 上一级引用 "root":{"$ref":"$"} // 跟对象引用 }], "id":2, "name":"平台技术部", "parent":{"$ref":".."}, // 上一级引用 "root":{"$ref":".."} // 上一级引用 }, { "children":\[], "id":4, "name":"中文站技术部", "parent":{"$ref":".."}, // 上一级引用 "root":{"$ref":".."} // 上一级引用 } ], "id":1, "name":"技术部", "root":{"$ref":"@"} // 引用自身 } \[/code] 2、支持非缺省构造函数和静态工厂方法 基于编码习惯或者安全的原因，一些JavaBean没有缺省构造函数。fastjson 1.13开始支持这个特性。 例如： \[code="java"]public static class Entity { private final int id; private final String name; @JSONCreator public Entity(@JSONField(name = "id") int id, @JSONField(name = "name") String name){ this.id = id; this.name = name; } public int getId() { return id; } public String getName() { return name; } }\[/code] \[code="java"]public static class Entity { private final int id; private final String name; // 静态工厂方法 @JSONCreator public static Entity create(@JSONField(name = "id") int id, @JSONField(name = "name") String name) { return new Entity(id, name); } private Entity(int id, String name){ this.id = id; this.name = name; } public int getId() { return id; } public String getName() { return name; } } \[/code] 3、原型 希望以接口的方式来操作json数据，这个特性是借鉴了jackson的功能。 \[code="java"]import junit.framework.Assert; import junit.framework.TestCase; import com.alibaba.fastjson.JSON; public class MaterializedInterfaceTest extends TestCase { public static interface Bean { int getId(); void setId(int value); String getName(); void setName(String value); } public void test\_parse() throws Exception { String text = "{\\"id\\":123, \\"name\\":\\"chris\\"}"; Bean bean = JSON.parseObject(text, Bean.class); // 按接口调用 Assert.assertEquals(123, bean.getId()); Assert.assertEquals("chris", bean.getName()); bean.setId(234); Assert.assertEquals(234, bean.getId()); } }\[/code] 总结 1.1.3提供的三个新特性，进一步完善了fastjson的功能，这也是一个重要的版本。希望大家帮忙测试，一起打造最好的json processor！