Java partial (de)serialization of objects

前端 未结 3 361
迷失自我
迷失自我 2021-01-20 02:45

Let\'s say we have 3 Classes:

class foo { // not a singleton
    String s;
}
class bar {
    foo f;
    int i;
}
class baz {
    foo sameF;
}
3条回答
  •  [愿得一人]
    2021-01-20 03:07

    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

提交回复
热议问题