update sa-token 1.28.0 => 1.29.0 修复Xss注解字段值为空时的异常问题 删除方法无返回值时,方法注释上的@HTTP4O4 update 使用 satoken 自带的 BCrypt 工具 替换 Security 加密工具 减少依赖 优化流程任务方法,统一响应格式 fix 修复 R 参数位置错误 fix 修复 验证码 强制校验问题 update 返回体 统一 修复自定义组件file-upload无法显示第一个文件,列表显示的文件比实际文件少一个的问题 update 修改验证码校验 增加 uuid 空判断 update 优化代码生成 fix 修复因升级 sa-token 导致 doLogin 无法获取 token 问题 update 更新 swagger 配置类错误注释 update 优化 TreeBuildUtils 工具 使用反射自动获取顶级父id fix 回滚代码生成部分优化 修复优化导致的问题 update 使用 hutool Dict 优化 JsonUtils 防止类型解析异常 update 优化代码生成 使用新 JsonUtils.parseMap 方法
109 lines
3.1 KiB
Java
109 lines
3.1 KiB
Java
package com.ruoyi.common.utils;
|
|
|
|
import cn.hutool.core.lang.Dict;
|
|
import cn.hutool.core.util.ArrayUtil;
|
|
import cn.hutool.core.util.ObjectUtil;
|
|
import com.fasterxml.jackson.core.JsonProcessingException;
|
|
import com.fasterxml.jackson.core.type.TypeReference;
|
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
|
import com.ruoyi.common.utils.spring.SpringUtils;
|
|
import lombok.AccessLevel;
|
|
import lombok.NoArgsConstructor;
|
|
|
|
import java.io.IOException;
|
|
import java.util.ArrayList;
|
|
import java.util.List;
|
|
|
|
/**
|
|
* JSON 工具类
|
|
*
|
|
* @author 芋道源码
|
|
*/
|
|
@NoArgsConstructor(access = AccessLevel.PRIVATE)
|
|
public class JsonUtils {
|
|
|
|
private static final ObjectMapper OBJECT_MAPPER = SpringUtils.getBean(ObjectMapper.class);
|
|
|
|
public static ObjectMapper getObjectMapper() {
|
|
return OBJECT_MAPPER;
|
|
}
|
|
|
|
public static String toJsonString(Object object) {
|
|
if (ObjectUtil.isNull(object)) {
|
|
return null;
|
|
}
|
|
try {
|
|
return OBJECT_MAPPER.writeValueAsString(object);
|
|
} catch (JsonProcessingException e) {
|
|
throw new RuntimeException(e);
|
|
}
|
|
}
|
|
|
|
public static <T> T parseObject(String text, Class<T> clazz) {
|
|
if (StringUtils.isEmpty(text)) {
|
|
return null;
|
|
}
|
|
try {
|
|
return OBJECT_MAPPER.readValue(text, clazz);
|
|
} catch (IOException e) {
|
|
throw new RuntimeException(e);
|
|
}
|
|
}
|
|
|
|
public static <T> T parseObject(byte[] bytes, Class<T> clazz) {
|
|
if (ArrayUtil.isEmpty(bytes)) {
|
|
return null;
|
|
}
|
|
try {
|
|
return OBJECT_MAPPER.readValue(bytes, clazz);
|
|
} catch (IOException e) {
|
|
throw new RuntimeException(e);
|
|
}
|
|
}
|
|
|
|
public static <T> T parseObject(String text, TypeReference<T> typeReference) {
|
|
if (StringUtils.isBlank(text)) {
|
|
return null;
|
|
}
|
|
try {
|
|
return OBJECT_MAPPER.readValue(text, typeReference);
|
|
} catch (IOException e) {
|
|
throw new RuntimeException(e);
|
|
}
|
|
}
|
|
|
|
public static Dict parseMap(String text) {
|
|
if (StringUtils.isBlank(text)) {
|
|
return null;
|
|
}
|
|
try {
|
|
return OBJECT_MAPPER.readValue(text, OBJECT_MAPPER.getTypeFactory().constructType(Dict.class));
|
|
} catch (IOException e) {
|
|
throw new RuntimeException(e);
|
|
}
|
|
}
|
|
|
|
public static List<Dict> parseArrayMap(String text) {
|
|
if (StringUtils.isBlank(text)) {
|
|
return null;
|
|
}
|
|
try {
|
|
return OBJECT_MAPPER.readValue(text, OBJECT_MAPPER.getTypeFactory().constructCollectionType(List.class, Dict.class));
|
|
} catch (IOException e) {
|
|
throw new RuntimeException(e);
|
|
}
|
|
}
|
|
|
|
public static <T> List<T> parseArray(String text, Class<T> clazz) {
|
|
if (StringUtils.isEmpty(text)) {
|
|
return new ArrayList<>();
|
|
}
|
|
try {
|
|
return OBJECT_MAPPER.readValue(text, OBJECT_MAPPER.getTypeFactory().constructCollectionType(List.class, clazz));
|
|
} catch (IOException e) {
|
|
throw new RuntimeException(e);
|
|
}
|
|
}
|
|
|
|
}
|