Skip to content

Commit

Permalink
Allow Jackson's serialization inclusion to be configured via the env
Browse files Browse the repository at this point in the history
This commit adds support for configuring an ObjectMapper's
serialization inclusion using the environment via the
spring.jackson.serialization-inclusion property. The property's value
should be one of the values on the JsonInclude.Include enumeration.
Relaxed binding of the property value to the enum is supported. For
example:

spring.jackson.serialization-inclusion: non_null

Closes gh-2532
  • Loading branch information
wilkinsona committed Apr 8, 2015
1 parent 6073454 commit 1dcf18b
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,10 @@ public Jackson2ObjectMapperBuilder jacksonObjectMapperBuilder() {
if (isJsonPrettyPrint != null && isJsonPrettyPrint) {
builder.featuresToEnable(SerializationFeature.INDENT_OUTPUT);
}
if (this.jacksonProperties.getSerializationInclusion() != null) {
builder.serializationInclusion(this.jacksonProperties
.getSerializationInclusion());
}
configureFeatures(builder, this.jacksonProperties.getDeserialization());
configureFeatures(builder, this.jacksonProperties.getSerialization());
configureFeatures(builder, this.jacksonProperties.getMapper());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

import org.springframework.boot.context.properties.ConfigurationProperties;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.DeserializationFeature;
Expand All @@ -45,8 +46,7 @@ public class JacksonProperties {

/**
* Joda date time format string (yyyy-MM-dd HH:mm:ss). If not configured,
* "date-format" will be used as a fallback if it is configured with a format
* string.
* "date-format" will be used as a fallback if it is configured with a format string.
*/
private String jodaDateTimeFormat;

Expand Down Expand Up @@ -82,6 +82,12 @@ public class JacksonProperties {
*/
private Map<JsonGenerator.Feature, Boolean> generator = new HashMap<JsonGenerator.Feature, Boolean>();

/**
* Controls the inclusion of properties during serialization. Configured with one of
* the values in Jackson's JsonInclude.Include enumeration.
*/
private JsonInclude.Include serializationInclusion;

public String getDateFormat() {
return this.dateFormat;
}
Expand Down Expand Up @@ -126,4 +132,12 @@ public Map<JsonGenerator.Feature, Boolean> getGenerator() {
return this.generator;
}

public JsonInclude.Include getSerializationInclusion() {
return this.serializationInclusion;
}

public void setSerializationInclusion(JsonInclude.Include serializationInclusion) {
this.serializationInclusion = serializationInclusion;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
import org.springframework.context.annotation.Primary;
import org.springframework.http.converter.json.Jackson2ObjectMapperBuilder;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonProcessingException;
Expand Down Expand Up @@ -387,6 +388,28 @@ public void moduleBeansAndWellKnownModulesAreRegisteredWithTheObjectMapperBuilde
assertThat(objectMapper.canSerialize(LocalDateTime.class), is(true));
}

@Test
public void defaultSerializationInclusion() {
this.context.register(JacksonAutoConfiguration.class);
this.context.refresh();
ObjectMapper objectMapper = this.context.getBean(
Jackson2ObjectMapperBuilder.class).build();
assertThat(objectMapper.getSerializationConfig().getSerializationInclusion(),
is(JsonInclude.Include.ALWAYS));
}

@Test
public void customSerializationInclusion() {
this.context.register(JacksonAutoConfiguration.class);
EnvironmentTestUtils.addEnvironment(this.context,
"spring.jackson.serialization-inclusion:non_null");
this.context.refresh();
ObjectMapper objectMapper = this.context.getBean(
Jackson2ObjectMapperBuilder.class).build();
assertThat(objectMapper.getSerializationConfig().getSerializationInclusion(),
is(JsonInclude.Include.NON_NULL));
}

@Configuration
protected static class MockObjectMapperConfig {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,7 @@ content into your application; rather pick only the properties that you need.
spring.jackson.mapper.*= # see Jackson's MapperFeature
spring.jackson.parser.*= # see Jackson's JsonParser.Feature
spring.jackson.serialization.*= # see Jackson's SerializationFeature
spring.jackson.serialization-inclusion= # Controls the inclusion of properties during serialization (see Jackson's JsonInclude.Include)
# THYMELEAF ({sc-spring-boot-autoconfigure}/thymeleaf/ThymeleafAutoConfiguration.{sc-ext}[ThymeleafAutoConfiguration])
spring.thymeleaf.check-template-location=true
Expand Down

0 comments on commit 1dcf18b

Please sign in to comment.