Java partial (de)serialization of objects

主宰稳场 提交于 2019-12-01 22:13:59
AlexWien

If you want more controll you could overwrite writeObject() and readObject() and serialize yourself.

class bar {
    ...

    private void writeObject(ObjectOutputStream stream) throws IOException {
      // let version 1, later when you need to have versioning. 
      stream.writeInt(version);
      stream.writeInt(i);
      // leave out 
      // stream.writeObject(foo);

    }
}
// read object the analog, see 

http://docs.oracle.com/javase/6/docs/platform/serialization/spec/output.html#861

Mark references you don't want serialized with transient keyword.

You can make foo transient as below.

class bar {
    transient foo f;
    int i;
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!