Interface TypeAdapter<T>

Type Parameters:
T - the type this adapter handles

public interface TypeAdapter<T>
Interface for custom type adapters used in message serialization.

Implement this interface to provide custom serialization/deserialization logic for your data types. This is useful for:

  • Complex types that Gson cannot handle automatically
  • Custom date/time formats
  • Polymorphic types requiring special handling
  • Performance optimizations

Example

// Custom adapter for a Location type
public class LocationAdapter implements TypeAdapter<Location> {

    @Override
    public Class<Location> getType() {
        return Location.class;
    }

    @Override
    public String serialize(Location location) {
        return location.world() + "," + location.x() + "," + location.y() + "," + location.z();
    }

    @Override
    public Location deserialize(String json) {
        String[] parts = json.split(",");
        return new Location(parts[0],
            Double.parseDouble(parts[1]),
            Double.parseDouble(parts[2]),
            Double.parseDouble(parts[3]));
    }
}

// Register with messaging service
messaging.registerTypeAdapter(new LocationAdapter());
See Also:
  • Method Summary

    Modifier and Type
    Method
    Description
    Deserialize a JSON string to an object.
    Get the class this adapter handles.
    serialize(T object)
    Serialize an object to JSON string.
  • Method Details

    • getType

      @Nonnull Class<T> getType()
      Get the class this adapter handles.
      Returns:
      the type class
    • serialize

      @Nonnull String serialize(@Nonnull T object)
      Serialize an object to JSON string.
      Parameters:
      object - the object to serialize
      Returns:
      the JSON representation
    • deserialize

      @Nonnull T deserialize(@Nonnull String json)
      Deserialize a JSON string to an object.
      Parameters:
      json - the JSON string
      Returns:
      the deserialized object