Class JsonUtil

java.lang.Object
org.a2aproject.sdk.jsonrpc.common.json.JsonUtil

public class JsonUtil extends Object
Utility class for JSON operations.
  • Field Details

    • OBJECT_MAPPER

      public static final com.google.gson.Gson OBJECT_MAPPER
      Pre-configured Gson instance for JSON operations.

      This mapper is configured with strict parsing mode and all necessary custom TypeAdapters for A2A Protocol types including polymorphic types, enums, and date/time types.

      Used throughout the SDK for consistent JSON serialization and deserialization.

      See Also:
      • createBaseGsonBuilder()
  • Constructor Details

    • JsonUtil

      public JsonUtil()
  • Method Details

    • fromJson

      public static <T> T fromJson(String json, Class<T> classOfT) throws JsonProcessingException
      Deserializes JSON string to an object of the specified class.
      Type Parameters:
      T - the type of the object to deserialize to
      Parameters:
      json - the JSON string to parse
      classOfT - the class of the object to deserialize to
      Returns:
      the deserialized object
      Throws:
      JsonProcessingException - if JSON parsing fails
    • fromJson

      public static <T> T fromJson(String json, Type type) throws JsonProcessingException
      Deserializes JSON string to an object of the specified type.
      Type Parameters:
      T - the type of the object to deserialize to
      Parameters:
      json - the JSON string to parse
      type - the type of the object to deserialize to (supports generics)
      Returns:
      the deserialized object
      Throws:
      JsonProcessingException - if JSON parsing fails
    • toJson

      public static String toJson(Object data) throws JsonProcessingException
      Serializes an object to a JSON string using Gson.

      This method uses the pre-configured OBJECT_MAPPER to produce JSON representation of the provided object.

      Parameters:
      data - the object to serialize
      Returns:
      JSON string representation of the object
      Throws:
      JsonProcessingException - if conversion fails
    • writeJsonRpcId

      public static void writeJsonRpcId(com.google.gson.stream.JsonWriter out, @Nullable Object id) throws IOException
      Writes a JSON-RPC id field. Handles null, String, and Number values, preserving fractional precision for non-integer numeric IDs.
      Throws:
      IOException
    • writeMetadata

      public static void writeMetadata(com.google.gson.stream.JsonWriter out, @Nullable Map<String,Object> metadata) throws IOException
      Writes a metadata map as a "metadata" JSON field to the given writer. Does nothing if the metadata is null or empty.
      Parameters:
      out - the JSON writer to write to
      metadata - the metadata map to write
      Throws:
      IOException - if an I/O error occurs
    • writeMetadata

      public static String writeMetadata(@Nullable Map<String,Object> metadata)
      Serializes a metadata map to a JSON string.
      Parameters:
      metadata - the metadata map to serialize
      Returns:
      JSON string representation of the metadata, or an empty string if the map is null or empty
    • readMetadata

      public static Map<String,Object> readMetadata(com.google.gson.JsonObject jsonObject)
      Reads the "metadata" field from a JSON object, if present.
      Parameters:
      jsonObject - the JSON object to read from
      Returns:
      the metadata map, or null if no "metadata" field is present
    • readMetadata

      public static Map<String,Object> readMetadata(@Nullable String json) throws JsonProcessingException
      Reads the "metadata" field from a JSON body string, if present.
      Parameters:
      json - the JSON body string to parse
      Returns:
      the metadata map, or an empty map if the input is null, blank, or has no "metadata" field
      Throws:
      JsonProcessingException - if the JSON is invalid