Primitives
Overview
This section covers the XML representation of WOX primitive types, which 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.
We will show you how primitives are serialized with WOX. The code is provided in Java and C#.
The Product class
This is a simple example of a Product
class, which has five fields: name
of type string
, price
of type double
,
grams
of type int
, registered
of type boolean
,
and category
of type char
.
//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 a Product object to XML
A Product
object is created below.
//Java Product p = new Product("Corn Flakes", 3.98, 500, true, 'A');
//C# Product p = new Product("Corn Flakes", 3.98, 500, true, 'A');
We now use WOX to serialize the product
object to XML. We need to specify the file name where
the product
object 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 = "product.xml"; Easy.save(product, filename);
//C# String filename = "product.xml"; Easy.save(product, filename);
The XML representation of the product
object 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="Product" id="0"> <field name="name" type="string" value="Corn Flakes" /> <field name="price" type="double" value="3.98" /> <field name="grams" type="int" value="500" /> <field name="registered" type="boolean" value="true" /> <field name="category" type="char" value="\u0041" /> </object>
You can see how simple the XML is. The root is an object
element, with type
attribute
equals Product
, which is the class of the object. The id
attribute is used to handle
object references (in this case we only have one object - the product
object). Every field in the
product
object is represented by field
elements, which have name
(it is
the name of the field in the class), type
(it is the WOX data type of the field), and
value
attributes. Primitive types are represented in WOX as field
elemets.
Note the category
field of type char
. It is represented as a the Unicode value
\u0041
, which corresponds to the character A
.
De-serializing a Product object 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 product
object back from XML either to Java or C#. WOX in Java does not require the Product
class to
have default constructor; but WOX in C# does.
//Java Product newProduct = (Product)Easy.load(filename);
//C# Product newProduct = (Product)Easy.load(filename);
Done! The Product
object has been reconstructed from XML to your chosen programming language.