Record Class DataPart
- Record Components:
data- the structured data (required, supports JSON objects, arrays, primitives, and null)metadata- additional metadata for the part
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);
-
Field Summary
Fields -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptiondata()Returns the value of thedatarecord component.final booleanIndicates whether some other object is "equal to" this one.static DataPartCreates a DataPart by parsing a JSON string into its corresponding Java type.static DataPartCreates a DataPart by parsing a JSON string into its corresponding Java type, with optional metadata.final inthashCode()Returns a hash code value for this object.metadata()Returns the value of themetadatarecord component.final StringtoString()Returns a string representation of this record class.
-
Field Details
-
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
Compact constructor with validation.Note: For mutable data types (Map, List), callers should ensure immutability by using
Map.copyOf()orList.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
Constructor.- Parameters:
data- the structured data (supports JSON objects, arrays, primitives, and not null)- Throws:
IllegalArgumentException- if data is null
-
-
Method Details
-
fromJson
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
- JSON objects →
-
fromJson
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
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. -
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. -
equals
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 withObjects::equals(Object,Object). -
data
Returns the value of thedatarecord component.- Returns:
- the value of the
datarecord component
-
metadata
Returns the value of themetadatarecord component.- Returns:
- the value of the
metadatarecord component
-