Home > java > Jettison – A JSON streaming parser that uses Stax

Jettison – A JSON streaming parser that uses Stax

December 26th, 2010 Leave a comment Go to comments

Jettison is a library that provides a streaming parser for the JSON data format. Reading that sentence again makes you want to compare it to Stax, the streaming parser for XML. Jettison provides implementation classes that inherit from the same interfaces that a caller would use for XML reading / writing. Diving into some code reveals how JSON is written using this API…

    public void go()
            MappedNamespaceConvention con = new MappedNamespaceConvention();
            XMLStreamWriter writer = new MappedXMLStreamWriter(con, new PrintWriter(System.out));
        catch (Exception e)

Output: {“alice”:”bob”}

Notice that the XMLStreamWriter is the one in the javax.xml.stream package. The underlying data format that is written using this interface is abstracted. You could use the XMLStreamWriter to write a XML document for example. A XMLStreamReader can be used in the same way to read a XML or JSON document.

This style is quite beneficial especially for those that write frameworks / APIs. Axis2 supports JSON using jettison. The amount of code changes necessary to support a new format would be minimal, if the code is already modularized into chunks and the interfaces used are the same. Of course we make the assumption that the data formats in question can be supported using a common set of callback methods / events.

While this style allows support for a new format with minimal code changes, the interfaces / method names / events can get in the way to create confusion. What business does one have writing JSON from an interface called XMLStreamWriter ? None really. The confusion that the interface and implementation can cause is also sizable. However after working with jettison for a while and reaping some of its benefits, at least for me, the benefits outweighed the minor drawbacks.

Categories: java Tags: , , ,
  1. February 20th, 2014 at 16:46 | #1

    Un grand merci pour le retour. Un article encore une
    fois très intéressent !

    Bone callumet

  2. September 22nd, 2014 at 02:44 | #2

    I have learn a few just right stuff here.
    Certainly price bookmarking for revisiting. I wonder how
    so much attempt you put to create this type of excellent informative web site.

  3. October 14th, 2015 at 17:09 | #3

    I use a micro SD card between my laptop and netbook as an external hardrive for my class files. I often download files on one computer or the other without the drive in that computer. Is it possible to set my computer up so that when I insert my drive my computer automatically updates the folder on my removable drive with the folder on my computer?.

  4. November 4th, 2015 at 19:58 | #4

    Thanks for that, I really appreciate the help. We have 20+ years’ experience in the IT industry especially in e-commerce and Marketing and this will help tremendously.

  1. No trackbacks yet.