Record Class DataPart

java.lang.Object
java.lang.Record
org.a2aproject.sdk.spec.DataPart
Record Components:
data - the structured data (required, supports JSON objects, arrays, primitives, and null)
metadata - additional metadata for the part
All Implemented Interfaces:
Part<Object>

public record DataPart(Object data, @Nullable Map<String,Object> metadata) extends Record implements Part<Object>
Represents a structured data content part within a Message or Artifact.

DataPart contains arbitrary JSON data for machine-to-machine communication. It is used when content needs to be processed programmatically rather than displayed as text, such as API responses, configuration data, analysis results, or structured metadata.

The data can be any valid JSON value:

  • JSON objects: Map<String, Object>
  • JSON arrays: List<Object>
  • Primitives: String, Number, Boolean
  • Null values: null

Example usage:


 // JSON object
 DataPart obj = new DataPart(Map.of(
     "status", "success",
     "count", 42,
     "items", List.of("item1", "item2")
 ));

 // JSON array
 DataPart array = new DataPart(List.of("item1", "item2", "item3"));

 // Primitive value
 DataPart primitive = new DataPart(42);
 
See Also:
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    static final String
    The JSON member name discriminator for data parts: "data".
  • Constructor Summary

    Constructors
    Constructor
    Description
    Constructor.
    DataPart(Object data, @Nullable Map<String,Object> metadata)
    Compact constructor with validation.
  • Method Summary

    Modifier and Type
    Method
    Description
    Returns the value of the data record component.
    final boolean
    Indicates whether some other object is "equal to" this one.
    static DataPart
    Creates a DataPart by parsing a JSON string into its corresponding Java type.
    static DataPart
    fromJson(String json, @Nullable Map<String,Object> metadata)
    Creates a DataPart by parsing a JSON string into its corresponding Java type, with optional metadata.
    final int
    Returns a hash code value for this object.
    @Nullable Map<String,Object>
    Returns the value of the metadata record component.
    final String
    Returns a string representation of this record class.

    Methods inherited from class java.lang.Object

    clone, finalize, getClass, notify, notifyAll, wait, wait, wait
  • Field Details

    • DATA

      public static final String DATA
      The JSON member name discriminator for data parts: "data".

      In protocol v1.0+, this constant defines the JSON member name used for serialization: { "data": { "data": { "temperature": 22.5, "unit": "C" } } }

      See Also:
  • Constructor Details

    • DataPart

      public DataPart(Object data, @Nullable Map<String,Object> metadata)
      Compact constructor with validation.

      Note: For mutable data types (Map, List), callers should ensure immutability by using Map.copyOf() or List.copyOf() before passing to this constructor.

      Parameters:
      data - the structured data (supports JSON objects, arrays, primitives, and null)
      Throws:
      IllegalArgumentException - if data is null
    • DataPart

      public DataPart(Object data)
      Constructor.
      Parameters:
      data - the structured data (supports JSON objects, arrays, primitives, and not null)
      Throws:
      IllegalArgumentException - if data is null
  • Method Details

    • fromJson

      public static DataPart fromJson(String json)
      Creates a DataPart by parsing a JSON string into its corresponding Java type.

      The JSON string is parsed using Gson with ToNumberPolicy.LONG_OR_DOUBLE, producing the following mappings:

      • JSON objects → Map<String, Object>
      • JSON arrays → List<Object>
      • JSON strings → String
      • JSON integers → Long
      • JSON decimals → Double
      • JSON booleans → Boolean

      Example usage:

      
       DataPart dataPart = DataPart.fromJson("""
           {
               "temperature": 22.5,
               "humidity": 65
           }""");
       
      Parameters:
      json - the JSON string to parse (must not be null or the JSON literal "null")
      Returns:
      a new DataPart containing the parsed data
      Throws:
      IllegalArgumentException - if json is null, parses to null, or is not valid
    • fromJson

      public static DataPart fromJson(String json, @Nullable Map<String,Object> metadata)
      Creates a DataPart by parsing a JSON string into its corresponding Java type, with optional metadata.
      Parameters:
      json - the JSON string to parse (must not be null or the JSON literal "null")
      metadata - additional metadata for the part
      Returns:
      a new DataPart containing the parsed data and metadata
      Throws:
      IllegalArgumentException - if json is null, parses to null, or is not valid
      See Also:
    • toString

      public final String toString()
      Returns a string representation of this record class. The representation contains the name of the class, followed by the name and value of each of the record components.
      Specified by:
      toString in class Record
      Returns:
      a string representation of this object
    • hashCode

      public final int hashCode()
      Returns a hash code value for this object. The value is derived from the hash code of each of the record components.
      Specified by:
      hashCode in class Record
      Returns:
      a hash code value for this object
    • equals

      public final boolean equals(Object o)
      Indicates whether some other object is "equal to" this one. The objects are equal if the other object is of the same class and if all the record components are equal. All components in this record class are compared with Objects::equals(Object,Object).
      Specified by:
      equals in class Record
      Parameters:
      o - the object with which to compare
      Returns:
      true if this object is the same as the o argument; false otherwise.
    • data

      public Object data()
      Returns the value of the data record component.
      Returns:
      the value of the data record component
    • metadata

      public @Nullable Map<String,Object> metadata()
      Returns the value of the metadata record component.
      Returns:
      the value of the metadata record component