Class JsonHelper

java.lang.Object
org.springframework.ai.util.JsonHelper

public class JsonHelper extends Object
Helper for JSON processing.
Since:
2.0.0
Author:
Sebastien Deleuze
  • Constructor Details

    • JsonHelper

      public JsonHelper()
    • JsonHelper

      public JsonHelper(tools.jackson.databind.json.JsonMapper jsonMapper)
  • Method Details

    • fromJson

      public <T> @Nullable T fromJson(String json, Class<T> type)
      Converts a JSON string to a Java object.
      Parameters:
      json - the JSON string to parse
      type - the target type
      Returns:
      the converted object
    • fromJson

      public <T> @Nullable T fromJson(String json, Type type)
      Converts a JSON string to a Java object.
      Parameters:
      json - the JSON string to parse
      type - the target type
      Returns:
      the converted object
    • fromJson

      public <T> @Nullable T fromJson(String json, org.springframework.core.ParameterizedTypeReference<T> type)
      Converts a JSON string to a Java object.
      Parameters:
      json - the JSON string to parse
      type - the target type
      Returns:
      the converted object
    • fromJsonToMap

      public Map<String,Object> fromJsonToMap(String json)
      Converts the given JSON string to a Map of String and Object.
      Parameters:
      json - the JSON string to parse
      Returns:
      the converted map
    • toJson

      public String toJson(@Nullable Object object)
      Converts a Java object to a JSON string.
    • toJson

      public String toJson(@Nullable Object object, boolean forwardIfValidJson)
      Converts a Java object to a JSON string.
      Parameters:
      forwardIfValidJson - when true and object being a valid JSON string, just return it
    • convertFromMap

      public <T> T convertFromMap(Map<String,Object> map, Class<T> type)
      Converts a map to a Java object.
      Parameters:
      map - the map to convert
      type - the target type
      Returns:
      the converted object
    • convertFromMap

      public <T> T convertFromMap(Map<String,Object> map, org.springframework.core.ParameterizedTypeReference<T> type)
      Converts a map to a Java object.
      Parameters:
      map - the map to convert
      type - the target type
      Returns:
      the converted object
    • convertToMap

      public Map<String,Object> convertToMap(Object object)
    • convertToTypedObject

      public Object convertToTypedObject(Object value, Class<?> type)
      Convert a Java Object to a typed Object. Based on the implementation in MethodToolCallback.
      Parameters:
      value - the object to convert
      type - the target type
      Returns:
      the converted typed object