Use JsonMapper instead of ObjectMapper when relevant

This commit updates Jackson 3 support to use JsonMapper instead
of ObjectMapper in converter, codec and view constructors.

Closes gh-35282
This commit is contained in:
Sébastien Deleuze 2025-08-12 10:18:49 +02:00
parent bde806b7fc
commit d115f36400
17 changed files with 94 additions and 105 deletions

View File

@ -32,7 +32,6 @@ import jakarta.jms.Session;
import jakarta.jms.TextMessage; import jakarta.jms.TextMessage;
import org.jspecify.annotations.Nullable; import org.jspecify.annotations.Nullable;
import tools.jackson.databind.JavaType; import tools.jackson.databind.JavaType;
import tools.jackson.databind.ObjectMapper;
import tools.jackson.databind.ObjectWriter; import tools.jackson.databind.ObjectWriter;
import tools.jackson.databind.cfg.MapperBuilder; import tools.jackson.databind.cfg.MapperBuilder;
import tools.jackson.databind.json.JsonMapper; import tools.jackson.databind.json.JsonMapper;
@ -63,7 +62,7 @@ public class JacksonJsonMessageConverter implements SmartMessageConverter, BeanC
public static final String DEFAULT_ENCODING = "UTF-8"; public static final String DEFAULT_ENCODING = "UTF-8";
private final ObjectMapper objectMapper; private final JsonMapper jsonMapper;
private MessageType targetType = MessageType.BYTES; private MessageType targetType = MessageType.BYTES;
@ -86,17 +85,17 @@ public class JacksonJsonMessageConverter implements SmartMessageConverter, BeanC
* {@link MapperBuilder#findModules(ClassLoader)}. * {@link MapperBuilder#findModules(ClassLoader)}.
*/ */
public JacksonJsonMessageConverter() { public JacksonJsonMessageConverter() {
this.objectMapper = JsonMapper.builder().findAndAddModules(JacksonJsonMessageConverter.class.getClassLoader()).build(); this.jsonMapper = JsonMapper.builder().findAndAddModules(JacksonJsonMessageConverter.class.getClassLoader()).build();
} }
/** /**
* Construct a new instance with the provided {@link ObjectMapper}. * Construct a new instance with the provided {@link JsonMapper}.
* @see JsonMapper#builder() * @see JsonMapper#builder()
* @see MapperBuilder#findModules(ClassLoader) * @see MapperBuilder#findModules(ClassLoader)
*/ */
public JacksonJsonMessageConverter(ObjectMapper objectMapper) { public JacksonJsonMessageConverter(JsonMapper jsonMapper) {
Assert.notNull(objectMapper, "ObjectMapper must not be null"); Assert.notNull(jsonMapper, "JsonMapper must not be null");
this.objectMapper = objectMapper; this.jsonMapper = jsonMapper;
} }
/** /**
@ -173,9 +172,9 @@ public class JacksonJsonMessageConverter implements SmartMessageConverter, BeanC
Message message; Message message;
try { try {
message = switch (this.targetType) { message = switch (this.targetType) {
case TEXT -> mapToTextMessage(object, session, this.objectMapper.writer()); case TEXT -> mapToTextMessage(object, session, this.jsonMapper.writer());
case BYTES -> mapToBytesMessage(object, session, this.objectMapper.writer()); case BYTES -> mapToBytesMessage(object, session, this.jsonMapper.writer());
default -> mapToMessage(object, session, this.objectMapper.writer(), this.targetType); default -> mapToMessage(object, session, this.jsonMapper.writer(), this.targetType);
}; };
} }
catch (IOException ex) { catch (IOException ex) {
@ -206,10 +205,10 @@ public class JacksonJsonMessageConverter implements SmartMessageConverter, BeanC
throws JMSException, MessageConversionException { throws JMSException, MessageConversionException {
if (jsonView != null) { if (jsonView != null) {
return toMessage(object, session, this.objectMapper.writerWithView(jsonView)); return toMessage(object, session, this.jsonMapper.writerWithView(jsonView));
} }
else { else {
return toMessage(object, session, this.objectMapper.writer()); return toMessage(object, session, this.jsonMapper.writer());
} }
} }
@ -363,7 +362,7 @@ public class JacksonJsonMessageConverter implements SmartMessageConverter, BeanC
throws JMSException, IOException { throws JMSException, IOException {
String body = message.getText(); String body = message.getText();
return this.objectMapper.readValue(body, targetJavaType); return this.jsonMapper.readValue(body, targetJavaType);
} }
/** /**
@ -386,7 +385,7 @@ public class JacksonJsonMessageConverter implements SmartMessageConverter, BeanC
if (encoding != null) { if (encoding != null) {
try { try {
String body = new String(bytes, encoding); String body = new String(bytes, encoding);
return this.objectMapper.readValue(body, targetJavaType); return this.jsonMapper.readValue(body, targetJavaType);
} }
catch (UnsupportedEncodingException ex) { catch (UnsupportedEncodingException ex) {
throw new MessageConversionException("Cannot convert bytes to String", ex); throw new MessageConversionException("Cannot convert bytes to String", ex);
@ -394,7 +393,7 @@ public class JacksonJsonMessageConverter implements SmartMessageConverter, BeanC
} }
else { else {
// Jackson internally performs encoding detection, falling back to UTF-8. // Jackson internally performs encoding detection, falling back to UTF-8.
return this.objectMapper.readValue(bytes, targetJavaType); return this.jsonMapper.readValue(bytes, targetJavaType);
} }
} }
@ -437,11 +436,11 @@ public class JacksonJsonMessageConverter implements SmartMessageConverter, BeanC
} }
Class<?> mappedClass = this.idClassMappings.get(typeId); Class<?> mappedClass = this.idClassMappings.get(typeId);
if (mappedClass != null) { if (mappedClass != null) {
return this.objectMapper.constructType(mappedClass); return this.jsonMapper.constructType(mappedClass);
} }
try { try {
Class<?> typeClass = ClassUtils.forName(typeId, this.beanClassLoader); Class<?> typeClass = ClassUtils.forName(typeId, this.beanClassLoader);
return this.objectMapper.constructType(typeClass); return this.jsonMapper.constructType(typeClass);
} }
catch (Throwable ex) { catch (Throwable ex) {
throw new MessageConversionException("Failed to resolve type id [" + typeId + "]", ex); throw new MessageConversionException("Failed to resolve type id [" + typeId + "]", ex);

View File

@ -27,7 +27,6 @@ import tools.jackson.core.JacksonException;
import tools.jackson.core.JsonEncoding; import tools.jackson.core.JsonEncoding;
import tools.jackson.core.JsonGenerator; import tools.jackson.core.JsonGenerator;
import tools.jackson.databind.JavaType; import tools.jackson.databind.JavaType;
import tools.jackson.databind.ObjectMapper;
import tools.jackson.databind.cfg.MapperBuilder; import tools.jackson.databind.cfg.MapperBuilder;
import tools.jackson.databind.json.JsonMapper; import tools.jackson.databind.json.JsonMapper;
@ -52,7 +51,7 @@ public class JacksonJsonMessageConverter extends AbstractMessageConverter {
private static final MimeType[] DEFAULT_MIME_TYPES = new MimeType[] { private static final MimeType[] DEFAULT_MIME_TYPES = new MimeType[] {
new MimeType("application", "json"), new MimeType("application", "*+json")}; new MimeType("application", "json"), new MimeType("application", "*+json")};
private final ObjectMapper objectMapper; private final JsonMapper jsonMapper;
/** /**
@ -73,35 +72,35 @@ public class JacksonJsonMessageConverter extends AbstractMessageConverter {
*/ */
public JacksonJsonMessageConverter(MimeType... supportedMimeTypes) { public JacksonJsonMessageConverter(MimeType... supportedMimeTypes) {
super(supportedMimeTypes); super(supportedMimeTypes);
this.objectMapper = JsonMapper.builder().findAndAddModules(JacksonJsonMessageConverter.class.getClassLoader()).build(); this.jsonMapper = JsonMapper.builder().findAndAddModules(JacksonJsonMessageConverter.class.getClassLoader()).build();
} }
/** /**
* Construct a new instance with the provided {@link ObjectMapper}. * Construct a new instance with the provided {@link JsonMapper}.
* @see JsonMapper#builder() * @see JsonMapper#builder()
* @see MapperBuilder#findModules(ClassLoader) * @see MapperBuilder#findModules(ClassLoader)
*/ */
public JacksonJsonMessageConverter(ObjectMapper objectMapper) { public JacksonJsonMessageConverter(JsonMapper jsonMapper) {
this(objectMapper, DEFAULT_MIME_TYPES); this(jsonMapper, DEFAULT_MIME_TYPES);
} }
/** /**
* Construct a new instance with the provided {@link ObjectMapper} and the * Construct a new instance with the provided {@link JsonMapper} and the
* provided {@link MimeType}s. * provided {@link MimeType}s.
* @see JsonMapper#builder() * @see JsonMapper#builder()
* @see MapperBuilder#findModules(ClassLoader) * @see MapperBuilder#findModules(ClassLoader)
*/ */
public JacksonJsonMessageConverter(ObjectMapper objectMapper, MimeType... supportedMimeTypes) { public JacksonJsonMessageConverter(JsonMapper jsonMapper, MimeType... supportedMimeTypes) {
super(supportedMimeTypes); super(supportedMimeTypes);
Assert.notNull(objectMapper, "ObjectMapper must not be null"); Assert.notNull(jsonMapper, "JsonMapper must not be null");
this.objectMapper = objectMapper; this.jsonMapper = jsonMapper;
} }
/** /**
* Return the underlying {@code ObjectMapper} for this converter. * Return the underlying {@code JsonMapper} for this converter.
*/ */
protected ObjectMapper getObjectMapper() { protected JsonMapper getJsonMapper() {
return this.objectMapper; return this.jsonMapper;
} }
@Override @Override
@ -122,7 +121,7 @@ public class JacksonJsonMessageConverter extends AbstractMessageConverter {
@Override @Override
protected @Nullable Object convertFromInternal(Message<?> message, Class<?> targetClass, @Nullable Object conversionHint) { protected @Nullable Object convertFromInternal(Message<?> message, Class<?> targetClass, @Nullable Object conversionHint) {
JavaType javaType = this.objectMapper.constructType(getResolvedType(targetClass, conversionHint)); JavaType javaType = this.jsonMapper.constructType(getResolvedType(targetClass, conversionHint));
Object payload = message.getPayload(); Object payload = message.getPayload();
Class<?> view = getSerializationView(conversionHint); Class<?> view = getSerializationView(conversionHint);
try { try {
@ -131,19 +130,19 @@ public class JacksonJsonMessageConverter extends AbstractMessageConverter {
} }
else if (payload instanceof byte[] bytes) { else if (payload instanceof byte[] bytes) {
if (view != null) { if (view != null) {
return this.objectMapper.readerWithView(view).forType(javaType).readValue(bytes); return this.jsonMapper.readerWithView(view).forType(javaType).readValue(bytes);
} }
else { else {
return this.objectMapper.readValue(bytes, javaType); return this.jsonMapper.readValue(bytes, javaType);
} }
} }
else { else {
// Assuming a text-based source payload // Assuming a text-based source payload
if (view != null) { if (view != null) {
return this.objectMapper.readerWithView(view).forType(javaType).readValue(payload.toString()); return this.jsonMapper.readerWithView(view).forType(javaType).readValue(payload.toString());
} }
else { else {
return this.objectMapper.readValue(payload.toString(), javaType); return this.jsonMapper.readValue(payload.toString(), javaType);
} }
} }
} }
@ -161,12 +160,12 @@ public class JacksonJsonMessageConverter extends AbstractMessageConverter {
if (byte[].class == getSerializedPayloadClass()) { if (byte[].class == getSerializedPayloadClass()) {
ByteArrayOutputStream out = new ByteArrayOutputStream(1024); ByteArrayOutputStream out = new ByteArrayOutputStream(1024);
JsonEncoding encoding = getJsonEncoding(getMimeType(headers)); JsonEncoding encoding = getJsonEncoding(getMimeType(headers));
try (JsonGenerator generator = this.objectMapper.createGenerator(out, encoding)) { try (JsonGenerator generator = this.jsonMapper.createGenerator(out, encoding)) {
if (view != null) { if (view != null) {
this.objectMapper.writerWithView(view).writeValue(generator, payload); this.jsonMapper.writerWithView(view).writeValue(generator, payload);
} }
else { else {
this.objectMapper.writeValue(generator, payload); this.jsonMapper.writeValue(generator, payload);
} }
payload = out.toByteArray(); payload = out.toByteArray();
} }
@ -175,10 +174,10 @@ public class JacksonJsonMessageConverter extends AbstractMessageConverter {
// Assuming a text-based target payload // Assuming a text-based target payload
Writer writer = new StringWriter(1024); Writer writer = new StringWriter(1024);
if (view != null) { if (view != null) {
this.objectMapper.writerWithView(view).writeValue(writer, payload); this.jsonMapper.writerWithView(view).writeValue(writer, payload);
} }
else { else {
this.objectMapper.writeValue(writer, payload); this.jsonMapper.writeValue(writer, payload);
} }
payload = writer.toString(); payload = writer.toString();
} }

View File

@ -45,7 +45,7 @@ import org.junit.jupiter.params.provider.ValueSource;
import org.skyscreamer.jsonassert.JSONCompareMode; import org.skyscreamer.jsonassert.JSONCompareMode;
import org.skyscreamer.jsonassert.JSONCompareResult; import org.skyscreamer.jsonassert.JSONCompareResult;
import org.skyscreamer.jsonassert.comparator.JSONComparator; import org.skyscreamer.jsonassert.comparator.JSONComparator;
import tools.jackson.databind.ObjectMapper; import tools.jackson.databind.json.JsonMapper;
import org.springframework.core.io.ByteArrayResource; import org.springframework.core.io.ByteArrayResource;
import org.springframework.core.io.ClassPathResource; import org.springframework.core.io.ClassPathResource;
@ -86,7 +86,7 @@ class AbstractJsonContentAssertTests {
private static final String DIFFERENT = loadJson("different.json"); private static final String DIFFERENT = loadJson("different.json");
private static final HttpMessageContentConverter jsonContentConverter = HttpMessageContentConverter.of( private static final HttpMessageContentConverter jsonContentConverter = HttpMessageContentConverter.of(
new JacksonJsonHttpMessageConverter(new ObjectMapper())); new JacksonJsonHttpMessageConverter(new JsonMapper()));
private static final JsonComparator comparator = JsonAssert.comparator(JsonCompareMode.LENIENT); private static final JsonComparator comparator = JsonAssert.comparator(JsonCompareMode.LENIENT);

View File

@ -27,7 +27,7 @@ import org.assertj.core.data.Offset;
import org.jspecify.annotations.Nullable; import org.jspecify.annotations.Nullable;
import org.junit.jupiter.api.Nested; import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import tools.jackson.databind.ObjectMapper; import tools.jackson.databind.json.JsonMapper;
import org.springframework.http.converter.json.JacksonJsonHttpMessageConverter; import org.springframework.http.converter.json.JacksonJsonHttpMessageConverter;
import org.springframework.test.http.HttpMessageContentConverter; import org.springframework.test.http.HttpMessageContentConverter;
@ -206,7 +206,7 @@ class JsonPathValueAssertTests {
class ConvertToTests { class ConvertToTests {
private static final HttpMessageContentConverter jsonContentConverter = HttpMessageContentConverter.of( private static final HttpMessageContentConverter jsonContentConverter = HttpMessageContentConverter.of(
new JacksonJsonHttpMessageConverter(new ObjectMapper())); new JacksonJsonHttpMessageConverter(new JsonMapper()));
@Test @Test
void convertToWithoutHttpMessageConverter() { void convertToWithoutHttpMessageConverter() {

View File

@ -27,7 +27,7 @@ import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource; import org.junit.jupiter.params.provider.ValueSource;
import tools.jackson.databind.JavaType; import tools.jackson.databind.JavaType;
import tools.jackson.databind.ObjectMapper; import tools.jackson.databind.json.JsonMapper;
import org.springframework.core.ParameterizedTypeReference; import org.springframework.core.ParameterizedTypeReference;
@ -385,14 +385,14 @@ class JsonPathExpectationsHelperTests {
*/ */
private static class JacksonMappingProvider implements MappingProvider { private static class JacksonMappingProvider implements MappingProvider {
private final ObjectMapper objectMapper; private final JsonMapper jsonMapper;
public JacksonMappingProvider() { public JacksonMappingProvider() {
this(new ObjectMapper()); this(new JsonMapper());
} }
public JacksonMappingProvider(ObjectMapper objectMapper) { public JacksonMappingProvider(JsonMapper jsonMapper) {
this.objectMapper = objectMapper; this.jsonMapper = jsonMapper;
} }
@ -402,7 +402,7 @@ class JsonPathExpectationsHelperTests {
return null; return null;
} }
try { try {
return objectMapper.convertValue(source, targetType); return jsonMapper.convertValue(source, targetType);
} }
catch (Exception ex) { catch (Exception ex) {
throw new MappingException(ex); throw new MappingException(ex);
@ -416,10 +416,10 @@ class JsonPathExpectationsHelperTests {
if (source == null){ if (source == null){
return null; return null;
} }
JavaType type = objectMapper.getTypeFactory().constructType(targetType.getType()); JavaType type = jsonMapper.getTypeFactory().constructType(targetType.getType());
try { try {
return (T) objectMapper.convertValue(source, type); return (T) jsonMapper.convertValue(source, type);
} }
catch (Exception ex) { catch (Exception ex) {
throw new MappingException(ex); throw new MappingException(ex);

View File

@ -22,7 +22,7 @@ import java.util.Map;
import com.jayway.jsonpath.Configuration; import com.jayway.jsonpath.Configuration;
import com.jayway.jsonpath.TypeRef; import com.jayway.jsonpath.TypeRef;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import tools.jackson.databind.ObjectMapper; import tools.jackson.databind.json.JsonMapper;
import org.springframework.http.codec.json.JacksonJsonDecoder; import org.springframework.http.codec.json.JacksonJsonDecoder;
import org.springframework.http.codec.json.JacksonJsonEncoder; import org.springframework.http.codec.json.JacksonJsonEncoder;
@ -36,10 +36,10 @@ import static org.assertj.core.api.Assertions.assertThat;
*/ */
class EncoderDecoderMappingProviderTests { class EncoderDecoderMappingProviderTests {
private static final ObjectMapper objectMapper = new ObjectMapper(); private static final JsonMapper jsonMapper = new JsonMapper();
private final EncoderDecoderMappingProvider mappingProvider = new EncoderDecoderMappingProvider( private final EncoderDecoderMappingProvider mappingProvider = new EncoderDecoderMappingProvider(
new JacksonJsonEncoder(objectMapper), new JacksonJsonDecoder(objectMapper)); new JacksonJsonEncoder(jsonMapper), new JacksonJsonDecoder(jsonMapper));
@Test @Test

View File

@ -19,7 +19,7 @@ package org.springframework.test.web.reactive.server;
import java.util.List; import java.util.List;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import tools.jackson.databind.ObjectMapper; import tools.jackson.databind.json.JsonMapper;
import org.springframework.http.codec.DecoderHttpMessageReader; import org.springframework.http.codec.DecoderHttpMessageReader;
import org.springframework.http.codec.EncoderHttpMessageWriter; import org.springframework.http.codec.EncoderHttpMessageWriter;
@ -39,13 +39,13 @@ import static org.assertj.core.api.Assertions.assertThat;
*/ */
class JsonEncoderDecoderTests { class JsonEncoderDecoderTests {
private static final ObjectMapper objectMapper = new ObjectMapper(); private static final JsonMapper jsonMapper = new JsonMapper();
private static final HttpMessageWriter<?> jacksonMessageWriter = new EncoderHttpMessageWriter<>( private static final HttpMessageWriter<?> jacksonMessageWriter = new EncoderHttpMessageWriter<>(
new JacksonJsonEncoder(objectMapper)); new JacksonJsonEncoder(jsonMapper));
private static final HttpMessageReader<?> jacksonMessageReader = new DecoderHttpMessageReader<>( private static final HttpMessageReader<?> jacksonMessageReader = new DecoderHttpMessageReader<>(
new JacksonJsonDecoder(objectMapper)); new JacksonJsonDecoder(jsonMapper));
@Test @Test
void fromWithEmptyWriters() { void fromWithEmptyWriters() {

View File

@ -25,7 +25,6 @@ import java.util.Map;
import org.jspecify.annotations.Nullable; import org.jspecify.annotations.Nullable;
import org.reactivestreams.Publisher; import org.reactivestreams.Publisher;
import reactor.core.publisher.Flux; import reactor.core.publisher.Flux;
import tools.jackson.databind.ObjectMapper;
import tools.jackson.databind.cfg.MapperBuilder; import tools.jackson.databind.cfg.MapperBuilder;
import tools.jackson.databind.json.JsonMapper; import tools.jackson.databind.json.JsonMapper;
@ -73,20 +72,20 @@ public class JacksonJsonDecoder extends AbstractJacksonDecoder {
} }
/** /**
* Construct a new instance with the provided {@link ObjectMapper}. * Construct a new instance with the provided {@link JsonMapper}.
* @see JsonMapper#builder() * @see JsonMapper#builder()
* @see MapperBuilder#findModules(ClassLoader) * @see MapperBuilder#findModules(ClassLoader)
*/ */
public JacksonJsonDecoder(ObjectMapper mapper) { public JacksonJsonDecoder(JsonMapper mapper) {
this(mapper, DEFAULT_JSON_MIME_TYPES); this(mapper, DEFAULT_JSON_MIME_TYPES);
} }
/** /**
* Construct a new instance with the provided {@link ObjectMapper} and {@link MimeType}s. * Construct a new instance with the provided {@link JsonMapper} and {@link MimeType}s.
* @see JsonMapper#builder() * @see JsonMapper#builder()
* @see MapperBuilder#findModules(ClassLoader) * @see MapperBuilder#findModules(ClassLoader)
*/ */
public JacksonJsonDecoder(ObjectMapper mapper, MimeType... mimeTypes) { public JacksonJsonDecoder(JsonMapper mapper, MimeType... mimeTypes) {
super(mapper, mimeTypes); super(mapper, mimeTypes);
} }

View File

@ -25,7 +25,6 @@ import reactor.core.publisher.Flux;
import tools.jackson.core.PrettyPrinter; import tools.jackson.core.PrettyPrinter;
import tools.jackson.core.util.DefaultIndenter; import tools.jackson.core.util.DefaultIndenter;
import tools.jackson.core.util.DefaultPrettyPrinter; import tools.jackson.core.util.DefaultPrettyPrinter;
import tools.jackson.databind.ObjectMapper;
import tools.jackson.databind.ObjectWriter; import tools.jackson.databind.ObjectWriter;
import tools.jackson.databind.SerializationFeature; import tools.jackson.databind.SerializationFeature;
import tools.jackson.databind.cfg.MapperBuilder; import tools.jackson.databind.cfg.MapperBuilder;
@ -80,21 +79,21 @@ public class JacksonJsonEncoder extends AbstractJacksonEncoder {
} }
/** /**
* Construct a new instance with the provided {@link ObjectMapper}. * Construct a new instance with the provided {@link JsonMapper}.
* @see JsonMapper#builder() * @see JsonMapper#builder()
* @see MapperBuilder#findModules(ClassLoader) * @see MapperBuilder#findModules(ClassLoader)
*/ */
public JacksonJsonEncoder(ObjectMapper mapper) { public JacksonJsonEncoder(JsonMapper mapper) {
this(mapper, DEFAULT_JSON_MIME_TYPES); this(mapper, DEFAULT_JSON_MIME_TYPES);
} }
/** /**
* Construct a new instance with the provided {@link ObjectMapper} and * Construct a new instance with the provided {@link JsonMapper} and
* {@link MimeType}s. * {@link MimeType}s.
* @see JsonMapper#builder() * @see JsonMapper#builder()
* @see MapperBuilder#findModules(ClassLoader) * @see MapperBuilder#findModules(ClassLoader)
*/ */
public JacksonJsonEncoder(ObjectMapper mapper, MimeType... mimeTypes) { public JacksonJsonEncoder(JsonMapper mapper, MimeType... mimeTypes) {
super(mapper, mimeTypes); super(mapper, mimeTypes);
setStreamingMediaTypes(List.of(MediaType.APPLICATION_NDJSON)); setStreamingMediaTypes(List.of(MediaType.APPLICATION_NDJSON));
this.ssePrettyPrinter = initSsePrettyPrinter(); this.ssePrettyPrinter = initSsePrettyPrinter();

View File

@ -21,7 +21,6 @@ import java.util.List;
import org.jspecify.annotations.Nullable; import org.jspecify.annotations.Nullable;
import tools.jackson.core.JsonGenerator; import tools.jackson.core.JsonGenerator;
import tools.jackson.databind.ObjectMapper;
import tools.jackson.databind.cfg.MapperBuilder; import tools.jackson.databind.cfg.MapperBuilder;
import tools.jackson.databind.json.JsonMapper; import tools.jackson.databind.json.JsonMapper;
@ -32,7 +31,7 @@ import org.springframework.http.converter.AbstractJacksonHttpMessageConverter;
/** /**
* Implementation of {@link org.springframework.http.converter.HttpMessageConverter} * Implementation of {@link org.springframework.http.converter.HttpMessageConverter}
* that can read and write JSON using <a href="https://github.com/FasterXML/jackson">Jackson 3.x's</a> * that can read and write JSON using <a href="https://github.com/FasterXML/jackson">Jackson 3.x's</a>
* {@link ObjectMapper}. * {@link JsonMapper}.
* *
* <p>This converter can be used to bind to typed beans, or untyped * <p>This converter can be used to bind to typed beans, or untyped
* {@code HashMap} instances. * {@code HashMap} instances.
@ -79,11 +78,11 @@ public class JacksonJsonHttpMessageConverter extends AbstractJacksonHttpMessageC
} }
/** /**
* Construct a new instance with the provided {@link ObjectMapper}. * Construct a new instance with the provided {@link JsonMapper}.
* @see JsonMapper#builder() * @see JsonMapper#builder()
* @see MapperBuilder#findModules(ClassLoader) * @see MapperBuilder#findModules(ClassLoader)
*/ */
public JacksonJsonHttpMessageConverter(ObjectMapper objectMapper) { public JacksonJsonHttpMessageConverter(JsonMapper objectMapper) {
super(objectMapper, DEFAULT_JSON_MIME_TYPES); super(objectMapper, DEFAULT_JSON_MIME_TYPES);
} }

View File

@ -31,7 +31,6 @@ import reactor.test.StepVerifier;
import tools.jackson.core.JsonParser; import tools.jackson.core.JsonParser;
import tools.jackson.databind.DeserializationContext; import tools.jackson.databind.DeserializationContext;
import tools.jackson.databind.JsonNode; import tools.jackson.databind.JsonNode;
import tools.jackson.databind.ObjectMapper;
import tools.jackson.databind.annotation.JsonDeserialize; import tools.jackson.databind.annotation.JsonDeserialize;
import tools.jackson.databind.deser.std.StdDeserializer; import tools.jackson.databind.deser.std.StdDeserializer;
import tools.jackson.databind.json.JsonMapper; import tools.jackson.databind.json.JsonMapper;
@ -102,8 +101,8 @@ class JacksonJsonDecoderTests extends AbstractDecoderTests<JacksonJsonDecoder> {
assertThat(decoder.canDecode(ResolvableType.forClass(Map.class), MediaType.APPLICATION_JSON)).isTrue(); assertThat(decoder.canDecode(ResolvableType.forClass(Map.class), MediaType.APPLICATION_JSON)).isTrue();
decoder.registerObjectMappersForType(Pojo.class, map -> { decoder.registerObjectMappersForType(Pojo.class, map -> {
map.put(halJsonMediaType, new ObjectMapper()); map.put(halJsonMediaType, new JsonMapper());
map.put(MediaType.APPLICATION_JSON, new ObjectMapper()); map.put(MediaType.APPLICATION_JSON, new JsonMapper());
}); });
assertThat(decoder.canDecode(ResolvableType.forClass(Pojo.class), halJsonMediaType)).isTrue(); assertThat(decoder.canDecode(ResolvableType.forClass(Pojo.class), halJsonMediaType)).isTrue();
@ -115,7 +114,7 @@ class JacksonJsonDecoderTests extends AbstractDecoderTests<JacksonJsonDecoder> {
@Test // SPR-15866 @Test // SPR-15866
void canDecodeWithProvidedMimeType() { void canDecodeWithProvidedMimeType() {
MimeType textJavascript = new MimeType("text", "javascript", StandardCharsets.UTF_8); MimeType textJavascript = new MimeType("text", "javascript", StandardCharsets.UTF_8);
JacksonJsonDecoder decoder = new JacksonJsonDecoder(new ObjectMapper(), textJavascript); JacksonJsonDecoder decoder = new JacksonJsonDecoder(new JsonMapper(), textJavascript);
assertThat(decoder.getDecodableMimeTypes()).isEqualTo(Collections.singletonList(textJavascript)); assertThat(decoder.getDecodableMimeTypes()).isEqualTo(Collections.singletonList(textJavascript));
} }
@ -124,7 +123,7 @@ class JacksonJsonDecoderTests extends AbstractDecoderTests<JacksonJsonDecoder> {
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
void decodableMimeTypesIsImmutable() { void decodableMimeTypesIsImmutable() {
MimeType textJavascript = new MimeType("text", "javascript", StandardCharsets.UTF_8); MimeType textJavascript = new MimeType("text", "javascript", StandardCharsets.UTF_8);
JacksonJsonDecoder decoder = new JacksonJsonDecoder(new ObjectMapper(), textJavascript); JacksonJsonDecoder decoder = new JacksonJsonDecoder(new JsonMapper(), textJavascript);
assertThatExceptionOfType(UnsupportedOperationException.class).isThrownBy(() -> assertThatExceptionOfType(UnsupportedOperationException.class).isThrownBy(() ->
decoder.getDecodableMimeTypes().add(new MimeType("text", "ecmascript"))); decoder.getDecodableMimeTypes().add(new MimeType("text", "ecmascript")));
@ -135,8 +134,8 @@ class JacksonJsonDecoderTests extends AbstractDecoderTests<JacksonJsonDecoder> {
MimeType mimeType1 = MediaType.parseMediaType("application/hal+json"); MimeType mimeType1 = MediaType.parseMediaType("application/hal+json");
MimeType mimeType2 = new MimeType("text", "javascript", StandardCharsets.UTF_8); MimeType mimeType2 = new MimeType("text", "javascript", StandardCharsets.UTF_8);
JacksonJsonDecoder decoder = new JacksonJsonDecoder(new ObjectMapper(), mimeType2); JacksonJsonDecoder decoder = new JacksonJsonDecoder(new JsonMapper(), mimeType2);
decoder.registerObjectMappersForType(Pojo.class, map -> map.put(mimeType1, new ObjectMapper())); decoder.registerObjectMappersForType(Pojo.class, map -> map.put(mimeType1, new JsonMapper()));
assertThat(decoder.getDecodableMimeTypes(ResolvableType.forClass(Pojo.class))) assertThat(decoder.getDecodableMimeTypes(ResolvableType.forClass(Pojo.class)))
.containsExactly(mimeType1); .containsExactly(mimeType1);

View File

@ -28,7 +28,6 @@ import org.junit.jupiter.api.Test;
import reactor.core.publisher.Flux; import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono; import reactor.core.publisher.Mono;
import reactor.test.StepVerifier; import reactor.test.StepVerifier;
import tools.jackson.databind.ObjectMapper;
import tools.jackson.databind.SerializationFeature; import tools.jackson.databind.SerializationFeature;
import tools.jackson.databind.json.JsonMapper; import tools.jackson.databind.json.JsonMapper;
@ -109,7 +108,7 @@ class JacksonJsonEncoderTests extends AbstractEncoderTests<JacksonJsonEncoder> {
@Test // SPR-15866 @Test // SPR-15866
public void canEncodeWithCustomMimeType() { public void canEncodeWithCustomMimeType() {
MimeType textJavascript = new MimeType("text", "javascript", StandardCharsets.UTF_8); MimeType textJavascript = new MimeType("text", "javascript", StandardCharsets.UTF_8);
JacksonJsonEncoder encoder = new JacksonJsonEncoder(new ObjectMapper(), textJavascript); JacksonJsonEncoder encoder = new JacksonJsonEncoder(new JsonMapper(), textJavascript);
assertThat(encoder.getEncodableMimeTypes()).isEqualTo(Collections.singletonList(textJavascript)); assertThat(encoder.getEncodableMimeTypes()).isEqualTo(Collections.singletonList(textJavascript));
} }
@ -117,7 +116,7 @@ class JacksonJsonEncoderTests extends AbstractEncoderTests<JacksonJsonEncoder> {
@Test @Test
void encodableMimeTypesIsImmutable() { void encodableMimeTypesIsImmutable() {
MimeType textJavascript = new MimeType("text", "javascript", StandardCharsets.UTF_8); MimeType textJavascript = new MimeType("text", "javascript", StandardCharsets.UTF_8);
JacksonJsonEncoder encoder = new JacksonJsonEncoder(new ObjectMapper(), textJavascript); JacksonJsonEncoder encoder = new JacksonJsonEncoder(new JsonMapper(), textJavascript);
assertThatExceptionOfType(UnsupportedOperationException.class).isThrownBy(() -> assertThatExceptionOfType(UnsupportedOperationException.class).isThrownBy(() ->
encoder.getEncodableMimeTypes().add(new MimeType("text", "ecmascript"))); encoder.getEncodableMimeTypes().add(new MimeType("text", "ecmascript")));
@ -231,7 +230,7 @@ class JacksonJsonEncoderTests extends AbstractEncoderTests<JacksonJsonEncoder> {
@Test // gh-22771 @Test // gh-22771
public void encodeWithFlushAfterWriteOff() { public void encodeWithFlushAfterWriteOff() {
ObjectMapper mapper = JsonMapper.builder().configure(SerializationFeature.FLUSH_AFTER_WRITE_VALUE, false).build(); JsonMapper mapper = JsonMapper.builder().configure(SerializationFeature.FLUSH_AFTER_WRITE_VALUE, false).build();
JacksonJsonEncoder encoder = new JacksonJsonEncoder(mapper); JacksonJsonEncoder encoder = new JacksonJsonEncoder(mapper);
Flux<DataBuffer> result = encoder.encode(Flux.just(new Pojo("foo", "bar")), this.bufferFactory, Flux<DataBuffer> result = encoder.encode(Flux.just(new Pojo("foo", "bar")), this.bufferFactory,

View File

@ -365,7 +365,7 @@ class JacksonJsonHttpMessageConverterTests {
PrettyPrintBean bean = new PrettyPrintBean(); PrettyPrintBean bean = new PrettyPrintBean();
bean.setName("Jason"); bean.setName("Jason");
ObjectMapper mapper = JsonMapper.builder().enable(SerializationFeature.INDENT_OUTPUT).build(); JsonMapper mapper = JsonMapper.builder().enable(SerializationFeature.INDENT_OUTPUT).build();
this.converter = new JacksonJsonHttpMessageConverter(mapper); this.converter = new JacksonJsonHttpMessageConverter(mapper);
this.converter.write(bean, ResolvableType.forType(PrettyPrintBean.class), this.converter.write(bean, ResolvableType.forType(PrettyPrintBean.class),
MediaType.APPLICATION_JSON, outputMessage, null); MediaType.APPLICATION_JSON, outputMessage, null);
@ -384,7 +384,7 @@ class JacksonJsonHttpMessageConverterTests {
PrettyPrintBean bean = new PrettyPrintBean(); PrettyPrintBean bean = new PrettyPrintBean();
bean.setName("Jason"); bean.setName("Jason");
ObjectMapper mapper = JsonMapper.builder().enable(SerializationFeature.INDENT_OUTPUT).build(); JsonMapper mapper = JsonMapper.builder().enable(SerializationFeature.INDENT_OUTPUT).build();
this.converter = new JacksonJsonHttpMessageConverter(mapper); this.converter = new JacksonJsonHttpMessageConverter(mapper);
this.converter.write(bean, ResolvableType.forType(PrettyPrintBean.class), this.converter.write(bean, ResolvableType.forType(PrettyPrintBean.class),
MediaType.APPLICATION_JSON, outputMessage, null); MediaType.APPLICATION_JSON, outputMessage, null);

View File

@ -24,7 +24,6 @@ import java.util.Set;
import jakarta.servlet.http.HttpServletRequest; import jakarta.servlet.http.HttpServletRequest;
import org.jspecify.annotations.Nullable; import org.jspecify.annotations.Nullable;
import tools.jackson.core.JsonGenerator; import tools.jackson.core.JsonGenerator;
import tools.jackson.databind.ObjectMapper;
import tools.jackson.databind.cfg.MapperBuilder; import tools.jackson.databind.cfg.MapperBuilder;
import tools.jackson.databind.json.JsonMapper; import tools.jackson.databind.json.JsonMapper;
@ -35,7 +34,7 @@ import org.springframework.web.servlet.view.AbstractJacksonView;
/** /**
* Spring MVC {@link View} that renders JSON content by serializing the model for the current request * Spring MVC {@link View} that renders JSON content by serializing the model for the current request
* using <a href="https://github.com/FasterXML/jackson">Jackson 3's</a> {@link ObjectMapper}. * using <a href="https://github.com/FasterXML/jackson">Jackson 3's</a> {@link JsonMapper}.
* *
* <p>By default, the entire contents of the model map (with the exception of framework-specific classes) * <p>By default, the entire contents of the model map (with the exception of framework-specific classes)
* will be encoded as JSON. If the model contains only one key, you can have it extracted encoded as JSON * will be encoded as JSON. If the model contains only one key, you can have it extracted encoded as JSON
@ -79,11 +78,11 @@ public class JacksonJsonView extends AbstractJacksonView {
} }
/** /**
* Construct a new instance using the provided {@link ObjectMapper} * Construct a new instance using the provided {@link JsonMapper}
* and setting the content type to {@value #DEFAULT_CONTENT_TYPE}. * and setting the content type to {@value #DEFAULT_CONTENT_TYPE}.
*/ */
public JacksonJsonView(ObjectMapper objectMapper) { public JacksonJsonView(JsonMapper jsonMapper) {
super(objectMapper, DEFAULT_CONTENT_TYPE); super(jsonMapper, DEFAULT_CONTENT_TYPE);
} }

View File

@ -24,7 +24,6 @@ import java.util.List;
import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import tools.jackson.databind.ObjectMapper;
import tools.jackson.databind.SerializationFeature; import tools.jackson.databind.SerializationFeature;
import tools.jackson.databind.json.JsonMapper; import tools.jackson.databind.json.JsonMapper;
@ -109,8 +108,8 @@ class SseServerResponseTests {
} }
}); });
ObjectMapper objectMapper = JsonMapper.builder().enable(SerializationFeature.INDENT_OUTPUT).build(); JsonMapper jsonMapper = JsonMapper.builder().enable(SerializationFeature.INDENT_OUTPUT).build();
JacksonJsonHttpMessageConverter converter = new JacksonJsonHttpMessageConverter(objectMapper); JacksonJsonHttpMessageConverter converter = new JacksonJsonHttpMessageConverter(jsonMapper);
ServerResponse.Context context = () -> List.of(converter); ServerResponse.Context context = () -> List.of(converter);
ModelAndView mav = response.writeTo(this.mockRequest, this.mockResponse, context); ModelAndView mav = response.writeTo(this.mockRequest, this.mockResponse, context);

View File

@ -33,7 +33,6 @@ import tools.jackson.core.JacksonException;
import tools.jackson.core.JsonGenerator; import tools.jackson.core.JsonGenerator;
import tools.jackson.databind.BeanDescription; import tools.jackson.databind.BeanDescription;
import tools.jackson.databind.JavaType; import tools.jackson.databind.JavaType;
import tools.jackson.databind.ObjectMapper;
import tools.jackson.databind.SerializationContext; import tools.jackson.databind.SerializationContext;
import tools.jackson.databind.SerializationFeature; import tools.jackson.databind.SerializationFeature;
import tools.jackson.databind.ValueSerializer; import tools.jackson.databind.ValueSerializer;
@ -181,7 +180,7 @@ class JacksonJsonViewTests {
@Test @Test
void renderWithCustomSerializerLocatedByFactory() throws Exception { void renderWithCustomSerializerLocatedByFactory() throws Exception {
SerializerFactory factory = new DelegatingSerializerFactory(null); SerializerFactory factory = new DelegatingSerializerFactory(null);
ObjectMapper mapper = JsonMapper.builder().serializerFactory(factory).build(); JsonMapper mapper = JsonMapper.builder().serializerFactory(factory).build();
view = new JacksonJsonView(mapper); view = new JacksonJsonView(mapper);
Object bean = new TestBeanSimple(); Object bean = new TestBeanSimple();

View File

@ -20,7 +20,6 @@ import java.io.InputStream;
import com.fasterxml.jackson.core.io.JsonStringEncoder; import com.fasterxml.jackson.core.io.JsonStringEncoder;
import org.jspecify.annotations.Nullable; import org.jspecify.annotations.Nullable;
import tools.jackson.databind.ObjectMapper;
import tools.jackson.databind.cfg.MapperBuilder; import tools.jackson.databind.cfg.MapperBuilder;
import tools.jackson.databind.json.JsonMapper; import tools.jackson.databind.json.JsonMapper;
@ -37,7 +36,7 @@ import org.springframework.util.Assert;
*/ */
public class JacksonJsonSockJsMessageCodec extends AbstractSockJsMessageCodec { public class JacksonJsonSockJsMessageCodec extends AbstractSockJsMessageCodec {
private final ObjectMapper objectMapper; private final JsonMapper jsonMapper;
/** /**
@ -46,28 +45,28 @@ public class JacksonJsonSockJsMessageCodec extends AbstractSockJsMessageCodec {
* {@link MapperBuilder#findModules(ClassLoader)}. * {@link MapperBuilder#findModules(ClassLoader)}.
*/ */
public JacksonJsonSockJsMessageCodec() { public JacksonJsonSockJsMessageCodec() {
this.objectMapper = JsonMapper.builder().findAndAddModules(JacksonJsonSockJsMessageCodec.class.getClassLoader()).build(); this.jsonMapper = JsonMapper.builder().findAndAddModules(JacksonJsonSockJsMessageCodec.class.getClassLoader()).build();
} }
/** /**
* Construct a new instance with the provided {@link ObjectMapper}. * Construct a new instance with the provided {@link JsonMapper}.
* @see JsonMapper#builder() * @see JsonMapper#builder()
* @see MapperBuilder#findAndAddModules(ClassLoader) * @see MapperBuilder#findAndAddModules(ClassLoader)
*/ */
public JacksonJsonSockJsMessageCodec(ObjectMapper objectMapper) { public JacksonJsonSockJsMessageCodec(JsonMapper jsonMapper) {
Assert.notNull(objectMapper, "ObjectMapper must not be null"); Assert.notNull(jsonMapper, "JsonMapper must not be null");
this.objectMapper = objectMapper; this.jsonMapper = jsonMapper;
} }
@Override @Override
public String @Nullable [] decode(String content) { public String @Nullable [] decode(String content) {
return this.objectMapper.readValue(content, String[].class); return this.jsonMapper.readValue(content, String[].class);
} }
@Override @Override
public String @Nullable [] decodeInputStream(InputStream content) { public String @Nullable [] decodeInputStream(InputStream content) {
return this.objectMapper.readValue(content, String[].class); return this.jsonMapper.readValue(content, String[].class);
} }
@Override @Override