Archive for October, 2008

Mason: Yet Another Java JSON Parser

Now if you dont know what JSON is, it is Javascript Object Notation. It is a way of exchanging Javascript Objects between a server and a client. Your current javascript supports parsing of JSON Strings to Javasript objects, but NOT all server side languages support JSON Script parsing by default. Scala has something, but still i find it a little bit weak in the sense that you still do a lot of work to actually get you data back as you might want it.

The only JSON parser for java on works, but for me, its too large a workhorse for such a small task as parsing a String into an object. On the JSON website, they have done a lot of work to describe a sort of BNF for what a JSON String looks like, so my solution is this, write a parser(I am comfortable with javacc) that parses JSON Strings and return simple everyday objects, not class wrappers.

A JSON string is either a List, A Map or any Value, so Mason is a parser that parses a JSON String and returns any of this value.

Its so simple to use and straight forward.

sample code?? I’ll show you some

            FileInputStream fis = new FileInputStream(new File("/home/trinisoftinc/src/mason/lib/test.json"));

            Mason parser = new Mason(fis);


            System.out.println("FINAL Map: " + parser.finalMap);

            System.out.println("FINAL Array: " + parser.finalArray);

If the file test.json is a List or A Value, only finalArray is populated. finalArray is an ArrayList, finalMap is an Hashtable.

Source Code:

Download Jar File + Example :


Leave a Comment