Polish "Add properties for configuring EnumFeature and JsonNodeFeature"
See gh-37885
This commit is contained in:
parent
8edb4b9729
commit
d796087dfa
|
@ -213,8 +213,8 @@ public class JacksonAutoConfiguration {
|
||||||
configureFeatures(builder, this.jacksonProperties.getMapper());
|
configureFeatures(builder, this.jacksonProperties.getMapper());
|
||||||
configureFeatures(builder, this.jacksonProperties.getParser());
|
configureFeatures(builder, this.jacksonProperties.getParser());
|
||||||
configureFeatures(builder, this.jacksonProperties.getGenerator());
|
configureFeatures(builder, this.jacksonProperties.getGenerator());
|
||||||
configureFeatures(builder, this.jacksonProperties.getEnumDatatype());
|
configureFeatures(builder, this.jacksonProperties.getDatatype().getEnum());
|
||||||
configureFeatures(builder, this.jacksonProperties.getJsonNodeDatatype());
|
configureFeatures(builder, this.jacksonProperties.getDatatype().getJsonNode());
|
||||||
configureDateFormat(builder);
|
configureDateFormat(builder);
|
||||||
configurePropertyNamingStrategy(builder);
|
configurePropertyNamingStrategy(builder);
|
||||||
configureModules(builder);
|
configureModules(builder);
|
||||||
|
|
|
@ -89,16 +89,6 @@ public class JacksonProperties {
|
||||||
*/
|
*/
|
||||||
private final Map<JsonGenerator.Feature, Boolean> generator = new EnumMap<>(JsonGenerator.Feature.class);
|
private final Map<JsonGenerator.Feature, Boolean> generator = new EnumMap<>(JsonGenerator.Feature.class);
|
||||||
|
|
||||||
/**
|
|
||||||
* Jackson on/off features for enum types.
|
|
||||||
*/
|
|
||||||
private final Map<EnumFeature, Boolean> enumDatatype = new EnumMap<>(EnumFeature.class);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Jackson on/off features for JsonNode types.
|
|
||||||
*/
|
|
||||||
private final Map<JsonNodeFeature, Boolean> jsonNodeDatatype = new EnumMap<>(JsonNodeFeature.class);
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Controls the inclusion of properties during serialization. Configured with one of
|
* Controls the inclusion of properties during serialization. Configured with one of
|
||||||
* the values in Jackson's JsonInclude.Include enumeration.
|
* the values in Jackson's JsonInclude.Include enumeration.
|
||||||
|
@ -127,6 +117,8 @@ public class JacksonProperties {
|
||||||
*/
|
*/
|
||||||
private Locale locale;
|
private Locale locale;
|
||||||
|
|
||||||
|
private final Datatype datatype = new Datatype();
|
||||||
|
|
||||||
public String getDateFormat() {
|
public String getDateFormat() {
|
||||||
return this.dateFormat;
|
return this.dateFormat;
|
||||||
}
|
}
|
||||||
|
@ -167,14 +159,6 @@ public class JacksonProperties {
|
||||||
return this.generator;
|
return this.generator;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Map<EnumFeature, Boolean> getEnumDatatype() {
|
|
||||||
return this.enumDatatype;
|
|
||||||
}
|
|
||||||
|
|
||||||
public Map<JsonNodeFeature, Boolean> getJsonNodeDatatype() {
|
|
||||||
return this.jsonNodeDatatype;
|
|
||||||
}
|
|
||||||
|
|
||||||
public JsonInclude.Include getDefaultPropertyInclusion() {
|
public JsonInclude.Include getDefaultPropertyInclusion() {
|
||||||
return this.defaultPropertyInclusion;
|
return this.defaultPropertyInclusion;
|
||||||
}
|
}
|
||||||
|
@ -215,6 +199,10 @@ public class JacksonProperties {
|
||||||
this.locale = locale;
|
this.locale = locale;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public Datatype getDatatype() {
|
||||||
|
return this.datatype;
|
||||||
|
}
|
||||||
|
|
||||||
public enum ConstructorDetectorStrategy {
|
public enum ConstructorDetectorStrategy {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -240,4 +228,26 @@ public class JacksonProperties {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static class Datatype {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Jackson on/off features for enums.
|
||||||
|
*/
|
||||||
|
private final Map<EnumFeature, Boolean> enumFeatures = new EnumMap<>(EnumFeature.class);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Jackson on/off features for JsonNodes.
|
||||||
|
*/
|
||||||
|
private final Map<JsonNodeFeature, Boolean> jsonNode = new EnumMap<>(JsonNodeFeature.class);
|
||||||
|
|
||||||
|
public Map<EnumFeature, Boolean> getEnum() {
|
||||||
|
return this.enumFeatures;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Map<JsonNodeFeature, Boolean> getJsonNode() {
|
||||||
|
return this.jsonNode;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -1559,6 +1559,10 @@
|
||||||
"name": "spring.jackson.constructor-detector",
|
"name": "spring.jackson.constructor-detector",
|
||||||
"defaultValue": "default"
|
"defaultValue": "default"
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
"name": "spring.jackson.datatype.enum",
|
||||||
|
"description": "Jackson on/off features for enums."
|
||||||
|
},
|
||||||
{
|
{
|
||||||
"name": "spring.jackson.joda-date-time-format",
|
"name": "spring.jackson.joda-date-time-format",
|
||||||
"type": "java.lang.String",
|
"type": "java.lang.String",
|
||||||
|
|
|
@ -294,7 +294,7 @@ class JacksonAutoConfigurationTests {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void enableEnumFeature() {
|
void enableEnumFeature() {
|
||||||
this.contextRunner.withPropertyValues("spring.jackson.enum-data-type.write_enums_to_lowercase:true")
|
this.contextRunner.withPropertyValues("spring.jackson.datatype.enum.write-enums-to-lowercase=true")
|
||||||
.run((context) -> {
|
.run((context) -> {
|
||||||
ObjectMapper mapper = context.getBean(ObjectMapper.class);
|
ObjectMapper mapper = context.getBean(ObjectMapper.class);
|
||||||
assertThat(EnumFeature.WRITE_ENUMS_TO_LOWERCASE.enabledByDefault()).isFalse();
|
assertThat(EnumFeature.WRITE_ENUMS_TO_LOWERCASE.enabledByDefault()).isFalse();
|
||||||
|
@ -304,7 +304,7 @@ class JacksonAutoConfigurationTests {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void disableJsonNodeFeature() {
|
void disableJsonNodeFeature() {
|
||||||
this.contextRunner.withPropertyValues("spring.jackson.json-node-data-type.write_null_properties:false")
|
this.contextRunner.withPropertyValues("spring.jackson.datatype.jsonnode.write-null-properties:false")
|
||||||
.run((context) -> {
|
.run((context) -> {
|
||||||
ObjectMapper mapper = context.getBean(ObjectMapper.class);
|
ObjectMapper mapper = context.getBean(ObjectMapper.class);
|
||||||
assertThat(JsonNodeFeature.WRITE_NULL_PROPERTIES.enabledByDefault()).isTrue();
|
assertThat(JsonNodeFeature.WRITE_NULL_PROPERTIES.enabledByDefault()).isTrue();
|
||||||
|
|
|
@ -64,11 +64,19 @@ Spring Boot also has some features to make it easier to customize this behavior.
|
||||||
|
|
||||||
You can configure the `ObjectMapper` and `XmlMapper` instances by using the environment.
|
You can configure the `ObjectMapper` and `XmlMapper` instances by using the environment.
|
||||||
Jackson provides an extensive suite of on/off features that can be used to configure various aspects of its processing.
|
Jackson provides an extensive suite of on/off features that can be used to configure various aspects of its processing.
|
||||||
These features are described in six enums (in Jackson) that map onto properties in the environment:
|
These features are described in several enums (in Jackson) that map onto properties in the environment:
|
||||||
|
|
||||||
|===
|
|===
|
||||||
| Enum | Property | Values
|
| Enum | Property | Values
|
||||||
|
|
||||||
|
| `com.fasterxml.jackson.databind.cfg.EnumFeature`
|
||||||
|
| `spring.jackson.datatype.enum.<feature_name>`
|
||||||
|
| `true`, `false`
|
||||||
|
|
||||||
|
| `com.fasterxml.jackson.databind.cfg.JsonNodeFeature`
|
||||||
|
| `spring.jackson.datatype.jsonnode.<feature_name>`
|
||||||
|
| `true`, `false`
|
||||||
|
|
||||||
| `com.fasterxml.jackson.databind.DeserializationFeature`
|
| `com.fasterxml.jackson.databind.DeserializationFeature`
|
||||||
| `spring.jackson.deserialization.<feature_name>`
|
| `spring.jackson.deserialization.<feature_name>`
|
||||||
| `true`, `false`
|
| `true`, `false`
|
||||||
|
|
Loading…
Reference in New Issue