Class GenericConverter

java.lang.Object
org.apache.juneau.utils.GenericConverter
All Implemented Interfaces:
Converter

public class GenericConverter extends Object implements Converter
Generic object converter implementation.

A simple implementation of the Converter interface that delegates to the default BeanContext session for type conversion.

This converter provides a convenient way to convert objects between different types using the same conversion logic that's used throughout the Juneau framework.

Example:

// Convert a string to an integer Integer result = GenericConverter.INSTANCE.convertTo(Integer.class, "123"); // result = 123 // Convert a map to a bean MyBean bean = GenericConverter.INSTANCE.convertTo(MyBean.class, map);

Thread Safety:

This class is thread-safe. The singleton instance can be safely shared across multiple threads.

See Also:
  • Field Details

    • INSTANCE

      public static final GenericConverter INSTANCE
      Singleton instance of the generic converter.

      This instance can be safely shared across multiple threads and reused for all conversion operations.

  • Constructor Details

  • Method Details

    • convertTo

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

      This method delegates to the default BeanContext session for the actual conversion logic. It supports all the same conversion types that are supported by the framework's bean conversion system.

      Supported conversions include:

      • Primitive types and their wrapper classes
      • String to Number conversions
      • Map to Bean conversions
      • Collection to Array conversions
      • Enum conversions
      • Object swap conversions
      • And many more...
      Specified by:
      convertTo in interface Converter
      Type Parameters:
      T - The target type to convert to.
      Parameters:
      type - The target class type.
      o - The object to convert.
      Returns:
      The converted object, or null if the input object is null.
      Throws:
      InvalidDataConversionException - If the object cannot be converted to the specified type.