-
Notifications
You must be signed in to change notification settings - Fork 824
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refine Jackson ObjectMapper handling
ObjectMapper instantiation is costly, so unless its usage is one-shot, it is better to create a reusable instance for upcoming usage. Also, before this commit, serialization of most Kotlin classes was not supported due to the lack of proper Jackson KotlinModule detection. This commit: - Avoids per invocation ObjectMapper instantiation when relevant - Automatically detects and enables well-known Jackson modules including the Kotlin one - Removes org.springframework.ai.vectorstore.JsonUtils which looks not needed anymore More optimizations are possible like reusing more ObjectMapper instances, but this could introduce more breaking changes so this commit intends to be a good first step. Kotlin tests will be provided in a follow-up commit. Additional changes: - Update ModelOptionsUtils to use JacksonUtils.instantiateAvailableModules() - Add missing license headers - Add missing author Javadoc comments
- Loading branch information
Showing
26 changed files
with
244 additions
and
155 deletions.
There are no files selected for viewing
This file contains 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 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 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 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 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 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
90 changes: 90 additions & 0 deletions
90
spring-ai-core/src/main/java/org/springframework/ai/util/JacksonUtils.java
This file contains 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,90 @@ | ||
/* | ||
* Copyright 2024 - 2024 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.ai.util; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
import com.fasterxml.jackson.databind.Module; | ||
|
||
import org.springframework.beans.BeanUtils; | ||
import org.springframework.core.KotlinDetector; | ||
import org.springframework.util.ClassUtils; | ||
|
||
/** | ||
* Utility methods for Jackson. | ||
* | ||
* @author Sebastien Deleuze | ||
*/ | ||
public abstract class JacksonUtils { | ||
|
||
/** | ||
* Instantiate well-known Jackson modules available in the classpath. | ||
* <p> | ||
* Supports the follow-modules: <code>Jdk8Module</code>, <code>JavaTimeModule</code>, | ||
* <code>ParameterNamesModule</code> and <code>KotlinModule</code>. | ||
* @return The list of instantiated modules. | ||
*/ | ||
@SuppressWarnings("unchecked") | ||
public static List<Module> instantiateAvailableModules() { | ||
List<Module> modules = new ArrayList<>(); | ||
try { | ||
Class<? extends com.fasterxml.jackson.databind.Module> jdk8ModuleClass = (Class<? extends Module>) ClassUtils | ||
.forName("com.fasterxml.jackson.datatype.jdk8.Jdk8Module", null); | ||
com.fasterxml.jackson.databind.Module jdk8Module = BeanUtils.instantiateClass(jdk8ModuleClass); | ||
modules.add(jdk8Module); | ||
} | ||
catch (ClassNotFoundException ex) { | ||
// jackson-datatype-jdk8 not available | ||
} | ||
|
||
try { | ||
Class<? extends com.fasterxml.jackson.databind.Module> javaTimeModuleClass = (Class<? extends Module>) ClassUtils | ||
.forName("com.fasterxml.jackson.datatype.jsr310.JavaTimeModule", null); | ||
com.fasterxml.jackson.databind.Module javaTimeModule = BeanUtils.instantiateClass(javaTimeModuleClass); | ||
modules.add(javaTimeModule); | ||
} | ||
catch (ClassNotFoundException ex) { | ||
// jackson-datatype-jsr310 not available | ||
} | ||
|
||
try { | ||
Class<? extends com.fasterxml.jackson.databind.Module> parameterNamesModuleClass = (Class<? extends Module>) ClassUtils | ||
.forName("com.fasterxml.jackson.module.paramnames.ParameterNamesModule", null); | ||
com.fasterxml.jackson.databind.Module parameterNamesModule = BeanUtils | ||
.instantiateClass(parameterNamesModuleClass); | ||
modules.add(parameterNamesModule); | ||
} | ||
catch (ClassNotFoundException ex) { | ||
// jackson-module-parameter-names not available | ||
} | ||
|
||
// Kotlin present? | ||
if (KotlinDetector.isKotlinPresent()) { | ||
try { | ||
Class<? extends com.fasterxml.jackson.databind.Module> kotlinModuleClass = (Class<? extends Module>) ClassUtils | ||
.forName("com.fasterxml.jackson.module.kotlin.KotlinModule", null); | ||
Module kotlinModule = BeanUtils.instantiateClass(kotlinModuleClass); | ||
modules.add(kotlinModule); | ||
} | ||
catch (ClassNotFoundException ex) { | ||
// jackson-module-kotlin not available | ||
} | ||
} | ||
return modules; | ||
} | ||
|
||
} |
Oops, something went wrong.