Skip to content

Commit

Permalink
Disable Jackson SerializationFeature.WRITE_DURATIONS_AS_TIMESTAMPS by…
Browse files Browse the repository at this point in the history
… default

Jackson added a breaking change in 2.10 for the way Duration is serialized.
Before that the DurationSerializer was using SerializationFeature.WRITE_DATES_AS_TIMESTAMPS
to check whether Duration should be serialized as a timestamp or not.

Since 2.10 Jackson uses SerializationFeature.WRITE_DURATIONS_AS_TIMESTAMPS
to check whether Duration should be serialized as a timestamp or not.

This commit aligns the default for SerializationFeature.WRITE_DURATIONS_AS_TIMESTAMPS
with the default for SerializationFeature.WRITE_DATES_AS_TIMESTAMPS

The change in Jackson was done in FasterXML/jackson-modules-java8#75

fixes spring-projectsgh-19345
  • Loading branch information
filiphr committed Dec 11, 2019
1 parent 9136f54 commit e6c37a6
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ public class JacksonAutoConfiguration {
static {
Map<Object, Boolean> featureDefaults = new HashMap<>();
featureDefaults.put(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false);
featureDefaults.put(SerializationFeature.WRITE_DURATIONS_AS_TIMESTAMPS, false);
FEATURE_DEFAULTS = Collections.unmodifiableMap(featureDefaults);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import java.io.IOException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.time.Duration;
import java.util.Date;
import java.util.HashSet;
import java.util.Set;
Expand Down Expand Up @@ -378,6 +379,15 @@ void writeDatesAsTimestampsDefault() {
});
}

@Test
void writeDurationAsTimestampsDefault() {
this.contextRunner.run((context) -> {
ObjectMapper mapper = context.getBean(ObjectMapper.class);
Duration duration = Duration.ofHours(2);
assertThat(mapper.writeValueAsString(duration)).isEqualTo("\"PT2H\"");
});
}

@Test
void writeWithVisibility() {
this.contextRunner
Expand Down

0 comments on commit e6c37a6

Please sign in to comment.