1.Introduction
In our previous tutorial, we serialized the DTO class field of type LocalDateTime to basic ISO date-time using DateTimeFormatter.ISO_DATE_TIME format. In this short tutorial, we'll show how to create a custom deserializer with Jackson that will deserialize received text to our field of type LocalDateTime.
If you want to know how to create a custom Jackson serializer, check out this guide.
Here, we'll create one class called Purchase. Then, we'll create PurchaseDTO class that will be served to clients. PurchaseDTO will have field purchaseDateTime of type LocalDateTime that we serialize to ISO date-time format. This format will be sent to our client that asked for PurchaseDTO object using, for example, GET request.
Since we want to work with LocalDateTime after the client sends us the instance of PurchaseDTO during, for example, a POST request, we'll also need to deserialize the field.
2. Implementation
We need to include Jackson in our project by adding it as a Maven dependency:
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.11.2</version>
</dependency>
Also, it's part of spring-boot-starter-web dependency, so if you already added it, there's no need to manually add jackson-databind dependency.
Let's create our Purchase class:
@Entity
public class Purchase {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private Long itemId;
private Long customerId;
private LocalDateTime purchaseDateTime;
// getters, setters, etc
}
And now, let's create our DTO class:
public class PurchaseDto {
@JsonSerialize(using = LocalDateTimeSerializer.class)
@JsonDeserialize(using = LocalDateTimeDeserializer.class)
private LocalDateTime purchaseDateTime;
private Long itemId;
// getters, setters
}
Notice the @JsonDeserialize annotation where we declared that we'll be using our custom deserializer named LocalDateTimeDeserializer.
And now, let's see how does our LocalDateTimeDeserializer class look like:
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.deser.std.StdDeserializer;
import java.io.IOException;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
public class LocalDateTimeDeserializer extends StdDeserializer<LocalDateTime> {
public LocalDateTimeDeserializer() {
super(LocalDateTime.class);
}
@Override
public LocalDateTime deserialize(JsonParser p, DeserializationContext ctxt) throws IOException, JsonProcessingException {
return LocalDateTime.parse(p.readValueAs(String.class), DateTimeFormatter.ISO_DATE_TIME);
}
}
For this example, we choose to use DateTimeFormatter.ISO_DATE_TIME format. You can check the available list of format types here.
3. Conclusion
In this short tutorial, we showed how to create a custom deserializer with Jackson. If you want to learn how to create a custom Jackson serializer, check out this tutorial.