Class ConverterUtils

java.lang.Object
org.apache.juneau.marshall.internal.ConverterUtils

public class ConverterUtils extends Object
Utility class for common type conversions and building typed collections and maps.
  • Method Details

    • toBoolean

      public static Boolean toBoolean(Object o)
      Converts an object to a Boolean.
      Parameters:
      o - The object to convert.
      Returns:
      The converted object.
    • toInteger

      public static Integer toInteger(Object o)
      Converts an object to an Integer.
      Parameters:
      o - The object to convert.
      Returns:
      The converted object.
    • toNumber

      public static Number toNumber(Object o)
      Converts an object to a Number.
      Parameters:
      o - The object to convert.
      Returns:
      The converted object.
    • toType

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

      Uses the full MarshallingContextConverter to support bean-aware conversions such as Map to bean, bean to bean, and ObjectSwap transforms.

      Type Parameters:
      T - The class type to convert the value to.
      Parameters:
      value - The value to convert.
      type - The class type to convert the value to.
      Returns:
      The converted value.
    • toListBuilder

      public static <T> Lists<T> toListBuilder(Object value, Class<T> type)
      Converts the specified object to a Lists with elements of the specified type.

      The input value can be any of the following:

      • An array of objects convertible to the element type
      • A Collection of objects convertible to the element type
      • A single object convertible to the element type (creates a list with one element)
      • A JSON array string that can be parsed into objects of the element type
      Type Parameters:
      T - The element type.
      Parameters:
      value - The value to convert. Can be null (no elements are added; an empty builder is returned).
      type - The element type class.
      Returns:
      A new Lists containing the converted elements.
    • toMapBuilder

      public static <K, V> Maps<K,V> toMapBuilder(Object value, Class<K> keyType, Class<V> valueType)
      Converts the specified object to a Maps with keys and values of the specified types.

      The input value can be any of the following:

      • A Map with entries convertible to the key/value types
      • A JSON object string that can be parsed into a map with the specified key/value types
      • An object with bean properties that can be converted to map entries
      Type Parameters:
      K - The key type.
      V - The value type.
      Parameters:
      value - The value to convert. Can be null (no entries are added; an empty builder is returned).
      keyType - The key type class.
      valueType - The value type class.
      Returns:
      A new Maps containing the converted entries.
    • toSetBuilder

      public static <T> Sets<T> toSetBuilder(Object value, Class<T> type)
      Converts the specified object to a Sets with elements of the specified type.

      The input value can be any of the following:

      • An array of objects convertible to the element type
      • A Collection of objects convertible to the element type
      • A single object convertible to the element type (creates a set with one element)
      • A JSON array string that can be parsed into objects of the element type

      Duplicate elements (after conversion) will be automatically removed as per Set semantics.

      Type Parameters:
      T - The element type.
      Parameters:
      value - The value to convert. Can be null (no elements are added; an empty builder is returned).
      type - The element type class.
      Returns:
      A new Sets containing the converted elements.