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
@@ -0,0 +1,101 @@
/*
* Copyright 2025-present the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.springframework.integration.support.json;

import java.util.LinkedHashMap;
import java.util.Map;

import org.jspecify.annotations.Nullable;
import tools.jackson.core.JacksonException;
import tools.jackson.core.JsonParser;
import tools.jackson.core.JsonToken;
import tools.jackson.core.ObjectReadContext;
import tools.jackson.core.json.JsonFactory;

import org.springframework.messaging.Message;
import org.springframework.util.Assert;

/**
* {@link JsonInboundMessageMapper.JsonMessageParser} implementation that parses JSON messages
* and builds a {@link Message} with the specified payload type from provided {@link JsonInboundMessageMapper}.
* Uses <a href="https://github.com/FasterXML">Jackson JSON Processor</a>.
*
* @author Jooyoung Pyoung
*
* @since 7.0
*/
public class JacksonJsonMessageParser extends AbstractJacksonJsonMessageParser<JsonParser> {

private static final JsonFactory JSON_FACTORY = JsonFactory.builder().build();

public JacksonJsonMessageParser() {
this(new JacksonJsonObjectMapper());
}

public JacksonJsonMessageParser(JacksonJsonObjectMapper objectMapper) {
super(objectMapper);
}

@Override
protected JsonParser createJsonParser(String jsonMessage) {
return JSON_FACTORY.createParser(ObjectReadContext.empty(), jsonMessage);
}

@Override
protected Message<?> parseWithHeaders(JsonParser parser, String jsonMessage,
@Nullable Map<String, Object> headersToAdd) {

String error = AbstractJsonInboundMessageMapper.MESSAGE_FORMAT_ERROR + jsonMessage;
Assert.isTrue(JsonToken.START_OBJECT == parser.nextToken(), error);
Map<String, Object> headers = null;
Object payload = null;
while (JsonToken.END_OBJECT != parser.nextToken()) {
Assert.isTrue(JsonToken.PROPERTY_NAME == parser.currentToken(), error);
String currentName = parser.currentName();
boolean isHeadersToken = "headers".equals(currentName);
boolean isPayloadToken = "payload".equals(currentName);
Assert.isTrue(isHeadersToken || isPayloadToken, error);
if (isHeadersToken) {
Assert.isTrue(parser.nextToken() == JsonToken.START_OBJECT, error);
headers = readHeaders(parser, jsonMessage);
}
else {
parser.nextToken();
payload = readPayload(parser, jsonMessage);
}
}
Assert.notNull(headers, error);

return getMessageBuilderFactory()
.withPayload(payload)
.copyHeaders(headers)
.copyHeadersIfAbsent(headersToAdd)
.build();
}

private Map<String, Object> readHeaders(JsonParser parser, String jsonMessage) throws JacksonException {
Map<String, Object> headers = new LinkedHashMap<>();
while (JsonToken.END_OBJECT != parser.nextToken()) {
String headerName = parser.currentName();
parser.nextToken();
Object headerValue = readHeader(parser, headerName, jsonMessage);
headers.put(headerName, headerValue);
}
return headers;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,199 @@
/*
* Copyright 2025-present the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.springframework.integration.support.json;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.Reader;
import java.io.Writer;
import java.lang.reflect.Type;
import java.net.URL;
import java.util.Collection;
import java.util.Map;

import tools.jackson.core.JacksonException;
import tools.jackson.core.JsonParser;
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;
import org.springframework.util.Assert;

/**
* Jackson 3 JSON-processor (@link https://github.com/FasterXML)
* {@linkplain JsonObjectMapper} implementation.
* Delegates {@link #toJson} and {@link #fromJson}
* to the {@linkplain ObjectMapper}
* <p>
* It customizes Jackson's default properties with the following ones:
* <ul>
* <li>The well-known modules are registered through the classpath scan</li>
* </ul>
*
* See {@code tools.jackson.databind.json.JsonMapper.builder} for more information.
*
* @author Jooyoung Pyoung
*
* @since 7.0
*
*/
public class JacksonJsonObjectMapper extends AbstractJacksonJsonObjectMapper<JsonNode, JsonParser, JavaType> {

private final ObjectMapper objectMapper;

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

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

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

@Override
public String toJson(Object value) throws IOException {
try {
return this.objectMapper.writeValueAsString(value);
}
catch (JacksonException e) {
throw new IOException(e);
}
}

@Override
public void toJson(Object value, Writer writer) throws IOException {
try {
this.objectMapper.writeValue(writer, value);
}
catch (JacksonException e) {
throw new IOException(e);
}
}

@Override
public JsonNode toJsonNode(Object json) throws IOException {
try {
if (json instanceof String) {
return this.objectMapper.readTree((String) json);
}
else if (json instanceof byte[]) {
return this.objectMapper.readTree((byte[]) json);
}
else if (json instanceof File) {
return this.objectMapper.readTree((File) json);
}
else if (json instanceof URL) {
return this.objectMapper.readTree((URL) json);
}
else if (json instanceof InputStream) {
return this.objectMapper.readTree((InputStream) json);
}
else if (json instanceof Reader) {
return this.objectMapper.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()
}

try {
return this.objectMapper.valueToTree(json);
}
catch (JacksonException e) {
throw new IOException(e);
}
}

@Override
protected <T> T fromJson(Object json, JavaType type) throws IOException {
try {
if (json instanceof String) {
return this.objectMapper.readValue((String) json, type);
}
else if (json instanceof byte[]) {
return this.objectMapper.readValue((byte[]) json, type);
}
else if (json instanceof File) {
return this.objectMapper.readValue((File) json, type);
}
else if (json instanceof URL) {
return this.objectMapper.readValue((URL) json, type);
}
else if (json instanceof InputStream) {
return this.objectMapper.readValue((InputStream) json, type);
}
else if (json instanceof Reader) {
return this.objectMapper.readValue((Reader) json, type);
}
else {
throw new IllegalArgumentException("'json' argument must be an instance of: " + SUPPORTED_JSON_TYPES
+ " , but gotten: " + json.getClass());
}
}
catch (JacksonException e) {
throw new IOException(e);
}
}

@Override
public <T> T fromJson(JsonParser parser, Type valueType) throws IOException {
try {
return this.objectMapper.readValue(parser, constructType(valueType));
}
catch (JacksonException e) {
throw new IOException(e);
}
}

@Override
@SuppressWarnings({"unchecked"})
protected JavaType extractJavaType(Map<String, Object> javaTypes) {
JavaType classType = this.createJavaType(javaTypes, JsonHeaders.TYPE_ID);
if (!classType.isContainerType() || classType.isArrayType()) {
return classType;
}

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

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

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

}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
* The utility to check if Jackson JSON processor is present in the classpath.
*
* @author Artem Bilan
* @author Jooyoung Pyoung
*
* @since 4.3.10
*/
Expand All @@ -31,10 +32,18 @@ public final class JacksonPresent {
ClassUtils.isPresent("com.fasterxml.jackson.databind.ObjectMapper", null) &&
ClassUtils.isPresent("com.fasterxml.jackson.core.JsonGenerator", null);

private static final boolean JACKSON_3_PRESENT =
ClassUtils.isPresent("tools.jackson.databind.ObjectMapper", null) &&
ClassUtils.isPresent("tools.jackson.core.JsonGenerator", null);

public static boolean isJackson2Present() {
return JACKSON_2_PRESENT;
}

public static boolean isJackson3Present() {
return JACKSON_3_PRESENT;
}

private JacksonPresent() {
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,11 @@
* @author Artem Bilan
* @author Gary Russell
* @author Vikas Prasad
* @author Jooyoung Pyoung
*
* @since 3.0
*
* @see JacksonJsonObjectMapper
* @see Jackson2JsonObjectMapper
*/
public final class JsonObjectMapperProvider {
Expand All @@ -43,6 +45,9 @@ private JsonObjectMapperProvider() {
if (JacksonPresent.isJackson2Present()) {
return new Jackson2JsonObjectMapper();
}
else if (JacksonPresent.isJackson3Present()) {
return new JacksonJsonObjectMapper();
}
else {
throw new IllegalStateException("No jackson-databind.jar is present in the classpath.");
}
Expand All @@ -54,7 +59,7 @@ private JsonObjectMapperProvider() {
* @since 4.2.7
*/
public static boolean jsonAvailable() {
return JacksonPresent.isJackson2Present();
return JacksonPresent.isJackson3Present() || JacksonPresent.isJackson2Present();
}

}
Loading