Refined Jackson configuration enhancements

Issue: SPR-12594
Issue: SPR-12634
This commit is contained in:
Juergen Hoeller 2015-02-11 20:37:04 +01:00
parent 7a6a13201f
commit 77fcf21401
4 changed files with 30 additions and 28 deletions

View File

@ -49,6 +49,7 @@ import org.springframework.beans.FatalBeanException;
import org.springframework.context.ApplicationContext;
import org.springframework.util.Assert;
import org.springframework.util.ClassUtils;
import org.springframework.util.StringUtils;
/**
* A builder used to create {@link ObjectMapper} instances with a fluent API.
@ -74,7 +75,7 @@ import org.springframework.util.ClassUtils;
*/
public class Jackson2ObjectMapperBuilder {
private boolean createXmlMapper;
private boolean createXmlMapper = false;
private DateFormat dateFormat;
@ -100,7 +101,7 @@ public class Jackson2ObjectMapperBuilder {
private Class<? extends Module>[] moduleClasses;
private boolean findModulesViaServiceLoader;
private boolean findModulesViaServiceLoader = false;
private boolean findWellKnownModules = true;
@ -153,6 +154,17 @@ public class Jackson2ObjectMapperBuilder {
return this;
}
/**
* Override the default {@link Locale} to use for formatting.
* Default value used is {@link Locale#getDefault()}.
* @param localeString the locale ID as a String representation
* @since 4.1.5
*/
public Jackson2ObjectMapperBuilder locale(String localeString) {
this.locale = StringUtils.parseLocaleString(localeString);
return this;
}
/**
* Override the default {@link TimeZone} to use for formatting.
* Default value used is UTC (NOT local timezone).
@ -166,11 +178,11 @@ public class Jackson2ObjectMapperBuilder {
/**
* Override the default {@link TimeZone} to use for formatting.
* Default value used is UTC (NOT local timezone).
* @param zoneId the time-zone ID
* @param timeZoneString the zone ID as a String representation
* @since 4.1.5
*/
public Jackson2ObjectMapperBuilder timeZone(String zoneId) {
this.timeZone = TimeZone.getTimeZone(zoneId);
public Jackson2ObjectMapperBuilder timeZone(String timeZoneString) {
this.timeZone = StringUtils.parseTimeZoneString(timeZoneString);
return this;
}
@ -528,8 +540,8 @@ public class Jackson2ObjectMapperBuilder {
else if (this.findWellKnownModules) {
registerWellKnownModulesIfAvailable(objectMapper);
}
if (this.modules != null) {
// Complete list of modules given
for (Module module : this.modules) {
// Using Jackson 2.0+ registerModule method, not Jackson 2.2+ registerModules
objectMapper.registerModule(module);

View File

@ -191,16 +191,6 @@ public class Jackson2ObjectMapperFactoryBean implements FactoryBean<ObjectMapper
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.
*/

View File

@ -56,13 +56,13 @@ import com.fasterxml.jackson.databind.ser.std.ClassSerializer;
import com.fasterxml.jackson.databind.ser.std.NumberSerializer;
import com.fasterxml.jackson.databind.type.SimpleType;
import com.fasterxml.jackson.dataformat.xml.XmlMapper;
import static org.hamcrest.Matchers.containsString;
import org.joda.time.DateTime;
import org.joda.time.DateTimeZone;
import org.junit.Test;
import org.springframework.beans.FatalBeanException;
import static org.hamcrest.Matchers.*;
import static org.junit.Assert.*;
/**
@ -201,13 +201,10 @@ public class Jackson2ObjectMapperBuilderTests {
assertEquals(timeZone, objectMapper.getDeserializationConfig().getTimeZone());
}
@Test
@Test(expected = IllegalArgumentException.class)
public void wrongTimeZoneStringSetter() {
String zoneId = "foo";
ObjectMapper objectMapper = Jackson2ObjectMapperBuilder.json().timeZone(zoneId).build();
TimeZone timeZone = TimeZone.getTimeZone("GMT");
assertEquals(timeZone, objectMapper.getSerializationConfig().getTimeZone());
assertEquals(timeZone, objectMapper.getDeserializationConfig().getTimeZone());
Jackson2ObjectMapperBuilder.json().timeZone(zoneId).build();
}
@Test
@ -428,10 +425,11 @@ public class Jackson2ObjectMapperBuilderTests {
}
}
public static class CustomIntegerSerializer extends JsonSerializer<Integer> {
@Override
public void serialize(Integer value, JsonGenerator gen, SerializerProvider serializers) throws IOException, JsonProcessingException {
public void serialize(Integer value, JsonGenerator gen, SerializerProvider serializers) throws IOException {
gen.writeStartObject();
gen.writeNumberField("customid", value);
gen.writeEndObject();

View File

@ -55,7 +55,6 @@ import com.fasterxml.jackson.databind.ser.std.ClassSerializer;
import com.fasterxml.jackson.databind.ser.std.NumberSerializer;
import com.fasterxml.jackson.databind.type.SimpleType;
import com.fasterxml.jackson.dataformat.xml.XmlMapper;
import static org.hamcrest.Matchers.containsString;
import org.joda.time.DateTime;
import org.joda.time.DateTimeZone;
import org.junit.Before;
@ -63,6 +62,7 @@ import org.junit.Test;
import org.springframework.beans.FatalBeanException;
import static org.hamcrest.Matchers.*;
import static org.junit.Assert.*;
/**
@ -198,7 +198,7 @@ public class Jackson2ObjectMapperFactoryBeanTests {
public void timeZoneStringSetter() {
String zoneId = "Europe/Paris";
this.factory.setTimeZone(zoneId);
this.factory.setTimeZone(TimeZone.getTimeZone(zoneId));
this.factory.afterPropertiesSet();
TimeZone timeZone = TimeZone.getTimeZone(zoneId);
@ -210,7 +210,7 @@ public class Jackson2ObjectMapperFactoryBeanTests {
public void wrongTimeZoneStringSetter() {
String zoneId = "foo";
this.factory.setTimeZone(zoneId);
this.factory.setTimeZone(TimeZone.getTimeZone(zoneId));
this.factory.afterPropertiesSet();
TimeZone timeZone = TimeZone.getTimeZone("GMT");
@ -224,7 +224,7 @@ public class Jackson2ObjectMapperFactoryBeanTests {
SimpleModule module = new SimpleModule();
module.addSerializer(Integer.class, serializer1);
this.factory.setModules(Arrays.asList(new Module[] {module}));
this.factory.setModules(Arrays.asList(new Module[]{module}));
this.factory.afterPropertiesSet();
ObjectMapper objectMapper = this.factory.getObject();
@ -397,6 +397,7 @@ public class Jackson2ObjectMapperFactoryBeanTests {
assertEquals(XmlMapper.class, this.factory.getObjectType());
}
public static class CustomIntegerModule extends Module {
@Override
@ -417,10 +418,11 @@ public class Jackson2ObjectMapperFactoryBeanTests {
}
}
public static class CustomIntegerSerializer extends JsonSerializer<Integer> {
@Override
public void serialize(Integer value, JsonGenerator gen, SerializerProvider serializers) throws IOException, JsonProcessingException {
public void serialize(Integer value, JsonGenerator gen, SerializerProvider serializers) throws IOException {
gen.writeStartObject();
gen.writeNumberField("customid", value);
gen.writeEndObject();