mirror of
https://gitee.com/zhijiantianya/ruoyi-vue-pro.git
synced 2026-03-22 05:07:17 +08:00
feat:【框架】TimestampLocalDateTimeSerializer 优化下代码格式
This commit is contained in:
@@ -1,11 +1,14 @@
|
||||
package cn.iocoder.yudao.framework.common.util.json.databind;
|
||||
|
||||
import cn.hutool.core.util.ObjUtil;
|
||||
import cn.hutool.core.util.ReflectUtil;
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import com.fasterxml.jackson.annotation.JsonFormat;
|
||||
import com.fasterxml.jackson.annotation.JsonProperty;
|
||||
import com.fasterxml.jackson.core.JsonGenerator;
|
||||
import com.fasterxml.jackson.databind.JsonSerializer;
|
||||
import com.fasterxml.jackson.databind.SerializerProvider;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.lang.reflect.Field;
|
||||
@@ -21,28 +24,13 @@ import java.util.concurrent.ConcurrentHashMap;
|
||||
*
|
||||
* @author 老五
|
||||
*/
|
||||
@Slf4j
|
||||
public class TimestampLocalDateTimeSerializer extends JsonSerializer<LocalDateTime> {
|
||||
|
||||
public static final TimestampLocalDateTimeSerializer INSTANCE = new TimestampLocalDateTimeSerializer();
|
||||
|
||||
private static final Map<Class<?>, Map<String, Field>> FIELD_CACHE = new ConcurrentHashMap<>();
|
||||
|
||||
private Map<String, Field> buildFieldMap(Class<?> clazz) {
|
||||
Map<String, Field> fieldMap = new HashMap<>();
|
||||
for (Field field : ReflectUtil.getFields(clazz)) {
|
||||
String fieldName = field.getName();
|
||||
JsonProperty jsonProperty = field.getAnnotation(JsonProperty.class);
|
||||
if (jsonProperty != null) {
|
||||
String value = jsonProperty.value();
|
||||
if (!value.isEmpty() && !"\u0000".equals(value)) {
|
||||
fieldName = value;
|
||||
}
|
||||
}
|
||||
fieldMap.put(fieldName, field);
|
||||
}
|
||||
return fieldMap;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void serialize(LocalDateTime value, JsonGenerator gen, SerializerProvider serializers) throws IOException {
|
||||
// 情况一:有 JsonFormat 自定义注解,则使用它。https://github.com/YunaiV/ruoyi-vue-pro/pull/1019
|
||||
@@ -53,13 +41,16 @@ public class TimestampLocalDateTimeSerializer extends JsonSerializer<LocalDateTi
|
||||
Class<?> clazz = currentValue.getClass();
|
||||
Map<String, Field> fieldMap = FIELD_CACHE.computeIfAbsent(clazz, this::buildFieldMap);
|
||||
Field field = fieldMap.get(fieldName);
|
||||
// 进一步修复:https://gitee.com/zhijiantianya/ruoyi-vue-pro/pulls/1480
|
||||
if (field != null && field.isAnnotationPresent(JsonFormat.class)) {
|
||||
JsonFormat jsonFormat = field.getAnnotation(JsonFormat.class);
|
||||
try {
|
||||
DateTimeFormatter formatter = DateTimeFormatter.ofPattern(jsonFormat.pattern());
|
||||
gen.writeString(formatter.format(value));
|
||||
return;
|
||||
} catch (Exception ignored) {
|
||||
} catch (Exception ex) {
|
||||
log.warn("[serialize][({}#{}) 使用 JsonFormat pattern 失败,尝试使用默认的 Long 时间戳]",
|
||||
clazz.getName(), fieldName, ex);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -69,4 +60,26 @@ public class TimestampLocalDateTimeSerializer extends JsonSerializer<LocalDateTi
|
||||
gen.writeNumber(value.atZone(ZoneId.systemDefault()).toInstant().toEpochMilli());
|
||||
}
|
||||
|
||||
/**
|
||||
* 构建字段映射(缓存)
|
||||
*
|
||||
* @param clazz 类
|
||||
* @return 字段映射
|
||||
*/
|
||||
private Map<String, Field> buildFieldMap(Class<?> clazz) {
|
||||
Map<String, Field> fieldMap = new HashMap<>();
|
||||
for (Field field : ReflectUtil.getFields(clazz)) {
|
||||
String fieldName = field.getName();
|
||||
JsonProperty jsonProperty = field.getAnnotation(JsonProperty.class);
|
||||
if (jsonProperty != null) {
|
||||
String value = jsonProperty.value();
|
||||
if (StrUtil.isNotEmpty(value) && ObjUtil.notEqual("\u0000", value)) {
|
||||
fieldName = value;
|
||||
}
|
||||
}
|
||||
fieldMap.put(fieldName, field);
|
||||
}
|
||||
return fieldMap;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user