How to serialize a non-serializable in Java?

前端 未结 5 1770
太阳男子
太阳男子 2020-11-27 19:54

How can I serialize an object that does not implement Serializable? I cannot mark it Serializable because the class is from a 3rd party library.

5条回答
  •  感动是毒
    2020-11-27 20:38

    Wrap the non-serializable class in a class of your own that implements Serializable. In your class's writeObject method, do whatever's necessary to serialize sufficient information on the non-serializable object so that your class's readObject method can reconstruct it.

    Alternatively, contact the developer of the non-serializable class and tell him to fix it. :-)

提交回复
热议问题