Reputation: 2715
I am trying to serialize my myBeanObject using jackson Objectmapper. This is what I have:
Objectmapper m= new Objectmapper()
m.setdateFormat(new SimpledateFormat("yyyy-MM-dd"));
String json = m.writeValueAsString(myBeanObject);
Problem is a LocalDate variable in my bean is getting serialised into a long String, something like {"year":1970,"month":"JANUARY"...}
I want the LocalDate to become simple like "1970-01-01"
Can someone please help?
Upvotes: 1
Views: 5348
Reputation: 212
If you don't want to add additional dependencies to your project, you can create a module for ObjectMapper
yourself. Here is an example:
package com.yourexample.serialization
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.Version;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializerProvider;
import com.fasterxml.jackson.databind.module.SimpleModule;
import com.fasterxml.jackson.databind.ser.std.StdSerializer;
import java.io.IOException;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import org.junit.Assert;
import org.junit.Test;
public class NoDependenciesTest {
@Test
public void testSerialization() throws JsonProcessingException {
TestUser user = new TestUser("John Doe", LocalDate.of(2022, 12, 11));
String json = writeToJson(user);
Assert.assertEquals("{\"name\":\"John Doe\",\"birthDate\":\"2022-12-11\"}", json);
}
public static String writeToJson(Object object) throws JsonProcessingException {
ObjectMapper mapper = new ObjectMapper();
SimpleModule module = new SimpleModule("LocalDateSerializer", new Version(1, 0, 0, null, null, null));
module.addSerializer(LocalDate.class, new LocalDateSerializer());
mapper.registerModule(module);
return mapper.writeValueAsString(object);
}
private static class LocalDateSerializer extends StdSerializer<LocalDate> {
private static final long serialVersionUID = -1203520161919841191L;
protected LocalDateSerializer() {
super(LocalDate.class);
}
@Override
public void serialize(LocalDate date, JsonGenerator generator, SerializerProvider serializerProvider)
throws IOException {
if (date != null) {
generator.writeString(date.format(DateTimeFormatter.ISO_LOCAL_DATE));
}
}
}
private static class TestUser {
private String name;
private java.time.LocalDate birthDate;
public TestUser(String name, java.time.LocalDate birthDate) {
this.name = name;
this.birthDate = birthDate;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public java.time.LocalDate getBirthDate() {
return birthDate;
}
public void setBirthDate(java.time.LocalDate birthDate) {
this.birthDate = birthDate;
}
}
}
Upvotes: 0
Reputation: 12665
Try to add this dependency:
<dependency>
<groupId>com.fasterxml.jackson.datatype</groupId>
<artifactId>jackson-datatype-jsr310</artifactId>
<version>2.8.6</version>
</dependency>
Then setup your ObjectMapper
as follows:
ObjectMapper mapper = new ObjectMapper();
mapper.registerModule(new JavaTimeModule());
mapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
Upvotes: 3