2

I am currently developing a web application and I would like to make java objects persistent at the server so that they can be retrieved at any time. Since a database is an overkill for my application, I choose the easiest way of persisting java objects: serialization to xml or to bytes. Unfortunately a big part of the code I use are java classes which I cannot modify and these classes do not implement the interface 'serializable'. What are my options regarding to serializing objects of these classes, as well as other interacting objects of my own classes?

Bavo Devocht
  • 173
  • 2
  • 9
  • 1
    If you can subclass them and implement Serializable in the child class, you're up to go. The other way is to implement a custom way to serialize them yourself. Your question is a bit too broad to be answered here. But as a short example : a Path could be serialized as a String if it will always be deserialized by the same machine. Please also understand that classes such as java.io.Stream simply cannot be serialized. You have to find a way for your code to recreate a Stream from something else. – Jeremy Grand Feb 21 '17 at 16:30
  • I think the solution in my case is to implement a custom way to serialize the object myself by providing my own implementation of the writeObject and readObject methods of the Serializable inteface. How would you suggest wrapping the original third party class? By extending this class or by creating a new class which has a reference to an object of the third party class? – Bavo Devocht Feb 22 '17 at 13:20
  • I would go for an interface `Serializer` which would present two methods : a `Serializable toSerializable(T objectToSerialize)` and a `T fromSerializable(Serializable serializedObject)`with unique Serializable classes for every class you need to serialiaze. Then wrap all of your Serializer in a SerializationService/Factory that can find its way to and from. Thus, you'll just have an intermediate (DAO) layer between your unserializable classes and your serialized files. – Jeremy Grand Feb 22 '17 at 13:34

3 Answers3

1

As I said in my comments, I'd go for a SerializationService which would find the proper Serializer<T> for every object you want to save.

Something like :

public interface Serializer<T> {

Serializable toSerializable(T objectToSerialize);

//to build a factory/service around it
boolean canDeserialize(Serializable serializedObject);

T fromSerializable(Serializable serializedObject);

}

And if you want a basic, concrete example : with the quite-common Path :

public class PathSerializer implements Serializer<Path> {

@Override
public Serializable toSerializable(Path objectToSerialize) {
    return objectToSerialize.toString();
}

@Override
public Path fromSerializable(Serializable serializedObject) {
    if(!canDeserialize(serializedObject)){
        throw new IllegalArgumentException("Cannot deserialize this");
    }
    return Paths.get((String)serializedObject);
}

@Override
public boolean canDeserialize(Serializable serializedObject) {
    return serializedObject != null && serializedObject instanceof String;
}

}

You could also very well store POJO containing the name your original object class and the list of parameters needed in its constructor an/or a map of its fields to be able to regenerate your objects by reflection.

It's all up to you and the complexity of your application.

Jeremy Grand
  • 2,300
  • 12
  • 18
0

I think JSON would be the go-to solution here. Take Googles GSON library for example. You don't need to annotate your classes, simply write

Gson gson = new Gson();
MyObj obj = gson.fromJson(jsonString);
String json = gson.toJson(obj);

For more general information about the JSON format see the official JSON documentation.

mammago
  • 247
  • 2
  • 13
0

One option would be to extend the classes that you don't have access to, in order to save their internal state, and implement Serializable on those.

More info on this SO question: Serializing a class variable which does not implement serializable

Besides this, I don't think there is any other option except building some wrappers and serializing the classes manually to XML or JSON.

Community
  • 1
  • 1
Sagito
  • 1,036
  • 1
  • 12
  • 31