Java部分序列化

huangapple go评论151阅读模式
英文:

Java Partial Serialization

问题

我有几个相互关联的类

public class A implements java.io.Serializable{
    public B objB;
}

public class B {//不实现序列化
}

当我尝试序列化 A 时,由于 B 不可序列化,所以会出错。我想要做的是序列化 A,在反序列化后重新加载 B。

英文:

I have a few classes linked to each other

public class A implements java.io.Serializable{
    public B objB;
}

public class B {//doesn't implement serializable
}

When I try to serialize A, I get error because B is not serializable
What I want to do is to serialize A and load B again after de-serializing

答案1

得分: 6

使用 transient 关键字在序列化过程中忽略 objB:

> 最简单的技术是将包含敏感数据的字段标记为 private transient。transient 字段不会持久化,也不会被任何持久化机制保存。

public class A implements java.io.Serializable {
    public transient B objB;
}
英文:

use transient for ignore objB during serialization:

> The easiest technique is to mark fields that contain sensitive data as
> private transient. Transient fields are not persistent and will not be
> saved by any persistence mechanism.

public class A implements java.io.Serializable {
    public transient B objB;
}

huangapple
  • 本文由 发表于 2020年3月4日 03:00:04
  • 转载请务必保留本文链接:https://go.coder-hub.com/60513898.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定