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

  1. No trackbacks yet.