Sapan Diwakar

Software developer

Follow me on Twitter Check out my code on GitHub View some of my designs on Dribbble Take a look at my Linked In profile

Getting started with Jackson JSON

I recently came across a project that required me to get JSON from POJOs. After searching I came across Jackson JSON. In this post I will try to give you brief idea about how to get started with it. I will do it by showing an example which converts POJOs to JSON and vice versa and some variations. So let's get started with it-

First of all, What is Jackson? (taken from here)

Jackson is a:

  • Streaming (reading, writing)
  • FAST (measured to be faster than any other Java json parser and data binder)
  • Powerful (full data binding for common JDK classes as well as any Java bean class, Collection, Map or Enum)
  • Zero-dependency (does not rely on other packages beyond JDK)
  • Open Source (LGPL or AL)
  • Fully conformant
  • Extremely configurable

JSON processor (JSON parser + JSON generator) written in Java. Beyond basic JSON reading/writing (parsing, generating), it also offers full node-based Tree Model, as well as full OJM (Object/Json Mapper) data binding functionality.

How to include it in the project?
If you are manually adding JARs to your class path then you can get JARs from here. If you adding dependencies to your maven project, add these lines to your pom.xml-

<!-- Jackson JSON Mapper -->  
<dependency>  
    <groupId>org.codehaus.jackson</groupId>
    <artifactId>jackson-mapper-asl</artifactId>
    <version>1.8.2</version>
</dependency>

<dependency>  
    <groupId>org.codehaus.jackson</groupId>
    <artifactId>jackson-core-asl</artifactId>
    <version>1.8.2</version>
</dependency>

Assuming that you have created your project, you now need to add following imports -

import org.codehaus.jackson.JsonGenerationException;  
import org.codehaus.jackson.map.JsonMappingException;  
import org.codehaus.jackson.map.ObjectMapper;  

To convert POJOs to JSON you first need to make a object of ObjectMapper class. If we have a class named University like this -

public class University {

    private String name;
    private int noOfDepartments;
    private ArrayList&amp;lt;String&amp;gt; departments;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getNoOfDepartments() {
        return noOfDepartments;
    }

    public void setNoOfDepartments(int noOfDepartments) {
        this.noOfDepartments = noOfDepartments;
    }

    public ArrayList&amp;lt;String&amp;gt; getDepartments() {
        return departments;
    }

    public void setDepartments(ArrayList&amp;lt;String&amp;gt; departments) {
        this.departments = departments;
    }

}

And we want to convert object of University class to JSON

ObjectMapper mapper = new ObjectMapper();  
String uniJSON = mapper.writeValueAsString(uni);  

where uni is an object of University class. If we want to print the JSON to a file we can do this by-

mapper.writeValue(new File("uni-json.json"), uni);  

To ignore some class variables from converting to JSON(for e.g. suppose we don't want to convert noOfDepartments then we have to add @JsonIgnore annotation before getter method so now, the getter method would now be -

@JsonIgnore
public int getNoOfDepartments() {  
    return noOfDepartments;
}