Class MarshallingContextConverter

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

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

A simple implementation of the Converter interface that delegates to the default MarshallingContext 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

      Singleton instance of the generic converter.

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

  • Method Details

    • to

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

      This method delegates to the default MarshallingContext 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:
      to in interface Converter
      Type Parameters:
      T - The target type to convert to.
      Parameters:
      o - The object to convert.
      type - The target class type.
      Returns:
      The converted object, or null if the input object is null.
      Throws:
      InvalidDataConversionException - If the object cannot be converted to the specified type.
    • to

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

      This method delegates to the default MarshallingContext session for the actual conversion logic, supporting complex parameterized types such as collections and maps.

      Specified by:
      to in interface Converter
      Type Parameters:
      T - The target type to convert to.
      Parameters:
      o - The object to convert.
      mainType - The main type to convert to.
      args - The type parameters of the main type.
      Returns:
      The converted object, or null if the input object is null.
      Throws:
      InvalidDataConversionException - If the object cannot be converted to the specified type.
    • to

      public <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 for non-static inner class construction.
      Specified by:
      to in interface Converter
      Type Parameters:
      T - The target type to convert to.
      Parameters:
      o - The object to convert.
      memberOf - The outer instance for non-static inner class construction, or null if the target is not a non-static inner member class.
      session - The converter session, or null (ignored either way; this implementation does not use a converter session).
      type - The target class type.
      Returns:
      The converted object, or null if the input object is null.
      Throws:
      InvalidDataConversionException - If the object cannot be converted to the specified type.
    • to

      public <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 for non-static inner class construction.
      Specified by:
      to in interface Converter
      Type Parameters:
      T - The target type to convert to.
      Parameters:
      o - The object to convert.
      memberOf - The outer instance for non-static inner class construction, or null if the target is not a non-static inner member class.
      session - The converter session, or null (ignored either way; this implementation does not use a converter session).
      mainType - The main type to convert to.
      args - The type parameters of the main type.
      Returns:
      The converted object, or null if the input object is null.
      Throws:
      InvalidDataConversionException - If the object cannot be converted to the specified type.