Java partial (de)serialization of objects

前端 未结 3 359
迷失自我
迷失自我 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

    0 讨论(0)
  • 2021-01-20 03:25

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

    0 讨论(0)
  • 2021-01-20 03:26

    You can make foo transient as below.

    class bar {
        transient foo f;
        int i;
    }
    
    0 讨论(0)
提交回复
热议问题