阿里巴巴开源平台发布fastjson 1.0.5版本

版本1.0.5,对Parser和Serializer提供了更多的可扩展性。在Parser方面,提供了新的扩展方式能够定制Deserializer,还支持TypeReference来提供类型信息。在Serializer方面,主要是提供新特性NameFilter和ValueFilter。

官方主页: http://code.alibabatech.com/wiki/display/FastJSON/Home

具体信息如下:
Bug
- PropertyFilter对Map类型数据不起作用

Improvement
- 序列化对JMX的CompositeData直接支持
- 序列化时对JMX的TablularData做特别支持!
- 序列化对异常类Throwable及派生类做特别支持

New Feature
-序列化输出时支持ValueFilter
- 序列化输出时支持NameFilter
- Parser提供扩展机制
- Parser支持TypeReference指定类型信息。

How to get it?
If you're Maven user, just use our maven repository(http://code.alibabatech.com/mvn/releases/) with folloging dependency
<dependency>
     <groupId>com.alibaba</groupId>
     <artifactId>fastjson</artifactId>
     <version>1.0.5</version>
</dependency>


Binary http://code.alibabatech.com/mvn/releases/com/alibaba/fastjson/1.0.5/fastjson-1.0.5.jar
Source http://code.alibabatech.com/mvn/releases/com/alibaba/fastjson/1.0.5/fastjson-1.0.5-sources.jar

Demo 1 (TypeReference)
public static class Bean {
    private int    id;
    private String name;
    public Bean(){
    }
    public Bean(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;}
}

/////////////////////

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;

String text;

{
    List list = new ArrayList();
    List mapList = new ArrayList();
    
    Map<String, Bean> map = new HashMap<String, Bean>();
    map.put("a", new Bean(123, "马加爵"));
    map.put("b", new Bean(234, "药家鑫"));
    map.put("c", new Bean(456, "刘+W"));
    
    mapList.add(map);
    
    list.add(mapList);

    text = JSON.toJSONString(list);
}

System.out.println(text);

// text = [[{"b":{"name":"药家鑫","id":234},"c":{"name":"刘+伟","id":456},"a":{"name":"马加爵","id":123}}]]
List<List<Map<String, Bean>>> list = JSON.parseObject(text, new TypeReference< List<List<Map<String, Bean>>> >() {}); // 注意这里
Map<String, Bean> map = list.get(0).get(0);

Assert.assertEquals(3, map.size());

Assert.assertEquals(123, ((Bean) map.get("a")).getId());
Assert.assertEquals(234, ((Bean) map.get("b")).getId());
Assert.assertEquals(456, ((Bean) map.get("c")).getId());

Assert.assertEquals("马加爵", ((Bean) map.get("a")).getName());
Assert.assertEquals("药家鑫", ((Bean) map.get("b")).getName());
Assert.assertEquals("刘+W", ((Bean) map.get("c")).getName());


Demo2 NameFilter
NameFilter filter = new NameFilter() {

    public String process(Object source, String name, Object value) {
        if (name.equals("id")) {
            return "ID";
        }

        return name;
    }

};

SerializeWriter out = new SerializeWriter();
JSONSerializer serializer = new JSONSerializer(out);
serializer.getNameFilters().add(filter);

Map<String, Object> map = new HashMap<String, Object>();
map.put("id", 0);
serializer.write(map);

String text = out.toString();
Assert.assertEquals("{\"ID\":0}", text);


Demo 3 ValueFilter
ValueFilter filter = new ValueFilter() {

    public Object process(Object source, String name, Object value) {
        if (name.equals("name")) {
            return null;
        }

        return value;
    }

};

SerializeWriter out = new SerializeWriter();
JSONSerializer serializer = new JSONSerializer(out);
serializer.getValueFilters().add(filter);

Map<String, Object> map = new HashMap<String, Object>();
map.put("name", "AA");
serializer.write(map);

String text = out.toString();
Assert.assertEquals("{}", text);

你可能感兴趣的:(maven,c,bean,json,阿里巴巴)