uk.gov.service.payments.commons.api.json.MicrosecondPrecisionDateTimeSerializer Maven / Gradle / Ivy
package uk.gov.service.payments.commons.api.json;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.SerializerProvider;
import com.fasterxml.jackson.databind.ser.std.StdSerializer;
import java.io.IOException;
import java.time.ZoneOffset;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeFormatterBuilder;
import java.util.Locale;
public class MicrosecondPrecisionDateTimeSerializer extends StdSerializer {
public static final DateTimeFormatter MICROSECOND_FORMATTER =
new DateTimeFormatterBuilder()
.appendInstant(6)
.toFormatter(Locale.ENGLISH)
.withZone(ZoneOffset.UTC);
public MicrosecondPrecisionDateTimeSerializer() {
this(null);
}
private MicrosecondPrecisionDateTimeSerializer(Class t) {
super(t);
}
@Override
public void serialize(ZonedDateTime value, JsonGenerator gen, SerializerProvider sp) throws IOException {
gen.writeString(MICROSECOND_FORMATTER.format(value));
}
}