Skip to content

InstantSerializer doesn't respect any format-related settings without replacing serializer instance #168

Open
@Thorn1089

Description

@Thorn1089

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?

Metadata

Metadata

Assignees

No one assigned

    Labels

    good first issueIssue that seems easy to resolve and is likely a good candidate for contributors new to projecthacktoberfestIssue related to Hactoberfest2020 activities, eligible for additional rewardsjson-formatMissing handling of `@JsonFormat` property annotation

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions