N-D Primitive Arrays
Overview
This section covers multi-dimensional primitive arrays. Multi-dimensional primitive arrays can be serialized as stand-alone arrays, as shown in the 1-D Primitive Arrays section, or as part of a class.
We mentioned in the previous section that primitive arrays are treated differently from object arrays, because they are serialized in a more efficient way. We will show you how multi-dimensional arrays are serialized when they are declared inside a class (as fields). The code is provided in Java and C#.
The TestMultiArray class
The TestMultiArray
class has only one field, which is a 2-dimensional array of int
.
//Java public class TestMultiArray { private int[][] matrix; }
//C# public class TestMultiArray { private int[][] matrix; }
Serializing the TestMultiArray class to XML
A TestMultiArray
object is created below.
//Java TestMultiArray test = new TestMultiArray(new int[][]{ {23, 56, 89, 36, 68}, {87, 64, 88, 32}, {78, 80, 21, 29, 34, 67} } );
//C# TestMultiArray test = new TestMultiArray(new int[][]{ new int[]{23, 56, 89, 36, 68}, new int[]{87, 64, 88, 32}, new int[]{78, 80, 21, 29, 34, 67} } );
We use 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 = "testMultiArray.xml"; Easy.save(test, filename);
//C# String filename = "testMultiArray.xml"; Easy.save(test, filename);
The TestMultiArray
object respresented in XML is below. The Java object and
the C# object will have the same XML representation.
<object type="TestMultiArray" id="0"> <field name="matrix"> <object type="array" elementType="int[]" length="3" id="1"> <object type="array" elementType="int" length="5" id="2">23 56 89 36 68</object> <object type="array" elementType="int" length="4" id="3">87 64 88 32</object> <object type="array" elementType="int" length="6" id="4">78 80 21 29 34 67</object> </object> </field> </object>
The XML is simple and easy to understand. The root is an object
element,
with type
attribute equals TestMultiArray
, which is the class of the root object.
The root element has one child: a field
element, which is the 2-dimensional primitive array.
Since this is a 2-dimensional array, it is actually serialized as an array of arrays. It is an array of
3 elements of type int[]
, where every element is an array of type int
. The first
array has 5 elements, the second array has 4 elements, and the last array has 6 elements.
The elements of each array are represented as a string separated by spaces.
Please note that every object has an id
attribute, which is used to handle object references.
Arrays of more than 2 dimensions are represented in XML following the same idea shown in this example.
De-serializing the TestMultiArray class back from XML
We will use the load
method of the Easy
class (either in Java or C#) to de-seralize the object
back from XML either to Java or C#. Note that WOX in Java does not require the TestMultiArray
class
to have a default constructor; but WOX in C# does.
//Java TestMultiArray newTest = (TestMultiArray)Easy.load(filename);
//C# TestMultiArray newTest = (TestMultiArray)Easy.load(filename);
The TestMultiArray
object has been reconstructed from XML to your chosen programming language.
You can iterate the matrix
2-dimensional array to display its elements.