Created
April 23, 2015 23:35
-
-
Save robertrv/bbc96c7253ea29910528 to your computer and use it in GitHub Desktop.
Sample to show [jackson-datatype-jsr310] Error while using InstantDeserializer (#23)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.example; | |
import static com.fasterxml.jackson.annotation.JsonAutoDetect.Visibility.ANY; | |
import static com.fasterxml.jackson.annotation.JsonAutoDetect.Visibility.NONE; | |
import java.time.Instant; | |
import java.time.LocalDateTime; | |
import java.util.Objects; | |
import com.fasterxml.jackson.annotation.JsonAutoDetect; | |
import com.fasterxml.jackson.annotation.JsonCreator; | |
import com.fasterxml.jackson.annotation.JsonIgnore; | |
import com.fasterxml.jackson.annotation.JsonProperty; | |
import com.fasterxml.jackson.databind.annotation.JsonDeserialize; | |
import com.fasterxml.jackson.databind.annotation.JsonSerialize; | |
import com.fasterxml.jackson.datatype.jsr310.deser.InstantDeserializer; | |
import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateTimeDeserializer; | |
import com.fasterxml.jackson.datatype.jsr310.ser.InstantSerializer; | |
import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateTimeSerializer; | |
@JsonAutoDetect(fieldVisibility = ANY, getterVisibility = NONE, setterVisibility = NONE) | |
public class SimplePojo { | |
@JsonProperty("name") | |
private final String name; | |
@JsonProperty("local") | |
@JsonSerialize(using=LocalDateTimeSerializer.class, include=JsonSerialize.Inclusion.NON_NULL) | |
@JsonDeserialize(using=LocalDateTimeDeserializer.class) | |
private final LocalDateTime local; | |
@JsonIgnore | |
@JsonProperty("instant") | |
@JsonSerialize(using=InstantSerializer.class, include=JsonSerialize.Inclusion.NON_NULL) | |
@JsonDeserialize(using=InstantDeserializer.class) | |
private final Instant instant; | |
@JsonCreator | |
public SimplePojo(@JsonProperty("name") String name, | |
@JsonProperty("local") LocalDateTime local, | |
@JsonProperty("instant") Instant instant) { | |
this.name = name; | |
this.local = local; | |
this.instant = instant; | |
} | |
public Instant getInstant() { | |
return instant; | |
} | |
public LocalDateTime getLocal() { | |
return local; | |
} | |
@Override | |
public boolean equals(Object obj) { | |
if (this == obj) { | |
return true; | |
} | |
if (obj == null || getClass() != obj.getClass()) { | |
return false; | |
} | |
final SimplePojo other = (SimplePojo) obj; | |
return Objects.equals(this.name, other.name) && | |
Objects.equals(this.instant, other.instant) && | |
Objects.equals(this.local, other.local); | |
} | |
@Override | |
public int hashCode() { | |
return 31 * super.hashCode() + Objects.hash(name, instant, local); | |
} | |
@Override | |
public String toString() { | |
return "SimplePojo{" + | |
"name='" + name + '\'' + | |
", local=" + local + | |
", instant=" + instant + | |
'}'; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.example; | |
import static org.assertj.core.api.Assertions.assertThat; | |
import java.time.Instant; | |
import java.time.LocalDateTime; | |
import org.assertj.core.api.Assertions; | |
import org.junit.Test; | |
import com.fasterxml.jackson.databind.ObjectMapper; | |
public class SimplePojoTest { | |
private ObjectMapper mapper = new ObjectMapper(); | |
@Test | |
public void testGetInitial() throws Exception { | |
SimplePojo pojo = new SimplePojo("name", LocalDateTime.now(), Instant.now()); | |
Assertions.assertThat(pojo.getInstant()).isGreaterThan(Instant.MIN); | |
} | |
@Test | |
public void testSerialization() throws Exception { | |
SimplePojo orig = new SimplePojo("name", LocalDateTime.now(), Instant.now()); | |
SimplePojo deserialized = mapper.readValue(mapper.writeValueAsString(orig), SimplePojo.class); | |
assertThat(deserialized).isEqualTo(orig); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment