SlideShare una empresa de Scribd logo
1 de 13
Java序列化 叔同
议程 什么是序列化 Java原生序列化 Hessian序列化 Protobuf序列化 比较 其他
什么是序列化    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
序列化没实现Serializable接口的对象 序列化无serialVersionUID的对象 反序列化 移动属性位置 加属性删属性 改变属性名字 改变属性类型 修改类的名字 修改继承关系 序列化有serialVersionUID的对象 反序列化时serialVersionUID不变 移动属性位置 加属性删属性 改变属性名字 改变属性类型 修改类的名字 修改继承关系 反序列化时serialVersionUID改变
学到什么 Transient/Static Serializable/Externalizable 序列化只保存状态不保存行为 序列化内容:对象的类签名,非瞬态和非静态字段的值、类型。其他对象的引用(瞬态和静态字段除外) (Serializable) serialVersionUID要设置,还要前后一致 serialVersionUID若要修改,清空持久化对象
Hessian序列化 Hessian: ByteArrayOutputStream bout = newByteArrayOutputStream(); Hessian2Output oos = new Hessian2Output(bout); oos.writeObject(hessianClass); oos.close();   Java: ByteArrayOutputStream bout = newByteArrayOutputStream(); ObjectOutputStream output = newObjectOutputStream(bout); output.writeObject(javaClass); output.close();
Protobuf序列化 Write description: package protobuf;  option java_package = "protobuf"; option java_outer_classname = "ProtobufClass"; option optimize_for = SPEED ;  //CODE_SIZE,LITE_RUNTIME option java_generic_services = false; messageMessage {     required string name = 1 [default="shutong"];     optional int32 id = 2; } Compile(C++, Java, Python ...): protoc --java_out=. Message.proto Run: ProtobufClass.Message.BuildermessageBuilder = ProtobufClass.Message.newBuilder(); messageBuilder.setId(1); messageBuilder.setName("shutong"); ProtobufClass.Message message = messageBuilder.build(); byte[] content = message.toByteArray(); ProtobufClass.MessagenewObject = ProtobufClass.Message.parseFrom(content);
比较
Protobuf序列化输出内容 Key (field_number << 3) | wire_type wire_type(0:Varint/1:64-bit/2:Length-delimited/..5) Number Varint (4 byte ->1-5 byte) zigzag String Varint表示长度,之后紧跟内容
Java序列化输出内容 Class identifier  72 2-byte length of class name class name 8-byte fingerprint 1-byte flag 2-byte count of data field descriptors data field descriptors 78 (end marker) superclass type (70 if none) B--byte  C--char  D--double  F--float  I--int J--long  L--object  S--short  Z--boolean [--array  Data field descriptor ,[object Object]
2-byte length of field name
field name
class name (if field is an object),[object Object]

Más contenido relacionado

Destacado

Product Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage EngineeringsProduct Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage EngineeringsPixeldarts
 
How Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthHow Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthThinkNow
 
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfAI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfmarketingartwork
 
PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024Neil Kimberley
 
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)contently
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024Albert Qian
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsKurio // The Social Media Age(ncy)
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Search Engine Journal
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summarySpeakerHub
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next Tessa Mero
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentLily Ray
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best PracticesVit Horky
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project managementMindGenius
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...RachelPearson36
 
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...Applitools
 
12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at Work12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at WorkGetSmarter
 

Destacado (20)

Product Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage EngineeringsProduct Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage Engineerings
 
How Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthHow Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental Health
 
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfAI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
 
Skeleton Culture Code
Skeleton Culture CodeSkeleton Culture Code
Skeleton Culture Code
 
PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024
 
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie Insights
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search Intent
 
How to have difficult conversations
How to have difficult conversations How to have difficult conversations
How to have difficult conversations
 
Introduction to Data Science
Introduction to Data ScienceIntroduction to Data Science
Introduction to Data Science
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best Practices
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project management
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
 
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
 
12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at Work12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at Work
 

Java序列化分享

  • 2. 议程 什么是序列化 Java原生序列化 Hessian序列化 Protobuf序列化 比较 其他
  • 3. 什么是序列化 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
  • 4. 序列化没实现Serializable接口的对象 序列化无serialVersionUID的对象 反序列化 移动属性位置 加属性删属性 改变属性名字 改变属性类型 修改类的名字 修改继承关系 序列化有serialVersionUID的对象 反序列化时serialVersionUID不变 移动属性位置 加属性删属性 改变属性名字 改变属性类型 修改类的名字 修改继承关系 反序列化时serialVersionUID改变
  • 5. 学到什么 Transient/Static Serializable/Externalizable 序列化只保存状态不保存行为 序列化内容:对象的类签名,非瞬态和非静态字段的值、类型。其他对象的引用(瞬态和静态字段除外) (Serializable) serialVersionUID要设置,还要前后一致 serialVersionUID若要修改,清空持久化对象
  • 6. Hessian序列化 Hessian: ByteArrayOutputStream bout = newByteArrayOutputStream(); Hessian2Output oos = new Hessian2Output(bout); oos.writeObject(hessianClass); oos.close();   Java: ByteArrayOutputStream bout = newByteArrayOutputStream(); ObjectOutputStream output = newObjectOutputStream(bout); output.writeObject(javaClass); output.close();
  • 7. Protobuf序列化 Write description: package protobuf; option java_package = "protobuf"; option java_outer_classname = "ProtobufClass"; option optimize_for = SPEED ; //CODE_SIZE,LITE_RUNTIME option java_generic_services = false; messageMessage { required string name = 1 [default="shutong"]; optional int32 id = 2; } Compile(C++, Java, Python ...): protoc --java_out=. Message.proto Run: ProtobufClass.Message.BuildermessageBuilder = ProtobufClass.Message.newBuilder(); messageBuilder.setId(1); messageBuilder.setName("shutong"); ProtobufClass.Message message = messageBuilder.build(); byte[] content = message.toByteArray(); ProtobufClass.MessagenewObject = ProtobufClass.Message.parseFrom(content);
  • 9. Protobuf序列化输出内容 Key (field_number << 3) | wire_type wire_type(0:Varint/1:64-bit/2:Length-delimited/..5) Number Varint (4 byte ->1-5 byte) zigzag String Varint表示长度,之后紧跟内容
  • 10.
  • 11. 2-byte length of field name
  • 13.