Code Sample

This code sample focuses on the problem of exchanging data between a HTTP client and a web server. It makes use of a single open-source library, the Jakarta Commons-Language library, which is used only for escaping JavaScript strings. The rest of the code is my own.

The central component of the sample is Java software that serializes Java objects into JavaScript Object Notation (JSON). Objects are inspected via the Java Reflection API. Rather than simply traversing the object graph to be serialized and outputting JavaScript, however, the serializer builds a linear, indexed "heap" of objects whose references to other objects have been replaced with pointers to their locations on the heap. When an object is encountered that is already on the heap, the reference to that object is replaced with a pointer to the existing heap entry. This ensures that each object from the server is deserialized as a single object on the client, even if it is referenced by several other objects being serialized. Perhaps more importantly, this system allows object graphs to be serialized successfully even when they contain cycles, where a simpler approach would result in infinite recursion.

Note: because duplicate objects are identified by comparing each newly encountered object with every object already on the heap, this solution operates in time O(n^2) with respect to the number of objects in the graph being serialized.

The code sample also comprises a simple HTML, JSP and JavaScript client application that tests the serializer. When accessed, the client application makes a call to the server and retrieves a graph of objects, serialized as described above, that represent the nodes of a binary tree, each of which has a pointer to its parent, grandparent and two children. The pointers are dereferenced and the objects on the heap "reconnected". A simple, text-based object browser allows the user to navigate the deserialized object graph.

An archive of the Java sources is here. The package structure is as follows:

A test client is available for deployment, along with the compiled serializer software, as a Java web application archive (WAR), downloadable here. Unfortunately, since I have moved to PHP hosting, I am no longer able to run the server-side serializer software on this website.