kotlinx.serialization icon indicating copy to clipboard operation
kotlinx.serialization copied to clipboard

UnixSeconds and UnixMilliseconds serializers for `kotlin.time.Instant`

Open dkhalanskyjb opened this issue 1 month ago • 0 comments

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:

  • https://www.jetbrains.com/help/youtrack/devportal/api-entity-Issue.html, Unix time in milliseconds.
  • https://en.wikipedia.org/wiki/JSON_Web_Token#Standard_fields, Unix time in seconds.

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.

dkhalanskyjb avatar Oct 30 '25 14:10 dkhalanskyjb