Using the Jackson JSON Processor – A Quick Overview

Adrienne Gessler Java, Technology Snapshot, Tutorial 1 Comment

A wide variety of parsers exist for use with JSON and can be viewed on the JSON website. One great option for use with Java objects is Jackson. Jackson allows for Java objects to be marshaled to and from JSON quickly and easily. Per the Jackson website, it has been measured to be faster than any other Java JSON parser and data binder.

Without any parser, creating JSON objects via Strings can be cumbersome and awkward. Take for example this simple Servlet that produces the following JSON with no parser (not recommended):

{
    "name" : "Sparky" ,
    "breed" : "Border Collie"
}

would require something like:

public class PetServlet extends HttpServlet{
    public void doGet(HttpServletRequest request,
    HttpServletResponse response)
    throws ServletException,IOException{
        StringBuffer sb =new StringBuffer();
        Dog userDog = new Dog ("Sparky", "Border Collie");
        StringBuffer sb = new StringBuffer();
        sb.append("{");
            sb.append("\"name\"");
            sb.append(" : ");
            sb.append("\""+userDog.getName()+"\"");
            sb.append(" , ");
            sb.append("\"breed\"");
            sb.append(" : ");
            sb.append("\""+userDog.getBreed()+"\"");
        sb.append(" } ");

        response.setContentType("application/json");
        PrintWriter out = response.getWriter();
        out.print(sb.toString);
    }

Obviously, this could be error prone and require extensive testing.

Another common Java option, which is much more user friendly is to use the org.jason API. In this instance something like the following would be used in the Servlet to produce the above JSON:

public class PetServlet extends HttpServlet{
    public void doGet(HttpServletRequest request,
    HttpServletResponse response)
    throws ServletException,IOException{

        Dog userDog = new Dog ("Sparky", "Border Collie");
        JSONObject dogObj = new JSONObject();
        dogObj.put("name", userDog.getName());
        dogObj.put(“breed”, userDog.getBreed());

        response.setContentType("application/json");
        PrintWriter out = response.getWriter();
        out.print(dogObj);

    }

But with Jackson this gets even better. Following along with our very simple Servlet example, using Jackson’s ObjectMapper class for use of the full data binding with a POJO the following is possible to create the same JSON object noted above with:

public class PetServlet extends HttpServlet{
    public void doGet(HttpServletRequest request,
    HttpServletResponse response)
    throws ServletException,IOException{

        Dog userDog = new Dog ("Sparky", "Border Collie");
        ObjectMapper mapper = new ObjectMapper();
        response.setContentType("application/json");

        PrintWriter out = response.getWriter();
        mapper.writeValue(out, userDog);

    }

The same thing is easily possible in reverse via the mapper’s readValue() method. For example, reading some userDog information in the same Servlet prior to processing could be done via:

userDog = mapper.readValue(request.getInputStream(), Dog.class);

Obviously, this is a very simple example. Jackson can handle parsing back and forth POJOs, beans, primitives, lists, maps and arrays. Jackson also offers a wide variety of annotations to allow for detailed configuration of the parsing, including ignore and auto-detect functionalities. The 1.9 version has added an unwrap annotation that allows for non-flat Java objects to be neatly parsed into flat JSON objects and has added an injection annotation feature available for de-serialization.

To download Jackson or learn more, check out the Jackson website at: http://jackson.codehaus.org/

–Adrienne Gessler, asktheteam@keyholesoftware.com


About the Author
Adrienne Gessler

Adrienne Gessler

Adrienne Gessler is a consultant for Keyhole Software with 8 years of experience in the wild and crazy world of IT and a primary focus on Java/Java EE development. Recently she has been working with rich user interfaces in JavaScript and is always excited by changes in technology and what those changes can bring to the dev community & her clients. Outside of software, Adrienne loves reading, running, concerts, and generally going about her weird life.


Share this Post

Comments 1

Leave a Reply