Pre Merge pull request !225 from wuKong/fix(json)-修复时间戳序列化时字段为空的问题
commit
dc6ccd22ca
|
|
@ -1,5 +1,6 @@
|
||||||
package cn.iocoder.yudao.framework.common.util.json.databind;
|
package cn.iocoder.yudao.framework.common.util.json.databind;
|
||||||
|
|
||||||
|
import cn.hutool.core.util.ObjectUtil;
|
||||||
import cn.hutool.core.util.ReflectUtil;
|
import cn.hutool.core.util.ReflectUtil;
|
||||||
import com.fasterxml.jackson.annotation.JsonFormat;
|
import com.fasterxml.jackson.annotation.JsonFormat;
|
||||||
import com.fasterxml.jackson.core.JsonGenerator;
|
import com.fasterxml.jackson.core.JsonGenerator;
|
||||||
|
|
@ -25,9 +26,10 @@ public class TimestampLocalDateTimeSerializer extends JsonSerializer<LocalDateTi
|
||||||
public void serialize(LocalDateTime value, JsonGenerator gen, SerializerProvider serializers) throws IOException {
|
public void serialize(LocalDateTime value, JsonGenerator gen, SerializerProvider serializers) throws IOException {
|
||||||
// 情况一:有 JsonFormat 自定义注解,则使用它。https://github.com/YunaiV/ruoyi-vue-pro/pull/1019
|
// 情况一:有 JsonFormat 自定义注解,则使用它。https://github.com/YunaiV/ruoyi-vue-pro/pull/1019
|
||||||
String fieldName = gen.getOutputContext().getCurrentName();
|
String fieldName = gen.getOutputContext().getCurrentName();
|
||||||
if (fieldName != null) {
|
if (ObjectUtil.isNotNull(fieldName)) {
|
||||||
Class<?> clazz = gen.getOutputContext().getCurrentValue().getClass();
|
Class<?> clazz = gen.getOutputContext().getCurrentValue().getClass();
|
||||||
Field field = ReflectUtil.getField(clazz, fieldName);
|
Field field = ReflectUtil.getField(clazz, fieldName);
|
||||||
|
if (ObjectUtil.isNotNull(field)) {
|
||||||
JsonFormat[] jsonFormats = field.getAnnotationsByType(JsonFormat.class);
|
JsonFormat[] jsonFormats = field.getAnnotationsByType(JsonFormat.class);
|
||||||
if (jsonFormats.length > 0) {
|
if (jsonFormats.length > 0) {
|
||||||
String pattern = jsonFormats[0].pattern();
|
String pattern = jsonFormats[0].pattern();
|
||||||
|
|
@ -36,6 +38,7 @@ public class TimestampLocalDateTimeSerializer extends JsonSerializer<LocalDateTi
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// 情况二:默认将 LocalDateTime 对象,转换为 Long 时间戳
|
// 情况二:默认将 LocalDateTime 对象,转换为 Long 时间戳
|
||||||
gen.writeNumber(value.atZone(ZoneId.systemDefault()).toInstant().toEpochMilli());
|
gen.writeNumber(value.atZone(ZoneId.systemDefault()).toInstant().toEpochMilli());
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue