Skip to content

UnixSeconds and UnixMilliseconds serializers for kotlin.time.Instant #3101

@dkhalanskyjb

Description

@dkhalanskyjb

What is your use-case and why do you need this feature?

There were several requests for kotlinx-datetime that kotlinx.datetime.Instant allows (de)serialization into Unix seconds or milliseconds. For example, so that this API request

{
    "timestamp": 123456789
}

could be deserialized into this:

@Serializable
data class Result(@Serializable(with=InstantUnixSecondsSerializer::class) val timestamp: Instant)

Specific APIs listed:

Describe the solution you'd like

Some idiomatic approach for handling this use case. I personally would expect an API like "serializable as T, with this way to convert to T and that way to obtain from T", with T here being Long, together with Instant::fromEpochSeconds and Instant::epochSeconds, but maybe it makes more sense or is more realistic to actually provide the requested serializers.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions