Package org.springframework.ai.model
Class ModelOptionsUtils
java.lang.Object
org.springframework.ai.model.ModelOptionsUtils
Utility class for manipulating
ModelOptions
objects.- Since:
- 0.8.0
- Author:
- Christian Tzolov, Thomas Vitale
-
Field Summary
Modifier and TypeFieldDescriptionstatic final com.fasterxml.jackson.databind.ObjectMapper
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionstatic <I,
S extends I, T extends S>
TcopyToTarget
(S sourceBean, Class<I> sourceInterfaceClazz, Class<T> targetBeanClazz) Returns a new instance of the targetBeanClazz that copies the bean values from the sourceBean instance.getJsonPropertyValues
(Class<?> clazz) Returns the list of name values of theJsonProperty
annotations.static String
getJsonSchema
(Class<?> clazz, boolean toUpperCaseTypeValues) Deprecated.static String
getJsonSchema
(Type inputType, boolean toUpperCaseTypeValues) Generates JSON Schema (version 2020_12) for the given class.Converts the given JSON string to a Map of String and Object.static <T> T
jsonToObject
(String json, Class<T> type) Converts the given JSON string to an Object of the given type.static <T> T
mapToClass
(Map<String, Object> source, Class<T> clazz) Converts the given Map to the given class.static <T> T
Merges the source object into the target object and returns an object represented by the given class.static <T> T
Merges the source object into the target object and returns an object represented by the given class.static <I,
S extends I, T extends S>
TmergeBeans
(S source, T target, Class<I> sourceInterfaceClazz, boolean overrideNonNullTargetValues) Merges the source object into the target object.static <T> T
mergeOption
(T runtimeValue, T defaultValue) Return the runtime value if not empty, or else the default value.objectToMap
(Object source) Converts the given object to a Map.static String
toJsonString
(Object object) Converts the given object to a JSON string.static void
toUpperCaseTypeValues
(com.fasterxml.jackson.databind.node.ObjectNode node)
-
Field Details
-
OBJECT_MAPPER
public static final com.fasterxml.jackson.databind.ObjectMapper OBJECT_MAPPER
-
-
Constructor Details
-
ModelOptionsUtils
public ModelOptionsUtils()
-
-
Method Details
-
jsonToMap
Converts the given JSON string to a Map of String and Object.- Parameters:
json
- the JSON string to convert to a Map.- Returns:
- the converted Map.
-
jsonToObject
Converts the given JSON string to an Object of the given type.- Type Parameters:
T
- the type of the object to return.- Parameters:
json
- the JSON string to convert to an object.type
- the type of the object to return.- Returns:
- Object instance of the given type.
-
toJsonString
Converts the given object to a JSON string.- Parameters:
object
- the object to convert to a JSON string.- Returns:
- the JSON string.
-
merge
public static <T> T merge(Object source, Object target, Class<T> clazz, List<String> acceptedFieldNames) Merges the source object into the target object and returns an object represented by the given class. The JSON property names are used to match the fields to merge. The source non-null values override the target values with the same field name. The source null values are ignored. If the acceptedFieldNames is not empty, only the fields with the given names are merged and returned. If the acceptedFieldNames is empty, use the@JsonProperty
names, inferred from the provided clazz.- Type Parameters:
T
- they type of the class to return.- Parameters:
source
- the source object to merge.target
- the target object to merge into.clazz
- the class to return.acceptedFieldNames
- the list of field names accepted for the target object.- Returns:
- the merged object represented by the given class.
-
merge
Merges the source object into the target object and returns an object represented by the given class. The JSON property names are used to match the fields to merge. The source non-null values override the target values with the same field name. The source null values are ignored. Returns the only field names that match the@JsonProperty
names, inferred from the provided clazz.- Type Parameters:
T
- they type of the class to return.- Parameters:
source
- the source object to merge.target
- the target object to merge into.clazz
- the class to return.- Returns:
- the merged object represented by the given class.
-
objectToMap
Converts the given object to a Map.- Parameters:
source
- the object to convert to a Map.- Returns:
- the converted Map.
-
mapToClass
Converts the given Map to the given class.- Type Parameters:
T
- the type of the class to return.- Parameters:
source
- the Map to convert to the given class.clazz
- the class to convert the Map to.- Returns:
- the converted class.
-
getJsonPropertyValues
Returns the list of name values of theJsonProperty
annotations.- Parameters:
clazz
- the class that contains fields annotated withJsonProperty
.- Returns:
- the list of values of the
JsonProperty
annotations.
-
copyToTarget
public static <I,S extends I, T copyToTargetT extends S> (S sourceBean, Class<I> sourceInterfaceClazz, Class<T> targetBeanClazz) Returns a new instance of the targetBeanClazz that copies the bean values from the sourceBean instance.- Type Parameters:
T
- the target class type.- Parameters:
sourceBean
- the source bean to copy the values from.sourceInterfaceClazz
- the source interface class. Only the fields with the same name as the interface methods are copied. This allow the source object to be a subclass of the source interface with additional, non-interface fields.targetBeanClazz
- the target class, a subclass of the ChatOptions, to convert into.- Returns:
- a new instance of the targetBeanClazz with the values from the sourceBean instance.
-
mergeBeans
public static <I,S extends I, T mergeBeansT extends S> (S source, T target, Class<I> sourceInterfaceClazz, boolean overrideNonNullTargetValues) Merges the source object into the target object. The source null values are ignored. Only objects with Getter and Setter methods are supported.- Type Parameters:
T
- the type of the source and target object.- Parameters:
source
- the source object to merge.target
- the target object to merge into.sourceInterfaceClazz
- the source interface class. Only the fields with the same name as the interface methods are merged. This allow the source object to be a subclass of the source interface with additional, non-interface fields.overrideNonNullTargetValues
- if true, the source non-null values override the target values with the same field name. If false, the source non-null values are ignored.- Returns:
- the merged target object.
-
getJsonSchema
@Deprecated(since="1.0 M4") public static String getJsonSchema(Class<?> clazz, boolean toUpperCaseTypeValues) Deprecated.usegetJsonSchema(Type, boolean)
instead.Generates JSON Schema (version 2020_12) for the given class.- Parameters:
clazz
- the class to generate JSON Schema from.toUpperCaseTypeValues
- if true, the type values are converted to upper case.- Returns:
- the generated JSON Schema as a String.
-
getJsonSchema
Generates JSON Schema (version 2020_12) for the given class.- Parameters:
inputType
- the inputType
to generate JSON Schema from.toUpperCaseTypeValues
- if true, the type values are converted to upper case.- Returns:
- the generated JSON Schema as a String.
-
toUpperCaseTypeValues
public static void toUpperCaseTypeValues(com.fasterxml.jackson.databind.node.ObjectNode node) -
mergeOption
public static <T> T mergeOption(T runtimeValue, T defaultValue) Return the runtime value if not empty, or else the default value.
-
getJsonSchema(Type, boolean)
instead.