Switch Jackson write-dates-as-timestamps default

Update `JacksonAutoConfiguration` so that `write-dates-as-timestamps`
now defaults to `false`.

Fixes gh-11079
pull/11128/head
Phillip Webb 7 years ago
parent 78dee309b1
commit 93ae71cf93

@ -20,16 +20,18 @@ import java.lang.reflect.Field;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Map.Entry;
import java.util.TimeZone;
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.databind.Module;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.PropertyNamingStrategy;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.databind.module.SimpleModule;
import com.fasterxml.jackson.datatype.joda.cfg.JacksonJodaDateFormat;
import com.fasterxml.jackson.datatype.joda.ser.DateTimeSerializer;
@ -70,12 +72,21 @@ import org.springframework.util.ReflectionUtils;
* @author Marcel Overdijk
* @author Sebastien Deleuze
* @author Johannes Edmeier
* @author Phillip Webb
* @since 1.1.0
*/
@Configuration
@ConditionalOnClass(ObjectMapper.class)
public class JacksonAutoConfiguration {
private static final Map<?, Boolean> FEATURE_DEFAULTS;
static {
Map<Object, Boolean> featureDefaults = new HashMap<>();
featureDefaults.put(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false);
FEATURE_DEFAULTS = Collections.unmodifiableMap(featureDefaults);
}
@Bean
public JsonComponentModule jsonComponentModule() {
return new JsonComponentModule();
@ -228,6 +239,7 @@ public class JacksonAutoConfiguration {
if (this.jacksonProperties.getTimeZone() != null) {
builder.timeZone(this.jacksonProperties.getTimeZone());
}
configureFeatures(builder, FEATURE_DEFAULTS);
configureFeatures(builder, this.jacksonProperties.getDeserialization());
configureFeatures(builder, this.jacksonProperties.getSerialization());
configureFeatures(builder, this.jacksonProperties.getMapper());
@ -241,14 +253,16 @@ public class JacksonAutoConfiguration {
private void configureFeatures(Jackson2ObjectMapperBuilder builder,
Map<?, Boolean> features) {
for (Entry<?, Boolean> entry : features.entrySet()) {
if (entry.getValue() != null && entry.getValue()) {
builder.featuresToEnable(entry.getKey());
}
else {
builder.featuresToDisable(entry.getKey());
features.forEach((feature, value) -> {
if (value != null) {
if (value) {
builder.featuresToEnable(feature);
}
else {
builder.featuresToDisable(feature);
}
}
}
});
}
private void configureDateFormat(Jackson2ObjectMapperBuilder builder) {

@ -22,6 +22,7 @@ import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashSet;
import java.util.Set;
import java.util.TimeZone;
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonCreator.Mode;
@ -42,6 +43,7 @@ import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.databind.SerializerProvider;
import com.fasterxml.jackson.databind.module.SimpleModule;
import com.fasterxml.jackson.databind.util.StdDateFormat;
import com.fasterxml.jackson.datatype.joda.cfg.FormatConfig;
import com.fasterxml.jackson.module.paramnames.ParameterNamesModule;
import org.joda.time.DateTime;
import org.joda.time.DateTimeZone;
@ -448,6 +450,18 @@ public class JacksonAutoConfigurationTests {
ParameterNamesModuleConfig.class, JacksonAutoConfiguration.class);
}
@Test
public void writeDatesAsTimestampsDefault() throws Exception {
this.context.register(JacksonAutoConfiguration.class);
this.context.refresh();
ObjectMapper mapper = this.context.getBean(ObjectMapper.class);
DateTime dateTime = new DateTime(1988, 6, 25, 20, 30, DateTimeZone.UTC);
String expected = FormatConfig.DEFAULT_DATETIME_PRINTER.rawFormatter()
.withZone(DateTimeZone.forTimeZone(TimeZone.getTimeZone("UTC")))
.print(dateTime);
assertThat(mapper.writeValueAsString(dateTime)).isEqualTo("\"" + expected + "\"");
}
private void assertParameterNamesModuleCreatorBinding(Mode expectedMode,
Class<?>... configClasses) {
this.context.register(configClasses);

Loading…
Cancel
Save