Skip to content

Commit 2666204

Browse files
committed
Use JsonMapper instead of ObjectMapper where feasible
Signed-off-by: Yanming Zhou <[email protected]>
1 parent 13cf666 commit 2666204

File tree

32 files changed

+121
-133
lines changed

32 files changed

+121
-133
lines changed

buildSrc/src/main/java/org/springframework/boot/build/AntoraConventions.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@
4141
import org.gradle.api.tasks.Copy;
4242
import org.gradle.api.tasks.TaskContainer;
4343
import org.gradle.api.tasks.TaskProvider;
44-
import tools.jackson.databind.ObjectMapper;
44+
import tools.jackson.databind.json.JsonMapper;
4545

4646
import org.springframework.boot.build.antora.AntoraAsciidocAttributes;
4747
import org.springframework.boot.build.antora.GenerateAntoraPlaybook;
@@ -197,8 +197,8 @@ private void logWarningIfNodeModulesInUserHome(Project project) {
197197

198198
private String getUiBundleUrl(Project project) {
199199
File packageJson = project.getRootProject().file("antora/package.json");
200-
ObjectMapper objectMapper = new ObjectMapper();
201-
Map<?, ?> json = objectMapper.readerFor(Map.class).readValue(packageJson);
200+
JsonMapper jsonMapper = new JsonMapper();
201+
Map<?, ?> json = jsonMapper.readerFor(Map.class).readValue(packageJson);
202202
Map<?, ?> config = (json != null) ? (Map<?, ?>) json.get("config") : null;
203203
String url = (config != null) ? (String) config.get("ui-bundle-url") : null;
204204
Assert.state(StringUtils.hasText(url.toString()), "package.json has not ui-bundle-url config");

buildSrc/src/main/java/org/springframework/boot/build/bom/ResolvedBom.java

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525
import java.util.List;
2626

2727
import com.fasterxml.jackson.annotation.JsonInclude.Include;
28-
import tools.jackson.databind.ObjectMapper;
2928
import tools.jackson.databind.json.JsonMapper;
3029

3130
/**
@@ -37,25 +36,25 @@
3736
*/
3837
public record ResolvedBom(Id id, List<ResolvedLibrary> libraries) {
3938

40-
private static final ObjectMapper objectMapper;
39+
private static final JsonMapper jsonMapper;
4140

4241
static {
43-
objectMapper = JsonMapper.builder()
42+
jsonMapper = JsonMapper.builder()
4443
.changeDefaultPropertyInclusion((value) -> value.withContentInclusion(Include.NON_EMPTY))
4544
.build();
4645
}
4746

4847
public static ResolvedBom readFrom(File file) {
4948
try (FileReader reader = new FileReader(file)) {
50-
return objectMapper.readValue(reader, ResolvedBom.class);
49+
return jsonMapper.readValue(reader, ResolvedBom.class);
5150
}
5251
catch (IOException ex) {
5352
throw new UncheckedIOException(ex);
5453
}
5554
}
5655

5756
public void writeTo(Writer writer) {
58-
objectMapper.writeValue(writer, this);
57+
jsonMapper.writeValue(writer, this);
5958
}
6059

6160
public record ResolvedLibrary(String name, String version, String versionProperty, List<Id> managedDependencies,

buildSrc/src/main/java/org/springframework/boot/build/context/properties/CheckAdditionalSpringConfigurationMetadata.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737
import org.gradle.api.tasks.SourceTask;
3838
import org.gradle.api.tasks.TaskAction;
3939
import org.gradle.api.tasks.VerificationException;
40-
import tools.jackson.databind.ObjectMapper;
40+
import tools.jackson.databind.json.JsonMapper;
4141

4242
/**
4343
* {@link SourceTask} that checks additional Spring configuration metadata files.
@@ -75,11 +75,11 @@ void check() throws IOException {
7575

7676
@SuppressWarnings("unchecked")
7777
private Report createReport() {
78-
ObjectMapper objectMapper = new ObjectMapper();
78+
JsonMapper jsonMapper = new JsonMapper();
7979
Report report = new Report();
8080
for (File file : getSource().getFiles()) {
8181
Analysis analysis = report.analysis(this.projectDir.toPath().relativize(file.toPath()));
82-
Map<String, Object> json = objectMapper.readValue(file, Map.class);
82+
Map<String, Object> json = jsonMapper.readValue(file, Map.class);
8383
check("groups", json, analysis);
8484
check("properties", json, analysis);
8585
check("hints", json, analysis);

buildSrc/src/main/java/org/springframework/boot/build/context/properties/CheckSpringConfigurationMetadata.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737
import org.gradle.api.tasks.SourceTask;
3838
import org.gradle.api.tasks.TaskAction;
3939
import org.gradle.api.tasks.VerificationException;
40-
import tools.jackson.databind.ObjectMapper;
40+
import tools.jackson.databind.json.JsonMapper;
4141

4242
/**
4343
* {@link SourceTask} that checks {@code spring-configuration-metadata.json} files.
@@ -75,10 +75,10 @@ void check() throws IOException {
7575

7676
@SuppressWarnings("unchecked")
7777
private Report createReport() {
78-
ObjectMapper objectMapper = new ObjectMapper();
78+
JsonMapper jsonMapper = new JsonMapper();
7979
File file = getMetadataLocation().get().getAsFile();
8080
Report report = new Report(this.projectRoot.relativize(file.toPath()));
81-
Map<String, Object> json = objectMapper.readValue(file, Map.class);
81+
Map<String, Object> json = jsonMapper.readValue(file, Map.class);
8282
List<Map<String, Object>> properties = (List<Map<String, Object>>) json.get("properties");
8383
for (Map<String, Object> property : properties) {
8484
String name = (String) property.get("name");

buildSrc/src/main/java/org/springframework/boot/build/context/properties/ConfigurationProperties.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
import java.util.Map;
2525
import java.util.stream.Stream;
2626

27-
import tools.jackson.databind.ObjectMapper;
27+
import tools.jackson.databind.json.JsonMapper;
2828

2929
/**
3030
* Configuration properties read from one or more
@@ -55,10 +55,10 @@ Stream<ConfigurationProperty> stream() {
5555

5656
@SuppressWarnings("unchecked")
5757
static ConfigurationProperties fromFiles(Iterable<File> files) {
58-
ObjectMapper objectMapper = new ObjectMapper();
58+
JsonMapper jsonMapper = new JsonMapper();
5959
List<ConfigurationProperty> properties = new ArrayList<>();
6060
for (File file : files) {
61-
Map<String, Object> json = objectMapper.readValue(file, Map.class);
61+
Map<String, Object> json = jsonMapper.readValue(file, Map.class);
6262
for (Map<String, Object> property : (List<Map<String, Object>>) json.get("properties")) {
6363
properties.add(ConfigurationProperty.fromJsonProperties(property));
6464
}

buildpack/spring-boot-buildpack-platform/src/main/java/org/springframework/boot/buildpack/platform/docker/type/ImageArchive.java

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131

3232
import org.jspecify.annotations.Nullable;
3333
import tools.jackson.databind.JsonNode;
34-
import tools.jackson.databind.ObjectMapper;
34+
import tools.jackson.databind.json.JsonMapper;
3535
import tools.jackson.databind.node.ArrayNode;
3636
import tools.jackson.databind.node.ObjectNode;
3737

@@ -67,7 +67,7 @@ public class ImageArchive implements TarArchive {
6767
private static final IOConsumer<Update> NO_UPDATES = (update) -> {
6868
};
6969

70-
private final ObjectMapper objectMapper;
70+
private final JsonMapper jsonMapper;
7171

7272
private final ImageConfig imageConfig;
7373

@@ -85,10 +85,10 @@ public class ImageArchive implements TarArchive {
8585

8686
private final List<Layer> newLayers;
8787

88-
ImageArchive(ObjectMapper objectMapper, ImageConfig imageConfig, Instant createDate, @Nullable ImageReference tag,
88+
ImageArchive(JsonMapper jsonMapper, ImageConfig imageConfig, Instant createDate, @Nullable ImageReference tag,
8989
String os, @Nullable String architecture, @Nullable String variant, List<LayerId> existingLayers,
9090
List<Layer> newLayers) {
91-
this.objectMapper = objectMapper;
91+
this.jsonMapper = jsonMapper;
9292
this.imageConfig = imageConfig;
9393
this.createDate = createDate;
9494
this.tag = tag;
@@ -158,7 +158,7 @@ private LayerId writeLayer(Layout writer, Layer layer) throws IOException {
158158
private String writeConfig(Layout writer, List<LayerId> writtenLayers) throws IOException {
159159
try {
160160
ObjectNode config = createConfig(writtenLayers);
161-
String json = this.objectMapper.writeValueAsString(config).replace("\r\n", "\n");
161+
String json = this.jsonMapper.writeValueAsString(config).replace("\r\n", "\n");
162162
MessageDigest digest = MessageDigest.getInstance("SHA-256");
163163
InspectedContent content = InspectedContent.of(Content.of(json), digest::update);
164164
String name = LayerId.ofSha256Digest(digest.digest()).getHash() + ".json";
@@ -171,7 +171,7 @@ private String writeConfig(Layout writer, List<LayerId> writtenLayers) throws IO
171171
}
172172

173173
private ObjectNode createConfig(List<LayerId> writtenLayers) {
174-
ObjectNode config = this.objectMapper.createObjectNode();
174+
ObjectNode config = this.jsonMapper.createObjectNode();
175175
config.set("Config", this.imageConfig.getNodeCopy());
176176
config.set("Created", config.stringNode(getCreatedDate()));
177177
config.set("History", createHistory(writtenLayers));
@@ -187,7 +187,7 @@ private String getCreatedDate() {
187187
}
188188

189189
private JsonNode createHistory(List<LayerId> writtenLayers) {
190-
ArrayNode history = this.objectMapper.createArrayNode();
190+
ArrayNode history = this.jsonMapper.createArrayNode();
191191
int size = this.existingLayers.size() + writtenLayers.size();
192192
for (int i = 0; i < size; i++) {
193193
history.addObject();
@@ -196,7 +196,7 @@ private JsonNode createHistory(List<LayerId> writtenLayers) {
196196
}
197197

198198
private JsonNode createRootFs(List<LayerId> writtenLayers) {
199-
ObjectNode rootFs = this.objectMapper.createObjectNode();
199+
ObjectNode rootFs = this.jsonMapper.createObjectNode();
200200
ArrayNode diffIds = rootFs.putArray("diff_ids");
201201
this.existingLayers.stream().map(Object::toString).forEach(diffIds::add);
202202
writtenLayers.stream().map(Object::toString).forEach(diffIds::add);
@@ -205,12 +205,12 @@ private JsonNode createRootFs(List<LayerId> writtenLayers) {
205205

206206
private void writeManifest(Layout writer, String config, List<LayerId> writtenLayers) throws IOException {
207207
ArrayNode manifest = createManifest(config, writtenLayers);
208-
String manifestJson = this.objectMapper.writeValueAsString(manifest);
208+
String manifestJson = this.jsonMapper.writeValueAsString(manifest);
209209
writer.file("manifest.json", Owner.ROOT, Content.of(manifestJson));
210210
}
211211

212212
private ArrayNode createManifest(String config, List<LayerId> writtenLayers) {
213-
ArrayNode manifest = this.objectMapper.createArrayNode();
213+
ArrayNode manifest = this.jsonMapper.createArrayNode();
214214
ObjectNode entry = manifest.addObject();
215215
entry.set("Config", entry.stringNode(config));
216216
entry.set("Layers", getManifestLayers(writtenLayers));
@@ -221,7 +221,7 @@ private ArrayNode createManifest(String config, List<LayerId> writtenLayers) {
221221
}
222222

223223
private ArrayNode getManifestLayers(List<LayerId> writtenLayers) {
224-
ArrayNode layers = this.objectMapper.createArrayNode();
224+
ArrayNode layers = this.jsonMapper.createArrayNode();
225225
for (int i = 0; i < this.existingLayers.size(); i++) {
226226
layers.add(EMPTY_LAYER_NAME_PREFIX + i);
227227
}

buildpack/spring-boot-buildpack-platform/src/main/java/org/springframework/boot/buildpack/platform/json/MappedObject.java

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@
3333
import org.jspecify.annotations.Nullable;
3434
import tools.jackson.core.JacksonException;
3535
import tools.jackson.databind.JsonNode;
36-
import tools.jackson.databind.ObjectMapper;
3736
import tools.jackson.databind.json.JsonMapper;
3837

3938
import org.springframework.util.Assert;
@@ -162,7 +161,7 @@ protected static <T extends MappedObject> T getRoot(Object proxy) {
162161
* @throws IOException on IO error
163162
*/
164163
protected static <T extends MappedObject> T of(String content, Function<JsonNode, T> factory) throws IOException {
165-
return of(content, ObjectMapper::readTree, factory);
164+
return of(content, JsonMapper::readTree, factory);
166165
}
167166

168167
/**
@@ -175,7 +174,7 @@ protected static <T extends MappedObject> T of(String content, Function<JsonNode
175174
*/
176175
protected static <T extends MappedObject> T of(InputStream content, Function<JsonNode, T> factory)
177176
throws IOException {
178-
return of(StreamUtils.nonClosing(content), ObjectMapper::readTree, factory);
177+
return of(StreamUtils.nonClosing(content), JsonMapper::readTree, factory);
179178
}
180179

181180
/**
@@ -205,12 +204,12 @@ protected interface ContentReader<C> {
205204

206205
/**
207206
* Read JSON content as a {@link JsonNode}.
208-
* @param objectMapper the source object mapper
207+
* @param jsonMapper the source json mapper
209208
* @param content the content to read
210209
* @return a {@link JsonNode}
211210
* @throws IOException on IO error
212211
*/
213-
JsonNode read(ObjectMapper objectMapper, C content) throws IOException;
212+
JsonNode read(JsonMapper jsonMapper, C content) throws IOException;
214213

215214
}
216215

core/spring-boot-docker-compose/src/main/java/org/springframework/boot/docker/compose/core/DockerJson.java

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
import tools.jackson.databind.DeserializationFeature;
2323
import tools.jackson.databind.JavaType;
2424
import tools.jackson.databind.MapperFeature;
25-
import tools.jackson.databind.ObjectMapper;
2625
import tools.jackson.databind.json.JsonMapper;
2726

2827
/**
@@ -34,7 +33,7 @@
3433
*/
3534
final class DockerJson {
3635

37-
private static final ObjectMapper objectMapper = JsonMapper.builder()
36+
private static final JsonMapper jsonMapper = JsonMapper.builder()
3837
.defaultLocale(Locale.ENGLISH)
3938
.enable(MapperFeature.ACCEPT_CASE_INSENSITIVE_PROPERTIES)
4039
.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES)
@@ -53,7 +52,7 @@ private DockerJson() {
5352
*/
5453
static <T> List<T> deserializeToList(String json, Class<T> itemType) {
5554
if (json.startsWith("[")) {
56-
JavaType javaType = objectMapper.getTypeFactory().constructCollectionType(List.class, itemType);
55+
JavaType javaType = jsonMapper.getTypeFactory().constructCollectionType(List.class, itemType);
5756
return deserialize(json, javaType);
5857
}
5958
return json.trim().lines().map((line) -> deserialize(line, itemType)).toList();
@@ -67,11 +66,11 @@ static <T> List<T> deserializeToList(String json, Class<T> itemType) {
6766
* @return the deserialized result
6867
*/
6968
static <T> T deserialize(String json, Class<T> type) {
70-
return deserialize(json, objectMapper.getTypeFactory().constructType(type));
69+
return deserialize(json, jsonMapper.getTypeFactory().constructType(type));
7170
}
7271

7372
private static <T> T deserialize(String json, JavaType type) {
74-
return objectMapper.readValue(json.trim(), type);
73+
return jsonMapper.readValue(json.trim(), type);
7574
}
7675

7776
}

core/spring-boot-test/src/main/java/org/springframework/boot/test/json/JacksonTester.java

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@
3737
import tools.jackson.core.JacksonException;
3838
import tools.jackson.core.JsonGenerator;
3939
import tools.jackson.databind.JavaType;
40-
import tools.jackson.databind.ObjectMapper;
4140
import tools.jackson.databind.ObjectReader;
4241
import tools.jackson.databind.ObjectWriter;
4342
import tools.jackson.databind.json.JsonMapper;
@@ -222,13 +221,13 @@ protected AbstractJsonMarshalTester<Object> createTester(Class<?> resourceLoadCl
222221

223222
private static final class JacksonJsonProvider extends AbstractJsonProvider {
224223

225-
private final ObjectMapper objectMapper;
224+
private final JsonMapper jsonMapper;
226225

227226
private final ObjectReader objectReader;
228227

229-
private JacksonJsonProvider(ObjectMapper objectMapper) {
230-
this.objectMapper = objectMapper;
231-
this.objectReader = objectMapper.reader().forType(Object.class);
228+
private JacksonJsonProvider(JsonMapper jsonMapper) {
229+
this.jsonMapper = jsonMapper;
230+
this.objectReader = jsonMapper.reader().forType(Object.class);
232231
}
233232

234233
@Override
@@ -264,8 +263,8 @@ public Object parse(InputStream jsonStream, String charset) throws InvalidJsonEx
264263
@Override
265264
public String toJson(Object obj) {
266265
StringWriter writer = new StringWriter();
267-
try (JsonGenerator generator = this.objectMapper.createGenerator(writer)) {
268-
this.objectMapper.writeValue(generator, obj);
266+
try (JsonGenerator generator = this.jsonMapper.createGenerator(writer)) {
267+
this.jsonMapper.writeValue(generator, obj);
269268
}
270269
catch (JacksonException ex) {
271270
throw new InvalidJsonException(ex);
@@ -287,10 +286,10 @@ public Object createMap() {
287286

288287
private static final class JacksonMappingProvider implements MappingProvider {
289288

290-
private final ObjectMapper objectMapper;
289+
private final JsonMapper jsonMapper;
291290

292-
private JacksonMappingProvider(ObjectMapper objectMapper) {
293-
this.objectMapper = objectMapper;
291+
private JacksonMappingProvider(JsonMapper jsonMapper) {
292+
this.jsonMapper = jsonMapper;
294293
}
295294

296295
@Override
@@ -299,7 +298,7 @@ private JacksonMappingProvider(ObjectMapper objectMapper) {
299298
return null;
300299
}
301300
try {
302-
return this.objectMapper.convertValue(source, targetType);
301+
return this.jsonMapper.convertValue(source, targetType);
303302
}
304303
catch (Exception ex) {
305304
throw new MappingException(ex);
@@ -313,9 +312,9 @@ private JacksonMappingProvider(ObjectMapper objectMapper) {
313312
if (source == null) {
314313
return null;
315314
}
316-
JavaType type = this.objectMapper.getTypeFactory().constructType(targetType.getType());
315+
JavaType type = this.jsonMapper.getTypeFactory().constructType(targetType.getType());
317316
try {
318-
return (T) this.objectMapper.convertValue(source, type);
317+
return (T) this.jsonMapper.convertValue(source, type);
319318
}
320319
catch (Exception ex) {
321320
throw new MappingException(ex);

0 commit comments

Comments
 (0)