Commit 018be292d970ec06d4aff4daa5e872fcbdcea1f2
1 parent
aeb862e1
feat:【框架】TimestampLocalDateTimeSerializer 优化下代码格式
Showing
1 changed file
with
30 additions
and
17 deletions
yudao-framework/yudao-common/src/main/java/cn/iocoder/yudao/framework/common/util/json/databind/TimestampLocalDateTimeSerializer.java
| 1 | package cn.iocoder.yudao.framework.common.util.json.databind; | 1 | package cn.iocoder.yudao.framework.common.util.json.databind; |
| 2 | 2 | ||
| 3 | +import cn.hutool.core.util.ObjUtil; | ||
| 3 | import cn.hutool.core.util.ReflectUtil; | 4 | import cn.hutool.core.util.ReflectUtil; |
| 5 | +import cn.hutool.core.util.StrUtil; | ||
| 4 | import com.fasterxml.jackson.annotation.JsonFormat; | 6 | import com.fasterxml.jackson.annotation.JsonFormat; |
| 5 | import com.fasterxml.jackson.annotation.JsonProperty; | 7 | import com.fasterxml.jackson.annotation.JsonProperty; |
| 6 | import com.fasterxml.jackson.core.JsonGenerator; | 8 | import com.fasterxml.jackson.core.JsonGenerator; |
| 7 | import com.fasterxml.jackson.databind.JsonSerializer; | 9 | import com.fasterxml.jackson.databind.JsonSerializer; |
| 8 | import com.fasterxml.jackson.databind.SerializerProvider; | 10 | import com.fasterxml.jackson.databind.SerializerProvider; |
| 11 | +import lombok.extern.slf4j.Slf4j; | ||
| 9 | 12 | ||
| 10 | import java.io.IOException; | 13 | import java.io.IOException; |
| 11 | import java.lang.reflect.Field; | 14 | import java.lang.reflect.Field; |
| @@ -21,28 +24,13 @@ import java.util.concurrent.ConcurrentHashMap; | @@ -21,28 +24,13 @@ import java.util.concurrent.ConcurrentHashMap; | ||
| 21 | * | 24 | * |
| 22 | * @author 老五 | 25 | * @author 老五 |
| 23 | */ | 26 | */ |
| 27 | +@Slf4j | ||
| 24 | public class TimestampLocalDateTimeSerializer extends JsonSerializer<LocalDateTime> { | 28 | public class TimestampLocalDateTimeSerializer extends JsonSerializer<LocalDateTime> { |
| 25 | 29 | ||
| 26 | public static final TimestampLocalDateTimeSerializer INSTANCE = new TimestampLocalDateTimeSerializer(); | 30 | public static final TimestampLocalDateTimeSerializer INSTANCE = new TimestampLocalDateTimeSerializer(); |
| 27 | 31 | ||
| 28 | private static final Map<Class<?>, Map<String, Field>> FIELD_CACHE = new ConcurrentHashMap<>(); | 32 | private static final Map<Class<?>, Map<String, Field>> FIELD_CACHE = new ConcurrentHashMap<>(); |
| 29 | 33 | ||
| 30 | - private Map<String, Field> buildFieldMap(Class<?> clazz) { | ||
| 31 | - Map<String, Field> fieldMap = new HashMap<>(); | ||
| 32 | - for (Field field : ReflectUtil.getFields(clazz)) { | ||
| 33 | - String fieldName = field.getName(); | ||
| 34 | - JsonProperty jsonProperty = field.getAnnotation(JsonProperty.class); | ||
| 35 | - if (jsonProperty != null) { | ||
| 36 | - String value = jsonProperty.value(); | ||
| 37 | - if (!value.isEmpty() && !"\u0000".equals(value)) { | ||
| 38 | - fieldName = value; | ||
| 39 | - } | ||
| 40 | - } | ||
| 41 | - fieldMap.put(fieldName, field); | ||
| 42 | - } | ||
| 43 | - return fieldMap; | ||
| 44 | - } | ||
| 45 | - | ||
| 46 | @Override | 34 | @Override |
| 47 | public void serialize(LocalDateTime value, JsonGenerator gen, SerializerProvider serializers) throws IOException { | 35 | public void serialize(LocalDateTime value, JsonGenerator gen, SerializerProvider serializers) throws IOException { |
| 48 | // 情况一:有 JsonFormat 自定义注解,则使用它。https://github.com/YunaiV/ruoyi-vue-pro/pull/1019 | 36 | // 情况一:有 JsonFormat 自定义注解,则使用它。https://github.com/YunaiV/ruoyi-vue-pro/pull/1019 |
| @@ -53,13 +41,16 @@ public class TimestampLocalDateTimeSerializer extends JsonSerializer<LocalDateTi | @@ -53,13 +41,16 @@ public class TimestampLocalDateTimeSerializer extends JsonSerializer<LocalDateTi | ||
| 53 | Class<?> clazz = currentValue.getClass(); | 41 | Class<?> clazz = currentValue.getClass(); |
| 54 | Map<String, Field> fieldMap = FIELD_CACHE.computeIfAbsent(clazz, this::buildFieldMap); | 42 | Map<String, Field> fieldMap = FIELD_CACHE.computeIfAbsent(clazz, this::buildFieldMap); |
| 55 | Field field = fieldMap.get(fieldName); | 43 | Field field = fieldMap.get(fieldName); |
| 44 | + // 进一步修复:https://gitee.com/zhijiantianya/ruoyi-vue-pro/pulls/1480 | ||
| 56 | if (field != null && field.isAnnotationPresent(JsonFormat.class)) { | 45 | if (field != null && field.isAnnotationPresent(JsonFormat.class)) { |
| 57 | JsonFormat jsonFormat = field.getAnnotation(JsonFormat.class); | 46 | JsonFormat jsonFormat = field.getAnnotation(JsonFormat.class); |
| 58 | try { | 47 | try { |
| 59 | DateTimeFormatter formatter = DateTimeFormatter.ofPattern(jsonFormat.pattern()); | 48 | DateTimeFormatter formatter = DateTimeFormatter.ofPattern(jsonFormat.pattern()); |
| 60 | gen.writeString(formatter.format(value)); | 49 | gen.writeString(formatter.format(value)); |
| 61 | return; | 50 | return; |
| 62 | - } catch (Exception ignored) { | 51 | + } catch (Exception ex) { |
| 52 | + log.warn("[serialize][({}#{}) 使用 JsonFormat pattern 失败,尝试使用默认的 Long 时间戳]", | ||
| 53 | + clazz.getName(), fieldName, ex); | ||
| 63 | } | 54 | } |
| 64 | } | 55 | } |
| 65 | } | 56 | } |
| @@ -69,4 +60,26 @@ public class TimestampLocalDateTimeSerializer extends JsonSerializer<LocalDateTi | @@ -69,4 +60,26 @@ public class TimestampLocalDateTimeSerializer extends JsonSerializer<LocalDateTi | ||
| 69 | gen.writeNumber(value.atZone(ZoneId.systemDefault()).toInstant().toEpochMilli()); | 60 | gen.writeNumber(value.atZone(ZoneId.systemDefault()).toInstant().toEpochMilli()); |
| 70 | } | 61 | } |
| 71 | 62 | ||
| 63 | + /** | ||
| 64 | + * 构建字段映射(缓存) | ||
| 65 | + * | ||
| 66 | + * @param clazz 类 | ||
| 67 | + * @return 字段映射 | ||
| 68 | + */ | ||
| 69 | + private Map<String, Field> buildFieldMap(Class<?> clazz) { | ||
| 70 | + Map<String, Field> fieldMap = new HashMap<>(); | ||
| 71 | + for (Field field : ReflectUtil.getFields(clazz)) { | ||
| 72 | + String fieldName = field.getName(); | ||
| 73 | + JsonProperty jsonProperty = field.getAnnotation(JsonProperty.class); | ||
| 74 | + if (jsonProperty != null) { | ||
| 75 | + String value = jsonProperty.value(); | ||
| 76 | + if (StrUtil.isNotEmpty(value) && ObjUtil.notEqual("\u0000", value)) { | ||
| 77 | + fieldName = value; | ||
| 78 | + } | ||
| 79 | + } | ||
| 80 | + fieldMap.put(fieldName, field); | ||
| 81 | + } | ||
| 82 | + return fieldMap; | ||
| 83 | + } | ||
| 84 | + | ||
| 72 | } | 85 | } |