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.15.2</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>2.15.2</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.15.2</version>
</dependency>
</dependencies>
- How do I create a string of repeated characters? - September 1, 2023
- How do I convert datetime string with optional part to a date object? - August 28, 2023
- How do I split large excel file into multiple smaller files? - April 15, 2023
Hi Wayan,
Can you please give an example for how to compare two excel files? I have two files. One file is new and one is old backup file. So I want to compare my new with old. I want to compare with the value what I am passing as a key (here key I am taking as a second column values of excel sheet).
Could you please guide how we can format date like yyyy-MM-dd HH:mm:ss ?
Hi Savani,
You can use
DateTimeFormatter.ofPattern(...)
method and pass the date and time pattern that you want, likeyyyy-MM-dd HH:mm:ss
in your case. If you want to use date and time you might need the change theLocalDate
in the code snippet above and use theLocalDateTime
instead.