Jackson - List, Set and Map Serialization and Deserialization in Java Examples

In our previous article, we have learned how to use the Jackson API for binding JSON data to Plain Old Java Object (POJO) and vice versa.
In this article, I show you how to use the Jackson API to serialize and deserialize the java collection objects such as List, Set, and Map.

Table of contents

  1. Using Jackson API for List Serialization
  2. Using Jackson API for Set Serialization
  3. Using Jackson API for Map Serialization
  4. Using Jackson API for List Deserialization
  5. Using Jackson API for Set Deserialization
  6. Using Jackson API for Map Deserialization

Dependencies

Let’s first add the following dependencies to the pom.xml:
<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-databind</artifactId>
    <version>2.9.8</version>
</dependency>
This dependency will also transitively add the following libraries to the classpath:
  • jackson-annotations-2.9.8.jar
  • jackson-core-2.9.8.jar
  • jackson-databind-2.9.8.jar
Always use the latest versions on the Maven central repository for Jackson databind.

1. Using Jackson API for List Serialization

Let's demonstrates how to convert List object to JSON is using the ObjectMapper.writeValueAsString() method.

JacksonListToJson

package net.javaguides.jackson;

import java.util.ArrayList;
import java.util.List;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;

/**
 * Using Jackson API for list serialization and deserialization
 * @author ramesh fadatare
 *
 */
public class JacksonListToJson {
    public static void main(String[] args) throws JsonProcessingException {

        // Create ObjectMapper object.
        ObjectMapper mapper = new ObjectMapper();
        mapper.enable(SerializationFeature.INDENT_OUTPUT);

        List < String > progLangs = new ArrayList < > ();
        progLangs.add("C");
        progLangs.add("C++");
        progLangs.add("Java");
        progLangs.add("Java EE");
        progLangs.add("Python");
        progLangs.add("Scala");
        progLangs.add("JavaScript");
        // Serialize Object to JSON.
        String json = mapper.writeValueAsString(progLangs);

        // Print json
        System.out.println(json);
    }
}
Output:
[ "C", "C++", "Java", "Java EE", "Python", "Scala", "JavaScript" ]

2. Using Jackson API for Set Serialization

Let's demonstrates how to convert a Set object to JSON is using the ObjectMapper.writeValueAsString() method.

JacksonSetToJson.java

package net.javaguides.jackson;

import java.util.HashSet;
import java.util.Set;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;

public class JacksonSetToJson {
    public static void main(String[] args) throws JsonProcessingException {
        // Create ObjectMapper object.
        ObjectMapper mapper = new ObjectMapper();
        mapper.enable(SerializationFeature.INDENT_OUTPUT);

        Set < String > progLangs = new HashSet < > ();
        progLangs.add("C");
        progLangs.add("C++");
        progLangs.add("Java");
        progLangs.add("Java EE");
        progLangs.add("Python");
        progLangs.add("Scala");
        progLangs.add("JavaScript");
        // Serialize Object to JSON.
        String json = mapper.writeValueAsString(progLangs);

        // Print json
        System.out.println(json);
    }
}
Output:
[ "Java EE", "Java", "C++", "C", "Scala", "JavaScript", "Python" ]

3. Using Jackson API for Map Serialization

Let's demonstrates how to convert a Map Java object to JSON is using the ObjectMapper.writeValueAsString() method.

JacksonMapToJson.java

package net.javaguides.jackson;

import java.util.Calendar;
import java.util.HashMap;
import java.util.Map;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;

public class JacksonMapToJson {

    public static void main(String[] args) throws JsonProcessingException {

        ObjectMapper mapper = new ObjectMapper();
        mapper.enable(SerializationFeature.INDENT_OUTPUT);

        Map < String, Integer > days = new HashMap < > ();
        days.put("MON", Calendar.MONDAY);
        days.put("TUE", Calendar.TUESDAY);
        days.put("WED", Calendar.WEDNESDAY);
        days.put("THU", Calendar.THURSDAY);
        days.put("FRI", Calendar.FRIDAY);
        days.put("SAT", Calendar.SATURDAY);
        days.put("SUN", Calendar.SUNDAY);

        String json = mapper.writeValueAsString(days);
        System.out.println(json);
    }
}
Output:
{
  "THU" : 5,
  "TUE" : 3,
  "WED" : 4,
  "SAT" : 7,
  "FRI" : 6,
  "MON" : 2,
  "SUN" : 1
}

4. Using Jackson API for List Deserialization

We can use the ObjectMapper.readValue() method for converting a JSON text to collection object.
The following example demonstrates how to convert the JSON text to List.

JacksonJsonToList.java

package net.javaguides.jackson;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;

public class JacksonJsonToList {

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

        // Create ObjectMapper object.
        ObjectMapper mapper = new ObjectMapper();
        mapper.enable(SerializationFeature.INDENT_OUTPUT);

        String json = "[ \"C\", \"C++\", \"Java\", \"Java EE\", \"Python\", \"Scala\", \"JavaScript\" ]";

        List < String > progLangs = new ArrayList < > ();
        progLangs = mapper.readValue(json, List.class);

        for (Iterator < String > iterator = progLangs.iterator(); iterator.hasNext();) {
            String progLang = (String) iterator.next();
            System.out.println(progLang);
        }
    }
}
Output:
C
C++
Java
Java EE
Python
Scala
JavaScript

5. Using Jackson API for Set Deserialization

We can use the ObjectMapper.readValue() method for converting JSON text to Set object.
The following example demonstrates how to convert the JSON text to Set.

JacksonJsonToSet.java

package net.javaguides.jackson;

import java.io.IOException;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;

import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;

public class JacksonJsonToSet {

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

        // Create ObjectMapper object.
        ObjectMapper mapper = new ObjectMapper();
        mapper.enable(SerializationFeature.INDENT_OUTPUT);

        String json = "[ \"C\", \"C++\", \"Java\", \"Java EE\", \"Python\", \"Scala\", \"JavaScript\" ]";

        Set < String > progLangs = new HashSet < > ();
        progLangs = mapper.readValue(json, Set.class);

        for (Iterator < String > iterator = progLangs.iterator(); iterator.hasNext();) {
            String progLang = (String) iterator.next();
            System.out.println(progLang);
        }
    }
}
Output:
Java EE
Java
C++
C
Scala
JavaScript
Python

6. Using Jackson API for Map Deserialization

We can use the ObjectMapper.readValue() method for converting a JSON text to a Map object.
The following example demonstrates how to convert the JSON text to Map.

JacksonJsonToMap.java

package net.javaguides.jackson;

import java.io.IOException;
import java.util.Map;
import java.util.Map.Entry;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;

public class JacksonJsonToMap {
    public static void main(String[] args) throws IOException {

        ObjectMapper mapper = new ObjectMapper();
        mapper.enable(SerializationFeature.INDENT_OUTPUT);

        String json = "{" +
            "  \"THU\" : 5," +
            "  \"TUE\" : 3," +
            "  \"WED\" : 4," +
            "  \"SAT\" : 7," +
            "  \"FRI\" : 6," +
            "  \"MON\" : 2," +
            "  \"SUN\" : 1" +
            "}";

        @SuppressWarnings("unchecked")
        Map < String, Integer > days = mapper.readValue(json, Map.class);

        for (Entry < String, Integer > day: days.entrySet()) {
            System.out.println(day.getKey() + "=" + day.getValue());
        }
    }
}
Output:
THU=5
TUE=3
WED=4
SAT=7
FRI=6
MON=2
SUN=1

Comments