<WOX> Web Objects in XML </WOX>

  Efficient and easy XML serialization of Java and C# objects

1-D Primitive Arrays

Overview

We will cover the serialization of unidimensional primitive arrays in this section. Primitive arrays are those which contain primitives as their elements. Primitives include: byte, short, int, long, float, double, char, and boolean. For a complete list of the data types supported by WOX see the Data types mapping section.

Primitive arrays are treated differently from other arrays, because they are serialized in a very efficient way. We will show you two examples. The first one is a stand-alone array, in which the array itself is the root object. In the second example, the primitive array is part of an object (it is declared as a field in a class). The code is provided in Java and C#.

FIRST EXAMPLE: The stand-alone primitive array

In this example we will serialize to XML the following array of double elements.

//Java
double[] primitiveDouble = new double[]{12.45, 878.98, 987.98, 435.87, 537.87, 89.0, 0.0, 667.332};
//C#
double[] primitiveDouble = new double[]{12.45, 878.98, 987.98, 435.87, 537.87, 89.0, 0.0, 667.332};

FIRST EXAMPLE: Serializing the stand-alone primitive array to XML

We need to specify the file name where the primitive array will be stored. 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.

//Java
String filename = "primitiveDouble.xml";
Easy.save(primitiveDouble , filename);
//C#
String filename = "primitiveDouble.xml";
Easy.save(primitiveDouble , filename);

The XML representation of the primitive array after it is serialized with WOX is below. Note that both, the Java object and the C# object, have the same XML representation.

<object type="array" elementType="double" length="8" id="0">12.45 878.98 987.98 435.87 537.87 89.0 0.0 667.332</object>         

The XML is simple and efficient. The root is an object element, with type attribute equals array, which indicates that the root object in this case is an array. The elementType attribute gives you the data type of the primitive array, and the length attribute the number of elements. The id attribute is used to handle object references (in this case we only have one object - the array itself). The elements of the array are represented as one string separated by spaces.

FIRST EXAMPLE: De-serializing the stand-alone primitive array back from XML

Given that the XML that WOX generates when serializing a Java or C# object is the same, we can use the load method of the Easy class (either in Java or C#) to de-seralize the array back from XML either to Java or C#.

//Java
double[] newPrimitiveDouble = (double[])Easy.load(filename);
//C#
double[] newPrimitiveDouble = (double[])Easy.load(filename);

The stand-alone primitive array has been reconstructed from XML to your chosen programming language. You can iterate through the array to display all its elements.

SECOND EXAMPLE: The TestArray class

In this example we show how primitive arrays are serialized when they are declared in a class. The only difference is that they are not root objects, but they are now fields of the main object. We will serialize to XML the TestArray class, which has three fields. The codes field is a primitive array of char, the values field is a primitive array of int, and the answers field is a primitive array of booloean.

//Java
public class TestArray {
    private char[] codes;
    private int[] values;
    private boolean[] answers;
}
//C#
public class TestArray {
    private char[] codes;
    private int[] values;
    private bool[] answers;
}

SECOND EXAMPLE: Serializing the TestArray class to XML

A TestArray object is created.

//Java
TestArray testArray = new TestArray(new char[]{'e', 't', 'r', 'g', 'w'},
                                    new int[]{23, 56, 78, 33, 69},
                                    new boolean[]{true, false, true, false, false});
//C#
TestArray testArray = new TestArray(new char[]{'e', 't', 'r', 'g', 'w'},
                                    new int[]{23, 56, 78, 33, 69},
                                    new bool[]{true, false, true, false, false});

We use again the save method of the Easy class to serialize the 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.

//Java
String filename = "testArray.xml";
Easy.save(testArray, filename);
//C#
String filename = "testArray.xml";
Easy.save(testArray, filename);

The testArray object respresented in XML is below. The Java object and the C# object will have the same XML representation.

<object type="TestArray" id="0">
    <field name="codes">
        <object type="array" elementType="char" length="5" id="1">\u0065 \u0074 \u0072 \u0067 \u0077</object>
    </field>
    <field name="values">
        <object type="array" elementType="int" length="5" id="2">23 56 78 33 69</object>
    </field>
    <field name="answers">
        <object type="array" elementType="boolean" length="5" id="3">true false true false false</object>
    </field>
</object>

The XML is simple and easy to understand. The root is an object element, with type attribute equals TestArray, which is the class of the root object. The root element has three field elements as children, which correspond to the three primitive arrays. Each array is treated as an object itself, with type, elementType, lenght, and id attributes. The elements of each array are represented as a string separated by spaces.

SECOND EXAMPLE: De-serializing the TestArray class back from XML

We will use the load method of the Easy class (either in Java or C#) to de-seralize the array back from XML either to Java or C#. Note that WOX in Java does not require the TestArray class to have a default constructor; but WOX in C# does.

//Java
TestArray newTestArray = (TestArray)Easy.load(filename);
//C#
TestArray newTestArray = (TestArray)Easy.load(filename);

The TestArray object has been reconstructed from XML to your chosen programming language.