具有不可序列化部分的 Java 序列化 [英] Java Serialization with non serializable parts

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

问题描述

我有:

class MyClass extends MyClass2 implements Serializable {
  //...
}

在 MyClass2 中是一个不可序列化的属性.我如何序列化(和反序列化)这个对象?

In MyClass2 is a property that is not serializable. How can I serialize (and de-serialize) this object?

更正:当然,MyClass2 不是接口而是类.

Correction: MyClass2 is, of course, not an interface but a class.

推荐答案

正如其他人所指出的,Josh Bloch 的 Effective Java 是 Java 序列化不可或缺的资源.

As someone else noted, chapter 11 of Josh Bloch's Effective Java is an indispensible resource on Java Serialization.

该章节中与您的问题相关的几点:

A couple points from that chapter pertinent to your question:

  • 假设您想序列化 MyClass2 中不可序列化字段的状态,MyClass 必须可以直接或通过 getter 和 setter 访问该字段.MyClass 必须通过提供 readObject 和 writeObject 方法来实现自定义序列化.
  • 不可序列化字段的类必须有一个 API 以允许获取它的状态(用于写入对象流),然后使用该状态实例化一个新实例(稍后从对象流中读取时).
  • 根据 Effective Java 的 Item 74,MyClass2 必须有一个可供 MyClass 访问的无参数构造函数,否则 MyClass 不可能扩展 MyClass2 并实现 Serializable.
  • assuming you want to serialize the state of the non-serializable field in MyClass2, that field must be accessible to MyClass, either directly or through getters and setters. MyClass will have to implement custom serialization by providing readObject and writeObject methods.
  • the non-serializable field's Class must have an API to allow getting it's state (for writing to the object stream) and then instantiating a new instance with that state (when later reading from the object stream.)
  • per Item 74 of Effective Java, MyClass2 must have a no-arg constructor accessible to MyClass, otherwise it is impossible for MyClass to extend MyClass2 and implement Serializable.

我在下面写了一个简单的例子来说明这一点.

I've written a quick example below illustrating this.


class MyClass extends MyClass2 implements Serializable{

  public MyClass(int quantity) {
    setNonSerializableProperty(new NonSerializableClass(quantity));
  }

  private void writeObject(java.io.ObjectOutputStream out)
  throws IOException{
    // note, here we don't need out.defaultWriteObject(); because
    // MyClass has no other state to serialize
    out.writeInt(super.getNonSerializableProperty().getQuantity());
  }

  private void readObject(java.io.ObjectInputStream in)
  throws IOException {
    // note, here we don't need in.defaultReadObject();
    // because MyClass has no other state to deserialize
    super.setNonSerializableProperty(new NonSerializableClass(in.readInt()));
  }
}

/* this class must have no-arg constructor accessible to MyClass */
class MyClass2 {

  /* this property must be gettable/settable by MyClass.  It cannot be final, therefore. */
  private NonSerializableClass nonSerializableProperty;

  public void setNonSerializableProperty(NonSerializableClass nonSerializableProperty) {
    this.nonSerializableProperty = nonSerializableProperty;
  }

  public NonSerializableClass getNonSerializableProperty() {
    return nonSerializableProperty;
  }
}

class NonSerializableClass{

  private final int quantity;

  public NonSerializableClass(int quantity){
    this.quantity = quantity;
  }

  public int getQuantity() {
    return quantity;
  }
}

这篇关于具有不可序列化部分的 Java 序列化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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