Serializable 序列化  The byte stream created is platform independent. So, the object serialized on one platform can be deserialized on a different platform.

Java 序列化接口Serializable详解 - 火星猿类 - 博客园 http://www.cnblogs.com/tomtiantao/p/6866083.html

深入理解JAVA序列化 - 简单爱_wxg - 博客园 https://www.cnblogs.com/wxgblogs/p/5849951.html

一个对象序列化的接口,一个类只有实现了Serializable搜索接口,它的对象才是可序列化的。因此如果要序列化某些类的对象,这些类就必须实现Serializable接口。而实际上,Serializable是一个空接口,没有什么具体内容,它的目的只是简单的标识一个类的对象可以被序列化。

什么情况下需要序列化?

a)当你想把的内存中的对象写入到硬盘的时候;

b)当你想用套接字在网络上传送对象的时候;

c)当你想通过RMI传输对象的时候;

具体解释:

a)比如说你的内存不够用了,那计算机就要将内存里面的一部分对象暂时的保存到硬盘中,等到要用的时候再读入到内存中,硬盘的那部分存储空间就是所谓的虚拟内存。在比如过你要将某个特定的对象保存到文件中,我隔几天在把它拿出来用,那么这时候就要实现Serializable接口;
b)在进行java的Socket编程的时候,你有时候可能要传输某一类的对象,那么也就要实现Serializable接口;最常见的你传输一个字符串,它是JDK里面的类,也实现了Serializable接口,所以可以在网络上传输。
c)如果要通过远程的方法调用(RMI)去调用一个远程对象的方法,如在计算机A中调用另一台计算机B的对象的方法,那么你需要通过JNDI服务获取计算机B目标对象的引用,将对象从B传送到A,就需要实现序列化接口。

jdk工具生成了序列化id

[root@hadoop1 MyBgJavaLan]# ll -as /usr/local/jdk1.8.0_144/bin
总用量 776
4 drwxr-xr-x. 2 10 143 4096 7月 22 2017 .
0 drwxr-xr-x. 8 10 143 255 7月 22 2017 ..
8 -rwxr-xr-x. 1 10 143 7965 7月 22 2017 appletviewer
0 lrwxrwxrwx. 1 10 143 8 7月 22 2017 ControlPanel -> jcontrol
8 -rwxr-xr-x. 1 10 143 7941 7月 22 2017 extcheck
8 -rwxr-xr-x. 1 10 143 7973 7月 22 2017 idlj
8 -rwxr-xr-x. 1 10 143 7925 7月 22 2017 jar
8 -rwxr-xr-x. 1 10 143 7957 7月 22 2017 jarsigner
8 -rwxr-xr-x. 1 10 143 7734 7月 22 2017 java
8 -rwxr-xr-x. 1 10 143 7941 7月 22 2017 javac
8 -rwxr-xr-x. 1 10 143 7941 7月 22 2017 javadoc
4 -rwxr-xr-x. 1 10 143 2293 6月 27 2017 javafxpackager
8 -rwxr-xr-x. 1 10 143 7941 7月 22 2017 javah
8 -rwxr-xr-x. 1 10 143 7941 7月 22 2017 javap
4 -rwxr-xr-x. 1 10 143 2293 6月 27 2017 javapackager
4 -rwxr-xr-x. 1 10 143 1809 7月 22 2017 java-rmi.cgi
128 -rwxr-xr-x. 1 10 143 129139 7月 22 2017 javaws
8 -rwxr-xr-x. 1 10 143 7925 7月 22 2017 jcmd
8 -rwxr-xr-x. 1 10 143 8013 7月 22 2017 jconsole
8 -rwxr-xr-x. 1 10 143 6264 7月 22 2017 jcontrol
8 -rwxr-xr-x. 1 10 143 7981 7月 22 2017 jdb
8 -rwxr-xr-x. 1 10 143 7941 7月 22 2017 jdeps
8 -rwxr-xr-x. 1 10 143 7941 7月 22 2017 jhat
8 -rwxr-xr-x. 1 10 143 8109 7月 22 2017 jinfo
8 -rwxr-xr-x. 1 10 143 7941 7月 22 2017 jjs
8 -rwxr-xr-x. 1 10 143 8109 7月 22 2017 jmap
76 -rwxr-xr-x. 1 10 143 74675 11月 25 2016 jmc
4 -rwxr-xr-x. 1 10 143 402 11月 25 2016 jmc.ini
8 -rwxr-xr-x. 1 10 143 7925 7月 22 2017 jps
8 -rwxr-xr-x. 1 10 143 7949 7月 22 2017 jrunscript
8 -rwxr-xr-x. 1 10 143 7981 7月 22 2017 jsadebugd
8 -rwxr-xr-x. 1 10 143 8109 7月 22 2017 jstack
8 -rwxr-xr-x. 1 10 143 7941 7月 22 2017 jstat
8 -rwxr-xr-x. 1 10 143 7941 7月 22 2017 jstatd
8 -rwxr-xr-x. 1 10 143 5110 1月 15 2017 jvisualvm
8 -rwxr-xr-x. 1 10 143 7941 7月 22 2017 keytool
8 -rwxr-xr-x. 1 10 143 7949 7月 22 2017 native2ascii
8 -rwxr-xr-x. 1 10 143 8149 7月 22 2017 orbd
8 -rwxr-xr-x. 1 10 143 7957 7月 22 2017 pack200
8 -rwxr-xr-x. 1 10 143 7997 7月 22 2017 policytool
8 -rwxr-xr-x. 1 10 143 7925 7月 22 2017 rmic
8 -rwxr-xr-x. 1 10 143 7941 7月 22 2017 rmid
8 -rwxr-xr-x. 1 10 143 7949 7月 22 2017 rmiregistry
8 -rwxr-xr-x. 1 10 143 7957 7月 22 2017 schemagen
8 -rwxr-xr-x. 1 10 143 7941 7月 22 2017 serialver
8 -rwxr-xr-x. 1 10 143 7965 7月 22 2017 servertool
8 -rwxr-xr-x. 1 10 143 8181 7月 22 2017 tnameserv
224 -rwxr-xr-x. 1 10 143 228933 7月 22 2017 unpack200
8 -rwxr-xr-x. 1 10 143 7941 7月 22 2017 wsgen
8 -rwxr-xr-x. 1 10 143 7957 7月 22 2017 wsimport
8 -rwxr-xr-x. 1 10 143 7957 7月 22 2017 xjc
[root@hadoop1 MyBgJavaLan]# pwd
/home/MyBgJavaLan
[root@hadoop1 MyBgJavaLan]# serialver
用法: serialver [-classpath 类路径] [-show] [类名称...]
[root@hadoop1 MyBgJavaLan]#

Serialization and Deserialization in Java with Example - GeeksforGeeks https://www.geeksforgeeks.org/serialization-in-java/

The byte stream created is platform independent. So, the object serialized on one platform can be deserialized on a different platform.

package com.mycom;

import java.io.Serializable;
import java.io.FileOutputStream;
import java.io.ObjectOutputStream;
import java.io.IOException;
import java.io.FileInputStream;
import java.io.ObjectInputStream; public class SerializationDemo implements Serializable {
public int a;
public String b; //Default constructor
public SerializationDemo(int a, String b) {
this.a = a;
this.b = b;
}
} class SerializationTest {
public static void main(String[] args) {
SerializationDemo object = new SerializationDemo(1, "geeksforgeeks");
String filename = "file.ser"; //Serialization
try {
//Saving of object in a file
FileOutputStream file = new FileOutputStream(filename);
ObjectOutputStream out = new ObjectOutputStream(file); //Method for serialization of object
out.writeObject(object); out.close();
file.close();
System.out.println("Object has been serialized");
} catch (IOException ex) {
System.out.println(ex);
} SerializationDemo object1 = null;
//Deserialization
try {
//Reading the object from a file
FileInputStream file = new FileInputStream(filename);
ObjectInputStream in = new ObjectInputStream(file); //Method for deserialization of object
object1 = (SerializationDemo) in.readObject(); in.close();
file.close();
System.out.println("Object has been deserialized");
System.out.println("a= " + object1.a);
System.out.println("b= " + object1.b);
} catch (IOException ex) {
System.out.println(ex);
} catch (ClassNotFoundException ex) {
System.out.println(ex);
}
}
}

  

Serialization (C#) | Microsoft Docs https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/concepts/serialization/

Serialization (C#)

  • 01/02/2020

Serialization is the process of converting an object into a stream of bytes to store the object or transmit it to memory, a database, or a file. Its main purpose is to save the state of an object in order to be able to recreate it when needed. The reverse process is called deserialization.

How serialization works

This illustration shows the overall process of serialization:

The object is serialized to a stream that carries the data. The stream may also have information about the object's type, such as its version, culture, and assembly name. From that stream, the object can be stored in a database, a file, or memory.

Uses for serialization

Serialization allows the developer to save the state of an object and re-create it as needed, providing storage of objects as well as data exchange. Through serialization, a developer can perform actions such as:

  • Sending the object to a remote application by using a web service
  • Passing an object from one domain to another
  • Passing an object through a firewall as a JSON or XML string
  • Maintaining security or user-specific information across applications

JSON serialization

The System.Text.Json namespace contains classes for JavaScript Object Notation (JSON) serialization and deserialization. JSON is an open standard that is commonly used for sharing data across the web.

JSON serialization serializes the public properties of an object into a string, byte array, or stream that conforms to the RFC 8259 JSON specification. To control the way JsonSerializer serializes or deserializes an instance of the class:

Binary and XML serialization

The System.Runtime.Serialization namespace contains classes for binary and XML serialization and deserialization.

Binary serialization uses binary encoding to produce compact serialization for uses such as storage or socket-based network streams. In binary serialization, all members, even members that are read-only, are serialized, and performance is enhanced.

Warning

Binary serialization can be dangerous. For more information, see BinaryFormatter security guide.

XML serialization serializes the public fields and properties of an object, or the parameters and return values of methods, into an XML stream that conforms to a specific XML Schema definition language (XSD) document. XML serialization results in strongly typed classes with public properties and fields that are converted to XML. System.Xml.Serialization contains classes for serializing and deserializing XML. You apply attributes to classes and class members to control the way the XmlSerializer serializes or deserializes an instance of the class.

Making an object serializable

For binary or XML serialization, you need:

Apply the SerializableAttribute attribute to a type to indicate that instances of the type can be serialized. An exception is thrown if you attempt to serialize but the type doesn't have the SerializableAttribute attribute.

To prevent a field from being serialized, apply the NonSerializedAttribute attribute. If a field of a serializable type contains a pointer, a handle, or some other data structure that is specific to a particular environment, and the field cannot be meaningfully reconstituted in a different environment, then you may want to make it nonserializable.

If a serialized class contains references to objects of other classes that are marked SerializableAttribute, those objects will also be serialized.

Basic and custom serialization

Binary and XML serialization can be performed in two ways, basic and custom.

Basic serialization uses .NET to automatically serialize the object. The only requirement is that the class has the SerializableAttributeattribute applied. The NonSerializedAttribute can be used to keep specific fields from being serialized.

When you use basic serialization, the versioning of objects may create problems. You would use custom serialization when versioning issues are important. Basic serialization is the easiest way to perform serialization, but it does not provide much control over the process.

In custom serialization, you can specify exactly which objects will be serialized and how it will be done. The class must be marked SerializableAttribute and implement the ISerializable interface. If you want your object to be deserialized in a custom manner as well, use a custom constructor.

Designer serialization

Designer serialization is a special form of serialization that involves the kind of object persistence associated with development tools. Designer serialization is the process of converting an object graph into a source file that can later be used to recover the object graph. A source file can contain code, markup, or even SQL table information.

Related Topics and Examples

System.Text.Json overview Shows how to get the System.Text.Json library.

How to serialize and deserialize JSON in .NET. Shows how to read and write object data to and from JSON using the JsonSerializerclass.

Walkthrough: Persisting an Object in Visual Studio (C#)
Demonstrates how serialization can be used to persist an object's data between instances, allowing you to store values and retrieve them the next time the object is instantiated.

How to read object data from an XML file (C#)
Shows how to read object data that was previously written to an XML file using the XmlSerializer class.

How to write object data to an XML file (C#)
Shows how to write the object from a class to an XML file using the XmlSerializer class.

序列化:对象怎么在网络中传输?

https://time.geekbang.org/column/article/202779

03 | 序列化:对象怎么在网络中传输?

网络传输的数据必须是二进制数据,但调用方请求的出入参数都是对象。对象是不能直接在网络中传输的,所以我们需要提前把它转成可传输的二进制,并且要求转换算法是可逆的,这个过程我们一般叫做“序列化”。 这时,服务提供方就可以正确地从二进制数据中分割出不同的请求,同时根据请求类型和序列化类型,把二进制的消息体逆向还原成请求对象,这个过程我们称之为“反序列化”。

总结来说,序列化就是将对象转换成二进制数据的过程,而反序列就是反过来将二进制转换为对象的过程。那么 RPC 框架为什么需要序列化呢?还是请你回想下 RPC 的通信流程:

序列化 serialize的更多相关文章

  1. 序列化serialize()与反序列化unserialize()的实例

    在写序列化serialize与反序列化unserialize()时,我们先来看看: serialize - 产生一个可存储的值的表示 描述 string serialize ( mixed $valu ...

  2. 【PHP基础】序列化serialize()与反序列化unserialize()

    序列化serialize()与反序列化unserialize(): 序列化serialize():就是将一个变量所代表的 “内存数据”转换为“字符串”的形式,并持久保存在硬盘(写入文件中保存)上的一种 ...

  3. 文件读取的时候要留意 -- 序列化serialize()与反序列化unserialize():

    原文:https://blog.csdn.net/little_rabbit_baby/article/details/53840543 序列化serialize()与反序列化unserialize( ...

  4. php数组序列化serialize与unserialize

    $arr=array('1','2','3');echo serialize($arr); //序列化 a:3:{i:0;s:1:"1";i:1;s:1:"2" ...

  5. 【MFC】序列化(Serialize)、反序列化(Deserialize)

    1.首先在头文件里面声明 DECLARE_SERIAL(CSelectionSerial) 2.重写CObject的Serialize函数 virtual void Serialize(CArchiv ...

  6. 【转】【C#】序列化(Serialize)、反序列化(Deserialize)

    序列化又称串行化,是.NET运行时环境用来支持用户定义类型的流化的机制.其目的是以某种存储形成使自定义对象持久化,或者将这种对象从一个地方传输到另一个地方. .NET框架提供了两种串行化的方式: 1. ...

  7. C# 序列化 Serialize 的应用

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.I ...

  8. jquery序列化serialize()方法空格变为+问题解决参考方法

    $("#sendNoticeData-form").serialize();会在value中存在空格的地方转化为+符合.比如:name:tiwax aaa序列化后为tiwax+aa ...

  9. 原生JS实现表单序列化serialize()

    有一个form表单,要用AJAX后台提交,原来想拼接json,但是数据多了麻烦,不灵活. 用HTML5的FormData来初始化表单 var formdata=new FormData(documen ...

  10. 序列化serialize与反序列化unserialize

    有利于存储和传递value(除了resource类型外),却不会丢失其原有类型和结构. serialize序列化时会调用魔术方法__sleep(); unserialize反序列化时会调用魔术方法__ ...

随机推荐

  1. 【整理】互联网服务端技术体系:高性能之并发(Java)

    分而合之,并行不悖. 综述入口见:"互联网应用服务端的常用技术思想与机制纲要" 引子 并发,就是在同一时间段内有多个任务同时进行着.这些任务或者互不影响互不干扰,或者共同协作来完成 ...

  2. Redis基础篇(五)AOF与RDB比较和选择策略

    RDB和AOF对比 关于RDB和AOF的优缺点,官网上面也给了比较详细的说明redis.io/topics/pers- RDB 优点: RDB快照是一个压缩过的非常紧凑的文件,保存着某个时间点的数据集 ...

  3. [LeetCode]223. Rectangle Area矩形面积

    /* 像是一道数据分析题 思路就是两个矩形面积之和减去叠加面积之和 */ public int computeArea(int A, int B, int C, int D, int E, int F ...

  4. IO输入和输出

    编程语言的I/O类库中常用使用流这个抽象概念,它代表任何有能力产出数据的数据源对象或者是有能力接收数据的接受端对象."流"屏蔽了实际的I/O设备中处理数据的细节. JAVA类库中的 ...

  5. 第一章节 BJROBOT ROS 网络配置及移动控制【ROS全开源阿克曼转向智能网联无人驾驶车】

    版权声明:该教程版权归北京智能佳科技有限公司所有,未经公司授权禁止引用.发布.转载等,否则将追究其法律责任. 使用前说明:本使用文档说明略微简明,请结合指导视频进行操作会更容易理解!! 第一章节 BJ ...

  6. 原来大数据 Hadoop 是这样存储数据的

    HDFS概述 产生背景 随着数据量越来越大,在一个操作系统中存不下所有的数据.需要将这些数据分配到更多的操作系统中,带来的问题是多操作系统不方便管理和维护.需要一种系统来管理多台机器上的文件,这就是分 ...

  7. skynet游戏服务器框架分享

    分享下我之前做的服务器框架;  游戏在线最高3万;  物理机I7的3台阿里云分服;性能及其强劲; 框架: 底层基于比较流行的skynet,基础采用c语言,脚本lua,部分服务golang; Skyne ...

  8. 网络爬虫第一步:通用代码框架(python版)

    import requests def getHTMLText(url):     try:         r=requests.get(url,timeout=30)         r.rais ...

  9. 【递归】P1157组合的输出

    题目相关 题目描述 排列与组合是常用的数学方法,其中组合就是从n个元素中抽出r个元素(不分顺序且 r ≤n),我们可以简单地将n个元素理解为自然数1,2,-,n从中任取r个数. 现要求你输出所有组合. ...

  10. Windows下如何玩转火热的go-zero

    作者:阿啄debugIT 前言 go-zero 是一个集成了各种工程实践的 web 和 rpc 框架.通过弹性设计保障了大并发服务端的稳定性,经受了充分的实战检验. go-zero 包含极简的 API ...