Last active
October 22, 2015 21:48
-
-
Save david-bakin/75451c5f772686cb3c2c to your computer and use it in GitHub Desktop.
Illustrates two problems with Jackson :jackson-datatype-jsr310 v2.6.2 when dealing with java.time.Instant.
This file contains hidden or 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.bakins_bits; | |
import static net.javacrumbs.jsonunit.fluent.JsonFluentAssert.assertThatJson; | |
import static org.assertj.core.api.StrictAssertions.assertThat; | |
import static org.assertj.core.api.StrictAssertions.fail; | |
import java.io.IOException; | |
import java.text.DateFormat; | |
import java.text.SimpleDateFormat; | |
import java.time.Instant; | |
import java.time.ZoneOffset; | |
import java.util.Date; | |
import java.util.Locale; | |
import java.util.TimeZone; | |
import org.testng.annotations.Test; | |
import com.fasterxml.jackson.annotation.JsonFormat; | |
import com.fasterxml.jackson.databind.ObjectMapper; | |
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule; | |
public class TestDateFormat | |
{ | |
public class HasInstantWithFormat | |
{ | |
private Instant a; | |
@JsonFormat(pattern = "yyyy-MM-dd'T'HH:mm:ss.SSSXXX", timezone = "Z") | |
public Instant getA() { | |
return a; | |
} | |
public void setA(Instant a) { | |
this.a = a; | |
} | |
} | |
@Test(enabled = true) | |
public void serialize_an_instant_via_JsonFormat_annotation() { | |
// ARRANGE | |
ObjectMapper mapper = new ObjectMapper(); | |
mapper.registerModule(new JavaTimeModule()); | |
HasInstantWithFormat sut = new HasInstantWithFormat(); | |
sut.setA(Instant.parse("2015-07-23T01:02:03.123Z")); | |
// ACT | |
String actual = null; | |
try { | |
actual = mapper.writeValueAsString(sut); | |
} | |
catch (IOException e) { | |
fail("failed to serialize", e); | |
// Possibly related to missing a timezone on the datetimeformatter? | |
// See http://stackoverflow.com/a/27483371/751579 | |
} | |
// ASSERT | |
String expected = "{\"a\":\"2015-07-23T01:02:03.123Z\"}"; | |
assertThatJson(actual).isEqualTo(expected); | |
} | |
public class HasInstantWithoutFormat | |
{ | |
private Instant a; | |
public Instant getA() { | |
return a; | |
} | |
public void setA(Instant a) { | |
this.a = a; | |
} | |
} | |
@Test(enabled = true) | |
public void serialize_an_instant_via_configured_mapper() { | |
// ARRANGE | |
ObjectMapper mapper = new ObjectMapper(); | |
mapper.registerModule(new JavaTimeModule()); | |
SimpleDateFormat formatter = (SimpleDateFormat) DateFormat.getDateTimeInstance(DateFormat.SHORT, | |
DateFormat.SHORT, Locale.US); | |
formatter.applyPattern("yyyy-MM-dd'T'HH:mm:ss.SSSXXX"); | |
formatter.setTimeZone(TimeZone.getTimeZone(ZoneOffset.UTC)); | |
mapper.setDateFormat(formatter).setTimeZone(TimeZone.getTimeZone(ZoneOffset.UTC)); | |
HasInstantWithoutFormat sut1 = new HasInstantWithoutFormat(); | |
HasInstantWithoutFormat sut2 = new HasInstantWithoutFormat(); | |
HasInstantWithoutFormat sut3 = new HasInstantWithoutFormat(); | |
sut1.setA(Instant.parse("2015-07-23T01:02:03.123Z")); | |
sut2.setA(Instant.parse("2015-07-23T01:02:03.5Z")); | |
sut3.setA(Instant.parse("2015-07-23T01:02:03.000Z")); | |
// ACT | |
String actual1 = null; | |
String actual2 = null; | |
String actual3 = null; | |
try { | |
actual1 = mapper.writeValueAsString(sut1); | |
actual2 = mapper.writeValueAsString(sut2); | |
actual3 = mapper.writeValueAsString(sut3); | |
} | |
catch (IOException e) { | |
fail("failed to serialize one or another", e); | |
} | |
// ASSERT | |
String expected1 = "{\"a\":\"2015-07-23T01:02:03.123Z\"}"; | |
String expected2 = "{\"a\":\"2015-07-23T01:02:03.500Z\"}"; | |
String expected3 = "{\"a\":\"2015-07-23T01:02:03.000Z\"}"; | |
assertThatJson(actual1).isEqualTo(expected1); | |
assertThatJson(actual2).isEqualTo(expected2); // OK, formats 500 ms as .500 | |
// The formatter formats 0ms as .000 but Jackson doesn't | |
assertThat(formatter.format(Date.from(sut3.getA()))).isEqualTo("2015-07-23T01:02:03.000Z"); | |
assertThatJson(actual3).isEqualTo(expected3); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment