Skip to content

Commit 0959271

Browse files
committed
Move jackson read and write to spring.jackson.json
The spring.jackson.read and spring.jackson.write properties are JSON-specific. To make this more clear, this commit moves them beneath spring.jackson.json. This also paves the way for spring.jackson.cbor and spring.jackson.xml properties for CBOR- and XML-specific settings should we add auto-configuration for XMLMapper and/or CBORMapper in the future. Closes gh-47328
1 parent 8c7e0c6 commit 0959271

File tree

5 files changed

+72
-41
lines changed

5 files changed

+72
-41
lines changed

documentation/spring-boot-docs/src/docs/antora/modules/how-to/pages/spring-mvc.adoc

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -74,31 +74,31 @@ These features are described in several enums (in Jackson) that map onto propert
7474
| `spring.jackson.datatype.json-node.<feature_name>`
7575
| `true`, `false`
7676

77+
| javadoc:com.fasterxml.jackson.annotation.JsonInclude$Include[]
78+
| configprop:spring.jackson.default-property-inclusion[]
79+
| `always`, `non_null`, `non_absent`, `non_default`, `non_empty`
80+
|===
81+
7782
| javadoc:tools.jackson.databind.DeserializationFeature[]
7883
| `spring.jackson.deserialization.<feature_name>`
7984
| `true`, `false`
8085

81-
| javadoc:tools.jackson.databind.MapperFeature[]
82-
| `spring.jackson.mapper.<feature_name>`
83-
| `true`, `false`
84-
8586
| javadoc:tools.jackson.core.JsonReadFeature[]
86-
| `spring.jackson.read.<feature_name>`
87+
| `spring.jackson.json.read.<feature_name>`
8788
| `true`, `false`
8889

8990
| javadoc:tools.jackson.core.JsonWriteFeature[]
90-
| `spring.jackson.write.<feature_name>`
91+
| `spring.jackson.json.write.<feature_name>`
92+
| `true`, `false`
93+
94+
| javadoc:tools.jackson.databind.MapperFeature[]
95+
| `spring.jackson.mapper.<feature_name>`
9196
| `true`, `false`
9297

9398
| javadoc:tools.jackson.databind.SerializationFeature[]
9499
| `spring.jackson.serialization.<feature_name>`
95100
| `true`, `false`
96101

97-
| javadoc:com.fasterxml.jackson.annotation.JsonInclude$Include[]
98-
| configprop:spring.jackson.default-property-inclusion[]
99-
| `always`, `non_null`, `non_absent`, `non_default`, `non_empty`
100-
|===
101-
102102
For example, to enable pretty print, set `spring.jackson.serialization.indent_output=true`.
103103
Note that, thanks to the use of xref:reference:features/external-config.adoc#features.external-config.typesafe-configuration-properties.relaxed-binding[relaxed binding], the case of `indent_output` does not have to match the case of the corresponding enum constant, which is `INDENT_OUTPUT`.
104104

module/spring-boot-jackson/src/main/java/org/springframework/boot/jackson/autoconfigure/JacksonAutoConfiguration.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -175,8 +175,8 @@ public void customize(JsonMapper.Builder builder) {
175175
configureFeatures(builder, this.jacksonProperties.getDeserialization(), builder::configure);
176176
configureFeatures(builder, this.jacksonProperties.getSerialization(), builder::configure);
177177
configureFeatures(builder, this.jacksonProperties.getMapper(), builder::configure);
178-
configureFeatures(builder, this.jacksonProperties.getRead(), builder::configure);
179-
configureFeatures(builder, this.jacksonProperties.getWrite(), builder::configure);
178+
configureFeatures(builder, this.jacksonProperties.getJson().getRead(), builder::configure);
179+
configureFeatures(builder, this.jacksonProperties.getJson().getWrite(), builder::configure);
180180
configureFeatures(builder, this.jacksonProperties.getDatatype().getDatetime(), builder::configure);
181181
configureFeatures(builder, this.jacksonProperties.getDatatype().getEnum(), builder::configure);
182182
configureFeatures(builder, this.jacksonProperties.getDatatype().getJsonNode(), builder::configure);

module/spring-boot-jackson/src/main/java/org/springframework/boot/jackson/autoconfigure/JacksonProperties.java

Lines changed: 28 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -81,16 +81,6 @@ public class JacksonProperties {
8181
*/
8282
private final Map<MapperFeature, Boolean> mapper = new EnumMap<>(MapperFeature.class);
8383

84-
/**
85-
* Jackson on/off features for readers.
86-
*/
87-
private final Map<JsonReadFeature, Boolean> read = new EnumMap<>(JsonReadFeature.class);
88-
89-
/**
90-
* Jackson on/off features for writers.
91-
*/
92-
private final Map<JsonWriteFeature, Boolean> write = new EnumMap<>(JsonWriteFeature.class);
93-
9484
/**
9585
* Controls the inclusion of properties during serialization. Configured with one of
9686
* the values in Jackson's JsonInclude.Include enumeration.
@@ -121,6 +111,8 @@ public class JacksonProperties {
121111

122112
private final Datatype datatype = new Datatype();
123113

114+
private final Json json = new Json();
115+
124116
public @Nullable String getDateFormat() {
125117
return this.dateFormat;
126118
}
@@ -153,14 +145,6 @@ public Map<MapperFeature, Boolean> getMapper() {
153145
return this.mapper;
154146
}
155147

156-
public Map<JsonReadFeature, Boolean> getRead() {
157-
return this.read;
158-
}
159-
160-
public Map<JsonWriteFeature, Boolean> getWrite() {
161-
return this.write;
162-
}
163-
164148
public JsonInclude.@Nullable Include getDefaultPropertyInclusion() {
165149
return this.defaultPropertyInclusion;
166150
}
@@ -205,6 +189,10 @@ public Datatype getDatatype() {
205189
return this.datatype;
206190
}
207191

192+
public Json getJson() {
193+
return this.json;
194+
}
195+
208196
public enum ConstructorDetectorStrategy {
209197

210198
/**
@@ -261,4 +249,26 @@ public Map<DateTimeFeature, Boolean> getDatetime() {
261249

262250
}
263251

252+
public static class Json {
253+
254+
/**
255+
* Jackson on/off token reader features that are specific to JSON.
256+
*/
257+
private final Map<JsonReadFeature, Boolean> read = new EnumMap<>(JsonReadFeature.class);
258+
259+
/**
260+
* Jackson on/off token writer features that are specific to JSON.
261+
*/
262+
private final Map<JsonWriteFeature, Boolean> write = new EnumMap<>(JsonWriteFeature.class);
263+
264+
public Map<JsonReadFeature, Boolean> getRead() {
265+
return this.read;
266+
}
267+
268+
public Map<JsonWriteFeature, Boolean> getWrite() {
269+
return this.write;
270+
}
271+
272+
}
273+
264274
}

module/spring-boot-jackson/src/main/resources/META-INF/additional-spring-configuration-metadata.json

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,26 @@
1515
"deprecation": {
1616
"level": "error"
1717
}
18+
},
19+
{
20+
"name": "spring.jackson.read",
21+
"type": "java.util.Map<tools.jackson.core.json.JsonReadFeature,java.lang.Boolean>",
22+
"description": "Jackson on/off token reader features that are specific to JSON.",
23+
"sourceType": "org.springframework.boot.jackson.autoconfigure.JacksonProperties",
24+
"deprecation": {
25+
"level": "error",
26+
"replacement": "spring.jackson.json.read"
27+
}
28+
},
29+
{
30+
"name": "spring.jackson.write",
31+
"type": "java.util.Map<tools.jackson.core.json.JsonWriteFeature,java.lang.Boolean>",
32+
"description": "Jackson on/off token writer features that are specific to JSON.",
33+
"sourceType": "org.springframework.boot.jackson.autoconfigure.JacksonProperties",
34+
"deprecation": {
35+
"level": "error",
36+
"replacement": "spring.jackson.json.write"
37+
}
1838
}
1939
]
2040
}

module/spring-boot-jackson/src/test/java/org/springframework/boot/jackson/autoconfigure/JacksonAutoConfigurationTests.java

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -237,27 +237,28 @@ void disableMapperFeature() {
237237
}
238238

239239
@Test
240-
void enableReadFeature() {
241-
this.contextRunner.withPropertyValues("spring.jackson.read.allow_single_quotes:true").run((context) -> {
240+
void enableJsonReadFeature() {
241+
this.contextRunner.withPropertyValues("spring.jackson.json.read.allow_single_quotes:true").run((context) -> {
242242
JsonMapper mapper = context.getBean(JsonMapper.class);
243243
assertThat(JsonReadFeature.ALLOW_SINGLE_QUOTES.enabledByDefault()).isFalse();
244244
assertThat(mapper.isEnabled(JsonReadFeature.ALLOW_SINGLE_QUOTES)).isTrue();
245245
});
246246
}
247247

248248
@Test
249-
void enableWriteFeature() {
250-
this.contextRunner.withPropertyValues("spring.jackson.write.write_numbers_as_strings:true").run((context) -> {
251-
JsonMapper mapper = context.getBean(JsonMapper.class);
252-
JsonWriteFeature feature = JsonWriteFeature.WRITE_NUMBERS_AS_STRINGS;
253-
assertThat(feature.enabledByDefault()).isFalse();
254-
assertThat(mapper.isEnabled(feature)).isTrue();
255-
});
249+
void enableJsonWriteFeature() {
250+
this.contextRunner.withPropertyValues("spring.jackson.json.write.write_numbers_as_strings:true")
251+
.run((context) -> {
252+
JsonMapper mapper = context.getBean(JsonMapper.class);
253+
JsonWriteFeature feature = JsonWriteFeature.WRITE_NUMBERS_AS_STRINGS;
254+
assertThat(feature.enabledByDefault()).isFalse();
255+
assertThat(mapper.isEnabled(feature)).isTrue();
256+
});
256257
}
257258

258259
@Test
259260
void disableWriteFeature() {
260-
this.contextRunner.withPropertyValues("spring.jackson.write.write_hex_upper_case:false").run((context) -> {
261+
this.contextRunner.withPropertyValues("spring.jackson.json.write.write_hex_upper_case:false").run((context) -> {
261262
JsonMapper mapper = context.getBean(JsonMapper.class);
262263
assertThat(JsonWriteFeature.WRITE_HEX_UPPER_CASE.enabledByDefault()).isTrue();
263264
assertThat(mapper.isEnabled(JsonWriteFeature.WRITE_HEX_UPPER_CASE)).isFalse();

0 commit comments

Comments
 (0)