Java to JSON like Serialization

I recently decided to write myself a JSON Encoder class that would allow me to build JSON Strings from my Java code. I didn’t like the look of any of the existing JSON API’s for Java, since they all seem to require you to build the JSON Strings by hand. So I wrote a class that gets invoked like this:

public class User {
    private static final JSONEncoder<User> USER_ENCODER = new JSONEncoder<User>(User.class);

    // other fields, getters and setters go here

    public String toJSONString() {
        return USER_ENCODER.encode(this);

    public void writeJSON(final Writer out) {
         USER_ENCODER.encode(this, out);

All very nice and easy. However, theres one small problem: how do I turn the JSON from the client back into Java objects? Easy, a JSONDecoder class. This class is invoked totally differently to the Encoder.

public class UserServlet extends HttpServlet {
    public void doPost(final HttpServletRequest req, final HttpServletResponse resp) throws IOException, ServletException {

        final User user = JSONDecoder.decode(req.getReader(), User.class);

As you can see, both are very easy to work with from the outside. They also take Dates and Times into account by allowing for the use of “new Date()” constructors in the JSON String, which means no Date decoding on the client side. The implementations are a bit long to post on my blog, if anyone is interested in the implementations: leave a comment.


5 Responses to “Java to JSON like Serialization”

  1. kingschnulli Says:

    Hey, let us see the sources please !

  2. kingschnulli Says:

    Hey buddy, I’m really interested in the implementation – please drop me an email.

  3. An automated JSON Encoder and Decoder for Java « Techno Lemming Says:

    […] automated JSON Encoder and Decoder for Java I posed a little while ago that I’d implemented a JSON Encoding that worked a bit like Java Serialization. It works as […]

  4. Andrew Says:

    Hi Jason! Why don’t you want using any existent mature solution like XStream (
    Client code will be almost the same.

    • Jason Says:

      Several reasons not to use XStream:

      1) Overkill – Massive overhead and 2 different API’s for a very simple task
      2) Overhead – XStream on top of Jettison on top of the generic JSON API
      3) JSON API – I was deliberately trying to avoid this API

      So why do I have anything against the JSON API for Java? Lets turn JSON data into JSON Objects and then map those to Java objects. Add in Jettison and we’re now working through a StAX layer as well. So 4 layers of decoders where my code has 1.

      Finally: no real Date support. I wanted the simplest possible handling of Date objects (ie: no need to hand-decode them on the client).

      That said: XStream is a mature product, and great for when you need to input and output multiple data formats.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: