tags:

views:

98

answers:

5

Hello, I need to save objects - instances of classes - in my app like some filetype. How I can write own serializer/deserializer for it?

Or exist some easier way how to save objects to some filetype?

Using of io.serialization was commented as not so good solution for "real" app. Why?

+1  A: 

Have you read that article. It is an old one but covers most of the basic stuff.

http://java.sun.com/developer/technicalArticles/Programming/serialization/

Birkan Cilingir
+2  A: 

Try the Java serialization mechanisms first. They ain't pretty, but for the most part, they'll do the job. If you run into specific problems using the in-built mechanisms, it might be worth looking into another option. There are specific issues with Java's serialization mechanisms, but they don't matter for the majority of cases...

Chinmay Kanchi
A: 

I recommend you to read this literature. It describes the various options to serialize objects with practical examples.

«The default mechanism, although simple to use, is not the best performer.»

XpiritO
+4  A: 

Serialization is difficult when objects change (such as when new fields are added). Objects may not be initialized as you expect or may be missing some data. There are methods for handing this though and custom serialization can be implemented by creating a readObject and writeObject method for deserialization and serialization. Tools such as XStream http://xstream.codehaus.org/ also exist for serializing to XML. Depending on the actual data you need to persist, you may want to consider writing just the data you need and reading it back in, and loading up your objects, rather than writing out the entire object graphs via serialization.

Jeff Storey
that is interesting answer. I will think about saving only data.
joseph
+1  A: 

The java.io.Serializable is a relative easy kind of serialization. It only validates against serialVersionUID and doesn't allow you to load older/newer versions of your objects. This means that you render your saved data invalid with significant changed to your objects.

Another approach is to save your objects in XML form. But such implementation can become tricky because you need to handle circle references. You might implement something like the following for all the classes you want to save:

public Element asXmlElement(Document doc) {
    Element element = doc.createElement(this.getClass().getSimpleName());
    element.setAttribute("class", this.getClass().getName());
    element.setAttribute("id", this.getId().toString());
    elements.put(this.getId(), element);
    // add more properties here... either as child nodes or attributes!
    // i.e. element.appendChild(referencedObj.asXmlElement(doc));
    return element;
}

Calling this method recursivly on your object graph can possibly end in an endless loop. You need to handle that (i.e. by managing a reference list of already handled objects and embedding refID elements in the XML structure).

By adding a version attribute to your XML nodes you could manage loading different versions of your object data. You need to implement some kind of Exporter/Importer classes that are able to use those methods. Of course you need the opposite method to asXmlElement... something like fromXmlNode(Node node) that decodes the data into the object.

This is only one possible approach.

Daniel Bleisteiner
Good answer, but there is already something that does what you describe, XStream: http://xstream.codehaus.org/
Chinmay Kanchi
Looks like a nice framework... I'll give it a try.
Daniel Bleisteiner
Can XStream handle persisted entities? I need to be able to reuse already existing entities defined by their primary key. Those need to be updated instead of created during loading of the XML. If not I have some more work to do... but the framework looks promising.
Daniel Bleisteiner