Quick look
Overview
This is a very quick introduction to the WOX serializer. We will first create two classes (in Java and C#). Then we will create some objects of those classes, which will be serialized to XML. Next, we will have a look at the standard XML generated by WOX, and finally we will see how the XML goes back to a Java or C# object.
The Student and Course classes
We will use the Student
and Course
classes for our serialization example.
//Java classes public class Student { private String name; private int registrationNumber; private Course[] courses; //constructors and methods omitted } public class Course { private int code; private String name; private int term; //constructors and methods omitted }
//C# classes public class Student { private String name; private Int32 registrationNumber; private Course[] courses; //constructors and methods omitted } public class Course { private Int32 code; private String name; private Int32 term; //constructors and methods omitted }
Please notice that the fields in both classes are private. WOX does not take into consideration the visibility of the fields - they will be serialized regardless their visibility. WOX in Java does not require that classes have default constructors, setters, or getters.
Serializing the Student object to XML
We first create a student
with some courses
.
//Java Course[] courses = { new Course(6756, "XML and Related Technologies", 2), new Course(9865, "Object Oriented Programming", 2), new Course(1134, "E-Commerce Programming", 3) }; Student student = new Student ("Carlos Jaimez", 76453, courses);
//C# Course[] courses = { new Course(6756, "XML and Related Technologies", 2), new Course(9865, "Object Oriented Programming", 2), new Course(1134, "E-Commerce Programming", 3) }; Student student = new Student ("Carlos Jaimez", 76453, courses);
We now use WOX to serialize the student
to XML. We need to specify the file name where the
student
object will be stored.
//Java String filename = "student.xml"; Easy.save(student, filename);
//C# String filename = "student.xml"; Easy.save(student, filename);
The save
method of the Easy
class allows you to serialize an object to XML
and store it to the specified XML file.
In Java you will require woxSerializer.jar and jdom.jar files in the classpath.
In C# you will require woxSerializer.dll. These files and the example classes can be
downloaded from the Download section.
The resulting XML is shown below:
<object type="Student" id="0"> <field name="name" type="string" value="Carlos Jaimez" /> <field name="registrationNumber" type="int" value="76453" /> <field name="courses"> <object type="array" elementType="Course" length="3" id="1"> <object type="Course" id="2"> <field name="code" type="int" value="6756" /> <field name="name" type="string" value="XML and Related Technologies" /> <field name="term" type="int" value="2" /> </object> <object type="Course" id="3"> <field name="code" type="int" value="9865" /> <field name="name" type="string" value="Object Oriented Programming" /> <field name="term" type="int" value="2" /> </object> <object type="Course" id="4"> <field name="code" type="int" value="1134" /> <field name="name" type="string" value="E-Commerce Programming" /> <field name="term" type="int" value="3" /> </object> </object> </field> </object>
The XML generated is a standard representation for the Student
object. Every field is mapped to a
field
element, and every object is mapped to an object
element. Also notice that the
type
attribute gives you the WOX data type of every field. The XML generated by WOX is simple,
easy to understand, and language independant. The same XML representation is generated for both Java and C#.
De-serializing the Student object back from XML
We will use the load
method of the Easy
class to de-seralize the Student
object.
WOX in Java does not require the Course
or the Student
classes to have default constructors;
but WOX in C# does.
//Java Student newStudent = (Student)Easy.load(filename);
//C# Student newStudent = (Student)Easy.load(filename);
Done! The Student
object has been reconstructed from XML to either Java or C#.
Summary
We covered the following:
- Create the object to be serialized.
- Serialize the object to XML, by using the method
Easy.save(Object obj, String filename)
. - De-serialize the object back from XML, by using the method
Easy.load(String filename)
.