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.
-
2`class SFoo extends com.library.Foo implements Serializable {}` ? – wchargin Nov 09 '13 at 02:47
-
@wchargin No. That only serializes the data of the serializable part, not of the on-serializable library part being extended. – user207421 Mar 08 '20 at 10:03
5 Answers
You can't serialise a class that doesn't implement Serializable
, but you can wrap it in a class that does. To do this, you should implement readObject
and writeObject
on your wrapper class so you can serialise its objects in a custom way.
- First, make your non-serialisable field
transient
. - In
writeObject
, first calldefaultWriteObject
on the stream to store all the non-transient fields, then call other methods to serialise the individual properties of your non-serialisable object. - In
readObject
, first calldefaultReadObject
on the stream to read back all the non-transient fields, then call other methods (corresponding to the ones you added towriteObject
) to deserialise your non-serialisable object.
I hope this makes sense. :-)

- 219,335
- 46
- 382
- 435
-
Doesn't this assume that everything that needs to get (de)serialized in the non-serialized object is visible to the class that is wrapping it? – whaley May 28 '11 at 19:55
-
@whaley: Yes. Usually, what needs saving is externally-visible state anyway, so this shouldn't be a problem. (It's also no different from the constraints on your XML/JSON/YAML solution. :-P) – C. K. Young May 28 '11 at 20:21
-
2not sure what your bullet points 2 and 3 mean could please give a sample code (newbie in java programming) – Mar 03 '15 at 12:10
-
This does not seem to answer the question, since it does not give a way to avoid adding 'implements Serializable' to any class that needs to be serialized. – bmargulies Sep 23 '16 at 21:11
-
1@bmargulies You cannot directly serialise a class not marked Serializable. End of story. My answer allows you to write a Serializable wrapper around a non-Serializable class, that preserves its essential state. – C. K. Young Sep 23 '16 at 21:13
-
@bmargulies I like the idea behind your edit, so I've changed the wording some to better reflect how I write. :-) – C. K. Young Sep 23 '16 at 21:53
-
I think, even after implementing `Serializable` and adding `readObject` and `writeObject` in serialized wrapper class, wrapped class must have a no - arg constructor so that serialized class can have a no - arg constructor. [See this question](http://stackoverflow.com/questions/17153686/no-valid-constructor-during-serialization) – Sabir Khan Sep 27 '16 at 05:30
-
Is there way to make [this class](https://lucene.apache.org/core/6_1_0/core/org/apache/lucene/index/IndexWriter.html) serializable? I don't think so because no - arg default constructor is missing in `IndexWriter` and a correct default constructor can't be added in sub class calling [super class constructor](https://lucene.apache.org/core/6_1_0/core/org/apache/lucene/index/IndexWriter.html#IndexWriter-org.apache.lucene.store.Directory-org.apache.lucene.index.IndexWriterConfig-) – Sabir Khan Sep 27 '16 at 05:41
-
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. :-)

- 7,702
- 5
- 33
- 59
You can use Kryo. It works on non serialized classes but classes needs registered aforehand.

- 878
- 11
- 24
If your class already implements the Serializable interface (required for serializing), all you must do is to declare the field you don't want to serialize with transient:
public transient String description;

- 49
- 1
- 4
-
7The question was specifically about classes that *do not* implement Serializable. – Martin Nyolt Sep 14 '16 at 11:26
If the class is not final, you can make your own class that extends it and implements Serializable. There are lots of other ways to serialize besides Java's built-in mechanism though.

- 6,528
- 8
- 42
- 70
-
It's worth adding that doing this won't serialize fields that are private or package-private of the super class though. – whaley May 28 '11 at 19:24
-
4@whaley @Michael McGowan This won't serialize *any* fields of the non-serializable base class. Wrong answer, down voted. – user207421 May 28 '11 at 23:39
-
@EJP Perhaps I should have been explicit about the need for `readObject` and `writeObject`, but those are issues any time one implements Serializable on something whose parent isn't `Object`. The answer is really just incomplete rather than wrong, no? – Michael McGowan May 29 '11 at 08:07
-
1this is a distinction without a difference. It is incomplete *and therefore* wrong. The part of your comment commencing 'but' is incorrect as well. – user207421 May 29 '11 at 09:42