public class Convert
extends cn.hutool.core.convert.Convert
提供简单全面的类型转换,适合更多的业务场景,内置hutool、fastjson、yue三种类型转换器,判断精确性能强大,未知类型兼容性更强
| 构造器和说明 |
|---|
Convert() |
| 限定符和类型 | 方法和说明 |
|---|---|
static <T> T |
convert(Class<T> type,
Object value)
已过时。
|
static <T> T |
convert(Class<T> type,
Object value,
T defaultValue)
转换值为指定类型
|
static <T> T |
convert(Object value,
Class<T> clazz)
转换值为指定类型
(推荐) |
static <T> T |
convert(Type type,
Object value)
转换值为指定类型
|
static <T> T |
convert(Type type,
Object value,
T defaultValue)
转换值为指定类型
|
static <T> T |
convert(cn.hutool.core.lang.TypeReference<T> reference,
Object value)
转换值为指定类型
|
static <T> T |
convertByClassName(String className,
Object value)
转换值为指定类型,类型采用字符串表示
|
static <T> T |
convertQuietly(Type type,
Object value)
转换值为指定类型,不抛异常转换
当转换失败时返回 null |
static <T> T |
convertQuietly(Type type,
Object value,
T defaultValue)
转换值为指定类型,不抛异常转换
当转换失败时返回默认值 |
static String |
toCamelCase(CharSequence name)
将下划线方式命名的字符串转换为驼峰式。
|
static com.alibaba.fastjson.JSONObject |
toCamelCase(Object param)
属性命名策略转换-驼峰命名法
|
static List<com.alibaba.fastjson.JSONObject> |
toCamelCaseJsonList(List<com.alibaba.fastjson.JSONObject> list)
JsonList属性命名策略转换-驼峰命名法
|
static <T> List<com.alibaba.fastjson.JSONObject> |
toCamelCaseJsonListT(List<T> list)
JsonList属性命名策略转换-驼峰命名法
|
static <T> T |
toJavaBean(Object value,
Class<T> clazz)
转换值为指定 POJO 类型
性能测试对比如下: |
static com.alibaba.fastjson.JSONArray |
toJSONArray(Object value)
转换为 JSONArray
|
static List<com.alibaba.fastjson.JSONObject> |
toJsonList(com.alibaba.fastjson.JSONArray jsonArray)
|
static List<com.alibaba.fastjson.JSONObject> |
toJsonList(List<Map<String,Object>> list)
|
static <T> List<com.alibaba.fastjson.JSONObject> |
toJsonListT(List<T> list)
|
static com.alibaba.fastjson.JSONObject |
toJSONObject(Object value)
转换为 JSONObject
|
static com.alibaba.fastjson.JSONObject[] |
toJsons(com.alibaba.fastjson.JSONArray jsonArray)
JSONArray 转 JSONObject[]
对象引用转换,内存指针依旧指向元数据
|
static com.alibaba.fastjson.JSONObject[] |
toJsons(List<com.alibaba.fastjson.JSONObject> list)
List-JSONObject 转 JSONObject[]
对象引用转换,内存指针依旧指向元数据
|
static com.alibaba.fastjson.JSONObject[] |
toJsons(String jsonString)
String 转 JSONObject[]
|
static com.alibaba.fastjson.JSONObject[] |
toJsons(String text,
String regex,
String key)
String 转 JSONObject[]
示例:
String text = "1,3,5,9";
JSONObject[] jsons = toJsons(text, ",", "id");
System.out.println(Arrays.toString(jsons));
结果:
[{"id":"1"}, {"id":"3"}, {"id":"5"}, {"id":"9"}]
|
static <T> com.alibaba.fastjson.JSONObject[] |
toJsonsT(List<T> list)
|
static <T> com.alibaba.fastjson.JSONObject[] |
toJsonsTAndRemoveEmpty(List<T> list)
|
static <T> List<T> |
toList(com.alibaba.fastjson.JSONArray jsonArray,
Class<T> clazz)
|
static <T> List<T> |
toList(List<com.alibaba.fastjson.JSONObject> list,
Class<T> clazz)
|
static List<String> |
toList(List<com.alibaba.fastjson.JSONObject> list,
String keepKey)
|
static <T> List<T> |
toList(List<com.alibaba.fastjson.JSONObject> list,
String keepKey,
Class<T> clazz)
|
static <T> ArrayList<T> |
toList(T[] array)
数组转List
此方法为 Arrays.asList(Object...)
|
static List<String> |
toListAndDistinct(List<com.alibaba.fastjson.JSONObject> list,
String keepKey)
|
static <T> List<T> |
toListAndDistinct(List<com.alibaba.fastjson.JSONObject> list,
String keepKey,
Class<T> clazz)
|
static <T> T |
toObject(Object value,
Class<T> clazz)
转换值为指定类型
|
static com.alibaba.fastjson.JSONObject |
toPropertyNamingStrategy(Object param,
com.alibaba.fastjson.PropertyNamingStrategy propertyNamingStrategy)
属性命名策略转换
|
static com.alibaba.fastjson.JSONObject |
toSnakeCase(Object param)
属性命名策略转换-下划线命名法
|
static List<com.alibaba.fastjson.JSONObject> |
toSnakeCaseJsonList(List<com.alibaba.fastjson.JSONObject> list)
JsonList属性命名策略转换-下划线命名法
|
static <T> List<com.alibaba.fastjson.JSONObject> |
toSnakeCaseJsonListT(List<T> list)
JsonList属性命名策略转换-下划线命名法
|
static String |
toSymbolCase(CharSequence str,
char symbol)
将驼峰式命名的字符串转换为使用符号连接方式。
|
static String |
toUnderlineCase(CharSequence str)
将驼峰式命名的字符串转换为下划线方式。
|
static com.alibaba.fastjson.JSONObject |
toUnderlineCase(Object param)
属性命名策略转换-下划线命名法
|
static List<com.alibaba.fastjson.JSONObject> |
toUnderlineCaseJsonList(List<com.alibaba.fastjson.JSONObject> list)
JsonList属性命名策略转换-下划线命名法
|
static <T> List<com.alibaba.fastjson.JSONObject> |
toUnderlineCaseJsonListT(List<T> list)
JsonList属性命名策略转换-下划线命名法
|
bytesToInt, bytesToLong, bytesToShort, byteToUnsignedInt, chineseToNumber, convertCharset, convertTime, convertWithCheck, digitToChinese, hexStrToStr, hexToBytes, hexToStr, intToByte, intToBytes, longToBytes, numberToChinese, numberToSimple, numberToWord, shortToBytes, strToUnicode, toBigDecimal, toBigDecimal, toBigInteger, toBigInteger, toBool, toBool, toBooleanArray, toByte, toByte, toByteArray, toChar, toChar, toCharArray, toCollection, toDate, toDate, toDBC, toDBC, toDouble, toDouble, toDoubleArray, toEnum, toEnum, toFloat, toFloat, toFloatArray, toHex, toHex, toInstant, toInt, toInt, toIntArray, toList, toList, toLocalDateTime, toLocalDateTime, toLong, toLong, toLongArray, toMap, toNumber, toNumber, toNumberArray, toPrimitiveByteArray, toSBC, toSBC, toShort, toShort, toShortArray, toStr, toStr, toStrArray, unicodeToStr, unWrap, wrappublic static <T> T convertByClassName(String className, Object value) throws cn.hutool.core.convert.ConvertException
T - 目标类型className - 类的字符串表示value - 值cn.hutool.core.convert.ConvertException - 转换器不存在@Deprecated public static <T> T convert(Class<T> type, Object value) throws cn.hutool.core.convert.ConvertException
T - 目标类型type - 类型value - 值cn.hutool.core.convert.ConvertException - 转换器不存在public static <T> T convert(cn.hutool.core.lang.TypeReference<T> reference,
Object value)
throws cn.hutool.core.convert.ConvertException
T - 目标类型reference - 类型参考,用于持有转换后的泛型类型value - 值cn.hutool.core.convert.ConvertException - 转换器不存在public static <T> T convert(Type type, Object value) throws cn.hutool.core.convert.ConvertException
T - 目标类型type - 类型value - 值cn.hutool.core.convert.ConvertException - 转换器不存在public static <T> T convert(Class<T> type, Object value, T defaultValue) throws cn.hutool.core.convert.ConvertException
T - 目标类型type - 类型value - 值defaultValue - 默认值cn.hutool.core.convert.ConvertException - 转换器不存在public static <T> T convert(Type type, Object value, T defaultValue) throws cn.hutool.core.convert.ConvertException
T - 目标类型type - 类型value - 值defaultValue - 默认值cn.hutool.core.convert.ConvertException - 转换器不存在public static <T> T convertQuietly(Type type, Object value)
nullT - 目标类型type - 目标类型value - 值public static <T> T convertQuietly(Type type, Object value, T defaultValue)
T - 目标类型type - 目标类型value - 值defaultValue - 默认值public static <T> T convert(Object value, Class<T> clazz)
(推荐)T - 泛型value - 被转换的值clazz - 泛型类型toObject(Object, Class)public static <T> T toObject(Object value, Class<T> clazz)
T - 泛型value - 被转换的值clazz - 泛型类型public static <T> T toJavaBean(Object value, Class<T> clazz)
性能测试对比如下:
1、Spring BeanUtils:性能伯仲,兼容性远超
2、Cglib BeanCopier:性能伯仲,兼容性远超
3、Apache BeanUtils:秒杀
4、Apache PropertyUtils:秒杀
5、Dozer:秒杀
T - 泛型value - 被转换的值clazz - 泛型类型public static com.alibaba.fastjson.JSONObject toJSONObject(Object value)
value - 被转换的值public static com.alibaba.fastjson.JSONArray toJSONArray(Object value)
value - 被转换的值public static <T> ArrayList<T> toList(T[] array)
此方法为 Arrays.asList(Object...) 的安全实现
T - 数组中的对象类array - 将被转换的数组ListUtils.toList(Object[])public static <T> List<T> toList(com.alibaba.fastjson.JSONArray jsonArray, Class<T> clazz)
T - 泛型jsonArray - 需要转换的JSONArrayclazz - json转换的POJO类型ListUtils.toList(JSONArray, Class)public static <T> List<T> toList(List<com.alibaba.fastjson.JSONObject> list, Class<T> clazz)
T - 泛型list - 需要转换的Listclazz - json转换的POJO类型ListUtils.toList(List, Class)public static List<String> toList(List<com.alibaba.fastjson.JSONObject> list, String keepKey)
list - 需要转换的ListkeepKey - 保留值的keyListUtils.toList(List, String)public static <T> List<T> toList(List<com.alibaba.fastjson.JSONObject> list, String keepKey, Class<T> clazz)
T - 泛型list - 需要转换的ListkeepKey - 保留值的keyclazz - 类型ListUtils.toList(List, String, Class)public static List<String> toListAndDistinct(List<com.alibaba.fastjson.JSONObject> list, String keepKey)
list - 需要转换的ListkeepKey - 保留值的keyListUtils.toListAndDistinct(List, String)public static <T> List<T> toListAndDistinct(List<com.alibaba.fastjson.JSONObject> list, String keepKey, Class<T> clazz)
T - 泛型list - 需要转换的ListkeepKey - 保留值的keyclazz - 类型ListUtils.toListAndDistinct(List, String, Class)public static List<com.alibaba.fastjson.JSONObject> toJsonList(List<Map<String,Object>> list)
list - 需要转换的ListListUtils.toJsonList(List)public static List<com.alibaba.fastjson.JSONObject> toJsonList(com.alibaba.fastjson.JSONArray jsonArray)
性能测试报告:
无类型转换(类型推断):见 toJsonList(List)
安全模式强制类型转换:暂未测试
jsonArray - 需要转换的JSONArrayListUtils.toJsonList(JSONArray)public static <T> List<com.alibaba.fastjson.JSONObject> toJsonListT(List<T> list)
T - 泛型list - 需要转换的ListListUtils.toJsonListT(List)public static com.alibaba.fastjson.JSONObject[] toJsons(com.alibaba.fastjson.JSONArray jsonArray)
对象引用转换,内存指针依旧指向元数据
jsonArray - 需要转换的JSONArrayListUtils.toJsons(JSONArray)public static com.alibaba.fastjson.JSONObject[] toJsons(List<com.alibaba.fastjson.JSONObject> list)
对象引用转换,内存指针依旧指向元数据
list - 需要转换的ListListUtils.toJsons(List)public static <T> com.alibaba.fastjson.JSONObject[] toJsonsT(List<T> list)
T - 泛型list - 需要转换的ListListUtils.toJsonsT(List)public static <T> com.alibaba.fastjson.JSONObject[] toJsonsTAndRemoveEmpty(List<T> list)
T - 泛型list - 需要转换的ListListUtils.toJsonsTAndRemoveEmpty(List)public static com.alibaba.fastjson.JSONObject[] toJsons(String jsonString)
jsonString - 需要转换的JSON字符串ListUtils.toJsons(String)public static com.alibaba.fastjson.JSONObject[] toJsons(String text, String regex, String key)
示例:
String text = "1,3,5,9";
JSONObject[] jsons = toJsons(text, ",", "id");
System.out.println(Arrays.toString(jsons));
结果:
[{"id":"1"}, {"id":"3"}, {"id":"5"}, {"id":"9"}]
text - 需要转换的文本regex - 文本分割表达式,同String类的split()方法key - JSON的key名称ListUtils.toJsons(String, String, String)public static String toUnderlineCase(CharSequence str)
HelloWorld=》hello_world Hello_World=》hello_world HelloWorld_test=》hello_world_test
str - 转换前的驼峰式命名的字符串,也可以为下划线形式CharSequenceUtil.toUnderlineCase(CharSequence)public static String toSymbolCase(CharSequence str, char symbol)
str - 转换前的驼峰式命名的字符串,也可以为符号连接形式symbol - 连接符CharSequenceUtil.toSymbolCase(CharSequence, char)public static String toCamelCase(CharSequence name)
name - 转换前的下划线大写方式命名的字符串CharSequenceUtil.toCamelCase(CharSequence)public static com.alibaba.fastjson.JSONObject toCamelCase(Object param)
param - Json参数 或 POJO对象MapUtils.toCamelCase(Object)public static com.alibaba.fastjson.JSONObject toUnderlineCase(Object param)
param - Json参数 或 POJO对象MapUtils.toUnderlineCase(Object)public static com.alibaba.fastjson.JSONObject toSnakeCase(Object param)
param - Json参数 或 POJO对象MapUtils.toSnakeCase(Object)public static com.alibaba.fastjson.JSONObject toPropertyNamingStrategy(Object param, com.alibaba.fastjson.PropertyNamingStrategy propertyNamingStrategy)
param - Json参数 或 POJO对象propertyNamingStrategy - 属性命名策略MapUtils.toPropertyNamingStrategy(Object, PropertyNamingStrategy)public static List<com.alibaba.fastjson.JSONObject> toSnakeCaseJsonList(List<com.alibaba.fastjson.JSONObject> list)
list - 需要转换的jsonListpublic static <T> List<com.alibaba.fastjson.JSONObject> toSnakeCaseJsonListT(List<T> list)
T - 泛型list - 需要转换的Listpublic static List<com.alibaba.fastjson.JSONObject> toUnderlineCaseJsonList(List<com.alibaba.fastjson.JSONObject> list)
list - 需要转换的jsonListpublic static <T> List<com.alibaba.fastjson.JSONObject> toUnderlineCaseJsonListT(List<T> list)
T - 泛型list - 需要转换的Listpublic static List<com.alibaba.fastjson.JSONObject> toCamelCaseJsonList(List<com.alibaba.fastjson.JSONObject> list)
list - 需要转换的jsonListCopyright © 2021 Pivotal Software, Inc.. All rights reserved.