Description
InstantSerializer doesn't respect any of the following standard Jackson configuration settings:
ObjectMapper#setDateFormat
@JsonFormat(pattern)
on an Instant
others?
Instead, I have to create and manually override the default InstantSerializer
registered by the module.
This isn't well documented and is definitely confusing when using the library.
Can we make InstantSerializer
actually respect date-related or pattern-related options from the core library?
Looking at InstantSerializerBase
, the provided context has the format from the mapper, but doesn't use it. The formatters attached to the serializer directly are both null by default.
public void serialize(T value, JsonGenerator generator, SerializerProvider provider) throws IOException {
if (this.useTimestamp(provider)) {
if (this.useNanoseconds(provider)) {
generator.writeNumber(DecimalUtils.toBigDecimal(this.getEpochSeconds.applyAsLong(value), this.getNanoseconds.applyAsInt(value)));
} else {
generator.writeNumber(this.getEpochMillis.applyAsLong(value));
}
} else {
String str;
if (this._formatter != null) {
str = this._formatter.format(value);
} else if (this.defaultFormat != null) {
str = this.defaultFormat.format(value);
} else {
str = value.toString();
}
generator.writeString(str);
}
}
So the SerializationFeature.WRITE_DATES_AS_TIMESTAMPS
is respected but that's it.
Does it make sense to use the provider's defaultSerializeDateValue(long timestamp, JsonGenerator gen)
method here and pass the timestamp from the Instant
?