Maps
Overview
A Map is an object that maps keys to values. A map cannot contain duplicate keys, and each key can map to at most one value.
A Map
in WOX is the equivalent to java.util.HashMap
in Java, and System.Collections.Hashtable
in C#, as can be seen in the Data types mapping section. A Map
is represented in a
unique way, which means that it can be de-serialized either to Java or C#.
The code for the example in this section is provided in both Java and C#.
The Course class
For this section, we will use again the Course
class, which has three fields: code
of type int
, name
of type string
, and term
of type int
.
//Java public class Course { private int code; private String name; private int term; //constructors and methods omitted }
//C# public class Course { private Int32 code; private String name; private Int32 term; //constructors and methods omitted }
Serializing a map of Course objects to XML
We will create a map of four entries: the keys will be of type int
, and the values will be of type
Course
objects.
//Java HashMap map = new HashMap(); map.put(6756, new Course(6756, "XML and Related Technologies", 3)); map.put(9865, new Course(9865, "Object Oriented Programming", 2)); map.put(1134, new Course(1134, "E-Commerce Programming", 2)); map.put(4598, new Course(4598, "Enterprise Component Architecture", 3));
//C# Hashtable map = new Hashtable(); map.Add(6756, new Course(6756, "XML and Related Technologies", 3)); map.Add(9865, new Course(9865, "Object Oriented Programming", 2)); map.Add(1134, new Course(1134, "E-Commerce Programming", 2)); map.Add(4598, new Course(4598, "Enterprise Component Architecture", 3));
We use the save
method of the Easy
class to serialize the
map 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 = "mapCourses.xml"; Easy.save(map, filename);
//C# String filename = "mapCourses.xml"; Easy.save(map, filename);
The XML representation of the map is shown below. Note that the order of the entries may vary between the XML representation generated by WOX in Java, and the XML representation generated by WOX in C#. This is because the hash function may not be the same in both languages. This does not affect the de-serialization process, which means you could take the XML representation of the map generated by WOX in Java and de-serialize it in C#; or viceversa.
<object type="map" id="0"> <object type="entry"> <object type="int" value="1134" id="1" /> <object type="Course" id="2"> <field name="code" type="int" value="1134" /> <field name="name" type="string" value="E-Commerce Programming" /> <field name="term" type="int" value="2" /> </object> </object> <object type="entry"> <object type="int" value="6756" id="3" /> <object type="Course" id="4"> <field name="code" type="int" value="6756" /> <field name="name" type="string" value="XML and Related Technologies" /> <field name="term" type="int" value="3" /> </object> </object> <object type="entry"> <object type="int" value="4598" id="5" /> <object type="Course" id="6"> <field name="code" type="int" value="4598" /> <field name="name" type="string" value="Enterprise Component Architecture" /> <field name="term" type="int" value="3" /> </object> </object> <object type="entry"> <object type="int" value="9865" id="7" /> <object type="Course" id="8"> <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> </object>
The root is an object
element, which is the map
of four entry
objects. Each entry
object has two children: the fisrt object
represents the key
,
and the second object is the value
associated with that key
. The key
is an
object of type int
, and the value
is an object of type Course
. The XML representation
of a Course
object has been explained in other sections.
De-serializing a map of Course objects back from XML
Since the map
is not specific to Java or C#, we can use the load
method of
the Easy
class de-seralize the map
back from XML either to Java or C#.
Note that WOX in Java does not require the Course
class
to have a default constructor; but WOX in C# does.
//Java HashMap newMap = (HashMap)Easy.load(filename);
//C# Hashtable newMap = (Hashtable)Easy.load(filename);
The map has been reconstructed and you can now iterate to display its elements.