提交 | 用户 | 时间
|
e7c126
|
1 |
package com.iailab.framework.jackson.core.databind; |
H |
2 |
|
|
3 |
import com.fasterxml.jackson.core.JsonGenerator; |
|
4 |
import com.fasterxml.jackson.databind.JsonSerializer; |
|
5 |
import com.fasterxml.jackson.databind.SerializerProvider; |
|
6 |
|
|
7 |
import java.io.IOException; |
|
8 |
import java.time.LocalDateTime; |
|
9 |
import java.time.ZoneId; |
|
10 |
|
|
11 |
/** |
|
12 |
* 基于时间戳的 LocalDateTime 序列化器 |
|
13 |
* |
|
14 |
* @author 老五 |
|
15 |
*/ |
|
16 |
public class TimestampLocalDateTimeSerializer extends JsonSerializer<LocalDateTime> { |
|
17 |
|
|
18 |
public static final TimestampLocalDateTimeSerializer INSTANCE = new TimestampLocalDateTimeSerializer(); |
|
19 |
|
|
20 |
@Override |
|
21 |
public void serialize(LocalDateTime value, JsonGenerator gen, SerializerProvider serializers) throws IOException { |
|
22 |
// 将 LocalDateTime 对象,转换为 Long 时间戳 |
|
23 |
gen.writeNumber(value.atZone(ZoneId.systemDefault()).toInstant().toEpochMilli()); |
|
24 |
} |
|
25 |
|
|
26 |
} |