Map Serialization and Deserialization with Jackson

1. Overview

In this article, we’ll look at serialization and deserialization of Java maps using Jackson.

We’ll illustrate how to serialize and deserialize Map<String, String>Map<Object, String>, and Map<Object, Object> to and from JSON-formatted Strings.

2. Maven Configuration

1
2
3
4
5
<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-databind</artifactId>
    <version>2.11.1</version>
</dependency>

You can get the latest version of Jackson here.

3. Serialization

Serialization converts a Java object into a stream of bytes, which can be persisted or shared as needed. Java Maps are collections which map a key Object to a value Object and are often the least intuitive objects to serialize.

3.1. Map<String, String> Serialization

For the simple case, let’s create a Map<String, String> and serialize it to JSON:

1
2
3
4
5
6
Map<String, String> map = new HashMap<>();
map.put("key", "value");
 
ObjectMapper mapper = new ObjectMapper();
String jsonResult = mapper.writerWithDefaultPrettyPrinter()
  .writeValueAsString(map);

ObjectMapper is Jackson’s serialization mapper, which allows us to serialize our map and write it out as a pretty-printed JSON String, using the toString() method in String:

1
2
3
{
  "key" : "value"
}

3.2. Map<Object, String> Serialization

You can serialize a map containing a custom Java class with a few extra steps. Let’s create a MyPair class to represent a pair of related String objects.

Note: the getters/setters should be public, and we annotate toString() with @JsonValue to ensure Jackson uses this custom toString() when serializing:

1
2
3
4
5
6
7
8
9
10
11
12
13
public class MyPair {
 
    private String first;
    private String second;
     
    @Override
    @JsonValue
    public String toString() {
        return first + " and " + second;
    }
  
    // standard getter, setters, equals, hashCode, constructors
}

Now let’s tell Jackson how to serialize MyPair by extending Jackson’s JsonSerializer:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
public class MyPairSerializer extends JsonSerializer<MyPair> {
 
    private ObjectMapper mapper = new ObjectMapper();
 
    @Override
    public void serialize(MyPair value,
      JsonGenerator gen,
      SerializerProvider serializers)
      throws IOException, JsonProcessingException {
  
        StringWriter writer = new StringWriter();
        mapper.writeValue(writer, value);
        gen.writeFieldName(writer.toString());
    }
}

JsonSerializer, as the name suggests, serializes MyPair to JSON using MyPair‘s toString() method. Jackson provides many Serializer classes to fit your serialization requirements.

We apply MyPairSerializer to our Map<MyPair, String> with the @JsonSerialize annotation. Note that we’ve only told Jackson how to serialize MyPair because it already knows how to serialize String:

1
2
@JsonSerialize(keyUsing = MyPairSerializer.class)
Map<MyPair, String> map;

Let’s test our map serialization:

1
2
3
4
5
6
map = new HashMap<>();
MyPair key = new MyPair("Abbott", "Costello");
map.put(key, "Comedy");
 
String jsonResult = mapper.writerWithDefaultPrettyPrinter()
  .writeValueAsString(map);

The serialized JSON output is:

1
2
3
{
  "Abbott and Costello" : "Comedy"
}

3.3. Map<Object, Object> Serialization

The most complex case is serializing a Map<Object, Object>, but most of the work is already done. Let’s use Jackson’s MapSerializer for our map, and MyPairSerializer from the previous section for the map’s key and value types:

1
2
3
4
5
6
7
8
@JsonSerialize(keyUsing = MapSerializer.class)
Map<MyPair, MyPair> map;
     
@JsonSerialize(keyUsing = MyPairSerializer.class)
MyPair mapKey;
 
@JsonSerialize(keyUsing = MyPairSerializer.class)
MyPair mapValue;

Let’s test out serializing our Map<MyPair, MyPair>:

1
2
3
4
5
6
mapKey = new MyPair("Abbott", "Costello");
mapValue = new MyPair("Comedy", "1940s");
map.put(mapKey, mapValue);
 
String jsonResult = mapper.writerWithDefaultPrettyPrinter()
  .writeValueAsString(map);

The serialized JSON output, using MyPair‘s toString() method, is:

1
2
3
{
  "Abbott and Costello" : "Comedy and 1940s"
}

4. Deserialization

Deserialization converts a stream of bytes into a Java object that we can use in code. In this section, we’ll deserialize JSON input into Mapof different signatures.

4.1. Map<String, String> Deserialization

For the simple case, let’s take a JSON-formatted input string and convert it to a Map<String, String> Java collection:

1
2
3
4
String jsonInput = "{\"key\": \"value\"}";
TypeReference<HashMap<String, String>> typeRef
  = new TypeReference<HashMap<String, String>>() {};
Map<String, String> map = mapper.readValue(jsonInput, typeRef);

We use Jackson’s ObjectMapper as we did for serialization, using readValue() to process the input. Also, note our use of Jackson’s TypeReference, which we’ll use in all of our deserialization examples, to describe the type of our destination Map. Here is the toString() representation of our map:

1
{key=value}

4.2. Map<Object, String> Deserialization

Now, let’s change our input JSON and the TypeReference of our destination to Map<MyPair, String>:

1
2
3
4
5
String jsonInput = "{\"Abbott and Costello\" : \"Comedy\"}";
 
TypeReference<HashMap<MyPair, String>> typeRef
  = new TypeReference<HashMap<MyPair, String>>() {};
Map<MyPair,String> map = mapper.readValue(jsonInput, typeRef);

We need to create a constructor for MyPair that takes a String with both elements and parses them to the MyPair elements:

1
2
3
4
5
public MyPair(String both) {
    String[] pairs = both.split("and");
    this.first = pairs[0].trim();
    this.second = pairs[1].trim();
}

And the toString() of our Map<MyPair,String> object is:

1
{Abbott and Costello=Comedy}

There is another option for the case when we deserialize into a Java class that contains a Map — we can use Jackson’s KeyDeserializer class, one of many Deserialization classes that Jackson offers. We annotate our ClassWithAMap with @JsonCreator@JsonProperty, and @JsonDeserialize:

1
2
3
4
5
6
7
8
9
10
11
12
13
public class ClassWithAMap {
 
  @JsonProperty("map")
  @JsonDeserialize(keyUsing = MyPairDeserializer.class)
  private Map<MyPair, String> map;
 
  @JsonCreator
  public ClassWithAMap(Map<MyPair, String> map) {
    this.map = map;
  }
  
  // public getters/setters omitted
}

We are telling Jackson to deserialize the Map<MyPair, String> contained in ClassWithAMap, so we need to extend KeyDeserializer to describe how to deserialize the map’s key, a MyPair object, from an input String:

1
2
3
4
5
6
7
8
9
10
11
public class MyPairDeserializer extends KeyDeserializer {
 
  @Override
  public MyPair deserializeKey(
    String key,
    DeserializationContext ctxt) throws IOException,
    JsonProcessingException {
       
      return new MyPair(key);
    }
}

We test the deserialization out using readValue:

1
2
3
4
String jsonInput = "{\"Abbott and Costello\":\"Comedy\"}";
 
ClassWithAMap classWithMap = mapper.readValue(jsonInput,
  ClassWithAMap.class);

Again, the toString() method of our ClassWithAMap’s map gives us the output we expect:

1
{Abbott and Costello=Comedy}

4.3. Map<Object,Object> Deserialization

Lastly, let’s change our input JSON and the TypeReference of our destination to Map<MyPair, MyPair>:

1
2
3
4
String jsonInput = "{\"Abbott and Costello\" : \"Comedy and 1940s\"}";
TypeReference<HashMap<MyPair, MyPair>> typeRef
  = new TypeReference<HashMap<MyPair, MyPair>>() {};
Map<MyPair,MyPair> map = mapper.readValue(jsonInput, typeRef);

And the toString() of our Map<MyPair, MyPair> object is:

1
{Abbott and Costello=Comedy and 1940s}

5. Conclusion

In this quick tutorial, we’ve seen how to serialize and deserialize Java Maps to and from JSON-formatted Strings.

As always, you can check out the example provided in this article in the GitHub repository.

Related posts:

Using Spring ResponseEntity to Manipulate the HTTP Response
Java Program to Implement Hash Tables with Double Hashing
Spring Data JPA @Modifying Annotation
Java Collections Interview Questions
Java Program to Check Whether an Input Binary Tree is the Sub Tree of the Binary Tree
Guava Collections Cookbook
Java Program to Implement Karatsuba Multiplication Algorithm
Mapping Nested Values with Jackson
Java Program to Implement SimpeBindings API
Jackson JSON Views
Java Program to Find a Good Feedback Edge Set in a Graph
Spring MVC Async vs Spring WebFlux
Java Program to Generate Date Between Given Range
Spring Cloud AWS – RDS
Java Program to Implement WeakHashMap API
Lớp Collectors trong Java 8
Java Program to Find SSSP (Single Source Shortest Path) in DAG (Directed Acyclic Graphs)
Spring Data MongoDB Transactions
Why String is Immutable in Java?
Java Program to Describe the Representation of Graph using Incidence Matrix
Transactions with Spring and JPA
Intro to Spring Boot Starters
HashSet trong Java hoạt động như thế nào?
Giới thiệu Swagger – Công cụ document cho RESTfull APIs
Java Program to Implement Circular Singly Linked List
Converting between an Array and a List in Java
Java Program to Implement Direct Addressing Tables
Spring Boot - Rest Controller Unit Test
Java Program to Perform String Matching Using String Library
Hướng dẫn sử dụng luồng vào ra nhị phân trong Java
Java Program to Implement the Binary Counting Method to Generate Subsets of a Set
Spring Boot - Enabling Swagger2