Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
import org.jspecify.annotations.Nullable;
import tools.jackson.core.JacksonException;
import tools.jackson.databind.JsonNode;
import tools.jackson.databind.ObjectMapper;
import tools.jackson.databind.json.JsonMapper;
import tools.jackson.databind.node.ArrayNode;
import tools.jackson.databind.node.NullNode;
Expand Down Expand Up @@ -55,13 +54,13 @@ public class JacksonPropertyAccessor implements PropertyAccessor {
JsonNode.class
};

private ObjectMapper objectMapper = JsonMapper.builder()
private JsonMapper jsonMapper = JsonMapper.builder()
.findAndAddModules(JacksonPropertyAccessor.class.getClassLoader())
.build();

public void setObjectMapper(ObjectMapper objectMapper) {
Assert.notNull(objectMapper, "'objectMapper' cannot be null");
this.objectMapper = objectMapper;
public void setObjectMapper(JsonMapper jsonMapper) {
Assert.notNull(jsonMapper, "'jsonMapper' cannot be null");
this.jsonMapper = jsonMapper;
}

@Override
Expand Down Expand Up @@ -94,7 +93,7 @@ else if (target instanceof JsonNodeWrapper<?> jsonNodeWrapper) {
}
else if (target instanceof String content) {
try {
return this.objectMapper.readTree(content);
return this.jsonMapper.readTree(content);
}
catch (JacksonException e) {
throw new AccessException("Exception while trying to deserialize String", e);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
import org.apache.commons.logging.LogFactory;
import org.jspecify.annotations.Nullable;
import tools.jackson.core.JacksonException;
import tools.jackson.databind.ObjectMapper;
import tools.jackson.databind.json.JsonMapper;

import org.springframework.integration.mapping.BytesMessageMapper;
import org.springframework.integration.support.MutableMessage;
Expand All @@ -40,13 +40,13 @@
import org.springframework.messaging.support.GenericMessage;

/**
* For outbound messages, uses a message-aware Jackson object mapper to render the message
* For outbound messages, uses a message-aware Jackson JSON mapper to render the message
* as JSON. For messages with {@code byte[]} payloads, if rendered as JSON, Jackson
* performs Base64 conversion on the bytes. If payload is {@code byte[]} and
* the {@link #setRawBytes(boolean) rawBytes} property is true (default), the result has the form
* {@code [headersLen][headers][payloadLen][payload]}; with the headers
* rendered in JSON and the payload unchanged.
* Otherwise, message is fully serialized and deserialized with Jackson object mapper.
* Otherwise, message is fully serialized and deserialized with Jackson JSON mapper.
* <p>
* By default, all headers are included; you can provide simple patterns to specify a
* subset of headers.
Expand All @@ -56,7 +56,7 @@
* <p>
* <b>IMPORTANT</b>
* <p>
* The default object mapper will only deserialize classes in certain packages.
* The default JSON mapper will only deserialize classes in certain packages.
*
* <pre class=code>
* "java.util",
Expand All @@ -69,10 +69,10 @@
* "org.springframework.integration.handler"
* </pre>
* <p>
* To add more packages, create an object mapper using
* To add more packages, create an JSON mapper using
* {@link JacksonMessagingUtils#messagingAwareMapper(String...)}.
* <p>
* A constructor is provided allowing the provision of such a configured object mapper.
* A constructor is provided allowing the provision of such a configured JSON mapper.
*
* @author Jooyoung Pyoung
*
Expand All @@ -82,7 +82,7 @@ public class EmbeddedHeadersJsonMessageMapper implements BytesMessageMapper {

protected final Log logger = LogFactory.getLog(getClass());

private final ObjectMapper objectMapper;
private final JsonMapper jsonMapper;

private final String[] headerPatterns;

Expand Down Expand Up @@ -113,20 +113,20 @@ public EmbeddedHeadersJsonMessageMapper(String... headerPatterns) {
/**
* Construct an instance that embeds all headers, using the
* supplied JSON object mapper.
* @param objectMapper the object mapper.
* @param jsonMapper the JSON mapper.
*/
public EmbeddedHeadersJsonMessageMapper(ObjectMapper objectMapper) {
this(objectMapper, "*");
public EmbeddedHeadersJsonMessageMapper(JsonMapper jsonMapper) {
this(jsonMapper, "*");
}

/**
* Construct an instance that embeds headers matching the supplied patterns using the
* supplied JSON object mapper.
* @param objectMapper the object mapper.
* @param jsonMapper the JSON mapper.
* @param headerPatterns the patterns.
*/
public EmbeddedHeadersJsonMessageMapper(ObjectMapper objectMapper, String... headerPatterns) {
this.objectMapper = objectMapper;
public EmbeddedHeadersJsonMessageMapper(JsonMapper jsonMapper, String... headerPatterns) {
this.jsonMapper = jsonMapper;
this.headerPatterns = Arrays.copyOf(headerPatterns, headerPatterns.length);
this.allHeaders = this.headerPatterns.length == 1 && this.headerPatterns[0].equals("*");
}
Expand Down Expand Up @@ -181,7 +181,7 @@ public byte[] fromMessage(Message<?> message) {
}

try {
return this.objectMapper.writeValueAsBytes(messageToEncode);
return this.jsonMapper.writeValueAsBytes(messageToEncode);
}
catch (JacksonException ex) {
throw new UncheckedIOException(new IOException(ex));
Expand All @@ -205,7 +205,7 @@ private boolean matchHeader(String header) {

private byte[] fromBytesPayload(byte[] payload, Map<String, Object> headersToEncode) {
try {
byte[] headers = this.objectMapper.writeValueAsBytes(headersToEncode);
byte[] headers = this.jsonMapper.writeValueAsBytes(headersToEncode);
ByteBuffer buffer = ByteBuffer.wrap(new byte[8 + headers.length + payload.length]);
buffer.putInt(headers.length);
buffer.put(headers);
Expand All @@ -229,7 +229,7 @@ public Message<?> toMessage(byte[] bytes, @Nullable Map<String, Object> headers)
}
if (message == null) {
try {
message = (Message<?>) this.objectMapper.readValue(bytes, Object.class);
message = (Message<?>) this.jsonMapper.readValue(bytes, Object.class);
}
catch (Exception ex) {
this.logger.debug("Failed to decode JSON", ex);
Expand All @@ -244,7 +244,7 @@ public Message<?> toMessage(byte[] bytes, @Nullable Map<String, Object> headers)
}

@Nullable
private Message<?> decodeNativeFormat(byte[] bytes, @Nullable Map<String, Object> headersToAdd) throws IOException {
private Message<?> decodeNativeFormat(byte[] bytes, @Nullable Map<String, Object> headersToAdd) {
ByteBuffer buffer = ByteBuffer.wrap(bytes);
if (buffer.remaining() > 4) {
int headersLen = buffer.getInt();
Expand All @@ -257,7 +257,7 @@ private Message<?> decodeNativeFormat(byte[] bytes, @Nullable Map<String, Object
else {
((Buffer) buffer).position(4);
@SuppressWarnings("unchecked")
Map<String, Object> headers = this.objectMapper.readValue(bytes, buffer.position(), headersLen,
Map<String, Object> headers = this.jsonMapper.readValue(bytes, buffer.position(), headersLen,
Map.class);

((Buffer) buffer).position(buffer.position() + headersLen);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@
import tools.jackson.databind.DeserializationFeature;
import tools.jackson.databind.JavaType;
import tools.jackson.databind.JsonNode;
import tools.jackson.databind.ObjectMapper;
import tools.jackson.databind.json.JsonMapper;

import org.springframework.integration.mapping.support.JsonHeaders;
Expand All @@ -41,7 +40,7 @@
* Jackson 3 JSON-processor (@link https://github.com/FasterXML)
* {@linkplain JsonObjectMapper} implementation.
* Delegates {@link #toJson} and {@link #fromJson}
* to the {@linkplain ObjectMapper}
* to the {@linkplain JsonMapper}
* <p>
* It customizes Jackson's default properties with the following ones:
* <ul>
Expand All @@ -57,28 +56,28 @@
*/
public class JacksonJsonObjectMapper extends AbstractJacksonJsonObjectMapper<JsonNode, JsonParser, JavaType> {

private final ObjectMapper objectMapper;
private final JsonMapper jsonMapper;

public JacksonJsonObjectMapper() {
this.objectMapper = JsonMapper.builder()
this.jsonMapper = JsonMapper.builder()
.findAndAddModules(JacksonJsonObjectMapper.class.getClassLoader())
.disable(DeserializationFeature.FAIL_ON_TRAILING_TOKENS)
.build();
}

public JacksonJsonObjectMapper(ObjectMapper objectMapper) {
Assert.notNull(objectMapper, "objectMapper must not be null");
this.objectMapper = objectMapper;
public JacksonJsonObjectMapper(JsonMapper jsonMapper) {
Assert.notNull(jsonMapper, "jsonMapper must not be null");
this.jsonMapper = jsonMapper;
}

public ObjectMapper getObjectMapper() {
return this.objectMapper;
public JsonMapper getObjectMapper() {
return this.jsonMapper;
}

@Override
public String toJson(Object value) throws IOException {
try {
return this.objectMapper.writeValueAsString(value);
return this.jsonMapper.writeValueAsString(value);
}
catch (JacksonException e) {
throw new IOException(e);
Expand All @@ -88,7 +87,7 @@ public String toJson(Object value) throws IOException {
@Override
public void toJson(Object value, Writer writer) throws IOException {
try {
this.objectMapper.writeValue(writer, value);
this.jsonMapper.writeValue(writer, value);
}
catch (JacksonException e) {
throw new IOException(e);
Expand All @@ -99,33 +98,33 @@ public void toJson(Object value, Writer writer) throws IOException {
public JsonNode toJsonNode(Object json) throws IOException {
try {
if (json instanceof String) {
return this.objectMapper.readTree((String) json);
return this.jsonMapper.readTree((String) json);
}
else if (json instanceof byte[]) {
return this.objectMapper.readTree((byte[]) json);
return this.jsonMapper.readTree((byte[]) json);
}
else if (json instanceof File) {
return this.objectMapper.readTree((File) json);
return this.jsonMapper.readTree((File) json);
}
else if (json instanceof URL) {
return this.objectMapper.readTree((URL) json);
return this.jsonMapper.readTree((URL) json);
}
else if (json instanceof InputStream) {
return this.objectMapper.readTree((InputStream) json);
return this.jsonMapper.readTree((InputStream) json);
}
else if (json instanceof Reader) {
return this.objectMapper.readTree((Reader) json);
return this.jsonMapper.readTree((Reader) json);
}
}
catch (JacksonException e) {
if (!(json instanceof String) && !(json instanceof byte[])) {
throw new IOException(e);
}
// Otherwise the input might not be valid JSON, fallback to TextNode with ObjectMapper.valueToTree()
// Otherwise the input might not be valid JSON, fallback to TextNode with JsonMapper.valueToTree()
}

try {
return this.objectMapper.valueToTree(json);
return this.jsonMapper.valueToTree(json);
}
catch (JacksonException e) {
throw new IOException(e);
Expand All @@ -136,22 +135,22 @@ else if (json instanceof Reader) {
protected <T> T fromJson(Object json, JavaType type) throws IOException {
try {
if (json instanceof String) {
return this.objectMapper.readValue((String) json, type);
return this.jsonMapper.readValue((String) json, type);
}
else if (json instanceof byte[]) {
return this.objectMapper.readValue((byte[]) json, type);
return this.jsonMapper.readValue((byte[]) json, type);
}
else if (json instanceof File) {
return this.objectMapper.readValue((File) json, type);
return this.jsonMapper.readValue((File) json, type);
}
else if (json instanceof URL) {
return this.objectMapper.readValue((URL) json, type);
return this.jsonMapper.readValue((URL) json, type);
}
else if (json instanceof InputStream) {
return this.objectMapper.readValue((InputStream) json, type);
return this.jsonMapper.readValue((InputStream) json, type);
}
else if (json instanceof Reader) {
return this.objectMapper.readValue((Reader) json, type);
return this.jsonMapper.readValue((Reader) json, type);
}
else {
throw new IllegalArgumentException("'json' argument must be an instance of: " + SUPPORTED_JSON_TYPES
Expand All @@ -166,7 +165,7 @@ else if (json instanceof Reader) {
@Override
public <T> T fromJson(JsonParser parser, Type valueType) throws IOException {
try {
return this.objectMapper.readValue(parser, constructType(valueType));
return this.jsonMapper.readValue(parser, constructType(valueType));
}
catch (JacksonException e) {
throw new IOException(e);
Expand All @@ -183,19 +182,19 @@ protected JavaType extractJavaType(Map<String, Object> javaTypes) {

JavaType contentClassType = this.createJavaType(javaTypes, JsonHeaders.CONTENT_TYPE_ID);
if (classType.getKeyType() == null) {
return this.objectMapper.getTypeFactory()
return this.jsonMapper.getTypeFactory()
.constructCollectionType((Class<? extends Collection<?>>) classType.getRawClass(),
contentClassType);
}

JavaType keyClassType = createJavaType(javaTypes, JsonHeaders.KEY_TYPE_ID);
return this.objectMapper.getTypeFactory()
return this.jsonMapper.getTypeFactory()
.constructMapType((Class<? extends Map<?, ?>>) classType.getRawClass(), keyClassType, contentClassType);
}

@Override
protected JavaType constructType(Type type) {
return this.objectMapper.constructType(type);
return this.jsonMapper.constructType(type);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
import tools.jackson.databind.DatabindContext;
import tools.jackson.databind.DefaultTyping;
import tools.jackson.databind.JavaType;
import tools.jackson.databind.ObjectMapper;
import tools.jackson.databind.cfg.MapperConfig;
import tools.jackson.databind.json.JsonMapper;
import tools.jackson.databind.jsontype.BasicPolymorphicTypeValidator;
Expand All @@ -45,7 +44,7 @@
import org.springframework.messaging.support.GenericMessage;

/**
* Utility for creating Jackson {@link ObjectMapper} instance for Spring messaging.
* Utility for creating Jackson {@link JsonMapper} instance for Spring messaging.
*
* <p>Provides custom serializers/deserializers for Spring messaging types
* and validates deserialization against trusted package patterns.
Expand Down Expand Up @@ -75,15 +74,15 @@ private JacksonMessagingUtils() {
}

/**
* Return an {@link ObjectMapper} if available,
* Return an {@link JsonMapper} if available,
* supplied with Message specific serializers and deserializers.
* Also configured to store typo info in the {@code @class} property.
* @param trustedPackages the trusted Java packages for deserialization.
* @return the mapper.
* @return the JSON mapper.
* @throws IllegalStateException if an implementation is not available.
* @since 7.0
*/
public static ObjectMapper messagingAwareMapper(String @Nullable ... trustedPackages) {
public static JsonMapper messagingAwareMapper(String @Nullable ... trustedPackages) {
if (JacksonPresent.isJackson3Present()) {
GenericMessageJsonDeserializer genericMessageDeserializer = new GenericMessageJsonDeserializer();
ErrorMessageJsonDeserializer errorMessageDeserializer = new ErrorMessageJsonDeserializer();
Expand All @@ -98,7 +97,7 @@ public static ObjectMapper messagingAwareMapper(String @Nullable ... trustedPack
.addDeserializer(AdviceMessage.class, adviceMessageDeserializer)
.addDeserializer(MutableMessage.class, mutableMessageDeserializer);

ObjectMapper mapper = JsonMapper.builder()
JsonMapper mapper = JsonMapper.builder()
.findAndAddModules(JacksonMessagingUtils.class.getClassLoader())
.setDefaultTyping(new AllowListTypeResolverBuilder(trustedPackages))
.addModules(simpleModule)
Expand Down
Loading