Skip to content

Instantly share code, notes, and snippets.

@mageddo
Created April 7, 2018 00:10
Show Gist options
  • Select an option

  • Save mageddo/8b81fae7462c391dc5af7ff0cb71d444 to your computer and use it in GitHub Desktop.

Select an option

Save mageddo/8b81fae7462c391dc5af7ff0cb71d444 to your computer and use it in GitHub Desktop.
Custom Jackon Serializer / Deserializer With Nullable Fields
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.JsonToken;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializerProvider;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import org.junit.Test;
import java.io.IOException;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.util.Objects;
import static org.junit.Assert.assertEquals;
public class JacksonCustomSerializerTest {
@Test
public void shouldSerializeWithNullFieldValue() throws Throwable {
assertEquals("{\"creation\":null}", mapper().writeValueAsString(new Person()));
}
@Test
public void shouldSerializeWithNotNullFieldValue() throws Throwable {
assertEquals(
"{\"creation\":1483288200000}",
mapper().writeValueAsString(new Person(LocalDateTime.of(2017, 1,1,16,30)))
);
}
@Test
public void shouldDserializeWithNullFieldValue() throws Throwable {
assertEquals(new Person(), mapper().readValue("{\"creation\":null}", Person.class));
}
@Test
public void shouldDeserializeWithNotNullFieldValue() throws Throwable {
assertEquals(
new Person(LocalDateTime.of(2017, 1,1,16,30)),
mapper().readValue("{\"creation\":1483288200000}", Person.class)
);
}
public ObjectMapper mapper() {
return new ObjectMapper();
}
static class Person {
public Person() {
}
public Person(LocalDateTime creation) {
this.creation = creation;
}
@JsonSerialize(using = LocalDateTimeSerializer.class)
@JsonDeserialize(using = LocalDateTimeDeserializer.class)
public LocalDateTime creation;
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Person person = (Person) o;
return Objects.equals(creation, person.creation);
}
@Override
public int hashCode() {
return Objects.hash(creation);
}
}
static class LocalDateTimeSerializer extends JsonSerializer<LocalDateTime> {
@Override
public void serialize(LocalDateTime value, JsonGenerator gen, SerializerProvider serializers) throws IOException {
if(value != null){
gen.writeNumber(value.toInstant(ZoneOffset.UTC).toEpochMilli());
}
}
}
static class LocalDateTimeDeserializer extends JsonDeserializer<LocalDateTime> {
@Override
public LocalDateTime deserialize(JsonParser p, DeserializationContext ctxt) throws IOException, JsonProcessingException {
if(p.currentToken() == JsonToken.VALUE_NULL){
return null;
}
return LocalDateTime.ofInstant(Instant.ofEpochMilli(p.getLongValue()), ZoneOffset.UTC);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment