Jackson Json Annotations Example

Previous data-binding posts gave the idea about how the POJO’s are mapped to JSON and viceversa using ObjectMapper API. This post will explain same POJO to JSON Data-binding using commonly used Jackson JSON Annotations. Basic operations read/write will be implemented exactly as before, but POJO’s are annotated this time.

We will start with code and explain each part subsequently. Be with us.

Step 1: Include JACKSON dependency in pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">




jackson-databind is required in order to use ObjectMapper API. jackson-annotations is needed so that JSON annotations can be used in POJO.

Step 2 : Declare Input JSON



We will be mapping this Input JSON to our POJO (declared below).

Step 3 : Declare POJO

package com.websystique.json.jackson.model;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.fasterxml.jackson.annotation.JsonAnyGetter;
import com.fasterxml.jackson.annotation.JsonAnySetter;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;

@JsonIgnoreProperties({ "ignoreme1", "ignoreme2" })
public class Car {

	private String name;

	private int model;

	private String price;

	private String ignoreme1;

	private String ignoreme2;

	private List<String> colors = new ArrayList<String>();

	@JsonSerialize(using = CustomDateSerializer.class)
	private Date promoDate;

	private Map<String, Object> otherProperties = new HashMap<String, Object>();

	public Map<String, Object> any() {
		return otherProperties;

	public void set(String name, Object value) {
		otherProperties.put(name, value);

	public String toString() {
		return "Car [name=" + name + ", model=" + model + ", price=" + price
				+ ", ignoreme1=" + ignoreme1 + ", ignoreme2=" + ignoreme2
				+ ", colors=" + colors + ", promoDate=" + promoDate
				+ ", otherProperties=" + otherProperties + "]";


Step 3 : Create Serialization Helper [Optional]

Just for the sake of showing a handy and popular customization annotation, we created this class. This is used on annotated Pojo ‘promoDate’ property. Explanation is provided further in post.

package com.websystique.json.jackson.model;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.SerializerProvider;

public class CustomDateSerializer extends JsonSerializer<Date>{

	private static final SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
	public void serialize(Date date, JsonGenerator generator, SerializerProvider provider)
			throws IOException, JsonProcessingException {
		String formattedDate = dateFormat.format(date);

Step 5 : Create Main

package com.websystique.json.jackson;

import java.io.File;
import java.io.IOException;

import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.websystique.json.jackson.model.Car;

public class JsonJacksonAnnotationsDemo {

	public static void main(String args[]) throws JsonParseException,
			JsonMappingException, IOException {

		ObjectMapper mapper = new ObjectMapper();
		Car car = mapper.readValue(new File("inputfile.json"), Car.class);

		mapper.writeValue(new File("outputfile.json"), car);


Main is pretty straight forward. First we deserialize the input JSON into a POJO using ObjectMapper readValue which simply reads from a file. Then we serialize the same POJO using writeValue and writes the JSON to a file.

Below is the output after running above Main:

Car [name=AUDI, model=2014, price=30000, ignoreme1=null, ignoreme2=null, colors=[GRAY, BLACK, WHITE], promoDate=Fri May 09 16:53:20 CEST 2014, otherProperties={tom=jerry, foo=bar}]

Below is the content of outputfile.json after running above Main:


Annotations Explained:

1) @JsonProperty : This annotation can be used on a property or method which will be used for Serialization and Deserialization of JSON. It takes an optional ‘name’ parameter which is useful in case the property name is different than ‘key’ name in json.

When to use:
– If you just want to declare property and not getter/setters.
– If you are using property and getter/setters BUT want to use a different getter/setter or property name than the one coming in JSON ['key' name]. Just set the name parameter in annotations with actual ‘key’ in JSON.

For example, above POJO [car.java] does not contains any getter/setter for individual property. Instead only the properties are declared and annotated with @JsonProperty. By default, if key name matches with property name, value is mapped to property value. But you can provide a name attribute in case the key name in input is different than property name in POJO [e.g. model property is mapped to carModel input]

2) @JsonIgnoreProperties : This Class level annotation can be used to prevent certain properties to be serialized & deserialized. What it means is that they will not be mapped to JSON content.

When to use:
– If you want to ignore serialization/deserialization of certain properties [For example some properties are valid in past but not anymore].

For example, In our case, we don’t want property “ignoreme1″ & “ignoreme2″ to be mapped. So we have included these properties in JsonIgnoreProperties. You can see from content of outputfile.json that it does not contain ignoreme1 & ignoreme2 (means they were not serialized). Similarly, from toString output it is clear that ignoreme1 & ignoreme2 were not deserailzed (they did not contain any value).

3) @JsonAnySetter, @JsonAnyGetter : These annotations works as a Catch-All and are applied on Getters/Setter working with a Map. If there is any JSON value which is not mapped to a property in POJO, then that value can be caught by @JsonAnySetter, and stored (deserialized) into Map. Similarly the values which are stored into Map can be serialized back to JSON using @JsonAnyGetter.

When to use:
– If you don’t want to declare a property or method for every possible ‘key’ in JSON, but still want to capture(serialize/deserialize) the data.

For example, in our case, wee don’t have properties ‘tom’ or ‘foo’ defined in pojo. But by using @JsonAnySetter & @JsonAnyGetter, we were able to store those values(deserialized) [otherProperties={tom=jerry, foo=bar}] and create them again(serialized)["tom":"jerry","foo":"bar"].

4) @JsonSerialize : This annotation can be used to change[or customize] the default serialization (Java to JSON) process.

When to use:
– When default serialization is not fitting your needs and you want a custom behavior to be applied.

For example, in our input JSON, the “promoDate” field {“promoDate”:1399647200000} contains date in milliseconds format. In case we want to write the date in some other format(dd/MM/yyyy e.g.), we can do so using a Custom Serializer. We wrote one CustomDateSerializer. So before writing JSON output, this serializer will be called for applicable field(the one annotated with @JsonSerialize(using = CustomDateSerializer.class), and as a result convert the output in desired format before writing that output.

These were few of the commonly used annotations. For complete list of Jackson annotations, please refer to JavaDocs

That’s it. In the next post, we will see another popular json library Google GSON , and it’s usage.


If you like tutorials on this site, why not take a step further and connect me on Facebook , Google Plus & Twitter as well? I would love to hear your thoughts on these articles, it will help me improve further our learning process.

If you appreciate the effort I have put in this learning site, help me improve the visibility of this site towards global audience by sharing and linking this site from within and beyond your network. You & your friends can always link my site from your site on www.websystique.com, and share the learning.

After all, we are here to learn together, aren’t we?

  • Jayram Rout

    Its a wonderful article. I did all my JSON requirement using this…Bravo !!!

    • websystique

      Thanks Jayram.

  • anchal dhiman

    nice one

    • websystique

      Glad you liked it.

  • Pingback: Jackson Streaming Api Example - WebSystique()