Object references
Overview
This section explains how WOX handles object references. We will use the Product
class below,
which has five fields.
//Java public class Product { private String name; private double price; private int grams; private boolean registered; private char category; //constructors and methods omitted }
//C# public class Product { private String name; private double price; private int grams; private bool registered; private char category; //constructors and methods omitted }
Serializing an array with duplicate Product objects to XML
An array of six Product
objects is created below. The array contains some duplicate objects: product
p1
is duplicated three times in the array, and product p3
is duplicated twice.
//Java Product p1 = new Product("Baked beans", 1.75, 250, true, 'B'); Product p2 = new Product("Basmati Rice", 3.89, 750, true, 'R'); Product p3 = new Product("White bread", 1.06, 300, false, 'H'); Product[] products = new Product[]{p1, p2, p1, p3, p3, p1};
//C# Product p1 = new Product("Baked beans", 1.75, 250, true, 'B'); Product p2 = new Product("Basmati Rice", 3.89, 750, true, 'R'); Product p3 = new Product("White bread", 1.06, 300, false, 'H'); Product[] products = new Product[]{p1, p2, p1, p3, p3, p1};
Lets see how WOX handle object references. We use the save
method of the Easy
class to serialize our
array of Product
objects to XML, and store it to the specified file.
In Java you will require woxSerializer.jar and jdom.jar files in the classpath. In C# you will require woxSerializer.dll.
//Java String filename = "arrayDuplicateProducts.xml"; Easy.save(products, filename);
//C# String filename = "arrayDuplicateProducts.xml"; Easy.save(products, filename);
The array represented in XML is shown below.
<object type="array" elementType="Product" length="6" id="0"> <object type="Product" id="1"> <field name="name" type="string" value="Baked beans" /> <field name="price" type="double" value="1.75" /> <field name="grams" type="int" value="250" /> <field name="registered" type="boolean" value="true" /> <field name="category" type="char" value="\u0042" /> </object> <object type="Product" id="2"> <field name="name" type="string" value="Basmati Rice" /> <field name="price" type="double" value="3.89" /> <field name="grams" type="int" value="750" /> <field name="registered" type="boolean" value="true" /> <field name="category" type="char" value="\u0052" /> </object> <object idref="1" /> <object type="Product" id="3"> <field name="name" type="string" value="White bread" /> <field name="price" type="double" value="1.06" /> <field name="grams" type="int" value="300" /> <field name="registered" type="boolean" value="false" /> <field name="category" type="char" value="\u0048" /> </object> <object idref="3" /> <object idref="1" /> </object>
The duplicated Product
objects are not duplicated in the XML representation. They are
referenced by using the idref
attribute, which actually refers to the unique id
given to every object. Look at the XML representation above and you will find the three object references
for the duplicated objects (two for product p1
identified by id="1"
,
and one for product p3
, identified by id="3"
).
De-serializing an array of duplicate Product objects back from XML
We can use the load
method of the Easy
class de-seralize the array back from XML either to Java or C#.
Note that WOX in Java does not require the Product
class
to have a default constructor; but WOX in C# does.
//Java Product[] newProducts = (Product[])Easy.load(filename);
//C# Product[] newProducts = (Product[])Easy.load(filename);
The array has been reconstructed, and you can now iterate to display its elements. Observe that the object references are also preserved.