Can we deny a java object from serialization other than giving transient keyword

后端 未结 4 969
被撕碎了的回忆
被撕碎了的回忆 2020-12-30 05:53

We can avoid serialising fields by using the transient keyword. Is there any other way of doing that?

4条回答
  •  粉色の甜心
    2020-12-30 06:22

    This is what transient means as a a keyword. Its whole purpose is to stop the serialization of the data for whatever reason.

    If you wanted a finer grain control over the process you can use the writeObject/readObject methods that the ObjectOutputStream/ObjectInputStream use as part of the serialization process, and you could combine that with some custom annotations or any logic you wanted.

    private void readObject(java.io.ObjectInputStream stream)
     throws IOException, ClassNotFoundException;
    private void writeObject(java.io.ObjectOutputStream stream)
     throws IOException
    

提交回复
热议问题