We have a Recording
class which has a Java 8 java.time.LocalDate
property. We need to deserialize and serialize this property from and to JSON string. To do this we can use the @JsonDeserialize
and @JsonSerialize
annotations to annotate the LocalDate
property of the Recording
class.
@JsonDeserialize(using = LocalDateDeserializer.class)
@JsonSerialize(using = LocalDateSerializer.class)
private LocalDate releaseDate;
To use the annotation we need to create a class to deserialize and serialize the value. To create a deserializer class we create a class that extends StdDeserializer
. The serializer class extends the StdSerializer
class. Below is the definition of the LocalDateSerializer
and LocalDateDeserializer
class.
package org.kodejava.jackson;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.SerializerProvider;
import com.fasterxml.jackson.databind.ser.std.StdSerializer;
import java.io.IOException;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
public class LocalDateSerializer extends StdSerializer<LocalDate> {
public LocalDateSerializer() {
super(LocalDate.class);
}
@Override
public void serialize(LocalDate value, JsonGenerator generator, SerializerProvider provider) throws IOException {
generator.writeString(value.format(DateTimeFormatter.ISO_LOCAL_DATE));
}
}
package org.kodejava.jackson;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.deser.std.StdDeserializer;
import java.io.IOException;
import java.time.LocalDate;
public class LocalDateDeserializer extends StdDeserializer<LocalDate> {
protected LocalDateDeserializer() {
super(LocalDate.class);
}
@Override
public LocalDate deserialize(JsonParser parser, DeserializationContext context) throws IOException {
return LocalDate.parse(parser.readValueAs(String.class));
}
}
Let’s create a simple class that convert Recording
object into JSON string and apply the date formatter defined in the LocalDateSerializer
class.
package org.kodejava.jackson;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.kodejava.jackson.support.Recording;
import java.time.LocalDate;
import java.time.Month;
public class RecordingToJson {
public static void main(String[] args) {
Recording recording = new Recording();
recording.setId(1L);
recording.setTitle("Twist and Shout");
recording.setReleaseDate(LocalDate.of(1964, Month.FEBRUARY, 3));
ObjectMapper mapper = new ObjectMapper();
try {
String json = mapper.writeValueAsString(recording);
System.out.println("JSON = " + json);
} catch (JsonProcessingException e) {
e.printStackTrace();
}
}
}
The output of the code snippet above is:
JSON = {"id":1,"title":"Twist and Shout","releaseDate":"1964-02-03"}
And here is the complete definition of the Recording
class.
package org.kodejava.jackson.support;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import org.kodejava.jackson.LocalDateDeserializer;
import org.kodejava.jackson.LocalDateSerializer;
import java.time.LocalDate;
public class Recording {
private Long id;
private String title;
@JsonDeserialize(using = LocalDateDeserializer.class)
@JsonSerialize(using = LocalDateSerializer.class)
private LocalDate releaseDate;
// Getters and Setters
@Override
public String toString() {
return "Recording{" +
"id=" + id +
", title='" + title + '\'' +
", releaseDate=" + releaseDate +
'}';
}
}
Maven Dependencies
<dependencies>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.17.1</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>2.17.1</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.17.1</version>
</dependency>
</dependencies>