-
Notifications
You must be signed in to change notification settings - Fork 1.1k
GH-10058: Add Jackson 3 ObjectMapper and MessageParser #10160
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
101 changes: 101 additions & 0 deletions
101
.../src/main/java/org/springframework/integration/support/json/JacksonJsonMessageParser.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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; | ||
| } | ||
|
|
||
| } |
199 changes: 199 additions & 0 deletions
199
...e/src/main/java/org/springframework/integration/support/json/JacksonJsonObjectMapper.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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); | ||
| } | ||
|
|
||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.