| | |
| | | import com.fasterxml.jackson.core.type.TypeReference; |
| | | import com.fasterxml.jackson.databind.ObjectMapper; |
| | | import com.fasterxml.jackson.databind.exc.MismatchedInputException; |
| | | import org.dromara.common.core.utils.SpringUtils; |
| | | import org.dromara.common.core.utils.StringUtils; |
| | | import lombok.AccessLevel; |
| | | import lombok.NoArgsConstructor; |
| | | import org.dromara.common.core.utils.SpringUtils; |
| | | import org.dromara.common.core.utils.StringUtils; |
| | | |
| | | import java.io.IOException; |
| | | import java.util.ArrayList; |
| | |
| | | return OBJECT_MAPPER; |
| | | } |
| | | |
| | | /** |
| | | * 将对象转换为JSON格式的字符串 |
| | | * |
| | | * @param object 要转换的对象 |
| | | * @return JSON格式的字符串,如果对象为null,则返回null |
| | | * @throws RuntimeException 如果转换过程中发生JSON处理异常,则抛出运行时异常 |
| | | */ |
| | | public static String toJsonString(Object object) { |
| | | if (ObjectUtil.isNull(object)) { |
| | | return null; |
| | |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 将JSON格式的字符串转换为指定类型的对象 |
| | | * |
| | | * @param text JSON格式的字符串 |
| | | * @param clazz 要转换的目标对象类型 |
| | | * @param <T> 目标对象的泛型类型 |
| | | * @return 转换后的对象,如果字符串为空则返回null |
| | | * @throws RuntimeException 如果转换过程中发生IO异常,则抛出运行时异常 |
| | | */ |
| | | public static <T> T parseObject(String text, Class<T> clazz) { |
| | | if (StringUtils.isEmpty(text)) { |
| | | return null; |
| | |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 将字节数组转换为指定类型的对象 |
| | | * |
| | | * @param bytes 字节数组 |
| | | * @param clazz 要转换的目标对象类型 |
| | | * @param <T> 目标对象的泛型类型 |
| | | * @return 转换后的对象,如果字节数组为空则返回null |
| | | * @throws RuntimeException 如果转换过程中发生IO异常,则抛出运行时异常 |
| | | */ |
| | | public static <T> T parseObject(byte[] bytes, Class<T> clazz) { |
| | | if (ArrayUtil.isEmpty(bytes)) { |
| | | return null; |
| | |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 将JSON格式的字符串转换为指定类型的对象,支持复杂类型 |
| | | * |
| | | * @param text JSON格式的字符串 |
| | | * @param typeReference 指定类型的TypeReference对象 |
| | | * @param <T> 目标对象的泛型类型 |
| | | * @return 转换后的对象,如果字符串为空则返回null |
| | | * @throws RuntimeException 如果转换过程中发生IO异常,则抛出运行时异常 |
| | | */ |
| | | public static <T> T parseObject(String text, TypeReference<T> typeReference) { |
| | | if (StringUtils.isBlank(text)) { |
| | | return null; |
| | |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 将JSON格式的字符串转换为Dict对象 |
| | | * |
| | | * @param text JSON格式的字符串 |
| | | * @return 转换后的Dict对象,如果字符串为空或者不是JSON格式则返回null |
| | | * @throws RuntimeException 如果转换过程中发生IO异常,则抛出运行时异常 |
| | | */ |
| | | public static Dict parseMap(String text) { |
| | | if (StringUtils.isBlank(text)) { |
| | | return null; |
| | |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 将JSON格式的字符串转换为Dict对象的列表 |
| | | * |
| | | * @param text JSON格式的字符串 |
| | | * @return 转换后的Dict对象的列表,如果字符串为空则返回null |
| | | * @throws RuntimeException 如果转换过程中发生IO异常,则抛出运行时异常 |
| | | */ |
| | | public static List<Dict> parseArrayMap(String text) { |
| | | if (StringUtils.isBlank(text)) { |
| | | return null; |
| | |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 将JSON格式的字符串转换为指定类型对象的列表 |
| | | * |
| | | * @param text JSON格式的字符串 |
| | | * @param clazz 要转换的目标对象类型 |
| | | * @param <T> 目标对象的泛型类型 |
| | | * @return 转换后的对象的列表,如果字符串为空则返回空列表 |
| | | * @throws RuntimeException 如果转换过程中发生IO异常,则抛出运行时异常 |
| | | */ |
| | | public static <T> List<T> parseArray(String text, Class<T> clazz) { |
| | | if (StringUtils.isEmpty(text)) { |
| | | return new ArrayList<>(); |