序列化/反序列化机制 [英] serialization/deserialization mechanism

查看:140
本文介绍了序列化/反序列化机制的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

说,我有一个 X 的类,它有一个字段值,即

Say, I have a class X which has a field value, that is,

class X implements Serializable {
    private int value;
    // ...
}

此外它还有吸气剂和固定剂在这里显示这个类是序列化的。
在反序列化中,结束同一类具有值字段,访问说明符是公共的。此外,这个类没有getter和setter。所以,我的问题是:

Further it has getters and setters not displayed here. This class is serialized. At the deserialzation, end same class has value field and access specifier is public. Further, this class does not have getters and setters. So, my questions are:


  1. 如果字段的访问说明符发生更改或部分或全部方法丢失,反序列化是否会失败反序列化结束时的类?

  2. 在反序列化过程中为字段分配值的机制是什么?


推荐答案

一些好的链接揭示了Java序列化算法


1)如果字段
的访问说明符更改或部分或全部,则反序列化失败在
反序列化结束的类中缺少方法?

1) does deserialization fail in case the access specifier of the field changes OR some or all of the methods go missing in the class at the deserialization end ?

使用使用反射进行序列化

Serialization happens using Using Reflection

Java使用

private static final long serialVersionUID

默认值涉及哈希码。序列化根据以下信息创建一个long类型的哈希码:

The default involves a hashcode. Serialization creates a single hashcode, of type long, from the following information:


  • 类名和修饰符

  • The class name and modifiers

类实现的任何接口的名称

The names of any interfaces the class implements

除私有方法和构造函数之外的所有方法和构造函数的描述

Descriptions of all methods and constructors except private methods and constructors

除私人,静态和私人瞬态以外的所有字段的描述

Descriptions of all fields except private, static, and private transient

序列化机制的默认行为是经典的更安全而不是抱歉策略。序列化机制使用suid(默认为极其敏感的索引)来告知类何时发生了更改。如果是这样,序列化机制拒绝使用使用旧类序列化的数据创建新类的实例。

The default behavior for the serialization mechanism is a classic "better safe than sorry" strategy. The serialization mechanism uses the suid, which defaults to an extremely sensitive index, to tell when a class has changed. If so, the serialization mechanism refuses to create instances of the new class using data that was serialized with the old classes.


2)在反序列化期间为字段分配值
的机制是什么?

2) what is the mechanism by which fields are assigned their values during deserialization ?

这篇关于序列化/反序列化机制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆