There is a specific class in a third party library that I want to serialize. How would I go about doing this?
I\'m assuming I will have to write a method that tak
You could just use a transfer object that implements Serializable, and has the same fields as the third party object. Let the transfer object implement a method that returns an object of the original third party class and you're done:
pseudocode:
class ThirdParty{
int field1;
int field2;
}
class Transfer implements Serializable{
int field1;
int field2;
/* Constructor takes the third party object as
an argument for copying the field values.
For private fields without getters
use reflection to get the values */
Transfer (ThirdParty orig){
this.field1=orig.field1;
this.field2=orig.field2;
}
ThirdParty getAsThirdParty(){
ThirdParty copy=new ThirdParty();
copy.field1=this.field1;
copy.field2=this.field2;
return copy;
}
/* override these methods for custom serialization */
void writeObject(OutputStream sink);
void readObject(InputStream src);
}
You just have to make sure that the members are serialized correctly if you got any special member objects.
Alternatively if the third party class isn't final you could just extend it, have that implement Serializable and write your own writeObject and readObject methods.
Check here for some serialization infos:
Serialization Secrets