Class Json

java.lang.Object
dev.langchain4j.internal.Json

public class Json extends Object
JSON helper class. It is supposed to be used by "tools" and "structured output" functionalities.
  • Method Details

    • toJson

      public static String toJson(Object o)
      Convert the given object to JSON.
      Parameters:
      o - the object to convert.
      Returns:
      the JSON string.
      Throws:
      JsonWriteException - if the object has no JSON representation, from an implementation that reports the typed exceptions; otherwise a RuntimeException.
    • fromJson

      public static <T> T fromJson(String json, Class<T> type)
      Convert the given JSON string to an object of the given class.
      Type Parameters:
      T - the type of the object.
      Parameters:
      json - the JSON string.
      type - the class of the object.
      Returns:
      the object.
      Throws:
      JsonReadException - if the JSON is malformed or does not describe the given type, from an implementation that reports the typed exceptions; otherwise a RuntimeException.
    • fromJson

      public static <T> T fromJson(String json, Type type)
      Convert the given JSON string to an object of the given type.
      Type Parameters:
      T - the type of the object.
      Parameters:
      json - the JSON string.
      type - the type of the object.
      Returns:
      the object.
      Throws:
      JsonReadException - if the JSON is malformed or does not describe the given type, from an implementation that reports the typed exceptions; otherwise a RuntimeException.