Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Jackson Annotations for JSON (Part 1): Serialization and Deserialization

DZone's Guide to

Jackson Annotations for JSON (Part 1): Serialization and Deserialization

This introductory post will cover the Jackson annotations you should keep in mind when you need to control both deserialization and serialization between POJOs and JSON.

· Java Zone ·
Free Resource

Java-based (JDBC) data connectivity to SaaS, NoSQL, and Big Data. Download Now.

Jackson is a suite of data-processing tools for Java comprising of three components:

  • Streaming (jackson-core) defines low-level streaming APIs and includes JSON-specific implementations.

  • Annotations (jackson-annotations) contains standard Jackson annotations.

  • Databind (jackson-databind) implements data-binding (and object serialization) support on the streaming package. This package depends on both the streaming and annotations packages.

In this series of articles, I will explain data binding Java objects to JSON using Jackson annotations. I will take up each of the Jackson annotations and explain, with code snippets, how to use them. Each annotation usage is accompanied with proper test cases.

Jackson Serialization and Deserialization Annotations

The Jackson library provides annotations that you can use in POJOs to control both serialization and deserialization between POJOs and JSON. These annotations that are used in both serialization and deserialization operations are:

  • @JsonIgnore
  • @JsonIgnoreProperties
  • @JsonIgnoreType
  • @JsonAutoDetect

@JsonIgnore

The @JsonIgnore annotation marks a field of a POJO to be ignored by Jackson during serialization and deserialization. Jackson ignores the field both JSON serialization and deserialization. An example of Java class that uses the @JsonIgnore annotation is this.

IgnoreDemoBean.java

package guru.springframework.blog.jsonannotation.domain.serializationanddeserialization;

import com.fasterxml.jackson.annotation.JsonIgnore;

public class IgnoreDemoBean {
    @JsonIgnore
    public long personId = 0;
    public String  name = "James Clark";

    @Override
    public String toString() {
        return "IgnoreDemoBean{" +
                "personId=" + personId +
                ", name='" + name + '\'' +
                '}';
    }
}


The test class to test the @JsonIgnore annotation is this.

IgnoreDemoBeanTest.java

package guru.springframework.blog.jsonannotation.domain.serializationanddeserialization;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import java.io.IOException;
import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.not;
import static org.hamcrest.core.IsEqual.equalTo;
import static org.junit.Assert.*;

public class IgnoreDemoBeanTest {
    private ObjectMapper objectMapper ;
    @Before
    public void setUp() throws Exception{
        objectMapper = new ObjectMapper();
    }
    @After
    public void tearDown() throws Exception{
        objectMapper = null;
    }
    @Test
    public void testSerializingWithJsonIgnore()
            throws JsonProcessingException {
        String jsonString = objectMapper.writeValueAsString(new IgnoreDemoBean());
        System.out.println(jsonString);
        assertThat(jsonString, containsString("James Clark"));
        assertThat(jsonString, not(containsString("productId")));
    }

    @Test
    public void testDeSerializingWithJsonIgnore() throws IOException {
        String jsonString = "{\"personId\": 231, \"name\": \"Mary Parker\"}";
        ObjectMapper mapper = new ObjectMapper();
        IgnoreDemoBean bean = objectMapper.readValue(jsonString, IgnoreDemoBean.class);
        System.out.println(bean);
        assertThat(bean.name, is(equalTo("Mary Parker")));
        assertThat(bean.personId, is(not(equalTo(231L))));
    }

}


The output on running the test in IntelliJ is this.
@JsonIgnore Annotation Test Output

As you can see, the @JsonIgnore annotation ignored the field personId both during serialization and deserialization.

@JsonIgnoreProperties

The @JsonIgnoreProperties annotation is used at the class level to ignore fields during serialization and deserialization. The properties that are declared in this annotation will not be mapped to the JSON content.

Let us consider an example of Java class that uses the @JsonIgnoreProperties annotation.

IgnorePropertiesDemoBean.java

package guru.springframework.blog.jsonannotation.domain.serializationanddeserialization;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;

@JsonIgnoreProperties({"userId", "gender"})
public class IgnorePropertiesDemoBean {
    public long  userId = 0;
    public String  name = "James Clark";
    public String  gender  = null;

    @Override
    public String toString() {
        return "IgnorePropertiesDemoBean{" +
                "userId=" + userId +
                ", name='" + name + '\'' +
                ", gender='" + gender + '\'' +
                '}';
    }
}


The test code to test the @JsonIgnoreProperties annotation is this.

IgnorePropertiesDemoBeanTest

package guru.springframework.blog.jsonannotation.domain.serializationanddeserialization;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import java.io.IOException;

import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.not;
import static org.hamcrest.core.IsEqual.equalTo;
import static org.junit.Assert.*;

public class IgnorePropertiesDemoBeanTest {
    private ObjectMapper objectMapper ;
    @Before
    public void setUp() throws Exception{
        objectMapper = new ObjectMapper();
    }
    @After
    public void tearDown() throws Exception{
        objectMapper = null;
    }

    @Test
    public void testSerializingWithJsonIgnoreProperties()
            throws JsonProcessingException {
        String jsonString = objectMapper.writeValueAsString(new IgnorePropertiesDemoBean());
        System.out.println(jsonString);
        assertThat(jsonString, containsString("James Clark"));
        assertThat(jsonString, not(containsString("userId")));
    }

    @Test
    public void testDeSerializingWithJsonIgnoreProperties() throws IOException {
        String jsonString = "{\"userId\": 231, \"name\": \"Mary Parker\", \"gender\": \"male\"}";
        ObjectMapper mapper = new ObjectMapper();
        IgnorePropertiesDemoBean bean = objectMapper.readValue(jsonString, IgnorePropertiesDemoBean.class);
        System.out.println(bean);
        assertThat(bean.name, is(equalTo("Mary Parker")));
        assertThat(bean.userId, is(not(equalTo(231L))));
    }

}


The output of running the test in IntelliJ is this.


As you can see, the @JsonIgnoreProperties annotation ignored the field userId and gender both during serialization and deserialization.

@JsonIgnoreType

The @JsonIgnoreType annotation is used to mark a class to be ignored during serialization and deserialization. It marks all the properties of the class to be ignored while generating JSON and reading JSON. An example of Java class that uses the @JsonIgnoreType annotation is this.

IgnoreTypeDemoBean.java

package guru.springframework.blog.jsonannotation.domain.serializationanddeserialization;

import com.fasterxml.jackson.annotation.JsonIgnoreType;

public class IgnoreTypeDemoBean {
    @JsonIgnoreType
    public static class Address {
        public String doorNumber = null;
        public String streetName = null;
        public String pinCode = null;
        public String city = null;

        @Override
        public String toString() {
            return "Address{" +
                    "doorNumber='" + doorNumber + '\'' +
                    ", streetName='" + streetName + '\'' +
                    ", pinCode='" + pinCode + '\'' +
                    ", city='" + city + '\'' +
                    '}';
        }
    }
    public long personId = 0;
    public String  name = "James Clark";
    public Address address = new Address();

    @Override
    public String toString() {
        return "IgnoreTypeDemoBean{" +
                "personId=" + personId +
                ", name='" + name + '\'' +
                ", address=" + address +
                '}';
    }
}


The test code to test the @JsonIgnoreProperties annotation is this.

IgnoreTypeDemoBeanTest.java

package guru.springframework.blog.jsonannotation.domain.serializationanddeserialization;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import java.io.IOException;
import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.not;
import static org.hamcrest.core.IsEqual.equalTo;
import static org.junit.Assert.*;

public class IgnoreTypeDemoBeanTest {
    private ObjectMapper objectMapper ;
    @Before
    public void setUp() throws Exception{
        objectMapper = new ObjectMapper();
    }
    @After
    public void tearDown() throws Exception{
        objectMapper = null;
    }
    @Test
    public void testSerializingWithJsonIgnoreType()
            throws JsonProcessingException {
        String jsonString = objectMapper.writeValueAsString(new IgnoreTypeDemoBean());
        System.out.println(jsonString);
        assertThat(jsonString, containsString("James Clark"));
        assertThat(jsonString, not(containsString("doorNumber")));
    }

    @Test
    public void testDeSerializingWithJsonIgnoreType() throws IOException {
        String jsonString = "{\"personId\": 123,\"name\": \"Mary Parker\",\"address\": {\"doorNumber\": \"123\",\"streetName\": \"Phase 1\",\"pincode\": \"123456\",\"city\": \"New York\"}}";
        ObjectMapper mapper = new ObjectMapper();
        IgnoreTypeDemoBean bean = objectMapper.readValue(jsonString, IgnoreTypeDemoBean.class);
        System.out.println(bean);
        assertThat(bean.name, is(equalTo("Mary Parker")));
        assertThat(bean.address.doorNumber, is(not(equalTo("123"))));
    }
}


The output of running the test in IntelliJ is this.

@JsonAutoDetect

The @JsonAutoDetect annotation is used at the class level to tell Jackson to override the visibility of the properties of a class during serialization and deserialization. You can set the visibility with the following elements:

  • creatorVisibility
  • fieldVisibility
  • getterVisibility
  • setterVisibility
  • isGetterVisibility

The JsonAutoDetect class defines public static constants that are similar to Java class visibility levels. They are:

  • ANY
  • DEFAULT
  • NON_PRIVATE
  • NONE
  • PROTECTED_AND_PRIVATE
  • PUBLIC_ONLY

Let us consider an example of Java class that uses the @JsonAutoDetect annotation.

AutoDetectDemoBean.java

package guru.springframework.blog.jsonannotation.domain.serializationanddeserialization;

import com.fasterxml.jackson.annotation.JsonAutoDetect;

@JsonAutoDetect(fieldVisibility = JsonAutoDetect.Visibility.ANY)
public class AutoDetectDemoBean {
    private long personId = 123L;
    private String  name = "James Clark";

    public long getPersonId() {
        return personId;
    }

    public void setPersonId(long personId) {
        this.personId = personId;
    }

    public String getName() {
        return name;
    }

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

    @Override
    public String toString() {
        return "IgnoreDemoBean{" +
                "personId=" + personId +
                ", name='" + name + '\'' +
                '}';
    }
}


The test code to test the @JsonAutoDetect annotation is this.

@Test
public void testSerializingWithJsonAutoDetect()
    throws JsonProcessingException {
        String jsonString = objectMapper.writeValueAsString(new AutoDetectDemoBean());
        System.out.println(jsonString);
        assertThat(jsonString, containsString("123"));
        assertThat(jsonString, containsString("James Clark"));
}


The output of running the test in IntelliJ is this.

Connect any Java based application to your SaaS data.  Over 100+ Java-based data source connectors.

Topics:
java ,jackson ,annotations ,json ,tutorial ,serialization and deserialization

Published at DZone with permission of

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}