Class JsonUtil
java.lang.Object
org.a2aproject.sdk.jsonrpc.common.json.JsonUtil
Utility class for JSON operations.
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final com.google.gson.GsonPre-configuredGsoninstance for JSON operations. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic <T> TDeserializes JSON string to an object of the specified class.static <T> TDeserializes JSON string to an object of the specified type.readMetadata(@Nullable String json) Reads the "metadata" field from a JSON body string, if present.readMetadata(com.google.gson.JsonObject jsonObject) Reads the "metadata" field from a JSON object, if present.static StringSerializes an object to a JSON string using Gson.static voidwriteJsonRpcId(com.google.gson.stream.JsonWriter out, @Nullable Object id) Writes a JSON-RPCidfield.static StringwriteMetadata(@Nullable Map<String, Object> metadata) Serializes a metadata map to a JSON string.static voidwriteMetadata(com.google.gson.stream.JsonWriter out, @Nullable Map<String, Object> metadata) Writes a metadata map as a "metadata" JSON field to the given writer.
-
Field Details
-
OBJECT_MAPPER
public static final com.google.gson.Gson OBJECT_MAPPERPre-configuredGsoninstance 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
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 parseclassOfT- the class of the object to deserialize to- Returns:
- the deserialized object
- Throws:
JsonProcessingException- if JSON parsing fails
-
fromJson
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 parsetype- the type of the object to deserialize to (supports generics)- Returns:
- the deserialized object
- Throws:
JsonProcessingException- if JSON parsing fails
-
toJson
Serializes an object to a JSON string using Gson.This method uses the pre-configured
OBJECT_MAPPERto 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-RPCidfield. 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 IOExceptionWrites 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 tometadata- the metadata map to write- Throws:
IOException- if an I/O error occurs
-
writeMetadata
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
Reads the "metadata" field from a JSON object, if present.- Parameters:
jsonObject- the JSON object to read from- Returns:
- the metadata map, or
nullif no "metadata" field is present
-
readMetadata
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
-