温绍锦(温高铁) http ://weibo/wengaotie

28
温温温 温温温温 () http ://weibo.com/wengaotie

Upload: mairwen-aderyn

Post on 30-Dec-2015

172 views

Category:

Documents


0 download

DESCRIPTION

温绍锦(温高铁) http ://weibo.com/wengaotie. How to get?. http://repo1.maven.org/maven2/com/alibaba/fastjson / Maven2 < groupId > com.alibaba < atifactId > fastjson 1.1.xxx Sourcecode https://github.com/alibaba/fastjson. - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: 温绍锦(温高铁) http ://weibo/wengaotie

温绍锦(温高铁)http://weibo.com/wengaotie

Page 2: 温绍锦(温高铁) http ://weibo/wengaotie

How to get?

• http://repo1.maven.org/maven2/com/alibaba/fastjson/

• Maven2<dependency>

<groupId>com.alibaba</groupId><atifactId>fastjson</artifactId><version>1.1.xxx</version>

</dependency>

Sourcecodehttps://github.com/alibaba/fastjson

Page 3: 温绍锦(温高铁) http ://weibo/wengaotie

特点• 最快• API 最简洁• 功能强大• 扩展性最好• 稳定

Page 4: 温绍锦(温高铁) http ://weibo/wengaotie

一直最快• 第三方测试表明, fastjson 是 Java 语言中最快的 JSON 库。• 两年来从未被超越。

序列化耗时 反序列化耗时 总耗时 大小kryo 588 814 1403 214

protobuf 1103 684 1787 238

fastjson 1201 1216 2417 486

jackson 1842 2421 4262 485

hessian 3812 6708 10519 501

bson/jackson 5645 6895 12541 506

gson 7421 5065 12485 486

java-built-in 5608 29649 35257 889

json-lib 27555 87292 114848 485

https://github.com/eishay/jvm-serializers/wiki

Page 5: 温绍锦(温高铁) http ://weibo/wengaotie

{“id”:123,”name”:”gaotie”}

假定有序, Key 不用读取,只做匹配

public class VO { public int id; public String name;}

类型预判,优化读取

内置裁剪版本的 ASM 实现算法

Page 6: 温绍锦(温高铁) http ://weibo/wengaotie

还能更快么?

能!

Page 7: 温绍锦(温高铁) http ://weibo/wengaotie

Fastjson 可以做什么?• Web 框架处理 JSON 参数返回 JSON 结果• Cache 缓存对象• 远程方法调用 RPC• Android/ 阿里云手机处理 JSON• MessageQueue 传输对象• 配置文件代替 XML• 保存数据到磁盘、数据库、 Hbase

Page 8: 温绍锦(温高铁) http ://weibo/wengaotie

功能完备• 支持序列化和反序列化• 支持循环引用• 支持泛型• 能够定制序列化,可以过滤和修改• 支持代理对象, cglib 和 javassist• 自动识别各种日期格式• 支持 GetOnly 的 List/Map 反序列化• Stream API 支持超大对象和 JSON 文本

Page 9: 温绍锦(温高铁) http ://weibo/wengaotie

API 简洁• 当前 Java JSON 库中最简洁的 API

public abstract class JSON {static <T> T parseObject(String text, Class<T> clazz) ; // JSON 文本 -> JavaBeanstatic String toJSONString(Object object) ; // JavaBean -> JSON 文本

static Object parse(String text) ; // JSON 文本 -> JSON 对象static Object toJSON(Object javaObject) ; // Java 对象 -> JSON 对象static <T> T toJavaObject(JSON json, Class<T> clazz) ; // JSON 对象 -> Java 对象

JSON 文本 JSON 对象 Java 对象

Page 10: 温绍锦(温高铁) http ://weibo/wengaotie

序列化

User user = new User();user.setId(123);user.setName("wenshao");String text = JSON.toJSONString(user);System.out.println(text);

{"id":123,"name":"wenshao"}

Page 11: 温绍锦(温高铁) http ://weibo/wengaotie

反序列化

String text = "{id:123,name:'wenshao'}";User o = JSON.parseObject(text, User.class);

String text = "[{'id':123,'name':'wenshao'}]";List<User> users = JSON.parseObject(text, new TypeReference<List<User>>() {});泛型支持

Page 12: 温绍锦(温高铁) http ://weibo/wengaotie

循环引用

Object[] array = new Object[1];array[0] = array;

String text = JSON.toJSONString(array);Assert.assertEquals("[{\"$ref\":\"@\"}]", text);

Object[] a2 = JSON.parseObject(text, Object[].class);Assert.assertSame(a2, a2[0]);

Page 13: 温绍锦(温高铁) http ://weibo/wengaotie

支持双引号、单引号、无引号

{“id”:123,”name”:”wenshao”}

{’id’:123, ’name’:’wenshao’}

{id:123,name:”wenshao”}

标准,针对性优化,在 fastjson 中性能最好

兼容

兼容

Page 14: 温绍锦(温高铁) http ://weibo/wengaotie

public interface NameFilter extends SerializeFilter { String process(Object source, String name, Object value);}

public interface PropertyFilter extends SerializeFilter { boolean apply(Object source, String name, Object value);}

public interface PropertyPreFilter extends SerializeFilter { boolean apply(JSONSerializer serializer, Object source, String name);}

public interface ValueFilter extends SerializeFilter { Object process(Object source, String name, Object value);}

修改 Name

修改 Value

根据 Name 和 Value 判断是否序列化

根据 Name 判断是否序列化

Page 15: 温绍锦(温高铁) http ://weibo/wengaotie

序列化时修改 Key

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

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

String text = JSON.toJSONString(map, filter);Assert.assertEquals("{\"ID\":0}", text);

按需要修改 Key

Page 16: 温绍锦(温高铁) http ://weibo/wengaotie

序列化时修改 Value

ValueFilter filter = new ValueFilter() { public Object process(Object source, String name, Object value) { if (name.equals("id")) { return 123; } return value; }};

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

String text = JSON.toJSONString(map, filter);Assert.assertEquals("{\"id\":123}", text);

按需要修改 Value

Page 17: 温绍锦(温高铁) http ://weibo/wengaotie

根据 Key 和 Value 判断是否序列化

class VO { public int id; public String name;}

PropertyFilter filter = new PropertyFilter() {

public boolean apply(Object source, String name, Object value) { return "id".equals(name); }};

VO vo = new VO();vo.id = 123;vo.name = "gaotie";

String text = JSON.toJSONString(vo, filter);Assert.assertEquals("{\"id\":123}", text);

按需要修改 Value

Page 18: 温绍锦(温高铁) http ://weibo/wengaotie

按名称过滤class VO { public int getId() { throw new RuntimeException(); }}

PropertyPreFilter filter = new PropertyPreFilter () { boolean apply(JSONSerializer serializer, Object source, String name) { return false; }};

VO vo = new VO();

String text = JSON.toJSONString(vo, filter);Assert.assertEquals("{}", text);

只按 Key 过滤,不会调用 get 方法

Page 19: 温绍锦(温高铁) http ://weibo/wengaotie

public abstract class BeforeFilter implements SerializeFilter { protected final void writeKeyValue(String key, Object value) { // ... ... }

public abstract void writeBefore(Object object);}

序列化定制添加内容

BeforeFilter filter = new BeforeFilter() { @Override public void writeBefore(Object object) { this.writeKeyValue("id", 123); this.writeKeyValue("name", "wenshao"); }};

String text = JSON.toJSONString(new Object(), filter);Assert.assertEquals("{\"id\":123,\"name\":\"wenshao\"}", text);

按需要添加内容

Page 20: 温绍锦(温高铁) http ://weibo/wengaotie

通过 Annotation 定制public static class User { @JSONField(name="ID") private int id;

public int getId() { return id; } public void setId(int id) { this.id = id; }}

public static class User { private int id;

@JSONField(name="ID") public int getId() { return id; } public void setId(int id) { this.id = id; }}

配置在 Field 上

配置在 Getter/Setter 上

还可以配置 interface 的 Getter/Setter 上

Page 21: 温绍锦(温高铁) http ://weibo/wengaotie

支持 Proxy 对象• 支持常见的 Proxy– java.reflect.Proxy– cglib Proxy– javaassist Proxy

• hibernate 对象序列化不会导致拖库

Page 22: 温绍锦(温高铁) http ://weibo/wengaotie

自动识别各种日期格式• ISO-8601 日期格式• yyyy-MM-dd• yyyy-MM-dd HH:mm:ss• yyyy-MM-dd HH:mm:ss.SSS• 毫秒数字• 毫秒数字字符串• .NET JSON 日期格式• new Date(198293238)

Page 23: 温绍锦(温高铁) http ://weibo/wengaotie

GetOnly 反序列化支持public class VO { final AtomicInteger counter = new AtomicInteger(); final List<Object> items = new ArrayList<Object>(); final Map<String, Object> values = new HashMap<String, Object>();

public AtomicInteger getCounter() { return counter; } public List<Object> getItems() { return items; } public Map<String, Object> getValues() { return values; }}

AtomcXXX 、 List 、 Map 这几个类型,没有setter 也支持反序列化

Page 24: 温绍锦(温高铁) http ://weibo/wengaotie

序列化和反序列化相关字段• 标准 JavaBean Getter/Setter (public)• public field• get_/set_• getfXX/setfXX• 自动识别 getter/setter 相关的 field– 标准 javaBean 的 fieldName– 下划线 _fieldName– m_ 前缀 m_fieldName

Page 25: 温绍锦(温高铁) http ://weibo/wengaotie

Stream Writer APIJSONWriter writer = new JSONWriter(new FileWriter("/tmp/huge.json"));writer.startArray();for (int i = 0; i < 1000 * 1000; ++i) { writer.writeValue(new VO());}writer.endArray();writer.close();

JSONWriter writer = new JSONWriter(new FileWriter("/tmp/huge.json"));writer.startObject();for (int i = 0; i < 1000 * 1000; ++i) { writer.writeKey("x" + i); writer.writeValue(new VO());}writer.endObject();writer.close();

支持巨大 JSON

Page 26: 温绍锦(温高铁) http ://weibo/wengaotie

Stream Reader APIJSONReader reader = new JSONReader(new FileReader("/tmp/huge.json"));reader.startArray();while(reader.hasNext()) { VO vo = reader.readObject(VO.class); // handle vo ...}reader.endArray();reader.close();

JSONReader reader = new JSONReader(new FileReader("/tmp/huge.json"));reader.startObject();while(reader.hasNext()) { String key = reader.readString(); VO vo = reader.readObject(VO.class); // handle vo ...}reader.endObject();reader.close(); 支持巨大 JSON

Page 27: 温绍锦(温高铁) http ://weibo/wengaotie

代码规模和覆盖率

Page 28: 温绍锦(温高铁) http ://weibo/wengaotie

大小恰当Json 库 大小jackson-core-2.2.2.jarjackson-annotations-2.2.2.jarjackson-databind-2.2.2.jarjackson-module-afterburner-0.7.1.jar

188k33k864k111k

fastjson-1.1.33.jar 350k

fastjson-1.1.33-android.jar 255k

gson-2.2.4.jar 190k

json-lib-2.4.jar 159k