Jackson and JSON in Java, finally learn with a coding-first approach:
>> Download the eBookMocking is an essential part of unit testing, and the Mockito library makes it easy to write clean and intuitive unit tests for your Java code.
Get started with mocking and improve your application tests using our Mockito guide:
Handling concurrency in an application can be a tricky process with many potential pitfalls. A solid grasp of the fundamentals will go a long way to help minimize these issues.
Get started with understanding multi-threaded applications with our Java Concurrency guide:
Spring 5 added support for reactive programming with the Spring WebFlux module, which has been improved upon ever since. Get started with the Reactor project basics and reactive programming in Spring Boot:
Since its introduction in Java 8, the Stream API has become a staple of Java development. The basic operations like iterating, filtering, mapping sequences of elements are deceptively simple to use.
But these can also be overused and fall into some common pitfalls.
To get a better understanding on how Streams work and how to combine them with other language features, check out our guide to Java Streams:
Get started with Spring and Spring Boot, through the Learn Spring course:
>> LEARN SPRINGExplore Spring Boot 3 and Spring 6 in-depth through building a full REST API with the framework:
Yes, Spring Security can be complex, from the more advanced functionality within the Core to the deep OAuth support in the framework.
I built the security material as two full courses - Core and OAuth, to get practical with these more complex scenarios. We explore when and how to use each feature and code through it on the backing project.
You can explore the course here:
Spring Data JPA is a great way to handle the complexity of JPA with the powerful simplicity of Spring Boot.
Get started with Spring Data JPA through the guided reference course:
Refactor Java code safely β and automatically β with OpenRewrite.
Refactoring big codebases by hand is slow, risky, and easy to put off. Thatβs where OpenRewrite comes in. The open-source framework for large-scale, automated code transformations helps teams modernize safely and consistently.
Each month, the creators and maintainers of OpenRewrite at Moderne run live, hands-on training sessions β one for newcomers and one for experienced users. Youβll see how recipes work, how to apply them across projects, and how to modernize code with confidence.
Join the next session, bring your questions, and learn how to automate the kind of work that usually eats your sprint time.
1. Overview
In this quick tutorial, weβll look at the 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.
Further reading:
Jackson β Working With Maps and Nulls
How To Serialize and Deserialize Enums with Jackson
XML Serialization and Deserialization with Jackson
2. Maven Configuration
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.17.2</version>
</dependency>
We 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 that map a key Object to a value Object, and are often the least intuitive objects to serialize.
3.1. Map<String, String> Serialization
For a simple case, letβs create a Map<String, String> and serialize it to JSON:
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. It allows us to serialize our map, and write it out as a pretty-printed JSON String using the toString() method in String:
{
"key" : "value"
}
3.2. Map<Object, String> Serialization
With a few extra steps, we can also serialize a map containing a custom Java class. 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:
public class MyPair {
private String first;
private String second;
@Override
@JsonValue
public String toString() {
return first + " and " + second;
}
// standard getter, setters, equals, hashCode, constructors
}
Then weβll tell Jackson how to serialize MyPair by extending Jacksonβs JsonSerializer:
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. Furthermore, Jackson provides many Serializer classes to fit our serialization requirements.
Next 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:
@JsonSerialize(keyUsing = MyPairSerializer.class)
Map<MyPair, String> map;
Then letβs test our map serialization:
map = new HashMap<>();
MyPair key = new MyPair("Abbott", "Costello");
map.put(key, "Comedy");
String jsonResult = mapper.writerWithDefaultPrettyPrinter()
.writeValueAsString(map);
The serialized JSON output is:
{
"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:
@JsonSerialize(keyUsing = MapSerializer.class)
Map<MyPair, MyPair> map;
@JsonSerialize(keyUsing = MyPairSerializer.class)
MyPair mapKey;
@JsonSerialize(keyUsing = MyPairSerializer.class)
MyPair mapValue;
Then letβs test out serializing our Map<MyPair, MyPair>:
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:
{
"Abbott and Costello" : "Comedy and 1940s"
}
3.4. @JsonKey Annotation
While creating a Map, an object could be a key or a value. Further, we might need different serialization strategies when an object appears as a key in a Map vs. when it appears as a value. So, letβs learn how we can use the @JsonKey annotation to do so.
Letβs start by defining the Fruit class with two members, namely variety and name:
public class Fruit {
public String variety;
@JsonKey
public String name;
public Fruit(String variety, String name) {
this.variety = variety;
this.name = name;
}
@JsonValue
public String getFullName() {
return this.variety + " " + this.name;
}
}
We must note that we want to use its name for serialization whenever a Fruit object appears as a key in a Map. However, when it appears as a value, we want to use its name along with the variety.
Now, letβs initialize two instances of the Fruit class and a single instance of the ObjectMapper class for serialization purposes:
private static final Fruit FRUIT1 = new Fruit("Alphonso", "Mango");
private static final Fruit FRUIT2 = new Fruit("Black", "Grapes");
private static final ObjectMapper OBJECT_MAPPER = new ObjectMapper();
Next, we should remember that when serializing a standalone instance, the @JsonValue property is used for serialization. Letβs verify this for the two objects:
@Test
public void givenObject_WhenSerialize_ThenUseJsonValueForSerialization()
throws JsonProcessingException {
String serializedValueForFruit1 = OBJECT_MAPPER.writeValueAsString(FRUIT1);
Assertions.assertEquals("\"Alphonso Mango\"", serializedValueForFruit1);
String serializedValueForFruit2 = OBJECT_MAPPER.writeValueAsString(FRUIT2);
Assertions.assertEquals("\"Black Grapes\"", serializedValueForFruit2);
}
Further on, letβs serialize the selectionByFruit Map that contains instances of the Fruit class as a key:
@Test
public void givenMapWithObjectKeys_WhenSerialize_ThenUseJsonKeyForSerialization()
throws JsonProcessingException {
// Given
Map<Fruit, String> selectionByFruit = new LinkedHashMap<>();
selectionByFruit.put(FRUIT1, "Hagrid");
selectionByFruit.put(FRUIT2, "Hercules");
// When
String serializedValue = OBJECT_MAPPER.writeValueAsString(selectionByFruit);
// Then
Assertions.assertEquals("{\"Mango\":\"Hagrid\",\"Grapes\":\"Hercules\"}", serializedValue);
}
As expected, the @JsonKey annotation is used for this serialization. Further, we shall note that weβve used LinkedHashMap to fix the order of keys during its access and serialization.
Finally, letβs also see the results of serializing the selectionByPerson Map that contains instances of the Fruit class as a value:
@Test
public void givenMapWithObjectValues_WhenSerialize_ThenUseJsonValueForSerialization()
throws JsonProcessingException {
// Given
Map<String, Fruit> selectionByPerson = new LinkedHashMap<>();
selectionByPerson.put("Hagrid", FRUIT1);
selectionByPerson.put("Hercules", FRUIT2);
// When
String serializedValue = OBJECT_MAPPER.writeValueAsString(selectionByPerson);
// Then
Assertions.assertEquals("{\"Hagrid\":\"Alphonso Mango\",\"Hercules\":\"Black Grapes\"}",
serializedValue);
}
Great! Weβve successfully switched serialization strategies for an object depending on its role as a key or a value in a Map.
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 Maps of different signatures.
4.1. Map<String, String> Deserialization
For a simple case, letβs take a JSON-formatted input string and convert it to a Map<String, String> Java collection:
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:
{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>:
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:
public MyPair(String both) {
String[] pairs = both.split("and");
this.first = pairs[0].trim();
this.second = pairs[1].trim();
}
The toString() of our Map<MyPair,String> object is:
{Abbott and Costello=Comedy}
There is another option when we deserialize into a Java class that contains a Map; we can use Jacksonβs KeyDeserializer class, one of the many Deserialization classes that Jackson offers. Letβs annotate our ClassWithAMap with @JsonCreator, @JsonProperty, and @JsonDeserialize:
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
}
Here weβre 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:
public class MyPairDeserializer extends KeyDeserializer {
@Override
public MyPair deserializeKey(
String key,
DeserializationContext ctxt) throws IOException,
JsonProcessingException {
return new MyPair(key);
}
}
Then we can test the deserialization using readValue:
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:
{Abbott and Costello=Comedy}
4.3. Map<Object,Object> Deserialization
Finally, letβs change our input JSON and the TypeReference of our destination to Map<MyPair, MyPair>:
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);
The toString() of our Map<MyPair, MyPair> object is:
{Abbott and Costello=Comedy and 1940s}
5. Conclusion
In this brief article, we learned how to serialize and deserialize Java Maps to and from JSON-formatted Strings.
