fuin.org
Small Open Source Java Tools and Libraries
Description
SerialVer4J supports versioning for serialized objects out-of-the-box.
Currently supported formats
Note: A good benchmark for JVM Serializers can be found here .Steps using SerialVer4J
-
Classes you want to serialize and deserialize must follow three simple rules:
- Make your class Serializable (as you always do)
- Give it a unique serialVersionUID (No default UID like 1L!)
-
Add an instance variable named versionUID that is initialized with the static serialVersionUID.
Caution: This has to be the first instance field in your class!
-
Create a history xml file that contains informations about your archived classes:
<?xml version="1.0" encoding="UTF-8"?> <history versionTag="versionUID"> <class package="my.test" name="Address"> <version> <serialVersionUID>-7393761964342362874</serialVersionUID> <oldClass>my.test.old.address.v1.Address</oldClass> <converterClass>my.test.old.address.v1.AddressConverter</converterClass> </version> <version> <serialVersionUID>-2466841694916192299</serialVersionUID> <oldClass>my.test.old.address.v2.Address</oldClass> <converterClass>my.test.old.address.v2.AddressConverter</converterClass> </version> </class> </history>
- Serialize your object with one of the supported formats. It's not necessary to use SerialVer4J for this task. You can use any serializer that supports the target format.
-
Deserialize the object using SerialVer4J
Pure Java Example:
final InputStream in = ... ; try { // Load the history ClassesHistory history = Utils.readFromFile(new File("versions.xml")); // Create an instance of the deserializer VersioningSerializer serializer = new VersioningJavaSerializer(history); // Returns the current version of the Address object Address address = (Address) serializer.deserialize(in); : } finally { in.close(); }