进一步优化 json 工具类,默认使用 Spring 初始化出来的

This commit is contained in:
YunaiV 2021-01-24 12:37:58 +08:00
parent eadc4f749a
commit fa3f382210
10 changed files with 59 additions and 38 deletions

View File

@ -0,0 +1,18 @@
package cn.iocoder.dashboard.framework.jackson.config;
import cn.iocoder.dashboard.util.json.JsonUtils;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class JacksonConfig {
@Bean
@SuppressWarnings("InstantiationOfUtilityClass")
public JsonUtils jsonUtils(ObjectMapper objectMapper) {
JsonUtils.init(objectMapper);
return new JsonUtils();
}
}

View File

@ -11,7 +11,7 @@ import cn.iocoder.dashboard.framework.logger.operatelog.core.service.OperateLogF
import cn.iocoder.dashboard.framework.security.core.util.SecurityUtils;
import cn.iocoder.dashboard.framework.tracer.core.util.TracerUtils;
import cn.iocoder.dashboard.modules.system.controller.logger.vo.operatelog.SysOperateLogCreateReqVO;
import cn.iocoder.dashboard.util.json.JSONUtils;
import cn.iocoder.dashboard.util.json.JsonUtils;
import cn.iocoder.dashboard.util.servlet.ServletUtils;
import com.google.common.collect.Maps;
import io.swagger.annotations.Api;
@ -319,7 +319,7 @@ public class OperateLogAspect {
// 被忽略时标记为 ignore 字符串避免和 null 混在一起
args.put(argName, !isIgnoreArgs(argValue) ? argValue : "[ignore]");
}
return JSONUtils.toJSONString(args);
return JsonUtils.toJsonString(args);
}
private static String obtainResultData(Object result) {
@ -327,7 +327,7 @@ public class OperateLogAspect {
if (result instanceof CommonResult) {
result = ((CommonResult<?>) result).getData();
}
return JSONUtils.toJSONString(result);
return JsonUtils.toJsonString(result);
}
private static boolean isIgnoreArgs(Object object) {

View File

@ -1,12 +1,10 @@
package cn.iocoder.dashboard.framework.mybatis.core.type;
import cn.iocoder.dashboard.modules.system.dal.mysql.dataobject.user.SysUserDO;
import cn.iocoder.dashboard.util.json.JsonUtils;
import com.baomidou.mybatisplus.extension.handlers.AbstractJsonTypeHandler;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.io.IOException;
import java.util.Set;
/**
@ -17,29 +15,18 @@ import java.util.Set;
*
* @author 芋道源码
*/
public class JacksonLongSetTypeHandler extends AbstractJsonTypeHandler<Object> {
// TODO 芋艿需要将 Spring 的设置下进来
private static final ObjectMapper objectMapper = new ObjectMapper();
public class JsonLongSetTypeHandler extends AbstractJsonTypeHandler<Object> {
private static final TypeReference<Set<Long>> typeReference = new TypeReference<Set<Long>>(){};
@Override
protected Object parse(String json) {
try {
return objectMapper.readValue(json, typeReference);
} catch (IOException e) {
throw new RuntimeException(e);
}
return JsonUtils.parseObject(json, typeReference);
}
@Override
protected String toJson(Object obj) {
try {
return objectMapper.writeValueAsString(obj);
} catch (JsonProcessingException e) {
throw new RuntimeException(e);
}
return JsonUtils.toJsonString(obj);
}
}

View File

@ -1,7 +1,7 @@
package cn.iocoder.dashboard.framework.redis.core.pubsub;
import cn.hutool.core.util.ArrayUtil;
import cn.iocoder.dashboard.util.json.JSONUtils;
import cn.iocoder.dashboard.util.json.JsonUtils;
import lombok.SneakyThrows;
import org.springframework.data.redis.connection.Message;
import org.springframework.data.redis.connection.MessageListener;
@ -44,7 +44,7 @@ public abstract class AbstractChannelMessageListener<T extends ChannelMessage> i
@Override
public final void onMessage(Message message, byte[] bytes) {
T messageObj = JSONUtils.parseObject(message.getBody(), messageType);
T messageObj = JsonUtils.parseObject(message.getBody(), messageType);
this.onMessage(messageObj);
}

View File

@ -1,7 +1,7 @@
package cn.iocoder.dashboard.framework.redis.core.util;
import cn.iocoder.dashboard.framework.redis.core.pubsub.ChannelMessage;
import cn.iocoder.dashboard.util.json.JSONUtils;
import cn.iocoder.dashboard.util.json.JsonUtils;
import org.springframework.data.redis.core.RedisTemplate;
/**
@ -18,7 +18,7 @@ public class RedisMessageUtils {
* @param message 消息
*/
public static <T extends ChannelMessage> void sendChannelMessage(RedisTemplate<?, ?> redisTemplate, T message) {
redisTemplate.convertAndSend(message.getChannel(), JSONUtils.toJSONString(message));
redisTemplate.convertAndSend(message.getChannel(), JsonUtils.toJsonString(message));
}
}

View File

@ -2,7 +2,7 @@ package cn.iocoder.dashboard.modules.system.dal.mysql.dataobject.permission;
import cn.iocoder.dashboard.common.enums.CommonStatusEnum;
import cn.iocoder.dashboard.framework.mybatis.core.dataobject.BaseDO;
import cn.iocoder.dashboard.framework.mybatis.core.type.JacksonLongSetTypeHandler;
import cn.iocoder.dashboard.framework.mybatis.core.type.JsonLongSetTypeHandler;
import cn.iocoder.dashboard.framework.security.core.enums.DataScopeEnum;
import cn.iocoder.dashboard.modules.system.enums.permission.RoleCodeEnum;
import cn.iocoder.dashboard.modules.system.enums.permission.SysRoleTypeEnum;
@ -71,7 +71,7 @@ public class SysRoleDO extends BaseDO {
*
* 适用于 {@link #dataScope} 的值为 {@link DataScopeEnum#DEPT_CUSTOM}
*/
@TableField(typeHandler = JacksonLongSetTypeHandler.class)
@TableField(typeHandler = JsonLongSetTypeHandler.class)
private Set<Long> dataScopeDeptIds;
}

View File

@ -2,7 +2,7 @@ package cn.iocoder.dashboard.modules.system.dal.mysql.dataobject.user;
import cn.iocoder.dashboard.common.enums.CommonStatusEnum;
import cn.iocoder.dashboard.framework.mybatis.core.dataobject.BaseDO;
import cn.iocoder.dashboard.framework.mybatis.core.type.JacksonLongSetTypeHandler;
import cn.iocoder.dashboard.framework.mybatis.core.type.JsonLongSetTypeHandler;
import cn.iocoder.dashboard.modules.system.enums.common.SysSexEnum;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
@ -54,7 +54,7 @@ public class SysUserDO extends BaseDO {
/**
* 岗位编号数组
*/
@TableField(typeHandler = JacksonLongSetTypeHandler.class)
@TableField(typeHandler = JsonLongSetTypeHandler.class)
private Set<Long> postIds;
/**
* 用户邮箱

View File

@ -1,7 +1,7 @@
package cn.iocoder.dashboard.modules.system.dal.redis.dao.auth;
import cn.iocoder.dashboard.framework.security.core.LoginUser;
import cn.iocoder.dashboard.util.json.JSONUtils;
import cn.iocoder.dashboard.util.json.JsonUtils;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Repository;
@ -22,12 +22,12 @@ public class SysLoginUserRedisDAO {
public LoginUser get(String sessionId) {
String redisKey = formatKey(sessionId);
return JSONUtils.parseObject(stringRedisTemplate.opsForValue().get(redisKey), LoginUser.class);
return JsonUtils.parseObject(stringRedisTemplate.opsForValue().get(redisKey), LoginUser.class);
}
public void set(String sessionId, LoginUser loginUser) {
String redisKey = formatKey(sessionId);
stringRedisTemplate.opsForValue().set(redisKey, JSONUtils.toJSONString(loginUser), LOGIN_USER.getTimeout());
stringRedisTemplate.opsForValue().set(redisKey, JsonUtils.toJsonString(loginUser), LOGIN_USER.getTimeout());
}
public void delete(String accessToken) {

View File

@ -1,25 +1,33 @@
package cn.iocoder.dashboard.util.json;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.io.IOException;
import java.util.Set;
/**
* JSON 工具类
*
* @author 芋道源码
*/
public class JSONUtils {
public class JsonUtils {
private static ObjectMapper objectMapper = new ObjectMapper();
// TODO 芋艿需要将 Spring 的设置下进来
public static void setObjectMapper(ObjectMapper objectMapper) {
JSONUtils.objectMapper = objectMapper;
/**
* 初始化 objectMapper 属性
*
* 通过这样的方式使用 Spring 创建的 ObjectMapper Bean
*
* @param objectMapper ObjectMapper 对象
*/
public static void init(ObjectMapper objectMapper) {
JsonUtils.objectMapper = objectMapper;
}
public static String toJSONString(Object object) {
public static String toJsonString(Object object) {
try {
return objectMapper.writeValueAsString(object);
} catch (JsonProcessingException e) {
@ -43,4 +51,12 @@ public class JSONUtils {
}
}
public static Object parseObject(String text, TypeReference<Set<Long>> typeReference) {
try {
return objectMapper.readValue(text, typeReference);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}

View File

@ -2,7 +2,7 @@ package cn.iocoder.dashboard.util.servlet;
import cn.hutool.core.io.IoUtil;
import cn.hutool.extra.servlet.ServletUtil;
import cn.iocoder.dashboard.util.json.JSONUtils;
import cn.iocoder.dashboard.util.json.JsonUtils;
import org.springframework.http.MediaType;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
@ -28,7 +28,7 @@ public class ServletUtils {
*/
@SuppressWarnings("deprecation") // 必须使用 APPLICATION_JSON_UTF8_VALUE否则会乱码
public static void writeJSON(HttpServletResponse response, Object object) {
String content = JSONUtils.toJSONString(object);
String content = JsonUtils.toJsonString(object);
ServletUtil.write(response, content, MediaType.APPLICATION_JSON_UTF8_VALUE);
}