Class JsonUtils

java.lang.Object
com.google.genkit.core.JsonUtils

public final class JsonUtils extends Object
JsonUtils provides JSON serialization and deserialization utilities for Genkit.
  • Method Details

    • getObjectMapper

      public static com.fasterxml.jackson.databind.ObjectMapper getObjectMapper()
      Returns the shared ObjectMapper instance.
      Returns:
      the ObjectMapper
    • toJson

      public static String toJson(Object value) throws GenkitException
      Converts an object to JSON string.
      Parameters:
      value - the object to convert
      Returns:
      the JSON string
      Throws:
      GenkitException - if serialization fails
    • toJsonNode

      public static com.fasterxml.jackson.databind.JsonNode toJsonNode(Object value)
      Converts an object to a JsonNode.
      Parameters:
      value - the object to convert
      Returns:
      the JsonNode
    • fromJson

      public static <T> T fromJson(String json, Class<T> clazz) throws GenkitException
      Parses a JSON string to the specified type.
      Type Parameters:
      T - the target type
      Parameters:
      json - the JSON string
      clazz - the target class
      Returns:
      the parsed object
      Throws:
      GenkitException - if parsing fails
    • fromJsonNode

      public static <T> T fromJsonNode(com.fasterxml.jackson.databind.JsonNode node, Class<T> clazz) throws GenkitException
      Converts a JsonNode to the specified type.
      Type Parameters:
      T - the target type
      Parameters:
      node - the JsonNode
      clazz - the target class
      Returns:
      the converted object
      Throws:
      GenkitException - if conversion fails
    • parseJson

      public static com.fasterxml.jackson.databind.JsonNode parseJson(String json) throws GenkitException
      Parses a JSON string to a JsonNode.
      Parameters:
      json - the JSON string
      Returns:
      the JsonNode
      Throws:
      GenkitException - if parsing fails
    • convert

      public static <T> T convert(Object value, Class<T> clazz) throws GenkitException
      Converts an object to the specified type.

      This is useful for converting Maps (from JSON parsing) to typed objects.

      Type Parameters:
      T - the target type
      Parameters:
      value - the object to convert (typically a Map from JSON parsing)
      clazz - the target class
      Returns:
      the converted object
      Throws:
      GenkitException - if conversion fails
    • toPrettyJson

      public static String toPrettyJson(Object value) throws GenkitException
      Pretty prints a JSON object.
      Parameters:
      value - the object to print
      Returns:
      the pretty-printed JSON string
      Throws:
      GenkitException - if serialization fails