The need commonly arises to serialize Java objects, either to a file or to send them across the network.
WOX serialization provides a simple and robust way of doing this using XML. It's easy to use: you don't have to modify your source files with any special declarations. The source code is provided, and you are free to adapt it for your own purposes. Obviously, there's no warranty, and you use it entirely at your own risk!
WOX is a complete system for making remote method invocations over the web, using XML and HTTP. WOX is currently under development. A working prototype of WOX used for deploying text locating algorithms can be found here: http://algoval.essex.ac.uk/textloc/upload.html.
This page is concerned only with WOX Serialization.
Serializes objects of most classes efficiently:
Collection classes (not thoroughly tested, but seem to work)
Byte arrays are base-64 encoded for efficiency
Objects of type java.lang.Class are saved by their String name
Handles cyclic graphs with id/idref
Robust to class changes (will simply use defaults for newly added fields)
Small footprint: the .jar file (which contains only .class files) is only 27k.
WOX Serialization is very easy to use. The easiest way is to use the static save and load methods from the Easy class. These catch all exceptions, however, and assume that you want to save / load to a file. For lower level access, use the SimpleWriter and SimpleReader classes directly.
The following code shows sample usage. It creates an object of class TestObject, save it to a file called test.xml, then reads it back in, and writes it to a file called back.xml.
package wox.serial;
public class EasyTest {
public static void main(String[] args) {
TestObject ob = new TestObject(5);
System.out.println(ob.inc());
Easy.save(ob, "test.xml");
Object back = Easy.load("test.xml");
System.out.println("Loaded object back in");
Easy.save(back, "back.xml");
System.out.println(((TestObject) back).inc());
}
}
To run the test program called 'EasyTest', type:
java -classpath .;jdom.jar wox.serial.EasyTest
That assumed that you typed the 'java' command in the WOX root directory (i.e. where you extracted WOX to), and that 'jdom.jar' was also in that directory.
If all is well, it will produce the following output:
5 Saved object to test.xml Loaded object back in Saved object to back.xml 6
The only third party library you need to run WOX is JDOM 1.0.
The WOX Serializer was developed for Java 1.4. It seems to compile and run okay under 1.5, but produces these warnings when compiled under 1.5:
Note: wox\serial\SimpleWriter.java uses unchecked or unsafe operations. Note: Recompile with -Xlint:unchecked for details.
There are other ways of doing serialization, but WOX fills a particular niche. Here are some problems with other methods:
WOX seems to handle most classes, but may trip over, or produce unexpected results on classes that Java thinks you should not be serializing, such as AWT or Swing classes, for instance.
Some problems have been noticed when serializing classes that declare synchronized methods, but the TestObject class includes such a method, and still works fine...
This directory contains everything http://algoval.essex.ac.uk/wox/serial/ :