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

查看:103
本文介绍了具有不可序列化部分的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的第11章有效的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方法来实现自定义序列化。

  • 非序列化字段的Class必须有一个API才能获得它的状态(用于写入对象流)然后实例化具有该状态的新实例(稍后从对象流中读取时)。

  • 对于Effective Java的第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天全站免登陆