当我们使用Java 8 Time 包和Jackson 时,最好使用jackson-modules-java8 项目,该项目为许多随时可用的序列化器和反序列化器提供服务。要启用它,我们需要注册JavaTimeModule 模块。序列化Instant 使用InstantSerializer。当我们检查它是如何实现的时,我们会发现在后台使用了DecimalUtils.toDecimal 方法。看起来总是在纳秒值的末尾添加零。
我们可以编写我们的InstantSerializer,它以所需的方式对其进行序列化。因为这个项目中的类还没有准备好轻松扩展,我们需要实现许多不需要的方法和构造函数。我们还需要在我们的项目com.fasterxml.jackson.datatype.jsr310.ser 包中创建并在那里创建我们的实现。见下例:
package com.fasterxml.jackson.datatype.jsr310.ser;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.SerializerProvider;
import java.io.IOException;
import java.math.BigDecimal;
import java.time.Instant;
import java.time.format.DateTimeFormatter;
import java.util.function.ToIntFunction;
import java.util.function.ToLongFunction;
public class ShortInstantSerializer extends InstantSerializerBase<Instant> {
private ToLongFunction<Instant> getEpochSeconds = Instant::getEpochSecond;
private ToIntFunction<Instant> getNanoseconds = i -> i.getNano() / 1_000_000;
public ShortInstantSerializer() {
super(Instant.class, Instant::toEpochMilli, Instant::getEpochSecond, Instant::getNano, null);
}
protected ShortInstantSerializer(ShortInstantSerializer base, Boolean useTimestamp, Boolean useNanoseconds, DateTimeFormatter formatter) {
super(base, useTimestamp, useNanoseconds, formatter);
}
@Override
protected JSR310FormattedSerializerBase<?> withFormat(Boolean useTimestamp, DateTimeFormatter formatter, JsonFormat.Shape shape) {
return new ShortInstantSerializer(this, useTimestamp, null, formatter);
}
@Override
public void serialize(Instant value, JsonGenerator generator, SerializerProvider provider) throws IOException {
if (useTimestamp(provider)) {
if (useNanoseconds(provider)) {
generator.writeNumber(new BigDecimal(toShortVersion(value)));
return;
}
}
super.serialize(value, generator, provider);
}
private String toShortVersion(final Instant value) {
return getEpochSeconds.applyAsLong(value) + "." + padWithZeros(getNanoseconds.applyAsInt(value));
}
private String padWithZeros(final int value) {
return String.format("%1$3s", String.valueOf(value)).replace(' ', '0');
}
}
以及如何使用它的示例:
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
import com.fasterxml.jackson.datatype.jsr310.ser.ShortInstantSerializer;
import java.time.Instant;
public class JsonApp {
public static void main(String[] args) throws Exception {
JavaTimeModule javaTimeModule = new JavaTimeModule();
javaTimeModule.addSerializer(Instant.class, new ShortInstantSerializer());
ObjectMapper mapper = new ObjectMapper();
mapper.registerModule(javaTimeModule);
mapper.disable(SerializationFeature.INDENT_OUTPUT);
System.out.println(mapper.writeValueAsString(new Element()));
}
}
class Element {
private Instant timestamp = Instant.now();
public Instant getTimestamp() {
return timestamp;
}
public void setTimestamp(Instant timestamp) {
this.timestamp = timestamp;
}
}
上面的代码打印:
{"timestamp":1559074287.223}
如果您想在所有情况下都消除所有零,请编写您自己在 ShortInstantSerializer 类中声明的 getNanoseconds 函数。