Jvrq
Jvrq

Reputation: 1

Serialization of an object from external API

I'm getting the java.io.NotSerializableException.

How can I serialize an object that does not implement java.io.Serializable?

I would like to serialize it in a stream.

I want to serialize an object from an external API. I can't modify this API.

In the external API, this object does not implement java.io.Serializable

Sure there are private fields, but there are a lot of getter and setter methods. There are public static fields, and there is not a no-arg constructor, really, there is no constructor.

I think it's difficult to serialize it, isn't it? Any suggestion?

Upvotes: 0

Views: 430

Answers (2)

Luciano
Luciano

Reputation: 8582

From what I understand from your question, you can create new instances of that object, and set their fields through setters. Then I suggest you use the getters to extract all the fields of the object into a Map, and then serialize that map.

Later you deserialize the map, create a new instance of the object, and set all the fields back with the setters.

Of course this is assuming there isn't any read only field that is set at construction time, like an ID. Because in that case the ids would be different.

Upvotes: 0

Rajesh Pantula
Rajesh Pantula

Reputation: 10241

if you can override the external API can create your own class which is a child of the external class.

Your child class can implement Serializable interface and you can serialize your class.

For ex-

ur external class is

class ExternalAPIClass
{
..
..
}

Class MyClass extends ExternalAPIClass implements Serializable
{

....
...
...
}

Upvotes: 1

Related Questions