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
-
Method Details
-
getType
-
serialize
-
deserialize
-