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. idea run dashbord使用

    idea 中使用dashbord可以迅速开启多个服务方便进行本地测试 开启步骤 1. 打开idea菜单 view-> toolWindows ->service 选项 2. 打开底部的se ...

  2. 使用@Cacheable注解时,Redis连不上,直接调用方法内部的解决方案

    最近redis 域名一致解析错误,导致业务多了很多异常.那么如何在这种情况下直接访问数据库,而不是报错呢 1. 解决方案 其实很简单,在配置 redis 时,只需要多一项配置,继承 CachingCo ...

  3. Qt学习笔记-更高级的文本编辑器-完善第一版-gif动画

    现在的浏览器大多都有动作图标,现在我们也为浏览器加上图标. 在网上搜索到了几个ico的图标.现在直接拿来用. 首先创建资源文件. 在工程名上鼠标右键,选择add new 然后再选择 Qt Resour ...

  4. mongoose连接collections会自动加s的问题解决

    问题的出现: 最近在用到mongoose连接数据库时遇到了这样的问题,我在mongoodb上创建了一个collection为course,然后在配置完mongoose连接数据库后拿到的是一个空对象. ...

  5. redis系列:分布式锁

    redis系列:分布式锁 1 介绍 这篇博文讲介绍如何一步步构建一个基于Redis的分布式锁.会从最原始的版本开始,然后根据问题进行调整,最后完成一个较为合理的分布式锁. 本篇文章会将分布式锁的实现分 ...

  6. springboot 启动jar正确方式

    首先需要pom.xml配置一个插件: IDEA 在右侧执行顶上m图片按钮 在command Line 中执行clean package命令 执行打包注意 打完jar包后最好解压jar查看一下META- ...

  7. 企业集群架构-03-NFS

    NFS 目录 NFS NFS基本概述 NFS应用场景 NFS实现原理 NFS总结 NFS服务端安装 环境准备 服务端安装NFS 服务端NFS配置 服务端开机自启 服务端验证配置 NFS客户端挂载卸载 ...

  8. Mybatis-plus的使用步骤

    Mybatis-plus的简单使用步骤 花开堪折直需折,莫待无花空折枝 导入依赖 <dependency> <groupId>org.projectlombok</gro ...

  9. [每日一题]面试官问:谈谈你对ES6的proxy的理解?

    [每日一题]面试官问:谈谈你对ES6的proxy的理解? 关注「松宝写代码」,精选好文,每日一题 作者:saucxs | songEagle 一.前言 2020.12.23 日刚立的 flag,每日一 ...

  10. 在kotlin用jni调用c++的dll中踩的坑

    在kotlin用jni调用c++的dll中踩的坑 can't find dependents libraries 不是个有效的32位程序(或者是?????32??????) 常规检查 java 指针 ...