Serializing Java Objects with JSON

This is the fourth project of the semester.

You are to define at least three Java classes to represent objects stored on the server.  The classes should implement an interface like the following:

interface CustomSerializable {

   // encode the object's member variables in JSON
   public String toJSON ();

   // encode the object's member variables in XML
   public String toXML ();

   // render the object in HTML
   public String toHTML ();

For example, a Person class which implements CustomSerializable might be encoded as follows:

/* JSON encoding */
   "name" : "Ryanne Dolan",
   "age" : 23,

<!-- XML encoding -->
   <name>Ryanne Dolan</name>

<!-- HTML encoding -->
<li>name: Ryanne Dolan</li>
<li>age: 23</li>

To implement the toJSON, toXML, and toHTML methods, I would recommend using Java's StringBuilder class.  You don't need to use any special library to automate the serialization; just write out the strings by hand.

Then implement a Java servlet that accepts GET request parameters.  Your request URI might look like /project4/Serializer?what=Person&format=JSON for example.  Based on the parameters passed to the servlet, instantiate an object of the corresponding class, initialize the object with values, and write the object back to the client in the requested format.  In other words, your servlet must return JSON, XML, and HTML when asked.

Additionally, have your Servlet spit out links (in an HTML page) to all of the above combinations of GET requests whenever an invalid request is made to the servlet.  For example, you might display:

bad request.  usage:

  • Person in JSON
  • Person in XML
  • Person in HTML

Compile your code into a WAR file called project4.war and deploy it using your Tomcat Manager servlet (or using Ant).  Make sure your servlet is running at