Package com.google.genkit.core
Class JsonUtils
java.lang.Object
com.google.genkit.core.JsonUtils
JsonUtils provides JSON serialization and deserialization utilities for
Genkit.
-
Method Summary
Modifier and TypeMethodDescriptionstatic <T> TConverts an object to the specified type.static <T> TParses a JSON string to the specified type.static <T> TfromJsonNode(com.fasterxml.jackson.databind.JsonNode node, Class<T> clazz) Converts a JsonNode to the specified type.static com.fasterxml.jackson.databind.ObjectMapperReturns the shared ObjectMapper instance.static com.fasterxml.jackson.databind.JsonNodeParses a JSON string to a JsonNode.static StringConverts an object to JSON string.static com.fasterxml.jackson.databind.JsonNodetoJsonNode(Object value) Converts an object to a JsonNode.static StringtoPrettyJson(Object value) Pretty prints a JSON object.
-
Method Details
-
getObjectMapper
public static com.fasterxml.jackson.databind.ObjectMapper getObjectMapper()Returns the shared ObjectMapper instance.- Returns:
- the ObjectMapper
-
toJson
Converts an object to JSON string.- Parameters:
value- the object to convert- Returns:
- the JSON string
- Throws:
GenkitException- if serialization fails
-
toJsonNode
Converts an object to a JsonNode.- Parameters:
value- the object to convert- Returns:
- the JsonNode
-
fromJson
Parses a JSON string to the specified type.- Type Parameters:
T- the target type- Parameters:
json- the JSON stringclazz- 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 JsonNodeclazz- the target class- Returns:
- the converted object
- Throws:
GenkitException- if conversion fails
-
parseJson
Parses a JSON string to a JsonNode.- Parameters:
json- the JSON string- Returns:
- the JsonNode
- Throws:
GenkitException- if parsing fails
-
convert
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
Pretty prints a JSON object.- Parameters:
value- the object to print- Returns:
- the pretty-printed JSON string
- Throws:
GenkitException- if serialization fails
-