Module bus.core

Class MapConverter

java.lang.Object
org.miaixz.bus.core.convert.ConverterWithRoot
org.miaixz.bus.core.convert.MapConverter
All Implemented Interfaces:
Serializable, Converter, MatcherConverter

public class MapConverter extends ConverterWithRoot implements MatcherConverter, Serializable
Map 转换器,通过预定义key和value的类型,实现:
  • Map 转 Map,key和value类型自动转换
  • Bean 转 Map,字段和字段值类型自动转换
Since:
Java 17+
Author:
Kimi Liu
See Also:
  • Field Details

    • INSTANCE

      public static final MapConverter INSTANCE
      单例
  • Constructor Details

    • MapConverter

      public MapConverter(Converter rootConverter)
      构造
      Parameters:
      rootConverter - 根转换器,用于转换Map中键值对中的值,非null
  • Method Details

    • match

      public boolean match(Type targetType, Class<?> rawType, Object value)
      Description copied from interface: MatcherConverter
      判断需要转换的对象是否匹配当前转换器,满足则转换,否则跳过
      Specified by:
      match in interface MatcherConverter
      Parameters:
      targetType - 转换的目标类型,不能为null
      rawType - 目标原始类型,当targetType为Class时,和此参数一致,不能为null
      value - 需要转换的值
      Returns:
      是否匹配
    • convert

      public Object convert(Type targetType, Object value) throws ConvertException
      Description copied from interface: Converter
      转换为指定类型 如果类型无法确定,将读取默认值的类型做为目标类型
      Specified by:
      convert in interface Converter
      Parameters:
      targetType - 目标Type,非泛型类使用
      value - 原始值,如果对象实现了此接口,则value为this
      Returns:
      转换后的值
      Throws:
      ConvertException - 转换无法正常完成或转换异常时抛出此异常
    • convert

      public Map<?,?> convert(Type targetType, Type keyType, Type valueType, Object value) throws ConvertException
      转换对象为指定键值类型的指定类型Map
      Parameters:
      targetType - 目标的Map类型
      keyType - 键类型
      valueType - 值类型
      value - 被转换的值
      Returns:
      转换后的Map
      Throws:
      ConvertException - 转换异常或不支持的类型