Jackson serialises java.time.Instant with WRITE_DATE_TIMESTAMPS_AS_NANOSECONDS enabled by default.
It produces JSON
I took Michal's idea above and wrapped the existing com.fasterxml.jackson.datatype.jsr310.DecimalUtils#toBigDecimal(long seconds, int nanoseconds) in a serializer
class ShortInstantSerializer extends StdSerializer {
ShortInstantSerializer() {
super(Instant.class);
}
@Override
public void serialize(Instant value, JsonGenerator gen, SerializerProvider provider) throws IOException {
gen.writeNumber(DecimalUtils.toBigDecimal(value.getEpochSecond(), value.getNano()));
}
}
Also, if you're going to be reading it back at some point, the value will be deserialized into a Double. To go back do this (thanks inverwebs!)
public static Instant toInstant(Double d) {
long seconds = d.longValue();
long micros = Math.round((d - seconds) * 1_000_000);
return Instant.ofEpochSecond(seconds).plus(micros , ChronoUnit.MICROS);
}