Class ModelOptionsUtils

java.lang.Object
org.springframework.ai.model.ModelOptionsUtils

public final class ModelOptionsUtils extends Object
Utility class for manipulating ModelOptions objects.
Since:
0.8.0
Author:
Christian Tzolov
  • Method Details

    • jsonToMap

      public static Map<String,Object> jsonToMap(String json)
      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

      public static <T> T jsonToObject(String json, Class<T> type)
      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

      public static String toJsonString(Object object)
      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

      public static <T> T merge(Object source, Object target, Class<T> clazz)
      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

      public static Map<String,Object> objectToMap(Object source)
      Converts the given object to a Map.
      Parameters:
      source - the object to convert to a Map.
      Returns:
      the converted Map.
    • mapToClass

      public static <T> T mapToClass(Map<String,Object> source, Class<T> clazz)
      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

      public static List<String> getJsonPropertyValues(Class<?> clazz)
      Returns the list of name values of the JsonProperty annotations.
      Parameters:
      clazz - the class that contains fields annotated with JsonProperty.
      Returns:
      the list of values of the JsonProperty annotations.
    • copyToTarget

      public static <I, S extends I, T extends S> T copyToTarget(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 extends S> T mergeBeans(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

      public static String getJsonSchema(Class<?> clazz, boolean toUpperCaseTypeValues)
      Generates JSON Schema (version 2020_12) for the given class.
      Parameters:
      clazz - the class to generate JSON Schema for.
      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)