Browse Source

Support specifying Jackson TimeZone and Locale

Issue: SPR-12594
pull/722/merge
Sebastien Deleuze 10 years ago
parent
commit
b89e62e5f6
  1. 44
      spring-web/src/main/java/org/springframework/http/converter/json/Jackson2ObjectMapperBuilder.java
  2. 30
      spring-web/src/main/java/org/springframework/http/converter/json/Jackson2ObjectMapperFactoryBean.java
  3. 34
      spring-web/src/test/java/org/springframework/http/converter/json/Jackson2ObjectMapperBuilderTests.java
  4. 44
      spring-web/src/test/java/org/springframework/http/converter/json/Jackson2ObjectMapperFactoryBeanTests.java

44
spring-web/src/main/java/org/springframework/http/converter/json/Jackson2ObjectMapperBuilder.java

@ -18,11 +18,14 @@ package org.springframework.http.converter.json; @@ -18,11 +18,14 @@ package org.springframework.http.converter.json;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.time.ZoneId;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.TimeZone;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.core.JsonGenerator;
@ -75,6 +78,10 @@ public class Jackson2ObjectMapperBuilder { @@ -75,6 +78,10 @@ public class Jackson2ObjectMapperBuilder {
private DateFormat dateFormat;
private Locale locale;
private TimeZone timeZone;
private AnnotationIntrospector annotationIntrospector;
private PropertyNamingStrategy propertyNamingStrategy;
@ -134,6 +141,37 @@ public class Jackson2ObjectMapperBuilder { @@ -134,6 +141,37 @@ public class Jackson2ObjectMapperBuilder {
return this;
}
/**
* Override the default {@link Locale} to use for formatting.
* Default value used is {@link Locale#getDefault()}.
* @since 4.1.5
*/
public Jackson2ObjectMapperBuilder locale(Locale locale) {
this.locale = locale;
return this;
}
/**
* Override the default {@link TimeZone} to use for formatting.
* Default value used is UTC (NOT local timezone).
* @since 4.1.5
*/
public Jackson2ObjectMapperBuilder timeZone(TimeZone timeZone) {
this.timeZone = timeZone;
return this;
}
/**
* Override the default {@link TimeZone} to use for formatting.
* Default value used is UTC (NOT local timezone).
* @param zoneId the time-zone ID
* @since 4.1.5
*/
public Jackson2ObjectMapperBuilder timeZone(String zoneId) {
this.timeZone = TimeZone.getTimeZone(ZoneId.of(zoneId));
return this;
}
/**
* Set an {@link AnnotationIntrospector} for both serialization and deserialization.
*/
@ -447,6 +485,12 @@ public class Jackson2ObjectMapperBuilder { @@ -447,6 +485,12 @@ public class Jackson2ObjectMapperBuilder {
if (this.dateFormat != null) {
objectMapper.setDateFormat(this.dateFormat);
}
if (this.locale != null) {
objectMapper.setLocale(this.locale);
}
if (this.timeZone != null) {
objectMapper.setTimeZone(this.timeZone);
}
if (this.annotationIntrospector != null) {
objectMapper.setAnnotationIntrospector(this.annotationIntrospector);

30
spring-web/src/main/java/org/springframework/http/converter/json/Jackson2ObjectMapperFactoryBean.java

@ -19,7 +19,9 @@ package org.springframework.http.converter.json; @@ -19,7 +19,9 @@ package org.springframework.http.converter.json;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.TimeZone;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.databind.AnnotationIntrospector;
@ -171,6 +173,34 @@ public class Jackson2ObjectMapperFactoryBean implements FactoryBean<ObjectMapper @@ -171,6 +173,34 @@ public class Jackson2ObjectMapperFactoryBean implements FactoryBean<ObjectMapper
this.builder.simpleDateFormat(format);
}
/**
* Override the default {@link Locale} to use for formatting.
* Default value used is {@link Locale#getDefault()}.
* @since 4.1.5
*/
public void setLocale(Locale locale) {
this.builder.locale(locale);
}
/**
* Override the default {@link TimeZone} to use for formatting.
* Default value used is UTC (NOT local timezone).
* @since 4.1.5
*/
public void setTimeZone(TimeZone timeZone) {
this.builder.timeZone(timeZone);
}
/**
* Override the default {@link TimeZone} to use for formatting.
* Default value used is UTC (NOT local timezone).
* @param zoneId the time-zone ID
* @since 4.1.5
*/
public void setTimeZone(String zoneId) {
this.builder.timeZone(zoneId);
}
/**
* Set an {@link AnnotationIntrospector} for both serialization and deserialization.
*/

34
spring-web/src/test/java/org/springframework/http/converter/json/Jackson2ObjectMapperBuilderTests.java

@ -17,11 +17,15 @@ @@ -17,11 +17,15 @@
package org.springframework.http.converter.json;
import java.text.SimpleDateFormat;
import java.time.ZoneId;
import java.time.zone.ZoneRulesException;
import java.util.Arrays;
import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;
import java.util.TimeZone;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.core.JsonGenerator;
@ -166,6 +170,36 @@ public class Jackson2ObjectMapperBuilderTests { @@ -166,6 +170,36 @@ public class Jackson2ObjectMapperBuilderTests {
assertEquals(dateFormat, objectMapper.getDeserializationConfig().getDateFormat());
}
@Test
public void localeSetter() {
ObjectMapper objectMapper = Jackson2ObjectMapperBuilder.json().locale(Locale.FRENCH).build();
assertEquals(Locale.FRENCH, objectMapper.getSerializationConfig().getLocale());
assertEquals(Locale.FRENCH, objectMapper.getDeserializationConfig().getLocale());
}
@Test
public void timeZoneSetter() {
TimeZone timeZone = TimeZone.getTimeZone(ZoneId.of("Europe/Paris"));
ObjectMapper objectMapper = Jackson2ObjectMapperBuilder.json().timeZone(timeZone).build();
assertEquals(timeZone, objectMapper.getSerializationConfig().getTimeZone());
assertEquals(timeZone, objectMapper.getDeserializationConfig().getTimeZone());
}
@Test
public void timeZoneStringSetter() {
String zoneId = "Europe/Paris";
ObjectMapper objectMapper = Jackson2ObjectMapperBuilder.json().timeZone(zoneId).build();
TimeZone timeZone = TimeZone.getTimeZone(ZoneId.of(zoneId));
assertEquals(timeZone, objectMapper.getSerializationConfig().getTimeZone());
assertEquals(timeZone, objectMapper.getDeserializationConfig().getTimeZone());
}
@Test(expected = ZoneRulesException.class)
public void wrongTimeZoneStringSetter() {
String zoneId = "foo";
Jackson2ObjectMapperBuilder.json().timeZone(zoneId).build();
}
@Test
public void setModules() {
NumberSerializer serializer1 = new NumberSerializer();

44
spring-web/src/test/java/org/springframework/http/converter/json/Jackson2ObjectMapperFactoryBeanTests.java

@ -17,11 +17,15 @@ @@ -17,11 +17,15 @@
package org.springframework.http.converter.json;
import java.text.SimpleDateFormat;
import java.time.ZoneId;
import java.time.zone.ZoneRulesException;
import java.util.Arrays;
import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;
import java.util.TimeZone;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.core.JsonGenerator;
@ -161,6 +165,46 @@ public class Jackson2ObjectMapperFactoryBeanTests { @@ -161,6 +165,46 @@ public class Jackson2ObjectMapperFactoryBeanTests {
assertEquals(dateFormat, this.factory.getObject().getDeserializationConfig().getDateFormat());
}
@Test
public void localeSetter() {
this.factory.setLocale(Locale.FRENCH);
this.factory.afterPropertiesSet();
assertEquals(Locale.FRENCH, this.factory.getObject().getSerializationConfig().getLocale());
assertEquals(Locale.FRENCH, this.factory.getObject().getDeserializationConfig().getLocale());
}
@Test
public void timeZoneSetter() {
TimeZone timeZone = TimeZone.getTimeZone(ZoneId.of("Europe/Paris"));
this.factory.setTimeZone(timeZone);
this.factory.afterPropertiesSet();
assertEquals(timeZone, this.factory.getObject().getSerializationConfig().getTimeZone());
assertEquals(timeZone, this.factory.getObject().getDeserializationConfig().getTimeZone());
}
@Test
public void timeZoneStringSetter() {
String zoneId = "Europe/Paris";
this.factory.setTimeZone(zoneId);
this.factory.afterPropertiesSet();
TimeZone timeZone = TimeZone.getTimeZone(ZoneId.of(zoneId));
assertEquals(timeZone, this.factory.getObject().getSerializationConfig().getTimeZone());
assertEquals(timeZone, this.factory.getObject().getDeserializationConfig().getTimeZone());
}
@Test(expected = ZoneRulesException.class)
public void wrongTimeZoneStringSetter() {
String zoneId = "foo";
this.factory.setTimeZone(zoneId);
this.factory.afterPropertiesSet();
}
@Test
public void setModules() {
NumberSerializer serializer1 = new NumberSerializer();

Loading…
Cancel
Save