Interface Converter

All Known Implementing Classes:
BasicConverter, CachingConverter, ConfigurableConverter, MarshallingContextConverter

public interface Converter
Interface for converting objects between types.

Use BasicConverter.INSTANCE for a default singleton implementation that supports a wide range of common type conversions without requiring a MarshallingContext or MarshallingSession.

  • Method Summary

    Modifier and Type
    Method
    Description
    default boolean
    canConvert(Class<?> inType, Class<?> outType)
    Returns true if this converter can convert from the specified input type to the specified output type.
    <T> T
    to(Object o, Class<T> type)
    Converts the specified object to the specified type.
    default <T> T
    to(Object o, Object memberOf, ConverterSession session, Class<T> type)
    Converts the specified object to the specified type, using the given outer instance and converter session.
    default <T> T
    to(Object o, Object memberOf, ConverterSession session, Type mainType, Type... args)
    Converts the specified object to the specified parameterized type, using the given outer instance and converter session.
    <T> T
    to(Object o, Type mainType, Type... args)
    Converts the specified object to the specified parameterized type.
  • Method Details

    • canConvert

      default boolean canConvert(Class<?> inType, Class<?> outType)
      Returns true if this converter can convert from the specified input type to the specified output type.
      Parameters:
      inType - The input type.
      outType - The output type.
      Returns:
      true if a conversion path exists.
    • to

      <T> T to(Object o, Class<T> type)
      Converts the specified object to the specified type.

      When the input is null, returns the JVM default for primitive types (e.g. 0 for int.class, false for boolean.class), or null for all other types. Throws InvalidConversionException when no conversion path exists. Use canConvert(Class, Class) to pre-check if uncertain.

      Type Parameters:
      T - The type to convert to.
      Parameters:
      o - The object to convert. Can be null.
      type - The type to convert to.
      Returns:
      The converted object, the primitive zero-value if input is null and type is primitive, or null if the input is null and type is non-primitive.
      Throws:
      InvalidConversionException - If no conversion path exists from the input type to the target type.
    • to

      <T> T to(Object o, Type mainType, Type... args)
      Converts the specified object to the specified parameterized type.

      This method allows conversion to complex parameterized types such as collections and maps. The args parameter specifies the type parameters of the main type.

      Returns null only when the input object is null. Throws InvalidConversionException when no conversion path exists.

      Example:

      // Convert to List<String> List<String> list = converter.to(o, List.class, String.class); // Convert to Map<String,Integer> Map<String,Integer> map = converter.to(o, Map.class, String.class, Integer.class);

      Type Parameters:
      T - The type to convert to.
      Parameters:
      o - The object to convert. Can be null.
      mainType - The main type to convert to.
      args - The type parameters of the main type.
      Returns:
      The converted object, or null if the input is null.
      Throws:
      InvalidConversionException - If no conversion path exists from the input type to the target type.
    • to

      default <T> T to(Object o, Object memberOf, ConverterSession session, Class<T> type)
      Converts the specified object to the specified type, using the given outer instance and converter session.

      The default implementation ignores both memberOf and session and delegates to to(Object, Class). Override in subclasses that wish to thread these into Conversion.to(Object, Object, ConverterSession, Class[]).

      Type Parameters:
      T - The type to convert to.
      Parameters:
      o - The object to convert. Can be null.
      memberOf - The outer instance for non-static inner class construction, or null.
      session - The converter session providing contextual objects, or null.
      type - The type to convert to.
      Returns:
      The converted object, or null if the input is null.
      Throws:
      InvalidConversionException - If no conversion path exists from the input type to the target type.
    • to

      default <T> T to(Object o, Object memberOf, ConverterSession session, Type mainType, Type... args)
      Converts the specified object to the specified parameterized type, using the given outer instance and converter session.

      The default implementation ignores both memberOf and session and delegates to to(Object, Type, Type[]). Override in subclasses that wish to thread these into Conversion.to(Object, Object, ConverterSession, Class[]).

      Type Parameters:
      T - The type to convert to.
      Parameters:
      o - The object to convert. Can be null.
      memberOf - The outer instance for non-static inner class construction, or null.
      session - The converter session providing contextual objects, or null.
      mainType - The main type to convert to.
      args - The type parameters of the main type.
      Returns:
      The converted object, or null if the input is null.
      Throws:
      InvalidConversionException - If no conversion path exists from the input type to the target type.