Jump to content
  • Advertisement
Sign in to follow this  
CrateMassacre

[java] Best approach to serialization

This topic is 3563 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi all, In your experience, what is the best approach to serializing objects in Java? By best I mean a combination of simple, powerful (for example able to serialize an Array or ArrayList), and durable. The last one is really important because so far I have been using the Serializable interface, and getting lovely "local class incompatible" errors every time I change the classes whose instances are being serialized. Given that I'm actively developing these classes this is simply unworkable, not if I also want to start making levels (I'm working on a mario-style platformer). The flip side is that my current serialization code is less than 50 lines. But I am very willing to trade additional complexity in exchange for levels that don't break every time I recompile.

Share this post


Link to post
Share on other sites
Advertisement
You could try managing the "serialVersionUID" yourself, depends on the changes you make to the classes. Have a look at the Version Control section in this article. (It's near the bottom).

I don't know of any alternative libraries if you really want to move away from java.io.Serializable (Be-sides implementing it yourself).

Share this post


Link to post
Share on other sites
There are several ways to customize standard Java serialization to support multiple class versions, but you cannot expect this sort of fixup to be easy or automatically managed.
Are you sure serialized objects are a good choice for your levels and for any other data that needs to be authored now and used after many iterations of your classes? A simple text or XML format with a custom reader would be more explicit and documented than tormented custom serialization; a well designed data format can be more stable than the game's object model and easier to make ad-hoc munging tools for when you change it.
You can have simple tools that use the latest version of your classes (not N old ones) to load source data and write serialized objects, if you still want the ease of use of loading serialized objects in the game.

[Edited by - LorenzoGatti on January 11, 2009 10:04:38 AM]

Share this post


Link to post
Share on other sites
Quote:
Original post by Angex
You could try managing the "serialVersionUID" yourself, depends on the changes you make to the classes. Have a look at the Version Control section in this article. (It's near the bottom).


Ooh, didn't know about this feature, thanks! This should help in the short term.

As a more long term solution I was indeed thinking of saving game data to XML or an equivalent format. What are some good libraries for reading/writing XML in java? I see there is a "javax.xml" package in the standard libraries but at a glance I'm not really sure where to start...

Share this post


Link to post
Share on other sites
As far as I know the best/easiest way is to just make all of your base classes serialization and that generally works very well.

Share this post


Link to post
Share on other sites
Quote:
Original post by CrateMassacre
As a more long term solution I was indeed thinking of saving game data to XML or an equivalent format. What are some good libraries for reading/writing XML in java? I see there is a "javax.xml" package in the standard libraries but at a glance I'm not really sure where to start...


The standard Java XML library has support for the DOM, SAX and StaX models of handling data. Of these, the only model I personally have used is DOM, which I think seems simpler to use than the others for simple XML parsing/writing.

For this (DOM), you may find JDOM more convenient to use than the standard library. The standard Java library implementation is meant to follow the DOM standard specification well, while JDOM is more adapted to the Java style of coding (although it still lacks generics support, which would be nice having).

When I started using JDOM, I found their own documentation good enough.

Share this post


Link to post
Share on other sites
My personal favorite XML parser is NanoXML.
It hasn't been updated in a while, but it uses standard Java containers (like Lists) as opposed to the annoying containers specified by DOM.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!