Java序列化<br />叔同<br />
议程<br />什么是序列化<br />Java原生序列化<br />Hessian序列化<br />Protobuf序列化<br />比较<br />其他<br />
什么是序列化<br />   Serialization is the process of converting a data structure or object into a format that can be stored (for...
序列化没实现Serializable接口的对象<br />序列化无serialVersionUID的对象<br />反序列化<br />移动属性位置<br />加属性删属性<br />改变属性名字<br />改变属性类型<br />修改类的名字...
学到什么<br />Transient/Static<br />Serializable/Externalizable<br />序列化只保存状态不保存行为<br />序列化内容:对象的类签名,非瞬态和非静态字段的值、类型。其他对象的引用(瞬态...
Hessian序列化<br />Hessian:<br />ByteArrayOutputStream bout = newByteArrayOutputStream();<br />Hessian2Output oos = new Hessi...
Protobuf序列化<br />Write description:<br />package protobuf; <br />option java_package = "protobuf";<br />option java_outer_...
比较<br />
Protobuf序列化输出内容<br />Key<br />(field_number << 3) | wire_type<br />wire_type(0:Varint/1:64-bit/2:Length-delimited/..5)<br ...
Java序列化输出内容<br />Class identifier <br />72<br />2-byte length of class name<br />class name<br />8-byte fingerprint<br />1...
2-byte length of field name
field name
class name (if field is an object)</li></li></ul><li>学到什么<br />不浪费一个Bit<br />只保存可变的部分<br />Number占比大,对Number进行优化<br />
Upcoming SlideShare
Loading in …5
×

Java序列化分享

10,210 views

Published on

给聚划算团队做的序列化分享

Published in: Technology
1 Comment
5 Likes
Statistics
Notes
No Downloads
Views
Total views
10,210
On SlideShare
0
From Embeds
0
Number of Embeds
8,242
Actions
Shares
0
Downloads
35
Comments
1
Likes
5
Embeds 0
No embeds

No notes for slide

Java序列化分享

  1. 1. Java序列化<br />叔同<br />
  2. 2. 议程<br />什么是序列化<br />Java原生序列化<br />Hessian序列化<br />Protobuf序列化<br />比较<br />其他<br />
  3. 3. 什么是序列化<br /> Serialization is the process of converting a data structure or object into a format that can be stored (for example, in a file or memory buffer, or transmitted across a network connection link) and "resurrected" later in the same or another computer environment<br />
  4. 4. 序列化没实现Serializable接口的对象<br />序列化无serialVersionUID的对象<br />反序列化<br />移动属性位置<br />加属性删属性<br />改变属性名字<br />改变属性类型<br />修改类的名字<br />修改继承关系<br />序列化有serialVersionUID的对象<br />反序列化时serialVersionUID不变<br />移动属性位置<br />加属性删属性<br />改变属性名字<br />改变属性类型<br />修改类的名字<br />修改继承关系<br />反序列化时serialVersionUID改变<br />
  5. 5. 学到什么<br />Transient/Static<br />Serializable/Externalizable<br />序列化只保存状态不保存行为<br />序列化内容:对象的类签名,非瞬态和非静态字段的值、类型。其他对象的引用(瞬态和静态字段除外) (Serializable)<br />serialVersionUID要设置,还要前后一致<br />serialVersionUID若要修改,清空持久化对象<br />
  6. 6. Hessian序列化<br />Hessian:<br />ByteArrayOutputStream bout = newByteArrayOutputStream();<br />Hessian2Output oos = new Hessian2Output(bout);<br />oos.writeObject(hessianClass);<br />oos.close();<br /> <br />Java:<br />ByteArrayOutputStream bout = newByteArrayOutputStream();<br />ObjectOutputStream output = newObjectOutputStream(bout);<br />output.writeObject(javaClass);<br />output.close();<br />
  7. 7. Protobuf序列化<br />Write description:<br />package protobuf; <br />option java_package = "protobuf";<br />option java_outer_classname = "ProtobufClass";<br />option optimize_for = SPEED ; //CODE_SIZE,LITE_RUNTIME<br />option java_generic_services = false;<br />messageMessage {<br /> required string name = 1 [default="shutong"];<br /> optional int32 id = 2;<br />}<br />Compile(C++, Java, Python ...):<br />protoc --java_out=. Message.proto<br />Run:<br />ProtobufClass.Message.BuildermessageBuilder = ProtobufClass.Message.newBuilder();<br />messageBuilder.setId(1);<br />messageBuilder.setName("shutong");<br />ProtobufClass.Message message = messageBuilder.build();<br />byte[] content = message.toByteArray();<br />ProtobufClass.MessagenewObject = ProtobufClass.Message.parseFrom(content);<br />
  8. 8. 比较<br />
  9. 9. Protobuf序列化输出内容<br />Key<br />(field_number << 3) | wire_type<br />wire_type(0:Varint/1:64-bit/2:Length-delimited/..5)<br />Number<br />Varint (4 byte ->1-5 byte)<br />zigzag<br />String<br />Varint表示长度,之后紧跟内容<br />
  10. 10. Java序列化输出内容<br />Class identifier <br />72<br />2-byte length of class name<br />class name<br />8-byte fingerprint<br />1-byte flag<br />2-byte count of data field descriptors<br />data field descriptors<br />78 (end marker)<br />superclass type (70 if none)<br />B--byte <br />C--char <br />D--double <br />F--float <br />I--int<br />J--long <br />L--object <br />S--short <br />Z--boolean<br />[--array <br />Data field descriptor<br /><ul><li>1-byte type code
  11. 11. 2-byte length of field name
  12. 12. field name
  13. 13. class name (if field is an object)</li></li></ul><li>学到什么<br />不浪费一个Bit<br />只保存可变的部分<br />Number占比大,对Number进行优化<br />
  14. 14. 其他<br />MessagePack<br />Thrift<br />Avro<br />
  15. 15. Questions ?<br />Thank You!<br />

×